-
Notifications
You must be signed in to change notification settings - Fork 10
/
dojo.hpp
1012 lines (808 loc) · 20 KB
/
dojo.hpp
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>
namespace dojo_bindings {
enum class BlockTag {
Latest,
Pending,
};
enum class ComparisonOperator {
Eq,
Neq,
Gt,
Gte,
Lt,
Lte,
};
enum class LogicalOperator {
And,
Or,
};
enum class PatternMatching {
FixedLen = 0,
VariableLen = 1,
};
struct Account;
struct Provider;
struct Subscription;
struct ToriiClient;
struct Error {
char *message;
};
template<typename T>
struct Result {
enum class Tag {
Ok,
Err,
};
struct Ok_Body {
T _0;
};
struct Err_Body {
Error _0;
};
Tag tag;
union {
Ok_Body ok;
Err_Body err;
};
static Result Ok(const T &_0) {
Result result;
::new (&result.ok._0) (T)(_0);
result.tag = Tag::Ok;
return result;
}
bool IsOk() const {
return tag == Tag::Ok;
}
static Result Err(const Error &_0) {
Result result;
::new (&result.err._0) (Error)(_0);
result.tag = Tag::Err;
return result;
}
bool IsErr() const {
return tag == Tag::Err;
}
};
struct FieldElement {
uint8_t data[32];
};
template<typename T>
struct CArray {
T *data;
uintptr_t data_len;
};
struct Primitive {
enum class Tag {
I8,
I16,
I32,
I64,
I128,
U8,
U16,
U32,
U64,
U128,
#if !defined(TARGET_POINTER_WIDTH_32)
U256,
#endif
#if defined(TARGET_POINTER_WIDTH_32)
U256,
#endif
USize,
Bool,
Felt252,
ClassHash,
ContractAddress,
};
struct I8_Body {
int8_t _0;
};
struct I16_Body {
int16_t _0;
};
struct I32_Body {
int32_t _0;
};
struct I64_Body {
int64_t _0;
};
struct I128_Body {
uint8_t _0[16];
};
struct U8_Body {
uint8_t _0;
};
struct U16_Body {
uint16_t _0;
};
struct U32_Body {
uint32_t _0;
};
struct U64_Body {
uint64_t _0;
};
struct U128_Body {
uint8_t _0[16];
};
#if !defined(TARGET_POINTER_WIDTH_32)
struct U256_Body {
uint64_t _0[4];
};
#endif
#if defined(TARGET_POINTER_WIDTH_32)
struct U256_Body {
uint32_t _0[8];
};
#endif
struct USize_Body {
uint32_t _0;
};
struct Bool_Body {
bool _0;
};
struct Felt252_Body {
FieldElement _0;
};
struct ClassHash_Body {
FieldElement _0;
};
struct ContractAddress_Body {
FieldElement _0;
};
Tag tag;
union {
I8_Body i8;
I16_Body i16;
I32_Body i32;
I64_Body i64;
I128_Body i128;
U8_Body u8;
U16_Body u16;
U32_Body u32;
U64_Body u64;
U128_Body u128;
#if !defined(TARGET_POINTER_WIDTH_32)
U256_Body u256;
#endif
#if defined(TARGET_POINTER_WIDTH_32)
U256_Body u256;
#endif
USize_Body u_size;
Bool_Body bool_;
Felt252_Body felt252;
ClassHash_Body class_hash;
ContractAddress_Body contract_address;
};
static Primitive I8(const int8_t &_0) {
Primitive result;
::new (&result.i8._0) (int8_t)(_0);
result.tag = Tag::I8;
return result;
}
bool IsI8() const {
return tag == Tag::I8;
}
static Primitive I16(const int16_t &_0) {
Primitive result;
::new (&result.i16._0) (int16_t)(_0);
result.tag = Tag::I16;
return result;
}
bool IsI16() const {
return tag == Tag::I16;
}
static Primitive I32(const int32_t &_0) {
Primitive result;
::new (&result.i32._0) (int32_t)(_0);
result.tag = Tag::I32;
return result;
}
bool IsI32() const {
return tag == Tag::I32;
}
static Primitive I64(const int64_t &_0) {
Primitive result;
::new (&result.i64._0) (int64_t)(_0);
result.tag = Tag::I64;
return result;
}
bool IsI64() const {
return tag == Tag::I64;
}
static Primitive I128(const uint8_t (&_0)[16]) {
Primitive result;
for (int i = 0; i < 16; i++) {
::new (&result.i128._0[i]) (uint8_t)(_0[i]);
}
result.tag = Tag::I128;
return result;
}
bool IsI128() const {
return tag == Tag::I128;
}
static Primitive U8(const uint8_t &_0) {
Primitive result;
::new (&result.u8._0) (uint8_t)(_0);
result.tag = Tag::U8;
return result;
}
bool IsU8() const {
return tag == Tag::U8;
}
static Primitive U16(const uint16_t &_0) {
Primitive result;
::new (&result.u16._0) (uint16_t)(_0);
result.tag = Tag::U16;
return result;
}
bool IsU16() const {
return tag == Tag::U16;
}
static Primitive U32(const uint32_t &_0) {
Primitive result;
::new (&result.u32._0) (uint32_t)(_0);
result.tag = Tag::U32;
return result;
}
bool IsU32() const {
return tag == Tag::U32;
}
static Primitive U64(const uint64_t &_0) {
Primitive result;
::new (&result.u64._0) (uint64_t)(_0);
result.tag = Tag::U64;
return result;
}
bool IsU64() const {
return tag == Tag::U64;
}
static Primitive U128(const uint8_t (&_0)[16]) {
Primitive result;
for (int i = 0; i < 16; i++) {
::new (&result.u128._0[i]) (uint8_t)(_0[i]);
}
result.tag = Tag::U128;
return result;
}
bool IsU128() const {
return tag == Tag::U128;
}
#if !defined(TARGET_POINTER_WIDTH_32)
static Primitive U256(const uint64_t (&_0)[4]) {
Primitive result;
for (int i = 0; i < 4; i++) {
::new (&result.u256._0[i]) (uint64_t)(_0[i]);
}
result.tag = Tag::U256;
return result;
}
bool IsU256() const {
return tag == Tag::U256;
}
#endif
#if defined(TARGET_POINTER_WIDTH_32)
static Primitive U256(const uint32_t (&_0)[8]) {
Primitive result;
for (int i = 0; i < 8; i++) {
::new (&result.u256._0[i]) (uint32_t)(_0[i]);
}
result.tag = Tag::U256;
return result;
}
bool IsU256() const {
return tag == Tag::U256;
}
#endif
static Primitive USize(const uint32_t &_0) {
Primitive result;
::new (&result.u_size._0) (uint32_t)(_0);
result.tag = Tag::USize;
return result;
}
bool IsUSize() const {
return tag == Tag::USize;
}
static Primitive Bool(const bool &_0) {
Primitive result;
::new (&result.bool_._0) (bool)(_0);
result.tag = Tag::Bool;
return result;
}
bool IsBool() const {
return tag == Tag::Bool;
}
static Primitive Felt252(const FieldElement &_0) {
Primitive result;
::new (&result.felt252._0) (FieldElement)(_0);
result.tag = Tag::Felt252;
return result;
}
bool IsFelt252() const {
return tag == Tag::Felt252;
}
static Primitive ClassHash(const FieldElement &_0) {
Primitive result;
::new (&result.class_hash._0) (FieldElement)(_0);
result.tag = Tag::ClassHash;
return result;
}
bool IsClassHash() const {
return tag == Tag::ClassHash;
}
static Primitive ContractAddress(const FieldElement &_0) {
Primitive result;
::new (&result.contract_address._0) (FieldElement)(_0);
result.tag = Tag::ContractAddress;
return result;
}
bool IsContractAddress() const {
return tag == Tag::ContractAddress;
}
};
struct EnumOption {
const char *name;
Ty *ty;
};
struct Enum {
const char *name;
uint8_t option;
CArray<EnumOption> options;
};
struct Ty {
enum class Tag {
Primitive_,
Struct_,
Enum_,
Tuple_,
Array_,
ByteArray,
};
struct Primitive__Body {
Primitive _0;
};
struct Struct__Body {
Struct _0;
};
struct Enum__Body {
Enum _0;
};
struct Tuple__Body {
CArray<Ty> _0;
};
struct Array__Body {
CArray<Ty> _0;
};
struct ByteArray_Body {
const char *_0;
};
Tag tag;
union {
Primitive__Body primitive;
Struct__Body struct_;
Enum__Body enum_;
Tuple__Body tuple;
Array__Body array;
ByteArray_Body byte_array;
};
static Ty Primitive_(const Primitive &_0) {
Ty result;
::new (&result.primitive._0) (Primitive)(_0);
result.tag = Tag::Primitive_;
return result;
}
bool IsPrimitive_() const {
return tag == Tag::Primitive_;
}
static Ty Struct_(const Struct &_0) {
Ty result;
::new (&result.struct_._0) (Struct)(_0);
result.tag = Tag::Struct_;
return result;
}
bool IsStruct_() const {
return tag == Tag::Struct_;
}
static Ty Enum_(const Enum &_0) {
Ty result;
::new (&result.enum_._0) (Enum)(_0);
result.tag = Tag::Enum_;
return result;
}
bool IsEnum_() const {
return tag == Tag::Enum_;
}
static Ty Tuple_(const CArray<Ty> &_0) {
Ty result;
::new (&result.tuple._0) (CArray<Ty>)(_0);
result.tag = Tag::Tuple_;
return result;
}
bool IsTuple_() const {
return tag == Tag::Tuple_;
}
static Ty Array_(const CArray<Ty> &_0) {
Ty result;
::new (&result.array._0) (CArray<Ty>)(_0);
result.tag = Tag::Array_;
return result;
}
bool IsArray_() const {
return tag == Tag::Array_;
}
static Ty ByteArray(const char *const &_0) {
Ty result;
::new (&result.byte_array._0) (const char*)(_0);
result.tag = Tag::ByteArray;
return result;
}
bool IsByteArray() const {
return tag == Tag::ByteArray;
}
};
struct Member {
const char *name;
Ty *ty;
bool key;
};
struct Struct {
const char *name;
CArray<Member> children;
};
struct Entity {
FieldElement hashed_keys;
CArray<Struct> models;
};
template<typename T>
struct COption {
enum class Tag {
Some,
None,
};
struct Some_Body {
T _0;
};
Tag tag;
union {
Some_Body some;
};
static COption Some(const T &_0) {
COption result;
::new (&result.some._0) (T)(_0);
result.tag = Tag::Some;
return result;
}
bool IsSome() const {
return tag == Tag::Some;
}
static COption None() {
COption result;
result.tag = Tag::None;
return result;
}
bool IsNone() const {
return tag == Tag::None;
}
};
struct KeysClause {
CArray<COption<FieldElement>> keys;
PatternMatching pattern_matching;
CArray<const char*> models;
};
struct MemberValue {
enum class Tag {
Primitive,
String,
};
struct Primitive_Body {
Primitive _0;
};
struct String_Body {
const char *_0;
};
Tag tag;
union {
Primitive_Body primitive;
String_Body string;
};
static MemberValue Primitive(const Primitive &_0) {
MemberValue result;
::new (&result.primitive._0) (Primitive)(_0);
result.tag = Tag::Primitive;
return result;
}
bool IsPrimitive() const {
return tag == Tag::Primitive;
}
static MemberValue String(const char *const &_0) {
MemberValue result;
::new (&result.string._0) (const char*)(_0);
result.tag = Tag::String;
return result;
}
bool IsString() const {
return tag == Tag::String;
}
};
struct MemberClause {
const char *model;
const char *member;
ComparisonOperator operator_;
MemberValue value;
};
struct CompositeClause {
LogicalOperator operator_;
CArray<Clause> clauses;
};
struct Clause {
enum class Tag {
Keys,
CMember,
Composite,
};
struct Keys_Body {
KeysClause _0;
};
struct CMember_Body {
MemberClause _0;
};
struct Composite_Body {
CompositeClause _0;
};
Tag tag;
union {
Keys_Body keys;
CMember_Body c_member;
Composite_Body composite;
};
static Clause Keys(const KeysClause &_0) {
Clause result;
::new (&result.keys._0) (KeysClause)(_0);
result.tag = Tag::Keys;
return result;
}
bool IsKeys() const {
return tag == Tag::Keys;
}
static Clause CMember(const MemberClause &_0) {
Clause result;
::new (&result.c_member._0) (MemberClause)(_0);
result.tag = Tag::CMember;
return result;
}
bool IsCMember() const {
return tag == Tag::CMember;
}
static Clause Composite(const CompositeClause &_0) {
Clause result;
::new (&result.composite._0) (CompositeClause)(_0);
result.tag = Tag::Composite;
return result;
}
bool IsComposite() const {
return tag == Tag::Composite;
}
};
struct Query {
uint32_t limit;
uint32_t offset;
COption<Clause> clause;
bool dont_include_hashed_keys;
};
struct ModelMetadata {
Ty schema;
const char *namespace_;
const char *name;
uint32_t packed_size;
uint32_t unpacked_size;
FieldElement class_hash;
FieldElement contract_address;
CArray<FieldElement> layout;
};
template<typename K, typename V>
struct CHashItem {
K key;
V value;
};
struct WorldMetadata {
FieldElement world_address;
CArray<CHashItem<FieldElement, ModelMetadata>> models;
};
struct EntityKeysClause {
enum class Tag {
HashedKeys,
EntityKeys,
};
struct HashedKeys_Body {
CArray<FieldElement> _0;
};
struct EntityKeys_Body {
KeysClause _0;
};
Tag tag;
union {
HashedKeys_Body hashed_keys;
EntityKeys_Body entity_keys;
};
static EntityKeysClause HashedKeys(const CArray<FieldElement> &_0) {
EntityKeysClause result;
::new (&result.hashed_keys._0) (CArray<FieldElement>)(_0);
result.tag = Tag::HashedKeys;
return result;
}
bool IsHashedKeys() const {
return tag == Tag::HashedKeys;
}
static EntityKeysClause EntityKeys(const KeysClause &_0) {
EntityKeysClause result;
::new (&result.entity_keys._0) (KeysClause)(_0);
result.tag = Tag::EntityKeys;
return result;
}
bool IsEntityKeys() const {
return tag == Tag::EntityKeys;
}
};
struct Event {
CArray<FieldElement> keys;
CArray<FieldElement> data;
FieldElement transaction_hash;
};
struct IndexerUpdate {
int64_t head;
int64_t tps;
int64_t last_block_timestamp;
FieldElement contract_address;
};
struct Signature {
/// The `r` value of a signature
FieldElement r;
/// The `s` value of a signature
FieldElement s;
};
struct Call {
FieldElement to;
const char *selector;
CArray<FieldElement> calldata;
};
/// Block hash, number or tag
struct BlockId {
enum class Tag {
Hash,
Number,
BlockTag_,
};
struct Hash_Body {
FieldElement _0;
};
struct Number_Body {
uint64_t _0;
};
struct BlockTag__Body {
BlockTag _0;
};
Tag tag;
union {
Hash_Body hash;
Number_Body number;
BlockTag__Body block_tag;
};
static BlockId Hash(const FieldElement &_0) {
BlockId result;
::new (&result.hash._0) (FieldElement)(_0);
result.tag = Tag::Hash;
return result;
}
bool IsHash() const {
return tag == Tag::Hash;
}
static BlockId Number(const uint64_t &_0) {
BlockId result;
::new (&result.number._0) (uint64_t)(_0);
result.tag = Tag::Number;
return result;
}
bool IsNumber() const {
return tag == Tag::Number;
}
static BlockId BlockTag_(const BlockTag &_0) {
BlockId result;
::new (&result.block_tag._0) (BlockTag)(_0);
result.tag = Tag::BlockTag_;
return result;
}
bool IsBlockTag_() const {
return tag == Tag::BlockTag_;
}
};
extern "C" {
Result<ToriiClient*> client_new(const char *torii_url,
const char *rpc_url,
const char *libp2p_relay_url,
FieldElement world);
void client_set_logger(ToriiClient *client, void (*logger)(const char*));
Result<CArray<uint8_t>> client_publish_message(ToriiClient *client,
const char *message,
const FieldElement *signature_felts,
uintptr_t signature_felts_len);
Result<CArray<Entity>> client_entities(ToriiClient *client, const Query *query);
Result<CArray<Entity>> client_event_messages(ToriiClient *client,
const Query *query,
bool historical);
WorldMetadata client_metadata(ToriiClient *client);
Result<Subscription*> client_on_entity_state_update(ToriiClient *client,
const EntityKeysClause *clauses,
uintptr_t clauses_len,
void (*callback)(FieldElement, CArray<Struct>));
Result<bool> client_update_entity_subscription(ToriiClient *client,
Subscription *subscription,
const EntityKeysClause *clauses,
uintptr_t clauses_len);
Result<Subscription*> client_on_event_message_update(ToriiClient *client,
const EntityKeysClause *clauses,
uintptr_t clauses_len,
bool historical,
void (*callback)(FieldElement, CArray<Struct>));
Result<bool> client_update_event_message_subscription(ToriiClient *client,
Subscription *subscription,
const EntityKeysClause *clauses,
uintptr_t clauses_len,
bool historical);
Result<Subscription*> client_on_starknet_event(ToriiClient *client,
const EntityKeysClause *clauses,
uintptr_t clauses_len,
void (*callback)(Event));
Result<Subscription*> on_indexer_update(ToriiClient *client,
const FieldElement *contract_address,
void (*callback)(IndexerUpdate));
Result<CArray<FieldElement>> bytearray_serialize(const char *str);
Result<const char*> bytearray_deserialize(const FieldElement *felts, uintptr_t felts_len);
FieldElement poseidon_hash(const FieldElement *felts, uintptr_t felts_len);
Result<FieldElement> get_selector_from_name(const char *name);
FieldElement get_selector_from_tag(const char *tag);
FieldElement starknet_keccak(const uint8_t *bytes, uintptr_t bytes_len);
Result<FieldElement> cairo_short_string_to_felt(const char *str);
Result<const char*> parse_cairo_short_string(FieldElement felt);
Result<FieldElement> typed_data_encode(const char *typed_data, FieldElement address);
FieldElement signing_key_new();
Result<Signature> signing_key_sign(FieldElement private_key, FieldElement hash);
FieldElement verifying_key_new(FieldElement signing_key);
Result<bool> verifying_key_verify(FieldElement verifying_key,
FieldElement hash,
Signature signature);
Result<Provider*> provider_new(const char *rpc_url);
Result<Account*> account_new(Provider *rpc, FieldElement private_key, const char *address);
Result<CArray<FieldElement>> starknet_call(Provider *provider, Call call, BlockId block_id);
Result<Account*> account_deploy_burner(Provider *provider,
Account *master_account,
FieldElement signing_key);
FieldElement account_address(Account *account);
FieldElement account_chain_id(Account *account);
void account_set_block_id(Account *account, BlockId block_id);
Result<FieldElement> account_nonce(Account *account);
Result<FieldElement> account_execute_raw(Account *account,
const Call *calldata,
uintptr_t calldata_len);
Result<bool> wait_for_transaction(Provider *rpc, FieldElement txn_hash);
FieldElement hash_get_contract_address(FieldElement class_hash,
FieldElement salt,
const FieldElement *constructor_calldata,
uintptr_t constructor_calldata_len,
FieldElement deployer_address);
void subscription_cancel(Subscription *subscription);
void client_free(ToriiClient *t);
void provider_free(Provider *rpc);
void model_free(Struct *model);
void account_free(Account *account);
void ty_free(Ty *ty);
void entity_free(Entity *entity);