-
Notifications
You must be signed in to change notification settings - Fork 66
/
lib.rs
689 lines (565 loc) · 21 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
extern crate alloc;
use fuel_indexer_utils::prelude::*;
#[indexer(
manifest = "packages/fuel-indexer-tests/indexers/fuel-indexer-test/fuel_indexer_test.yaml"
)]
mod fuel_indexer_test {
fn fuel_indexer_test_blocks(block_data: BlockData) {
let block = BlockEntity::new(block_data.height, block_data.time).get_or_create();
let input_data = r#"{"foo":"bar"}"#.to_string();
for _tx in block_data.transactions.iter() {
TxEntity::new(block.id.clone(), Json(input_data.clone()), block_data.time)
.get_or_create();
}
}
fn fuel_indexer_test_ping(ping: Ping) {
info!("fuel_indexer_test_ping handling a Ping event: {:?}.", ping);
PingEntity::new(ping.value, ping.message.to_string()).get_or_create();
}
fn fuel_indexer_test_u16(_ping: Ping) {
info!("fuel_indexer_test_ping handling a U16 event.");
U16Entity::new(
340282366920938463463374607431768211454,
170141183460469231731687303715884105727,
)
.get_or_create();
}
fn fuel_indexer_test_transfer(transfer: Transfer) {
info!(
"fuel_indexer_test_transfer handling Transfer event: {:?}",
transfer
);
let Transfer {
contract_id,
to,
asset_id,
amount,
..
} = transfer;
TransferEntity::new(contract_id, to, amount, asset_id).get_or_create();
}
fn fuel_indexer_test_transferout(transferout: TransferOut) {
info!(
"fuel_indexer_test_transferout handling TransferOut event: {:?}",
transferout
);
let TransferOut {
contract_id,
to,
asset_id,
amount,
..
} = transferout;
TransferOutEntity::new(contract_id, to, amount, asset_id).get_or_create();
}
fn fuel_indexer_test_log(log: Log) {
info!("fuel_indexer_test_log handling Log event.");
let Log {
contract_id,
rb,
ra,
..
} = log;
LogEntity::new(contract_id, ra, rb).get_or_create();
}
fn fuel_indexer_test_logdata(logdata_entity: Pung) {
info!("fuel_indexer_test_logdata handling LogData event.");
PungEntity::new(
logdata_entity.value,
logdata_entity.is_pung,
logdata_entity.pung_from,
)
.get_or_create();
}
fn fuel_indexer_test_scriptresult(scriptresult: ScriptResult) {
info!("fuel_indexer_test_scriptresult handling ScriptResult event: result={}, gas_used={}", scriptresult.result, scriptresult.gas_used);
let ScriptResult { result, gas_used } = scriptresult;
ScriptResultEntity::new(result, gas_used, vec![1u8; 5].into()).get_or_create();
}
fn fuel_indexer_test_messageout_data(example_message: ExampleMessageStruct) {
info!("fuel_indexer_test_messageout_data handling MessageOut data event");
MessageEntity::new(example_message.message.to_string()).get_or_create();
}
fn fuel_indexer_test_messageout(messageout: MessageOut) {
info!("fuel_indexer_test_messageout handling MessageOut event");
let MessageOut {
sender,
message_id,
recipient,
amount,
nonce,
len,
digest,
..
} = messageout;
MessageOutEntity::new(
bytes32(message_id),
sender,
recipient,
amount,
bytes32(nonce),
len,
digest,
)
.get_or_create();
}
fn fuel_indexer_test_callreturn(pungentity: Pung) {
info!("fuel_indexer_test_callreturn handling Pung event.");
PungEntity::new(pungentity.value, pungentity.is_pung, pungentity.pung_from)
.get_or_create();
}
fn fuel_indexer_test_multiargs(
pung: Pung,
pong: Pong,
ping: Ping,
block_data: BlockData,
) {
info!(
"fuel_indexer_test_multiargs handling Pung, Pong, Ping, and BlockData events."
);
BlockEntity::new(block_data.height, block_data.time).get_or_create();
PungEntity::new(pung.value, pung.is_pung, pung.pung_from).get_or_create();
PongEntity::new(pong.value).get_or_create();
PingEntity::new(ping.value, ping.message.to_string()).get_or_create();
}
fn fuel_indexer_test_optional_schema_fields(_block: BlockData) {
info!("fuel_indexer_test_optional_schema_fields handling Ping event and setting optional fields.");
let entity = OptionEntity {
id: uid(8675309_u32.to_le_bytes()),
int_required: 100,
int_optional_some: Some(999),
addr_optional_none: None,
};
entity.save();
}
fn fuel_indexer_test_tuple(
event: ComplexTupleStruct,
logdata_entity: SimpleTupleStruct,
) {
info!("fuel_indexer_test_tuple handling ComplexTupleStruct and SimpleTupleStruct events.");
let data: (u32, (u64, bool, (SizedAsciiString<5>, TupleStructItem))) = event.data;
TupleEntity::new(
data.1 .2 .0.to_string(),
data.1 .2 .1.id,
logdata_entity.data.2.to_string(),
)
.get_or_create();
}
fn fuel_indexer_test_pure_function(call: Call) {
info!("fuel_indexer_test_tuple handling Call event.");
let Call {
contract_id,
to,
asset_id,
gas,
fn_name,
amount,
} = call;
CallEntity::new(contract_id, to, asset_id, gas, fn_name, amount).get_or_create();
}
fn fuel_indexer_test_deeply_nested_schema_fields(_deeply_nested: SimpleQueryStruct) {
info!("fuel_indexer_test_deeply_nested_schema_fields handling DeeplyNestedQueryTestStruct event.");
let genre1 = Genre::new("horror".to_string()).get_or_create();
let genre2 = Genre::new("mystery".to_string()).get_or_create();
let genre3 = Genre::new("business".to_string()).get_or_create();
let author1 = Author::new("Brian".to_string(), genre1.id.clone()).get_or_create();
let author2 = Author::new("James".to_string(), genre2.id.clone()).get_or_create();
let author3 = Author::new("Susan".to_string(), genre3.id.clone()).get_or_create();
let person1 = Person::new("Ava".to_string()).get_or_create();
let person2 = Person::new("Noel".to_string()).get_or_create();
let person3 = Person::new("Julio".to_string()).get_or_create();
let planet = Planet::new("Earth".to_string()).get_or_create();
let continent1 = Continent::new("North America".to_string(), planet.id.clone())
.get_or_create();
let continent2 = Continent::new("Europe".to_string(), planet.id).get_or_create();
let country1 = Country::new("Jamaica".to_string(), continent1.id).get_or_create();
let country2 =
Country::new("France".to_string(), continent2.id.clone()).get_or_create();
let country3 = Country::new("Ireland".to_string(), continent2.id).get_or_create();
let region1 =
Region::new("Westmoreland".to_string(), country1.id).get_or_create();
let region2 = Region::new("Corsica".to_string(), country2.id).get_or_create();
let region3 = Region::new("Donegal".to_string(), country3.id).get_or_create();
let city1 = City::new("Savanna-la-Mar".to_string(), region1.id).get_or_create();
let city2 = City::new("Ajaccio".to_string(), region2.id).get_or_create();
let city3 = City::new("Letterkenny".to_string(), region3.id).get_or_create();
let library1 =
Library::new("Scholar Library".to_string(), city1.id).get_or_create();
let library2 =
Library::new("Ronoke Library".to_string(), city2.id).get_or_create();
let library3 =
Library::new("Scholastic Library".to_string(), city3.id).get_or_create();
let book1 = Book::new(
"Gone with the Wind".to_string(),
author1.id,
library1.id,
genre1.id,
)
.get_or_create();
let book2 = Book::new("Othello".to_string(), author2.id, library2.id, genre2.id)
.get_or_create();
let book3 = Book::new(
"Cyberpunk 2021".to_string(),
author3.id,
library3.id,
genre3.id,
)
.get_or_create();
let sponsor1 = Sponsor::new("Fuel Labs".to_string(), 100, person1.id.clone())
.get_or_create();
let sponsor2 =
Sponsor::new("Fuel Labs, Part Two".to_string(), 200, person2.id.clone())
.get_or_create();
let sponsor3 = Sponsor::new(
"Fuel Labs, Yet A Third".to_string(),
300,
person3.id.clone(),
)
.get_or_create();
let bookclub1 = BookClub {
id: uid([1]),
book: book1.id,
member: person1.id,
corporate_sponsor: sponsor1.name,
};
let bookclub2 = BookClub {
id: uid([2]),
book: book2.id,
member: person2.id,
corporate_sponsor: sponsor2.name,
};
let bookclub3 = BookClub {
id: uid([3]),
book: book3.id,
member: person3.id,
corporate_sponsor: sponsor3.name,
};
bookclub1.save();
bookclub2.save();
bookclub3.save();
}
fn fuel_indexer_test_nested_query_explicit_foreign_keys_schema_fields(
_block: BlockData,
) {
info!("fuel_indexer_test_nested_query_explicit_foreign_keys_schema_fields handling ExplicitQueryTestStruct event.");
let municipality =
Municipality::new("Republic of Indexia".to_string()).get_or_create();
SportsTeam::new("The Indexers".to_string(), municipality.name).get_or_create();
}
fn fuel_indexer_test_panic(panic: Panic) {
info!("fuel_indexer_test_panic handling Panic event.");
let Panic {
contract_id,
reason,
} = panic;
PanicEntity::new(contract_id, reason).get_or_create();
}
fn fuel_indexer_trigger_revert(revert: Revert) {
info!("fuel_indexer_trigger_revert handling trigger_revert event.");
let Revert {
contract_id,
error_val,
} = revert;
RevertEntity::new(contract_id, error_val).get_or_create();
}
fn fuel_indexer_trigger_mint(mint: Mint) {
info!("fuel_indexer_trigger_mint handling trigger_mint event.");
let Mint {
sub_id,
contract_id,
val,
..
} = mint;
MintEntity::new(sub_id, contract_id, val).get_or_create();
}
fn fuel_indexer_trigger_burn(burn: Burn) {
info!("fuel_indexer_trigger_burn handling trigger_burn event.");
let Burn {
sub_id,
contract_id,
val,
..
} = burn;
BurnEntity::new(sub_id, contract_id, val).get_or_create();
}
fn fuel_indexer_test_filterable_fields(_ping: Ping) {
let inner_entity1 = InnerFilterEntity {
id: uid([1]),
inner_foo: "spam".to_string(),
inner_bar: 100,
inner_baz: 200,
};
let inner_entity2 = InnerFilterEntity {
id: uid([2]),
inner_foo: "ham".to_string(),
inner_bar: 300,
inner_baz: 400,
};
let inner_entity3 = InnerFilterEntity {
id: uid([3]),
inner_foo: "eggs".to_string(),
inner_bar: 500,
inner_baz: 600,
};
inner_entity1.save();
inner_entity2.save();
inner_entity3.save();
let f1 = FilterEntity {
id: uid([1]),
foola: "beep".to_string(),
maybe_null_bar: Some(123),
bazoo: 1,
inner_entity: inner_entity1.id,
};
let f2 = FilterEntity {
id: uid([2]),
foola: "boop".to_string(),
maybe_null_bar: None,
bazoo: 5,
inner_entity: inner_entity2.id,
};
let f3 = FilterEntity {
id: uid([3]),
foola: "blorp".to_string(),
maybe_null_bar: Some(456),
bazoo: 1000,
inner_entity: inner_entity3.id,
};
f1.save();
f2.save();
f3.save();
}
fn fuel_indexer_trigger_enum_error(enum_error: Revert) {
info!("fuel_indexer_trigger_enum_error handling trigger_enum_error event.");
let Revert {
contract_id,
error_val,
} = enum_error;
EnumError::new(contract_id, error_val).get_or_create();
}
fn fuel_indexer_block_explorer_types(_b: BlockData) {
info!("fuel_indexer_block_explorer_types handling explorer_types event.");
let e = ExplorerEntity {
id: uid(8675309_u32.to_le_bytes()),
nonce: Bytes32::default(),
// TOOD: Finish
time: None,
hex: Some(Bytes::from("hello world!")),
sig: Bytes64::default(),
bytes: Bytes64::default(),
};
e.save();
}
fn fuel_indexer_trigger_enum(
_first: AnotherSimpleEnum,
_second: NestedEnum,
_third: AnotherSimpleEnum,
) {
info!("fuel_indexer_trigger_enum handling trigger_enum event..");
let e = ComplexEnumEntity {
id: uid([1]),
one: Some(EnumEntity::One.into()),
};
e.save();
}
fn fuel_indexer_trigger_non_indexable_type(_b: BlockData) {
info!("fuel_indexer_trigger_non_indexable_type handling trigger_non_indexable_type event.");
let e = UsesVirtualEntity {
id: uid([1]),
name: "hello world".to_string(),
no_table: VirtualEntity {
name: Some("virtual".to_string()),
size: 1,
}
.into(),
};
e.save();
}
fn fuel_indexer_trigger_union_type(_b: BlockData) {
info!("fuel_indexer_trigger_union_type handling trigger_union_type event.");
let v = VirtualUnionEntity {
a: Some(2),
b: None,
c: Some(6),
union_type: Some(UnionType::B.into()),
};
let vc = VirtualUnionContainerEntity {
id: uid([1]),
union_entity: Some(v.into()),
union_type: UnionType::B.into(),
};
vc.save();
let e = IndexableUnionEntity {
id: uid([1]),
a: Some(5),
b: Some(10),
c: None,
union_type: Some(UnionType::A.into()),
};
e.save();
}
fn fuel_indexer_test_trigger_list(_block: BlockData) {
info!("fuel_indexer_test_trigger_list handling trigger_list event.");
let list_fk1 = ListFKType {
id: uid([1]),
value: 1,
};
list_fk1.save();
let list_fk2 = ListFKType {
id: uid([2]),
value: 2,
};
list_fk2.save();
let list_fk3 = ListFKType {
id: uid([3]),
value: 3,
};
list_fk3.save();
let e = ListTypeEntity {
id: uid([1]),
foo_field: "hello world".to_string(),
required_all: vec![list_fk1.id, list_fk2.id, list_fk3.id],
optional_inner: vec![
Some("hello".to_string()),
None,
Some("world".to_string()),
None,
],
optional_outer: Some(vec![1, 2, 3, 4, 5]),
optional_all: Some(vec![Some(1), None, Some(3), None]),
virtual_optional_inner: vec![
Some(
VirtualEntity {
name: Some("foo".to_string()),
size: 1,
}
.into(),
),
None,
None,
Some(
VirtualEntity {
name: Some("bar".to_string()),
size: 2,
}
.into(),
),
],
enum_required_all: vec![EnumEntity::One.into(), EnumEntity::Two.into()],
};
e.save();
}
fn fuel_indexer_test_trigger_generics(ping: Option<Ping>) {
info!("fuel_indexer_test_trigger_generics handling trigger_generics event.");
assert!(ping.is_some());
let ping = ping.unwrap();
assert_eq!(ping.id, 8888);
assert_eq!(ping.value, 8888);
assert_eq!(
ping.message,
SizedAsciiString::<32>::new("aaaasdfsdfasdfsdfaasdfsdfasdfsdf".to_string())
.unwrap()
);
let ping = PingEntity {
id: uid(ping.id.to_le_bytes()),
value: ping.value,
message: ping.message.to_string(),
};
ping.save();
}
fn fuel_indexer_test_trigger_find(_find: Find, block_data: BlockData) {
FindEntity::new(
block_data.height as u64,
format!("find{}", block_data.height),
)
.save();
if block_data.height == 4 {
// Test == 1
let i = IndexMetadataEntity::find(IndexMetadataEntity::block_height().eq(1))
.unwrap();
assert_eq!(i.block_height, 1);
// Test > 1 && < 3 (expected: 2)
let i = IndexMetadataEntity::find(
IndexMetadataEntity::block_height()
.gt(1)
.and(IndexMetadataEntity::block_height().lt(3)),
)
.unwrap();
assert_eq!(i.block_height, 2);
// Test < 2 (expected: 1)
let i = IndexMetadataEntity::find(IndexMetadataEntity::block_height().lt(2))
.unwrap();
assert_eq!(i.block_height, 1);
} else if block_data.height == 5 {
// Test >= 2 order_by_asc. We have 4 indexed blocks, so the result should be 2.
let i = IndexMetadataEntity::find(
IndexMetadataEntity::block_height()
.ge(2)
.order_by(IndexMetadataEntity::block_height().asc()),
)
.unwrap();
assert_eq!(i.block_height, 2);
// Test >= 2 order_by_desc. We have 4 indexed blocks, so the result should be 4.
let i = IndexMetadataEntity::find(
IndexMetadataEntity::block_height()
.ge(2)
.order_by(IndexMetadataEntity::block_height().desc()),
)
.unwrap();
assert_eq!(i.block_height, 4);
// Test searching for a string field
let f = FindEntity::find(
FindEntity::string_value()
.gt("f".to_string())
.order_by(FindEntity::value().asc()),
)
.unwrap();
assert_eq!(&f.string_value, "find2");
// Test searching for a string field
let f = FindEntity::find(
FindEntity::string_value()
.gt("f".to_string())
.order_by(FindEntity::value().desc()),
)
.unwrap();
assert_eq!(&f.string_value, "find5");
// Test searching for multiple entities, no limit
let fs: Vec<FindEntity> = FindEntity::find_many(
FindEntity::string_value()
.gt("f".to_string())
.order_by(FindEntity::value().asc()),
);
assert_eq!(fs.len(), 4);
assert_eq!(fs[0].string_value, "find2");
assert_eq!(fs[1].string_value, "find3");
assert_eq!(fs[2].string_value, "find4");
assert_eq!(fs[3].string_value, "find5");
// Test searching for multiple entities, with limit
let fs: Vec<FindEntity> = FindEntity::find_many(
FindEntity::string_value()
.gt("f".to_string())
.order_by(FindEntity::value().desc())
.limit(10),
);
assert_eq!(fs.len(), 4);
assert_eq!(fs[0].string_value, "find5");
assert_eq!(fs[1].string_value, "find4");
assert_eq!(fs[2].string_value, "find3");
assert_eq!(fs[3].string_value, "find2");
// Test searching for multiple entities, with limit
let fs: Vec<FindEntity> = FindEntity::find_many(
FindEntity::string_value()
.gt("f".to_string())
.order_by(FindEntity::value().desc())
.limit(2),
);
assert_eq!(fs.len(), 2);
assert_eq!(fs[0].string_value, "find5");
assert_eq!(fs[1].string_value, "find4");
} else if block_data.height == 6 {
// There is no such block. The lookup will fail.
IndexMetadataEntity::find(IndexMetadataEntity::block_height().eq(777))
.unwrap();
}
}
}