diff --git a/Cargo.lock b/Cargo.lock index 4bf4747..85db283 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -981,9 +981,9 @@ dependencies = [ [[package]] name = "cairo-lang-casm" -version = "2.9.2" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3929a38c1d586e35e19dbdf7798b146fba3627b308417a6d373fea8939535b6b" +checksum = "0e5568d33e98e2af9530d7dc2353699cb2a0cd0d7c32b105114615371d6ee030" dependencies = [ "cairo-lang-utils", "indoc", @@ -995,9 +995,9 @@ dependencies = [ [[package]] name = "cairo-lang-eq-solver" -version = "2.9.2" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d778ec864e92c82293370a512195715b12775b05981e14065d85eb5dd3dd96b6" +checksum = "40f01748564e51d96074f1ce6cd85c46a525954637143c083aaeaa5cbe969257" dependencies = [ "cairo-lang-utils", "good_lp", @@ -1005,9 +1005,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra" -version = "2.9.2" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "075c6457642ada82b32cf657d871a8545ae7a9d61c78dd5588a794c8c905abdc" +checksum = "95ce5b707226e42868dc8da9a63c6c5baef6a13c54be22b70080361e686d5e0d" dependencies = [ "anyhow", "cairo-lang-utils", @@ -1032,9 +1032,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-ap-change" -version = "2.9.2" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b69172fe8354b1dd564bba318ccb5233aa78f70d57145b8c92a0b1cf009fa0fc" +checksum = "78c228f0ad5bb6ae8d8086b53fca1c64d5298dc33cd590e9ab15dc85d4e800ce" dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", @@ -1048,9 +1048,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-gas" -version = "2.9.2" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42b571b73d9b02103f780aeee05dbf9a71d68d8a16341a04aa1dd581d0db3ad6" +checksum = "241ba43dc88925635dbae05fccdb471e3bcdb44a6284b6499993ee63517d4171" dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", @@ -1064,9 +1064,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-to-casm" -version = "2.9.2" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add264b156dfb01f18292282a6037070c078acca3bccde05787da1e1c997b78c" +checksum = "4ebcfc50dfc0f926ba87a51931b75ff234aa8e8e965a9dbbdb2776fa38cfb903" dependencies = [ "assert_matches", "cairo-lang-casm", @@ -1085,9 +1085,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-type-size" -version = "2.9.2" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bda5388ef862bc26388e999ac7ad62dd8ab3064720c3483b81fd761b051e627" +checksum = "403e2524c27f28423405ecc160a8000f45e5a450c1d3015989e20fb4c5e55df9" dependencies = [ "cairo-lang-sierra", "cairo-lang-utils", @@ -1095,9 +1095,9 @@ dependencies = [ [[package]] name = "cairo-lang-starknet-classes" -version = "2.9.2" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe691200b431e51e3d6cfa84f256a3dd2e8405f44d182843fbe124f803d085ff" +checksum = "3295bc0e3ead98195d5e280dbaf280bc458ee42ef2dccf77d5a1c8ce71a40953" dependencies = [ "cairo-lang-casm", "cairo-lang-sierra", @@ -1118,9 +1118,9 @@ dependencies = [ [[package]] name = "cairo-lang-utils" -version = "2.9.2" +version = "2.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff5d7609abc99c15de7d7f90b8441b27e2bd52e930a3014c95a9b620e5d3211a" +checksum = "34609db9f2589340e217cd02254548d6e65dad1d0306fa17af1afba949509692" dependencies = [ "hashbrown 0.14.5", "indexmap 2.7.0", @@ -1760,8 +1760,8 @@ dependencies = [ [[package]] name = "dojo-types" -version = "1.3.1" -source = "git+https://github.com/dojoengine/dojo?rev=271cf44055f2b42e50e898e3dc34a13ae530b329#271cf44055f2b42e50e898e3dc34a13ae530b329" +version = "1.4.0" +source = "git+https://github.com/dojoengine/dojo?rev=2348232#234823291281c6522ff6a48ea5739d85d59bcdf9" dependencies = [ "anyhow", "cainome 0.5.0", @@ -1782,8 +1782,8 @@ dependencies = [ [[package]] name = "dojo-world" -version = "1.3.1" -source = "git+https://github.com/dojoengine/dojo?rev=271cf44055f2b42e50e898e3dc34a13ae530b329#271cf44055f2b42e50e898e3dc34a13ae530b329" +version = "1.4.0" +source = "git+https://github.com/dojoengine/dojo?rev=2348232#234823291281c6522ff6a48ea5739d85d59bcdf9" dependencies = [ "anyhow", "async-trait", @@ -7190,8 +7190,8 @@ dependencies = [ [[package]] name = "torii-client" -version = "1.3.1" -source = "git+https://github.com/dojoengine/dojo?rev=271cf44055f2b42e50e898e3dc34a13ae530b329#271cf44055f2b42e50e898e3dc34a13ae530b329" +version = "1.4.0" +source = "git+https://github.com/dojoengine/dojo?rev=2348232#234823291281c6522ff6a48ea5739d85d59bcdf9" dependencies = [ "async-trait", "crypto-bigint", @@ -7218,8 +7218,8 @@ dependencies = [ [[package]] name = "torii-grpc" -version = "1.3.1" -source = "git+https://github.com/dojoengine/dojo?rev=271cf44055f2b42e50e898e3dc34a13ae530b329#271cf44055f2b42e50e898e3dc34a13ae530b329" +version = "1.4.0" +source = "git+https://github.com/dojoengine/dojo?rev=2348232#234823291281c6522ff6a48ea5739d85d59bcdf9" dependencies = [ "crypto-bigint", "dojo-types", @@ -7257,8 +7257,8 @@ dependencies = [ [[package]] name = "torii-relay" -version = "1.3.1" -source = "git+https://github.com/dojoengine/dojo?rev=271cf44055f2b42e50e898e3dc34a13ae530b329#271cf44055f2b42e50e898e3dc34a13ae530b329" +version = "1.4.0" +source = "git+https://github.com/dojoengine/dojo?rev=2348232#234823291281c6522ff6a48ea5739d85d59bcdf9" dependencies = [ "anyhow", "futures", @@ -7277,8 +7277,8 @@ dependencies = [ [[package]] name = "torii-typed-data" -version = "1.3.1" -source = "git+https://github.com/dojoengine/dojo?rev=271cf44055f2b42e50e898e3dc34a13ae530b329#271cf44055f2b42e50e898e3dc34a13ae530b329" +version = "1.4.0" +source = "git+https://github.com/dojoengine/dojo?rev=2348232#234823291281c6522ff6a48ea5739d85d59bcdf9" dependencies = [ "cainome 0.5.0", "crypto-bigint", diff --git a/Cargo.toml b/Cargo.toml index d953b14..01d256b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,18 +7,18 @@ version = "1.3.1" crate-type = ["cdylib", "rlib", "staticlib"] [dependencies] -dojo-world = { git = "https://github.com/dojoengine/dojo", rev = "271cf44055f2b42e50e898e3dc34a13ae530b329" } -dojo-types = { git = "https://github.com/dojoengine/dojo", rev = "271cf44055f2b42e50e898e3dc34a13ae530b329"} -torii-client = { git = "https://github.com/dojoengine/dojo", rev = "271cf44055f2b42e50e898e3dc34a13ae530b329" } +dojo-world = { git = "https://github.com/dojoengine/dojo", rev = "2348232" } +dojo-types = { git = "https://github.com/dojoengine/dojo", rev = "2348232" } +torii-client = { git = "https://github.com/dojoengine/dojo", rev = "2348232" } torii-grpc = { git = "https://github.com/dojoengine/dojo", features = [ "client", -], rev = "271cf44055f2b42e50e898e3dc34a13ae530b329" } -torii-typed-data = { git = "https://github.com/dojoengine/dojo", rev = "271cf44055f2b42e50e898e3dc34a13ae530b329" } -torii-relay = { git = "https://github.com/dojoengine/dojo", rev = "271cf44055f2b42e50e898e3dc34a13ae530b329" } +], rev = "2348232" } +torii-typed-data = { git = "https://github.com/dojoengine/dojo", rev = "2348232" } +torii-relay = { git = "https://github.com/dojoengine/dojo", rev = "2348232" } starknet = "0.13.0" starknet-crypto = "0.7.2" -starknet-types-core = { version = "0.1.6", features = [ "arbitrary" ] } +starknet-types-core = { version = "0.1.6", features = ["arbitrary"] } parking_lot = "0.12.1" tokio = { version = "1.39.2", default-features = false, features = ["rt"] } @@ -56,7 +56,11 @@ num-traits = "0.2.19" axum = "0.8.1" open = "5.3.2" urlencoding = "2.1.3" -keyring = { version = "3.6.1", features = ["apple-native", "windows-native", "sync-secret-service"] } +keyring = { version = "3.6.1", features = [ + "apple-native", + "windows-native", + "sync-secret-service", +] } directories = "6.0.0" tower-http = { version = "0.6.2", features = ["cors"] } base64 = "0.22.1" diff --git a/dojo.h b/dojo.h index 42b7282..76e8378 100644 --- a/dojo.h +++ b/dojo.h @@ -13,19 +13,17 @@ struct ControllerAccount; struct Call; struct Controller; struct Entity; -struct Query; +struct COptionFieldElement; +struct OrderBy; struct CHashItemFieldElementModelMetadata; struct Subscription; struct EntityKeysClause; struct Struct; struct Token; -struct U256; struct TokenBalance; struct Provider; struct Account; struct Ty; -struct COptionFieldElement; -struct OrderBy; struct Member; struct EnumOption; @@ -202,6 +200,192 @@ typedef struct ResultCArrayEntity { }; } ResultCArrayEntity; +typedef struct CArrayCOptionFieldElement { + struct COptionFieldElement *data; + uintptr_t data_len; +} CArrayCOptionFieldElement; + +typedef struct CArrayc_char { + const char **data; + uintptr_t data_len; +} CArrayc_char; + +typedef struct KeysClause { + struct CArrayCOptionFieldElement keys; + enum PatternMatching pattern_matching; + struct CArrayc_char models; +} KeysClause; + +typedef struct U256 { + uint8_t data[32]; +} U256; + +typedef enum Primitive_Tag { + I8, + I16, + I32, + I64, + I128, + U8, + U16, + U32, + U64, + U128, + U256_, + Bool, + Felt252, + ClassHash, + ContractAddress, + EthAddress, +} Primitive_Tag; + +typedef struct Primitive { + Primitive_Tag tag; + union { + struct { + int8_t i8; + }; + struct { + int16_t i16; + }; + struct { + int32_t i32; + }; + struct { + int64_t i64; + }; + struct { + uint8_t i128[16]; + }; + struct { + uint8_t u8; + }; + struct { + uint16_t u16; + }; + struct { + uint32_t u32; + }; + struct { + uint64_t u64; + }; + struct { + uint8_t u128[16]; + }; + struct { + struct U256 u256; + }; + struct { + bool bool_; + }; + struct { + struct FieldElement felt252; + }; + struct { + struct FieldElement class_hash; + }; + struct { + struct FieldElement contract_address; + }; + struct { + struct FieldElement eth_address; + }; + }; +} Primitive; + +typedef struct CArrayMemberValue { + struct MemberValue *data; + uintptr_t data_len; +} CArrayMemberValue; + +typedef enum MemberValue_Tag { + PrimitiveValue, + String, + List, +} MemberValue_Tag; + +typedef struct MemberValue { + MemberValue_Tag tag; + union { + struct { + struct Primitive primitive_value; + }; + struct { + const char *string; + }; + struct { + struct CArrayMemberValue list; + }; + }; +} MemberValue; + +typedef struct MemberClause { + const char *model; + const char *member; + enum ComparisonOperator operator_; + struct MemberValue value; +} MemberClause; + +typedef struct CArrayClause { + struct Clause *data; + uintptr_t data_len; +} CArrayClause; + +typedef struct CompositeClause { + enum LogicalOperator operator_; + struct CArrayClause clauses; +} CompositeClause; + +typedef enum Clause_Tag { + Keys, + CMember, + Composite, +} Clause_Tag; + +typedef struct Clause { + Clause_Tag tag; + union { + struct { + struct KeysClause keys; + }; + struct { + struct MemberClause c_member; + }; + struct { + struct CompositeClause composite; + }; + }; +} Clause; + +typedef enum COptionClause_Tag { + SomeClause, + NoneClause, +} COptionClause_Tag; + +typedef struct COptionClause { + COptionClause_Tag tag; + union { + struct { + struct Clause some; + }; + }; +} COptionClause; + +typedef struct CArrayOrderBy { + struct OrderBy *data; + uintptr_t data_len; +} CArrayOrderBy; + +typedef struct Query { + uint32_t limit; + uint32_t offset; + struct COptionClause clause; + bool dont_include_hashed_keys; + struct CArrayOrderBy order_by; + struct CArrayc_char entity_models; + uint64_t entity_updated_after; +} Query; + typedef struct CArrayCHashItemFieldElementModelMetadata { struct CHashItemFieldElementModelMetadata *data; uintptr_t data_len; @@ -267,26 +451,41 @@ typedef struct CArrayToken { uintptr_t data_len; } CArrayToken; -typedef enum ResultCArrayToken_Tag { - OkCArrayToken, - ErrCArrayToken, -} ResultCArrayToken_Tag; +typedef enum COptionc_char_Tag { + Somec_char, + Nonec_char, +} COptionc_char_Tag; -typedef struct ResultCArrayToken { - ResultCArrayToken_Tag tag; +typedef struct COptionc_char { + COptionc_char_Tag tag; union { struct { - struct CArrayToken ok; + const char *some; + }; + }; +} COptionc_char; + +typedef struct PageToken { + struct CArrayToken items; + struct COptionc_char next_cursor; +} PageToken; + +typedef enum ResultPageToken_Tag { + OkPageToken, + ErrPageToken, +} ResultPageToken_Tag; + +typedef struct ResultPageToken { + ResultPageToken_Tag tag; + union { + struct { + struct PageToken ok; }; struct { struct Error err; }; }; -} ResultCArrayToken; - -typedef struct U256 { - uint8_t data[32]; -} U256; +} ResultPageToken; typedef struct Token { struct FieldElement contract_address; @@ -302,22 +501,27 @@ typedef struct CArrayTokenBalance { uintptr_t data_len; } CArrayTokenBalance; -typedef enum ResultCArrayTokenBalance_Tag { - OkCArrayTokenBalance, - ErrCArrayTokenBalance, -} ResultCArrayTokenBalance_Tag; +typedef struct PageTokenBalance { + struct CArrayTokenBalance items; + struct COptionc_char next_cursor; +} PageTokenBalance; + +typedef enum ResultPageTokenBalance_Tag { + OkPageTokenBalance, + ErrPageTokenBalance, +} ResultPageTokenBalance_Tag; -typedef struct ResultCArrayTokenBalance { - ResultCArrayTokenBalance_Tag tag; +typedef struct ResultPageTokenBalance { + ResultPageTokenBalance_Tag tag; union { struct { - struct CArrayTokenBalance ok; + struct PageTokenBalance ok; }; struct { struct Error err; }; }; -} ResultCArrayTokenBalance; +} ResultPageTokenBalance; typedef struct IndexerUpdate { int64_t head; @@ -476,187 +680,25 @@ typedef struct Entity { struct CArrayStruct models; } Entity; -typedef struct CArrayCOptionFieldElement { - struct COptionFieldElement *data; - uintptr_t data_len; -} CArrayCOptionFieldElement; - -typedef struct CArrayc_char { - const char **data; - uintptr_t data_len; -} CArrayc_char; - -typedef struct KeysClause { - struct CArrayCOptionFieldElement keys; - enum PatternMatching pattern_matching; - struct CArrayc_char models; -} KeysClause; - -typedef enum Primitive_Tag { - I8, - I16, - I32, - I64, - I128, - U8, - U16, - U32, - U64, - U128, - U256_, - Bool, - Felt252, - ClassHash, - ContractAddress, - EthAddress, -} Primitive_Tag; - -typedef struct Primitive { - Primitive_Tag tag; - union { - struct { - int8_t i8; - }; - struct { - int16_t i16; - }; - struct { - int32_t i32; - }; - struct { - int64_t i64; - }; - struct { - uint8_t i128[16]; - }; - struct { - uint8_t u8; - }; - struct { - uint16_t u16; - }; - struct { - uint32_t u32; - }; - struct { - uint64_t u64; - }; - struct { - uint8_t u128[16]; - }; - struct { - struct U256 u256; - }; - struct { - bool bool_; - }; - struct { - struct FieldElement felt252; - }; - struct { - struct FieldElement class_hash; - }; - struct { - struct FieldElement contract_address; - }; - struct { - struct FieldElement eth_address; - }; - }; -} Primitive; - -typedef struct CArrayMemberValue { - struct MemberValue *data; - uintptr_t data_len; -} CArrayMemberValue; - -typedef enum MemberValue_Tag { - PrimitiveValue, - String, - List, -} MemberValue_Tag; +typedef enum COptionFieldElement_Tag { + SomeFieldElement, + NoneFieldElement, +} COptionFieldElement_Tag; -typedef struct MemberValue { - MemberValue_Tag tag; +typedef struct COptionFieldElement { + COptionFieldElement_Tag tag; union { struct { - struct Primitive primitive_value; - }; - struct { - const char *string; - }; - struct { - struct CArrayMemberValue list; + struct FieldElement some; }; }; -} MemberValue; +} COptionFieldElement; -typedef struct MemberClause { +typedef struct OrderBy { const char *model; const char *member; - enum ComparisonOperator operator_; - struct MemberValue value; -} MemberClause; - -typedef struct CArrayClause { - struct Clause *data; - uintptr_t data_len; -} CArrayClause; - -typedef struct CompositeClause { - enum LogicalOperator operator_; - struct CArrayClause clauses; -} CompositeClause; - -typedef enum Clause_Tag { - Keys, - CMember, - Composite, -} Clause_Tag; - -typedef struct Clause { - Clause_Tag tag; - union { - struct { - struct KeysClause keys; - }; - struct { - struct MemberClause c_member; - }; - struct { - struct CompositeClause composite; - }; - }; -} Clause; - -typedef enum COptionClause_Tag { - SomeClause, - NoneClause, -} COptionClause_Tag; - -typedef struct COptionClause { - COptionClause_Tag tag; - union { - struct { - struct Clause some; - }; - }; -} COptionClause; - -typedef struct CArrayOrderBy { - struct OrderBy *data; - uintptr_t data_len; -} CArrayOrderBy; - -typedef struct Query { - uint32_t limit; - uint32_t offset; - struct COptionClause clause; - bool dont_include_hashed_keys; - struct CArrayOrderBy order_by; - struct CArrayc_char entity_models; - uint64_t entity_updated_after; -} Query; + enum OrderDirection direction; +} OrderBy; typedef struct CArrayMember { struct Member *data; @@ -750,26 +792,6 @@ typedef struct EntityKeysClause { }; } EntityKeysClause; -typedef enum COptionFieldElement_Tag { - SomeFieldElement, - NoneFieldElement, -} COptionFieldElement_Tag; - -typedef struct COptionFieldElement { - COptionFieldElement_Tag tag; - union { - struct { - struct FieldElement some; - }; - }; -} COptionFieldElement; - -typedef struct OrderBy { - const char *model; - const char *member; - enum OrderDirection direction; -} OrderBy; - typedef struct Member { const char *name; struct Ty *ty; @@ -998,7 +1020,7 @@ struct ResultCArrayController client_controllers(struct ToriiClient *client, * Result containing array of matching entities or error */ struct ResultCArrayEntity client_entities(struct ToriiClient *client, - const struct Query *query, + struct Query query, bool historical); /** @@ -1013,7 +1035,7 @@ struct ResultCArrayEntity client_entities(struct ToriiClient *client, * Result containing array of matching event message entities or error */ struct ResultCArrayEntity client_event_messages(struct ToriiClient *client, - const struct Query *query, + struct Query query, bool historical); /** @@ -1127,11 +1149,14 @@ struct ResultSubscription client_on_starknet_event(struct ToriiClient *client, * # Returns * Result containing array of Token information or error */ -struct ResultCArrayToken client_tokens(struct ToriiClient *client, - const struct FieldElement *contract_addresses, - uintptr_t contract_addresses_len, - const struct U256 *token_ids, - uintptr_t token_ids_len); +struct ResultPageToken client_tokens(struct ToriiClient *client, + const struct FieldElement *contract_addresses, + uintptr_t contract_addresses_len, + const struct U256 *token_ids, + uintptr_t token_ids_len, + uint32_t limit, + uint32_t offset, + const char *cursor); /** * Subscribes to token updates @@ -1164,13 +1189,16 @@ struct ResultSubscription client_on_token_update(struct ToriiClient *client, * # Returns * Result containing array of TokenBalance information or error */ -struct ResultCArrayTokenBalance client_token_balances(struct ToriiClient *client, - const struct FieldElement *contract_addresses, - uintptr_t contract_addresses_len, - const struct FieldElement *account_addresses, - uintptr_t account_addresses_len, - const struct U256 *token_ids, - uintptr_t token_ids_len); +struct ResultPageTokenBalance client_token_balances(struct ToriiClient *client, + const struct FieldElement *contract_addresses, + uintptr_t contract_addresses_len, + const struct FieldElement *account_addresses, + uintptr_t account_addresses_len, + const struct U256 *token_ids, + uintptr_t token_ids_len, + uint32_t limit, + uint32_t offset, + const char *cursor); /** * Subscribes to indexer updates diff --git a/dojo.hpp b/dojo.hpp index 5bfe50a..4ee6f67 100644 --- a/dojo.hpp +++ b/dojo.hpp @@ -11,7 +11,6 @@ struct Policy; struct ControllerAccount; struct Call; struct Ty; -struct Query; struct Subscription; struct EntityKeysClause; struct Provider; @@ -128,6 +127,50 @@ struct Entity { CArray models; }; +template +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> keys; + PatternMatching pattern_matching; + CArray models; +}; + struct U256 { uint8_t data[32]; }; @@ -417,6 +460,154 @@ struct Primitive { } }; +struct MemberValue { + enum class Tag { + PrimitiveValue, + String, + List, + }; + + struct PrimitiveValue_Body { + Primitive _0; + }; + + struct String_Body { + const char *_0; + }; + + struct List_Body { + CArray _0; + }; + + Tag tag; + union { + PrimitiveValue_Body primitive_value; + String_Body string; + List_Body list; + }; + + static MemberValue PrimitiveValue(const Primitive &_0) { + MemberValue result; + ::new (&result.primitive_value._0) (Primitive)(_0); + result.tag = Tag::PrimitiveValue; + return result; + } + + bool IsPrimitiveValue() const { + return tag == Tag::PrimitiveValue; + } + + 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; + } + + static MemberValue List(const CArray &_0) { + MemberValue result; + ::new (&result.list._0) (CArray)(_0); + result.tag = Tag::List; + return result; + } + + bool IsList() const { + return tag == Tag::List; + } +}; + +struct MemberClause { + const char *model; + const char *member; + ComparisonOperator operator_; + MemberValue value; +}; + +struct CompositeClause { + LogicalOperator operator_; + CArray 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 OrderBy { + const char *model; + const char *member; + OrderDirection direction; +}; + +struct Query { + uint32_t limit; + uint32_t offset; + COption clause; + bool dont_include_hashed_keys; + CArray order_by; + CArray entity_models; + uint64_t entity_updated_after; +}; + struct EnumOption { const char *name; Ty *ty; @@ -576,6 +767,12 @@ struct Token { const char *metadata; }; +template +struct Page { + CArray items; + COption next_cursor; +}; + struct TokenBalance { U256 balance; FieldElement account_address; @@ -670,198 +867,6 @@ struct Policy { const char *description; }; -template -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> keys; - PatternMatching pattern_matching; - CArray models; -}; - -struct MemberValue { - enum class Tag { - PrimitiveValue, - String, - List, - }; - - struct PrimitiveValue_Body { - Primitive _0; - }; - - struct String_Body { - const char *_0; - }; - - struct List_Body { - CArray _0; - }; - - Tag tag; - union { - PrimitiveValue_Body primitive_value; - String_Body string; - List_Body list; - }; - - static MemberValue PrimitiveValue(const Primitive &_0) { - MemberValue result; - ::new (&result.primitive_value._0) (Primitive)(_0); - result.tag = Tag::PrimitiveValue; - return result; - } - - bool IsPrimitiveValue() const { - return tag == Tag::PrimitiveValue; - } - - 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; - } - - static MemberValue List(const CArray &_0) { - MemberValue result; - ::new (&result.list._0) (CArray)(_0); - result.tag = Tag::List; - return result; - } - - bool IsList() const { - return tag == Tag::List; - } -}; - -struct MemberClause { - const char *model; - const char *member; - ComparisonOperator operator_; - MemberValue value; -}; - -struct CompositeClause { - LogicalOperator operator_; - CArray 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 OrderBy { - const char *model; - const char *member; - OrderDirection direction; -}; - -struct Query { - uint32_t limit; - uint32_t offset; - COption clause; - bool dont_include_hashed_keys; - CArray order_by; - CArray entity_models; - uint64_t entity_updated_after; -}; - struct EntityKeysClause { enum class Tag { HashedKeys, @@ -1091,7 +1096,7 @@ Result> client_controllers(ToriiClient *client, /// /// # Returns /// Result containing array of matching entities or error -Result> client_entities(ToriiClient *client, const Query *query, bool historical); +Result> client_entities(ToriiClient *client, Query query, bool historical); /// Retrieves event messages matching the given query /// @@ -1102,9 +1107,7 @@ Result> client_entities(ToriiClient *client, const Query *query, /// /// # Returns /// Result containing array of matching event message entities or error -Result> client_event_messages(ToriiClient *client, - const Query *query, - bool historical); +Result> client_event_messages(ToriiClient *client, Query query, bool historical); /// Gets the world metadata for the client /// @@ -1201,11 +1204,14 @@ Result client_on_starknet_event(ToriiClient *client, /// /// # Returns /// Result containing array of Token information or error -Result> client_tokens(ToriiClient *client, - const FieldElement *contract_addresses, - uintptr_t contract_addresses_len, - const U256 *token_ids, - uintptr_t token_ids_len); +Result> client_tokens(ToriiClient *client, + const FieldElement *contract_addresses, + uintptr_t contract_addresses_len, + const U256 *token_ids, + uintptr_t token_ids_len, + uint32_t limit, + uint32_t offset, + const char *cursor); /// Subscribes to token updates /// @@ -1234,13 +1240,16 @@ Result client_on_token_update(ToriiClient *client, /// /// # Returns /// Result containing array of TokenBalance information or error -Result> client_token_balances(ToriiClient *client, - const FieldElement *contract_addresses, - uintptr_t contract_addresses_len, - const FieldElement *account_addresses, - uintptr_t account_addresses_len, - const U256 *token_ids, - uintptr_t token_ids_len); +Result> client_token_balances(ToriiClient *client, + const FieldElement *contract_addresses, + uintptr_t contract_addresses_len, + const FieldElement *account_addresses, + uintptr_t account_addresses_len, + const U256 *token_ids, + uintptr_t token_ids_len, + uint32_t limit, + uint32_t offset, + const char *cursor); /// Subscribes to indexer updates /// diff --git a/dojo.pyx b/dojo.pyx index cb6f39f..6b7cfbd 100644 --- a/dojo.pyx +++ b/dojo.pyx @@ -128,6 +128,120 @@ cdef extern from *: CArrayEntity ok; Error err; + cdef struct CArrayCOptionFieldElement: + COptionFieldElement *data; + uintptr_t data_len; + + cdef struct CArrayc_char: + const char **data; + uintptr_t data_len; + + cdef struct KeysClause: + CArrayCOptionFieldElement keys; + PatternMatching pattern_matching; + CArrayc_char models; + + cdef struct U256: + uint8_t data[32]; + + cdef enum Primitive_Tag: + I8, + I16, + I32, + I64, + I128, + U8, + U16, + U32, + U64, + U128, + U256_, + Bool, + Felt252, + ClassHash, + ContractAddress, + EthAddress, + + cdef struct Primitive: + Primitive_Tag tag; + int8_t i8; + int16_t i16; + int32_t i32; + int64_t i64; + uint8_t i128[16]; + uint8_t u8; + uint16_t u16; + uint32_t u32; + uint64_t u64; + uint8_t u128[16]; + U256 u256; + bool bool_; + FieldElement felt252; + FieldElement class_hash; + FieldElement contract_address; + FieldElement eth_address; + + cdef struct CArrayMemberValue: + MemberValue *data; + uintptr_t data_len; + + cdef enum MemberValue_Tag: + PrimitiveValue, + String, + List, + + cdef struct MemberValue: + MemberValue_Tag tag; + Primitive primitive_value; + const char *string; + CArrayMemberValue list; + + cdef struct MemberClause: + const char *model; + const char *member; + ComparisonOperator operator_; + MemberValue value; + + cdef struct CArrayClause: + Clause *data; + uintptr_t data_len; + + cdef struct CompositeClause: + LogicalOperator operator_; + CArrayClause clauses; + + cdef enum Clause_Tag: + Keys, + CMember, + Composite, + + cdef struct Clause: + Clause_Tag tag; + KeysClause keys; + MemberClause c_member; + CompositeClause composite; + + cdef enum COptionClause_Tag: + SomeClause, + NoneClause, + + cdef struct COptionClause: + COptionClause_Tag tag; + Clause some; + + cdef struct CArrayOrderBy: + OrderBy *data; + uintptr_t data_len; + + cdef struct Query: + uint32_t limit; + uint32_t offset; + COptionClause clause; + bool dont_include_hashed_keys; + CArrayOrderBy order_by; + CArrayc_char entity_models; + uint64_t entity_updated_after; + cdef struct CArrayCHashItemFieldElementModelMetadata: CHashItemFieldElementModelMetadata *data; uintptr_t data_len; @@ -171,17 +285,26 @@ cdef extern from *: Token *data; uintptr_t data_len; - cdef enum ResultCArrayToken_Tag: - OkCArrayToken, - ErrCArrayToken, + cdef enum COptionc_char_Tag: + Somec_char, + Nonec_char, - cdef struct ResultCArrayToken: - ResultCArrayToken_Tag tag; - CArrayToken ok; - Error err; + cdef struct COptionc_char: + COptionc_char_Tag tag; + const char *some; - cdef struct U256: - uint8_t data[32]; + cdef struct PageToken: + CArrayToken items; + COptionc_char next_cursor; + + cdef enum ResultPageToken_Tag: + OkPageToken, + ErrPageToken, + + cdef struct ResultPageToken: + ResultPageToken_Tag tag; + PageToken ok; + Error err; cdef struct Token: FieldElement contract_address; @@ -195,13 +318,17 @@ cdef extern from *: TokenBalance *data; uintptr_t data_len; - cdef enum ResultCArrayTokenBalance_Tag: - OkCArrayTokenBalance, - ErrCArrayTokenBalance, + cdef struct PageTokenBalance: + CArrayTokenBalance items; + COptionc_char next_cursor; + + cdef enum ResultPageTokenBalance_Tag: + OkPageTokenBalance, + ErrPageTokenBalance, - cdef struct ResultCArrayTokenBalance: - ResultCArrayTokenBalance_Tag tag; - CArrayTokenBalance ok; + cdef struct ResultPageTokenBalance: + ResultPageTokenBalance_Tag tag; + PageTokenBalance ok; Error err; cdef struct IndexerUpdate: @@ -298,116 +425,18 @@ cdef extern from *: FieldElement hashed_keys; CArrayStruct models; - cdef struct CArrayCOptionFieldElement: - COptionFieldElement *data; - uintptr_t data_len; - - cdef struct CArrayc_char: - const char **data; - uintptr_t data_len; - - cdef struct KeysClause: - CArrayCOptionFieldElement keys; - PatternMatching pattern_matching; - CArrayc_char models; - - cdef enum Primitive_Tag: - I8, - I16, - I32, - I64, - I128, - U8, - U16, - U32, - U64, - U128, - U256_, - Bool, - Felt252, - ClassHash, - ContractAddress, - EthAddress, - - cdef struct Primitive: - Primitive_Tag tag; - int8_t i8; - int16_t i16; - int32_t i32; - int64_t i64; - uint8_t i128[16]; - uint8_t u8; - uint16_t u16; - uint32_t u32; - uint64_t u64; - uint8_t u128[16]; - U256 u256; - bool bool_; - FieldElement felt252; - FieldElement class_hash; - FieldElement contract_address; - FieldElement eth_address; - - cdef struct CArrayMemberValue: - MemberValue *data; - uintptr_t data_len; - - cdef enum MemberValue_Tag: - PrimitiveValue, - String, - List, + cdef enum COptionFieldElement_Tag: + SomeFieldElement, + NoneFieldElement, - cdef struct MemberValue: - MemberValue_Tag tag; - Primitive primitive_value; - const char *string; - CArrayMemberValue list; + cdef struct COptionFieldElement: + COptionFieldElement_Tag tag; + FieldElement some; - cdef struct MemberClause: + cdef struct OrderBy: const char *model; const char *member; - ComparisonOperator operator_; - MemberValue value; - - cdef struct CArrayClause: - Clause *data; - uintptr_t data_len; - - cdef struct CompositeClause: - LogicalOperator operator_; - CArrayClause clauses; - - cdef enum Clause_Tag: - Keys, - CMember, - Composite, - - cdef struct Clause: - Clause_Tag tag; - KeysClause keys; - MemberClause c_member; - CompositeClause composite; - - cdef enum COptionClause_Tag: - SomeClause, - NoneClause, - - cdef struct COptionClause: - COptionClause_Tag tag; - Clause some; - - cdef struct CArrayOrderBy: - OrderBy *data; - uintptr_t data_len; - - cdef struct Query: - uint32_t limit; - uint32_t offset; - COptionClause clause; - bool dont_include_hashed_keys; - CArrayOrderBy order_by; - CArrayc_char entity_models; - uint64_t entity_updated_after; + OrderDirection direction; cdef struct CArrayMember: Member *data; @@ -470,19 +499,6 @@ cdef extern from *: CArrayFieldElement hashed_keys; KeysClause entity_keys; - cdef enum COptionFieldElement_Tag: - SomeFieldElement, - NoneFieldElement, - - cdef struct COptionFieldElement: - COptionFieldElement_Tag tag; - FieldElement some; - - cdef struct OrderBy: - const char *model; - const char *member; - OrderDirection direction; - cdef struct Member: const char *name; Ty *ty; @@ -676,7 +692,7 @@ cdef extern from *: # # # Returns # Result containing array of matching entities or error - ResultCArrayEntity client_entities(ToriiClient *client, const Query *query, bool historical); + ResultCArrayEntity client_entities(ToriiClient *client, Query query, bool historical); # Retrieves event messages matching the given query # @@ -687,9 +703,7 @@ cdef extern from *: # # # Returns # Result containing array of matching event message entities or error - ResultCArrayEntity client_event_messages(ToriiClient *client, - const Query *query, - bool historical); + ResultCArrayEntity client_event_messages(ToriiClient *client, Query query, bool historical); # Gets the world metadata for the client # @@ -786,11 +800,14 @@ cdef extern from *: # # # Returns # Result containing array of Token information or error - ResultCArrayToken client_tokens(ToriiClient *client, - const FieldElement *contract_addresses, - uintptr_t contract_addresses_len, - const U256 *token_ids, - uintptr_t token_ids_len); + ResultPageToken client_tokens(ToriiClient *client, + const FieldElement *contract_addresses, + uintptr_t contract_addresses_len, + const U256 *token_ids, + uintptr_t token_ids_len, + uint32_t limit, + uint32_t offset, + const char *cursor); # Subscribes to token updates # @@ -819,13 +836,16 @@ cdef extern from *: # # # Returns # Result containing array of TokenBalance information or error - ResultCArrayTokenBalance client_token_balances(ToriiClient *client, - const FieldElement *contract_addresses, - uintptr_t contract_addresses_len, - const FieldElement *account_addresses, - uintptr_t account_addresses_len, - const U256 *token_ids, - uintptr_t token_ids_len); + ResultPageTokenBalance client_token_balances(ToriiClient *client, + const FieldElement *contract_addresses, + uintptr_t contract_addresses_len, + const FieldElement *account_addresses, + uintptr_t account_addresses_len, + const U256 *token_ids, + uintptr_t token_ids_len, + uint32_t limit, + uint32_t offset, + const char *cursor); # Subscribes to indexer updates # diff --git a/example/main.c b/example/main.c index 2fcc306..bb42a79 100644 --- a/example/main.c +++ b/example/main.c @@ -118,15 +118,18 @@ int main() // Transfer a bit of ETH to another account Call create_Game = { - .to = katana, - .selector = "create_Game", + .to = "0x0589634122ff8b84021b3f683cbf9d5adf15482289e1d70b1f8ad3052cc08711", + .selector = "move", .calldata = { - .data = malloc(sizeof(FieldElement) * 1), - .data_len = 1, + .data = malloc(sizeof(FieldElement) * 2), + .data_len = 2, }}; - hex_to_bytes("0x01", &create_Game.calldata.data[0]); + + hex_to_bytes("0x0", &create_Game.calldata.data[0]); + hex_to_bytes("0x2", &create_Game.calldata.data[1]); + // call send_transaction (rpc_url) ResultFieldElement resCreateGame = controller_execute_from_outside(g_session_account, &create_Game, 1); if (resCreateGame.tag == ErrFieldElement) { diff --git a/src/c/mod.rs b/src/c/mod.rs index b0bf3dd..65c106c 100644 --- a/src/c/mod.rs +++ b/src/c/mod.rs @@ -52,7 +52,7 @@ use torii_relay::types::Message; use torii_typed_data::TypedData; use tower_http::cors::{AllowOrigin, CorsLayer}; use types::{ - BlockId, CArray, Call, Controller, Entity, EntityKeysClause, Error, Event, IndexerUpdate, + BlockId, CArray, Call, Controller, Entity, EntityKeysClause, Error, Event, IndexerUpdate, Page, Policy, Query, Result, Signature, Struct, Token, TokenBalance, ToriiClient, Ty, WorldMetadata, }; use url::Url; @@ -88,7 +88,7 @@ pub unsafe extern "C" fn client_new( let libp2p_relay_url = unsafe { CStr::from_ptr(libp2p_relay_url).to_string_lossy().into_owned() }; - let client_future = TClient::new(torii_url, libp2p_relay_url, (&world).into()); + let client_future = TClient::new(torii_url, libp2p_relay_url, world.into()); let client = match RUNTIME.block_on(client_future) { Ok(client) => client, @@ -249,9 +249,9 @@ pub unsafe extern "C" fn controller_connect( let policies = unsafe { std::slice::from_raw_parts(policies, policies_len) }; let account_policies = policies .iter() - .map(|p| account_sdk::account::session::policy::Policy::Call(p.into())) + .map(|p| account_sdk::account::session::policy::Policy::Call(p.clone().into())) .collect::>(); - let policies = policies.iter().map(|p| p.into()).collect::>(); + let policies = policies.iter().map(|p| p.clone().into()).collect::>(); // Generate new random signing key let signing_key = SigningKey::from_random(); @@ -336,10 +336,10 @@ pub unsafe extern "C" fn controller_account( let policies = unsafe { std::slice::from_raw_parts(policies, policies_len) }; let account_policies: Vec = policies .iter() - .map(|p| account_sdk::account::session::policy::Policy::Call(p.into())) + .map(|p| account_sdk::account::session::policy::Policy::Call(p.clone().into())) .collect(); - let chain_id: Felt = (&chain_id).into(); + let chain_id: Felt = chain_id.into(); // Get project directories let project_dirs = match ProjectDirs::from("org", "dojoengine", "dojo") { @@ -467,10 +467,10 @@ pub unsafe extern "C" fn controller_clear( let policies = unsafe { std::slice::from_raw_parts(policies, policies_len) }; let account_policies: Vec = policies .iter() - .map(|p| account_sdk::account::session::policy::Policy::Call(p.into())) + .map(|p| account_sdk::account::session::policy::Policy::Call(p.clone().into())) .collect(); - let chain_id: Felt = (&chain_id).into(); + let chain_id: Felt = chain_id.into(); // Get project directories let project_dirs = match ProjectDirs::from("org", "dojoengine", "dojo") { @@ -555,7 +555,7 @@ pub unsafe extern "C" fn controller_username(controller: *mut ControllerAccount) pub unsafe extern "C" fn controller_address( controller: *mut ControllerAccount, ) -> types::FieldElement { - (&(*controller).account.address()).into() + (*controller).account.address().into() } /// Gets account chain ID @@ -569,7 +569,7 @@ pub unsafe extern "C" fn controller_address( pub unsafe extern "C" fn controller_chain_id( controller: *mut ControllerAccount, ) -> types::FieldElement { - (&(*controller).account.chain_id()).into() + (*controller).account.chain_id().into() } /// Gets account nonce @@ -588,7 +588,7 @@ pub unsafe extern "C" fn controller_nonce( Err(e) => return Result::Err(e.into()), }; - Result::Ok((&nonce).into()) + Result::Ok(nonce.into()) } /// Executes raw transaction @@ -608,11 +608,11 @@ pub unsafe extern "C" fn controller_execute_raw( ) -> Result { let calldata = unsafe { std::slice::from_raw_parts(calldata, calldata_len).to_vec() }; let calldata = - calldata.into_iter().map(|c| (&c).into()).collect::>(); + calldata.into_iter().map(|c| c.into()).collect::>(); let call = (*controller).account.execute_v3(calldata); match RUNTIME.block_on(call.send()) { - Ok(result) => Result::Ok((&result.transaction_hash).into()), + Ok(result) => Result::Ok(result.transaction_hash.into()), Err(e) => { println!("Error executing call: {:?}", e); Result::Err(e.into()) @@ -637,7 +637,7 @@ pub unsafe extern "C" fn controller_execute_from_outside( ) -> Result { let caller = OutsideExecutionCaller::Any; let calls = unsafe { std::slice::from_raw_parts(calldata, calldata_len).to_vec() }; - let calls = calls.iter().map(|c| c.into()).collect::>(); + let calls = calls.into_iter().map(|c| c.into()).collect::>(); let now = get_current_timestamp(); let outside_execution = OutsideExecutionV3 { caller: caller.into(), @@ -669,7 +669,7 @@ pub unsafe extern "C" fn controller_execute_from_outside( } }; - Result::Ok((&res.transaction_hash).into()) + Result::Ok(res.transaction_hash.into()) } /// Sets a logger callback function for the client @@ -712,7 +712,7 @@ pub unsafe extern "C" fn client_publish_message( }; let signature = unsafe { std::slice::from_raw_parts(signature_felts, signature_felts_len) }; - let signature = signature.iter().map(|f| (&f.clone()).into()).collect::>(); + let signature = signature.iter().map(|f| f.clone().into()).collect::>(); let client_future = unsafe { (*client).inner.publish_message(Message { message, signature }) }; @@ -742,14 +742,14 @@ pub unsafe extern "C" fn client_controllers( } else { let addresses = unsafe { std::slice::from_raw_parts(contract_addresses, contract_addresses_len) }; - addresses.iter().map(|f| (&f.clone()).into()).collect::>() + addresses.iter().map(|f| f.clone().into()).collect::>() }; let controllers_future = unsafe { (*client).inner.controllers(contract_addresses) }; match RUNTIME.block_on(controllers_future) { Ok(controllers) => { - let controllers: Vec = controllers.iter().map(|c| c.into()).collect(); + let controllers: Vec = controllers.into_iter().map(|c| c.into()).collect(); Result::Ok(controllers.into()) } Err(e) => Result::Err(e.into()), @@ -767,14 +767,15 @@ pub unsafe extern "C" fn client_controllers( #[no_mangle] pub unsafe extern "C" fn client_entities( client: *mut ToriiClient, - query: &Query, + query: Query, historical: bool, ) -> Result> { - let entities_future = unsafe { (*client).inner.entities(query.into(), historical) }; + let query = query.clone().into(); + let entities_future = unsafe { (*client).inner.entities(query, historical) }; match RUNTIME.block_on(entities_future) { Ok(entities) => { - let entities: Vec = entities.into_iter().map(|e| (&e).into()).collect(); + let entities: Vec = entities.into_iter().map(|e| e.into()).collect(); Result::Ok(entities.into()) } @@ -794,15 +795,16 @@ pub unsafe extern "C" fn client_entities( #[no_mangle] pub unsafe extern "C" fn client_event_messages( client: *mut ToriiClient, - query: &Query, + query: Query, historical: bool, ) -> Result> { - let event_messages_future = unsafe { (*client).inner.event_messages(query.into(), historical) }; + let query = query.clone().into(); + let event_messages_future = unsafe { (*client).inner.event_messages(query, historical) }; match RUNTIME.block_on(event_messages_future) { Ok(event_messages) => { let event_messages: Vec = - event_messages.into_iter().map(|e| (&e).into()).collect(); + event_messages.into_iter().map(|e| e.into()).collect(); Result::Ok(event_messages.into()) } @@ -821,7 +823,7 @@ pub unsafe extern "C" fn client_event_messages( pub unsafe extern "C" fn client_metadata(client: *mut ToriiClient) -> Result { let metadata_future = unsafe { (*client).inner.metadata() }; match RUNTIME.block_on(metadata_future) { - Ok(metadata) => Result::Ok((&metadata).into()), + Ok(metadata) => Result::Ok(metadata.into()), Err(e) => Result::Err(e.into()), } } @@ -848,7 +850,7 @@ pub unsafe extern "C" fn client_on_entity_state_update( Vec::new() } else { let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) }; - clauses.iter().map(|c| c.into()).collect::>() + clauses.iter().map(|c| c.clone().into()).collect::>() }; let subscription_id = Arc::new(AtomicU64::new(0)); @@ -872,9 +874,8 @@ pub unsafe extern "C" fn client_on_entity_state_update( while let Some(Ok((id, entity))) = rcv.next().await { subscription_id_clone.store(id, Ordering::SeqCst); - let key: types::FieldElement = (&entity.hashed_keys).into(); - let models: Vec = - entity.models.into_iter().map(|e| (&e).into()).collect(); + let key: types::FieldElement = entity.hashed_keys.into(); + let models: Vec = entity.models.into_iter().map(|e| e.into()).collect(); callback(key, models.into()); } } @@ -913,7 +914,7 @@ pub unsafe extern "C" fn client_update_entity_subscription( Vec::new() } else { let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) }; - clauses.iter().map(|c| c.into()).collect::>() + clauses.iter().map(|c| c.clone().into()).collect::>() }; match RUNTIME.block_on( @@ -949,7 +950,7 @@ pub unsafe extern "C" fn client_on_event_message_update( Vec::new() } else { let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) }; - clauses.iter().map(|c| c.into()).collect::>() + clauses.iter().map(|c| c.clone().into()).collect::>() }; let subscription_id = Arc::new(AtomicU64::new(0)); @@ -973,9 +974,8 @@ pub unsafe extern "C" fn client_on_event_message_update( while let Some(Ok((id, entity))) = rcv.next().await { subscription_id_clone.store(id, Ordering::SeqCst); - let key: types::FieldElement = (&entity.hashed_keys).into(); - let models: Vec = - entity.models.into_iter().map(|e| (&e).into()).collect(); + let key: types::FieldElement = entity.hashed_keys.into(); + let models: Vec = entity.models.into_iter().map(|e| e.into()).collect(); callback(key, models.into()); } } @@ -1015,7 +1015,7 @@ pub unsafe extern "C" fn client_update_event_message_subscription( Vec::new() } else { let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) }; - clauses.iter().map(|c| c.into()).collect::>() + clauses.iter().map(|c| c.clone().into()).collect::>() }; match RUNTIME.block_on( @@ -1050,7 +1050,7 @@ pub unsafe extern "C" fn client_on_starknet_event( Vec::new() } else { let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) }; - clauses.iter().map(|c| c.into()).collect::>() + clauses.iter().map(|c| c.clone().into()).collect::>() }; let subscription_id = Arc::new(AtomicU64::new(0)); @@ -1073,7 +1073,7 @@ pub unsafe extern "C" fn client_on_starknet_event( let mut rcv = rcv.take_until_if(tripwire.clone()); while let Some(Ok(event)) = rcv.next().await { - callback((&event).into()); + callback(event.into()); } } @@ -1106,27 +1106,43 @@ pub unsafe extern "C" fn client_tokens( contract_addresses_len: usize, token_ids: *const types::U256, token_ids_len: usize, -) -> Result> { + limit: u32, + offset: u32, + cursor: *const c_char, +) -> Result> { let contract_addresses = if contract_addresses.is_null() || contract_addresses_len == 0 { Vec::new() } else { let addresses = unsafe { std::slice::from_raw_parts(contract_addresses, contract_addresses_len) }; - addresses.iter().map(|f| (&f.clone()).into()).collect::>() + addresses.iter().map(|f| f.clone().into()).collect::>() }; let token_ids = if token_ids.is_null() || token_ids_len == 0 { Vec::new() } else { let ids = unsafe { std::slice::from_raw_parts(token_ids, token_ids_len) }; - ids.iter().map(|f| (&f.clone()).into()).collect::>() + ids.iter().map(|f| f.clone().into()).collect::>() }; - let tokens = match RUNTIME.block_on((*client).inner.tokens(contract_addresses, token_ids)) { + let limit = if limit == 0 { None } else { Some(limit) }; + let offset = if offset == 0 { None } else { Some(offset) }; + let cursor = if cursor.is_null() { + None + } else { + Some(unsafe { std::ffi::CStr::from_ptr(cursor).to_string_lossy().into_owned() }) + }; + + let tokens = match RUNTIME.block_on((*client).inner.tokens( + contract_addresses, + token_ids, + limit, + offset, + cursor, + )) { Ok(tokens) => tokens, Err(e) => return Result::Err(e.into()), }; - let tokens = tokens.iter().map(|t| t.into()).collect::>(); Result::Ok(tokens.into()) } @@ -1156,14 +1172,14 @@ pub unsafe extern "C" fn client_on_token_update( } else { let addresses = unsafe { std::slice::from_raw_parts(contract_addresses, contract_addresses_len) }; - addresses.iter().map(|f| (&f.clone()).into()).collect::>() + addresses.iter().map(|f| f.clone().into()).collect::>() }; let token_ids = if token_ids.is_null() || token_ids_len == 0 { Vec::new() } else { let ids = unsafe { std::slice::from_raw_parts(token_ids, token_ids_len) }; - ids.iter().map(|f| (&f.clone()).into()).collect::>() + ids.iter().map(|f| f.clone().into()).collect::>() }; let subscription_id = Arc::new(AtomicU64::new(0)); @@ -1190,7 +1206,7 @@ pub unsafe extern "C" fn client_on_token_update( while let Some(Ok((id, token))) = rcv.next().await { subscription_id.store(id, Ordering::SeqCst); - let token: Token = (&token).into(); + let token: Token = token.into(); callback(token); } } @@ -1228,13 +1244,16 @@ pub unsafe extern "C" fn client_token_balances( account_addresses_len: usize, token_ids: *const types::U256, token_ids_len: usize, -) -> Result> { + limit: u32, + offset: u32, + cursor: *const c_char, +) -> Result> { let account_addresses = if account_addresses.is_null() || account_addresses_len == 0 { Vec::new() } else { let addresses = unsafe { std::slice::from_raw_parts(account_addresses, account_addresses_len) }; - addresses.iter().map(|f| (&f.clone()).into()).collect::>() + addresses.iter().map(|f| f.clone().into()).collect::>() }; let contract_addresses = if contract_addresses.is_null() || contract_addresses_len == 0 { @@ -1242,26 +1261,32 @@ pub unsafe extern "C" fn client_token_balances( } else { let addresses = unsafe { std::slice::from_raw_parts(contract_addresses, contract_addresses_len) }; - addresses.iter().map(|f| (&f.clone()).into()).collect::>() + addresses.iter().map(|f| f.clone().into()).collect::>() }; let token_ids = if token_ids.is_null() || token_ids_len == 0 { Vec::new() } else { let ids = unsafe { std::slice::from_raw_parts(token_ids, token_ids_len) }; - ids.iter().map(|f| (&f.clone()).into()).collect::>() + ids.iter().map(|f| f.clone().into()).collect::>() }; let token_balances = match RUNTIME.block_on((*client).inner.token_balances( account_addresses, contract_addresses, token_ids, + if limit == 0 { None } else { Some(limit) }, + if offset == 0 { None } else { Some(offset) }, + if cursor.is_null() { + None + } else { + Some(unsafe { std::ffi::CStr::from_ptr(cursor).to_string_lossy().into_owned() }) + }, )) { Ok(balances) => balances, Err(e) => return Result::Err(e.into()), }; - let token_balances = token_balances.iter().map(|t| t.into()).collect::>(); Result::Ok(token_balances.into()) } @@ -1284,7 +1309,7 @@ pub unsafe extern "C" fn on_indexer_update( let contract_address = if contract_address.is_null() { None } else { - Some(unsafe { (&*contract_address).into() }) + Some(unsafe { (*contract_address).clone().into() }) }; let subscription_id = Arc::new(AtomicU64::new(0)); @@ -1306,7 +1331,7 @@ pub unsafe extern "C" fn on_indexer_update( let mut rcv = rcv.take_until_if(tripwire.clone()); while let Some(Ok(update)) = rcv.next().await { - callback((&update).into()); + callback(update.into()); } } @@ -1354,7 +1379,7 @@ pub unsafe extern "C" fn client_on_token_balance_update( } else { let addresses = unsafe { std::slice::from_raw_parts(account_addresses, account_addresses_len) }; - addresses.iter().map(|f| (&f.clone()).into()).collect::>() + addresses.iter().map(|f| f.clone().into()).collect::>() }; // Convert contract addresses array to Vec if not empty @@ -1363,14 +1388,14 @@ pub unsafe extern "C" fn client_on_token_balance_update( } else { let addresses = unsafe { std::slice::from_raw_parts(contract_addresses, contract_addresses_len) }; - addresses.iter().map(|f| (&f.clone()).into()).collect::>() + addresses.iter().map(|f| f.clone().into()).collect::>() }; let token_ids = if token_ids.is_null() || token_ids_len == 0 { Vec::new() } else { let ids = unsafe { std::slice::from_raw_parts(token_ids, token_ids_len) }; - ids.iter().map(|f| (&f.clone()).into()).collect::>() + ids.iter().map(|f| f.clone().into()).collect::>() }; let subscription_id = Arc::new(AtomicU64::new(0)); @@ -1401,7 +1426,7 @@ pub unsafe extern "C" fn client_on_token_balance_update( while let Some(Ok((id, balance))) = rcv.next().await { subscription_id.store(id, Ordering::SeqCst); - let balance: TokenBalance = (&balance).into(); + let balance: TokenBalance = balance.into(); callback(balance); } } @@ -1447,7 +1472,7 @@ pub unsafe extern "C" fn client_update_token_balance_subscription( } else { let addresses = unsafe { std::slice::from_raw_parts(contract_addresses, contract_addresses_len) }; - addresses.iter().map(|f| (&f.clone()).into()).collect::>() + addresses.iter().map(|f| f.clone().into()).collect::>() }; let account_addresses = if account_addresses.is_null() || account_addresses_len == 0 { @@ -1455,14 +1480,14 @@ pub unsafe extern "C" fn client_update_token_balance_subscription( } else { let addresses = unsafe { std::slice::from_raw_parts(account_addresses, account_addresses_len) }; - addresses.iter().map(|f| (&f.clone()).into()).collect::>() + addresses.iter().map(|f| f.clone().into()).collect::>() }; let token_ids = if token_ids.is_null() || token_ids_len == 0 { Vec::new() } else { let ids = unsafe { std::slice::from_raw_parts(token_ids, token_ids_len) }; - ids.iter().map(|f| (&f.clone()).into()).collect::>() + ids.iter().map(|f| f.clone().into()).collect::>() }; match RUNTIME.block_on((*client).inner.update_token_balance_subscription( @@ -1494,7 +1519,7 @@ pub unsafe extern "C" fn bytearray_serialize( }; let felts = cairo_serde::ByteArray::cairo_serialize(&bytearray); - let felts = felts.iter().map(|f| f.into()).collect::>(); + let felts = felts.into_iter().map(|f| f.into()).collect::>(); Result::Ok(felts.into()) } @@ -1512,7 +1537,7 @@ pub unsafe extern "C" fn bytearray_deserialize( felts_len: usize, ) -> Result<*const c_char> { let felts = unsafe { std::slice::from_raw_parts(felts, felts_len) }; - let felts = felts.iter().map(|f| (&f.clone()).into()).collect::>(); + let felts = felts.iter().map(|f| f.clone().into()).collect::>(); let bytearray = match cairo_serde::ByteArray::cairo_deserialize(&felts, 0) { Ok(bytearray) => bytearray, Err(e) => return Result::Err(e.into()), @@ -1540,9 +1565,9 @@ pub unsafe extern "C" fn poseidon_hash( felts_len: usize, ) -> types::FieldElement { let felts = unsafe { std::slice::from_raw_parts(felts, felts_len) }; - let felts = felts.iter().map(|f| (&f.clone()).into()).collect::>(); + let felts = felts.iter().map(|f| f.clone().into()).collect::>(); - (&poseidon_hash_many(&felts)).into() + poseidon_hash_many(&felts).into() } /// Gets selector from name string @@ -1562,7 +1587,7 @@ pub unsafe extern "C" fn get_selector_from_name( Err(e) => return Result::Err(e.into()), }; - Result::Ok((&selector).into()) + Result::Ok(selector.into()) } /// Gets selector from tag string @@ -1577,7 +1602,7 @@ pub unsafe extern "C" fn get_selector_from_tag(tag: *const c_char) -> types::Fie let tag = unsafe { CStr::from_ptr(tag).to_string_lossy().into_owned() }; let selector = compute_selector_from_tag(tag.as_str()); - (&selector).into() + selector.into() } /// Computes Starknet keccak hash of bytes @@ -1596,7 +1621,7 @@ pub unsafe extern "C" fn starknet_keccak( let bytes = unsafe { std::slice::from_raw_parts(bytes, bytes_len) }; let hash = starknet::core::utils::starknet_keccak(bytes); - (&hash).into() + hash.into() } /// Converts a short string to field element @@ -1616,7 +1641,7 @@ pub unsafe extern "C" fn cairo_short_string_to_felt( Err(e) => return Result::Err(e.into()), }; - Result::Ok((&felt).into()) + Result::Ok(felt.into()) } /// Parses a field element into a short string @@ -1630,7 +1655,7 @@ pub unsafe extern "C" fn cairo_short_string_to_felt( pub unsafe extern "C" fn parse_cairo_short_string( felt: types::FieldElement, ) -> Result<*const c_char> { - let felt = (&felt).into(); + let felt = felt.into(); let str = match starknet::core::utils::parse_cairo_short_string(&felt) { Ok(str) => str, Err(e) => return Result::Err(e.into()), @@ -1662,13 +1687,13 @@ pub unsafe extern "C" fn typed_data_encode( } }; - let address = (&address).into(); + let address = address.into(); let encoded = match typed_data.encode(address) { Ok(encoded) => encoded, Err(err) => return Result::Err(err.into()), }; - Result::Ok((&encoded).into()) + Result::Ok(encoded.into()) } /// Generates a new signing key @@ -1678,7 +1703,7 @@ pub unsafe extern "C" fn typed_data_encode( #[no_mangle] pub unsafe extern "C" fn signing_key_new() -> types::FieldElement { let private_key = SigningKey::from_random(); - (&private_key.secret_scalar()).into() + private_key.secret_scalar().into() } /// Signs a hash with a private key @@ -1694,11 +1719,11 @@ pub unsafe extern "C" fn signing_key_sign( private_key: types::FieldElement, hash: types::FieldElement, ) -> Result { - let private_key = SigningKey::from_secret_scalar((&private_key).into()); - let sig = private_key.sign(&(&hash).into()); + let private_key = SigningKey::from_secret_scalar(private_key.into()); + let sig = private_key.sign(&hash.into()); match sig { - Ok(sig) => Result::Ok((&sig).into()), + Ok(sig) => Result::Ok(sig.into()), Err(e) => Result::Err(e.into()), } } @@ -1714,10 +1739,10 @@ pub unsafe extern "C" fn signing_key_sign( pub unsafe extern "C" fn verifying_key_new( signing_key: types::FieldElement, ) -> types::FieldElement { - let signing_key = (&signing_key).into(); + let signing_key = signing_key.into(); let verifying_key = starknet_crypto::get_public_key(&signing_key); - (&verifying_key).into() + verifying_key.into() } /// Verifies a signature @@ -1735,9 +1760,9 @@ pub unsafe extern "C" fn verifying_key_verify( hash: types::FieldElement, signature: types::Signature, ) -> Result { - let verifying_key = VerifyingKey::from_scalar((&verifying_key).into()); - let signature = &(&signature).into(); - let hash = &(&hash).into(); + let verifying_key = VerifyingKey::from_scalar(verifying_key.into()); + let signature = &signature.into(); + let hash = &hash.into(); match verifying_key.verify(hash, signature) { Ok(result) => Result::Ok(result), @@ -1791,8 +1816,7 @@ pub unsafe extern "C" fn account_new( Err(e) => return Result::Err(e.into()), }; - let signer = - LocalWallet::from_signing_key(SigningKey::from_secret_scalar((&private_key).into())); + let signer = LocalWallet::from_signing_key(SigningKey::from_secret_scalar(private_key.into())); let account = SingleOwnerAccount::new( (*rpc).0.clone(), signer, @@ -1819,16 +1843,15 @@ pub unsafe extern "C" fn starknet_call( call: Call, block_id: BlockId, ) -> Result> { - let res = - match RUNTIME.block_on((*provider).0.call::( - (&call).into(), - (&block_id).into(), - )) { - Ok(res) => res, - Err(e) => return Result::Err(e.into()), - }; + let res = match RUNTIME.block_on( + (*provider) + .0 + .call::(call.into(), block_id.into()), + ) { + Ok(res) => res, + Err(e) => return Result::Err(e.into()), + }; - let res: Vec<_> = res.iter().map(|f| f.into()).collect::>(); Result::Ok(res.into()) } @@ -1847,7 +1870,7 @@ pub unsafe extern "C" fn account_deploy_burner( master_account: *mut Account, signing_key: types::FieldElement, ) -> Result<*mut Account> { - let signing_key = SigningKey::from_secret_scalar((&signing_key).into()); + let signing_key = SigningKey::from_secret_scalar(signing_key.into()); let verifying_key = signing_key.verifying_key(); let address = get_contract_address( verifying_key.scalar(), @@ -1900,7 +1923,7 @@ pub unsafe extern "C" fn account_deploy_burner( /// FieldElement containing the account address #[no_mangle] pub unsafe extern "C" fn account_address(account: *mut Account) -> types::FieldElement { - (&(*account).0.address()).into() + (*account).0.address().into() } /// Gets account chain ID @@ -1912,7 +1935,7 @@ pub unsafe extern "C" fn account_address(account: *mut Account) -> types::FieldE /// FieldElement containing the chain ID #[no_mangle] pub unsafe extern "C" fn account_chain_id(account: *mut Account) -> types::FieldElement { - (&(*account).0.chain_id()).into() + (*account).0.chain_id().into() } /// Sets block ID for account @@ -1922,7 +1945,7 @@ pub unsafe extern "C" fn account_chain_id(account: *mut Account) -> types::Field /// * `block_id` - New block ID #[no_mangle] pub unsafe extern "C" fn account_set_block_id(account: *mut Account, block_id: BlockId) { - let block_id = (&block_id).into(); + let block_id = block_id.into(); (*account).0.set_block_id(block_id); } @@ -1940,7 +1963,7 @@ pub unsafe extern "C" fn account_nonce(account: *mut Account) -> Result return Result::Err(e.into()), }; - Result::Ok((&nonce).into()) + Result::Ok(nonce.into()) } /// Executes raw transaction @@ -1960,11 +1983,11 @@ pub unsafe extern "C" fn account_execute_raw( ) -> Result { let calldata = unsafe { std::slice::from_raw_parts(calldata, calldata_len).to_vec() }; let calldata = - calldata.into_iter().map(|c| (&c).into()).collect::>(); + calldata.into_iter().map(|c| c.into()).collect::>(); let call = (*account).0.execute_v3(calldata); match RUNTIME.block_on(call.send()) { - Ok(result) => Result::Ok((&result.transaction_hash).into()), + Ok(result) => Result::Ok(result.transaction_hash.into()), Err(e) => Result::Err(e.into()), } } @@ -1982,7 +2005,7 @@ pub unsafe extern "C" fn wait_for_transaction( rpc: *mut Provider, txn_hash: types::FieldElement, ) -> Result { - let txn_hash = (&txn_hash).into(); + let txn_hash = txn_hash.into(); match RUNTIME.block_on(watch_tx(&(*rpc).0, txn_hash)) { Ok(_) => Result::Ok(true), Err(e) => Result::Err(Error { message: CString::new(e.to_string()).unwrap().into_raw() }), @@ -2008,18 +2031,18 @@ pub unsafe extern "C" fn hash_get_contract_address( constructor_calldata_len: usize, deployer_address: types::FieldElement, ) -> types::FieldElement { - let class_hash = (&class_hash).into(); - let salt = (&salt).into(); + let class_hash = class_hash.into(); + let salt = salt.into(); let constructor_calldata = unsafe { std::slice::from_raw_parts(constructor_calldata, constructor_calldata_len).to_vec() }; let constructor_calldata = - constructor_calldata.iter().map(|f| (&f.clone()).into()).collect::>(); - let deployer_address = (&deployer_address).into(); + constructor_calldata.iter().map(|f| f.clone().into()).collect::>(); + let deployer_address = deployer_address.into(); let address = get_contract_address(salt, class_hash, &constructor_calldata, deployer_address); - (&address).into() + address.into() } /// Cancels a subscription @@ -2069,7 +2092,7 @@ pub unsafe extern "C" fn provider_free(rpc: *mut Provider) { #[no_mangle] pub unsafe extern "C" fn model_free(model: *mut Struct) { if !model.is_null() { - let _: dojo_types::schema::Struct = (&*Box::::from_raw(model)).into(); + let _: dojo_types::schema::Struct = (*Box::::from_raw(model)).into(); } } @@ -2093,7 +2116,7 @@ pub unsafe extern "C" fn account_free(account: *mut Account) { #[no_mangle] pub unsafe extern "C" fn ty_free(ty: *mut Ty) { if !ty.is_null() { - let _: dojo_types::schema::Ty = (&*Box::::from_raw(ty)).into(); + let _: dojo_types::schema::Ty = (*Box::::from_raw(ty)).into(); } } @@ -2104,7 +2127,7 @@ pub unsafe extern "C" fn ty_free(ty: *mut Ty) { #[no_mangle] pub unsafe extern "C" fn entity_free(entity: *mut Entity) { if !entity.is_null() { - let _: torii_grpc::types::schema::Entity = (&*Box::::from_raw(entity)).into(); + let _: torii_grpc::types::schema::Entity = (*Box::::from_raw(entity)).into(); } } @@ -2126,7 +2149,7 @@ pub unsafe extern "C" fn error_free(error: *mut Error) { #[no_mangle] pub unsafe extern "C" fn world_metadata_free(metadata: *mut WorldMetadata) { if !metadata.is_null() { - let _: dojo_types::WorldMetadata = (&*Box::::from_raw(metadata)).into(); + let _: dojo_types::WorldMetadata = (*Box::::from_raw(metadata)).into(); } } diff --git a/src/c/types.rs b/src/c/types.rs index 4aec7f1..7d2b886 100644 --- a/src/c/types.rs +++ b/src/c/types.rs @@ -4,6 +4,30 @@ use crypto_bigint::Encoding; use starknet::core::utils::get_selector_from_name; use torii_client::client::Client; +#[derive(Debug, Clone)] +#[repr(C)] +pub struct Page { + pub items: CArray, + pub next_cursor: COption<*const c_char>, +} + +impl From> for Page +where + U: From, +{ + fn from(val: torii_grpc::types::Page) -> Self { + let items = val.items.into_iter().map(|t| t.into()).collect::>(); + Page { + items: items.into(), + next_cursor: if val.next_cursor.is_empty() { + COption::None + } else { + COption::Some(CString::new(val.next_cursor).unwrap().into_raw()) + }, + } + } +} + #[derive(Debug, Clone)] #[repr(C)] pub enum Result { @@ -34,10 +58,13 @@ impl COption { } } -impl From> for COption { +impl From> for COption +where + U: From, +{ fn from(val: Option) -> Self { match val { - Some(v) => COption::Some(v), + Some(v) => COption::Some(v.into()), None => COption::None, } } @@ -60,20 +87,20 @@ pub struct Policy { pub description: *const c_char, } -impl From<&Policy> for crate::types::Policy { - fn from(val: &Policy) -> Self { +impl From for crate::types::Policy { + fn from(val: Policy) -> Self { crate::types::Policy { - target: (&val.target).into(), + target: val.target.into(), method: unsafe { CStr::from_ptr(val.method).to_string_lossy().to_string() }, description: unsafe { CStr::from_ptr(val.description).to_string_lossy().to_string() }, } } } -impl From<&Policy> for account_sdk::account::session::policy::CallPolicy { - fn from(val: &Policy) -> Self { +impl From for account_sdk::account::session::policy::CallPolicy { + fn from(val: Policy) -> Self { account_sdk::account::session::policy::CallPolicy { - contract_address: (&val.target).into(), + contract_address: val.target.into(), selector: get_selector_from_name(&unsafe { CStr::from_ptr(val.method).to_string_lossy().to_string() }) @@ -91,10 +118,10 @@ pub struct Controller { pub deployed_at_timestamp: u64, } -impl From<&torii_grpc::types::Controller> for Controller { - fn from(val: &torii_grpc::types::Controller) -> Self { +impl From for Controller { + fn from(val: torii_grpc::types::Controller) -> Self { Controller { - address: (&val.address).into(), + address: val.address.into(), username: CString::new(val.username.clone()).unwrap().into_raw(), deployed_at_timestamp: val.deployed_at, } @@ -112,11 +139,11 @@ pub struct Token { pub metadata: *const c_char, } -impl From<&torii_grpc::types::Token> for Token { - fn from(val: &torii_grpc::types::Token) -> Self { +impl From for Token { + fn from(val: torii_grpc::types::Token) -> Self { Token { - token_id: (&val.token_id).into(), - contract_address: (&val.contract_address).into(), + token_id: val.token_id.into(), + contract_address: val.contract_address.into(), name: CString::new(val.name.clone()).unwrap().into_raw(), symbol: CString::new(val.symbol.clone()).unwrap().into_raw(), decimals: val.decimals, @@ -134,13 +161,13 @@ pub struct TokenBalance { pub token_id: U256, } -impl From<&torii_grpc::types::TokenBalance> for TokenBalance { - fn from(val: &torii_grpc::types::TokenBalance) -> Self { +impl From for TokenBalance { + fn from(val: torii_grpc::types::TokenBalance) -> Self { TokenBalance { - balance: (&val.balance).into(), - account_address: (&val.account_address).into(), - contract_address: (&val.contract_address).into(), - token_id: (&val.token_id).into(), + balance: val.balance.into(), + account_address: val.account_address.into(), + contract_address: val.contract_address.into(), + token_id: val.token_id.into(), } } } @@ -154,24 +181,24 @@ pub struct IndexerUpdate { pub contract_address: FieldElement, } -impl From<&IndexerUpdate> for torii_grpc::types::IndexerUpdate { - fn from(val: &IndexerUpdate) -> Self { +impl From for torii_grpc::types::IndexerUpdate { + fn from(val: IndexerUpdate) -> Self { torii_grpc::types::IndexerUpdate { head: val.head, tps: val.tps, last_block_timestamp: val.last_block_timestamp, - contract_address: (&val.contract_address).into(), + contract_address: val.contract_address.into(), } } } -impl From<&torii_grpc::types::IndexerUpdate> for IndexerUpdate { - fn from(val: &torii_grpc::types::IndexerUpdate) -> Self { +impl From for IndexerUpdate { + fn from(val: torii_grpc::types::IndexerUpdate) -> Self { IndexerUpdate { head: val.head, tps: val.tps, last_block_timestamp: val.last_block_timestamp, - contract_address: (&val.contract_address).into(), + contract_address: val.contract_address.into(), } } } @@ -185,15 +212,15 @@ pub struct Signature { pub s: FieldElement, } -impl From<&Signature> for starknet::core::crypto::Signature { - fn from(val: &Signature) -> Self { - Self { r: (&val.r).into(), s: (&val.s).into() } +impl From for starknet::core::crypto::Signature { + fn from(val: Signature) -> Self { + Self { r: val.r.into(), s: val.s.into() } } } -impl From<&starknet::core::crypto::Signature> for Signature { - fn from(val: &starknet::core::crypto::Signature) -> Self { - Signature { r: (&val.r).into(), s: (&val.s).into() } +impl From for Signature { + fn from(val: starknet::core::crypto::Signature) -> Self { + Signature { r: val.r.into(), s: val.s.into() } } } @@ -221,18 +248,18 @@ pub enum BlockTag { Pending, } -impl From<&BlockId> for starknet::core::types::BlockId { - fn from(val: &BlockId) -> Self { +impl From for starknet::core::types::BlockId { + fn from(val: BlockId) -> Self { match val { - BlockId::Hash(hash) => starknet::core::types::BlockId::Hash((&hash.clone()).into()), - BlockId::Number(number) => starknet::core::types::BlockId::Number(*number), - BlockId::BlockTag_(tag) => starknet::core::types::BlockId::Tag((&tag.clone()).into()), + BlockId::Hash(hash) => starknet::core::types::BlockId::Hash(hash.into()), + BlockId::Number(number) => starknet::core::types::BlockId::Number(number), + BlockId::BlockTag_(tag) => starknet::core::types::BlockId::Tag(tag.into()), } } } -impl From<&BlockTag> for starknet::core::types::BlockTag { - fn from(val: &BlockTag) -> Self { +impl From for starknet::core::types::BlockTag { + fn from(val: BlockTag) -> Self { match val { BlockTag::Latest => starknet::core::types::BlockTag::Latest, BlockTag::Pending => starknet::core::types::BlockTag::Pending, @@ -240,32 +267,26 @@ impl From<&BlockTag> for starknet::core::types::BlockTag { } } -impl From<&Call> for starknet::core::types::Call { - fn from(val: &Call) -> Self { +impl From for starknet::core::types::Call { + fn from(val: Call) -> Self { let selector = unsafe { CStr::from_ptr(val.selector).to_string_lossy().to_string() }; - - let calldata: Vec = (&val.calldata).into(); - let calldata = std::mem::ManuallyDrop::new(calldata); - let calldata = calldata.iter().map(|c| (&c.clone()).into()).collect(); + let calldata: Vec<_> = val.calldata.into(); starknet::core::types::Call { - to: (&val.to).into(), + to: val.to.into(), selector: get_selector_from_name(&selector).unwrap(), calldata, } } } -impl From<&Call> for starknet::core::types::FunctionCall { - fn from(val: &Call) -> Self { +impl From for starknet::core::types::FunctionCall { + fn from(val: Call) -> Self { let selector = unsafe { CStr::from_ptr(val.selector).to_string_lossy().to_string() }; - - let calldata: Vec = (&val.calldata).into(); - let calldata = std::mem::ManuallyDrop::new(calldata); - let calldata = calldata.iter().map(|c| (&c.clone()).into()).collect(); + let calldata: Vec<_> = val.calldata.into(); starknet::core::types::FunctionCall { - contract_address: (&val.to).into(), + contract_address: val.to.into(), entry_point_selector: get_selector_from_name(&selector).unwrap(), calldata, } @@ -284,8 +305,12 @@ pub struct CArray { pub data_len: usize, } -impl From> for CArray { +impl From> for CArray +where + U: From, +{ fn from(val: Vec) -> Self { + let val = val.into_iter().map(|v| v.into()).collect::>(); let mut val = std::mem::ManuallyDrop::new(val); val.shrink_to_fit(); @@ -293,34 +318,58 @@ impl From> for CArray { } } -impl From<&CArray> for Vec { - fn from(val: &CArray) -> Self { - unsafe { std::slice::from_raw_parts(val.data, val.data_len).to_vec() } +impl> From> for Vec { + fn from(val: CArray) -> Self { + let mut vec = Vec::with_capacity(val.data_len); + unsafe { + for i in 0..val.data_len { + vec.push((*val.data.add(i)).clone().into()); + } + } + vec } } -impl From<&CArray<*const c_char>> for Vec { - fn from(val: &CArray<*const c_char>) -> Self { - let mut strings = Vec::with_capacity(val.data_len); - for i in 0..val.data_len { - let c_str = unsafe { CStr::from_ptr(val.data.wrapping_add(i).read()) }; - strings.push(c_str.to_string_lossy().into_owned()); +#[derive(Clone, Debug)] +pub struct COptionArray(CArray>); + +impl> From> for Vec> { + fn from(val: COptionArray) -> Self { + let mut vec = Vec::with_capacity(val.0.data_len); + unsafe { + for i in 0..val.0.data_len { + vec.push((*val.0.data.add(i)).clone().map(|v| v.into()).into()); + } } - strings + vec } } -impl From> for CArray<*const c_char> { - fn from(val: Vec) -> Self { - let c_strings: Vec<*const c_char> = - val.into_iter().map(|s| CString::new(s).unwrap().into_raw() as *const c_char).collect(); +#[derive(Clone, Debug)] +pub struct StringVec(Vec); + +impl From for CArray<*const c_char> { + fn from(val: StringVec) -> Self { + let vec = val + .0 + .into_iter() + .map(|s| CString::new(s).unwrap().into_raw() as *const c_char) + .collect::>(); - let data = c_strings.as_ptr() as *mut *const c_char; - let data_len = c_strings.len(); + vec.into() + } +} - std::mem::forget(c_strings); +#[derive(Clone, Debug)] +pub struct CStringArray(CArray<*const c_char>); - CArray { data, data_len } +impl From for Vec { + fn from(val: CStringArray) -> Self { + let mut vec = Vec::with_capacity(val.0.data_len); + for i in 0..val.0.data_len { + vec.push(unsafe { CStr::from_ptr(*val.0.data.add(i)).to_string_lossy().into_owned() }); + } + vec } } @@ -353,14 +402,14 @@ pub struct U256 { data: [u8; 32], } -impl From<&U256> for crypto_bigint::U256 { - fn from(val: &U256) -> Self { +impl From for crypto_bigint::U256 { + fn from(val: U256) -> Self { crypto_bigint::U256::from_be_slice(&val.data) } } -impl From<&crypto_bigint::U256> for U256 { - fn from(val: &crypto_bigint::U256) -> Self { +impl From for U256 { + fn from(val: crypto_bigint::U256) -> Self { U256 { data: val.to_be_bytes() } } } @@ -371,14 +420,14 @@ pub struct FieldElement { data: [u8; 32], } -impl From<&FieldElement> for starknet::core::types::Felt { - fn from(val: &FieldElement) -> Self { +impl From for starknet::core::types::Felt { + fn from(val: FieldElement) -> Self { starknet::core::types::Felt::from_bytes_be(&val.data) } } -impl From<&starknet::core::types::Felt> for FieldElement { - fn from(val: &starknet::core::types::Felt) -> Self { +impl From for FieldElement { + fn from(val: starknet::core::types::Felt) -> Self { FieldElement { data: val.to_bytes_be() } } } @@ -403,22 +452,22 @@ pub struct OrderBy { pub direction: OrderDirection, } -impl From<&OrderBy> for torii_grpc::types::OrderBy { - fn from(val: &OrderBy) -> Self { +impl From for torii_grpc::types::OrderBy { + fn from(val: OrderBy) -> Self { torii_grpc::types::OrderBy { model: unsafe { CStr::from_ptr(val.model).to_string_lossy().to_string() }, member: unsafe { CStr::from_ptr(val.member).to_string_lossy().to_string() }, - direction: (&val.direction).into(), + direction: val.direction.into(), } } } -impl From<&torii_grpc::types::OrderBy> for OrderBy { - fn from(val: &torii_grpc::types::OrderBy) -> Self { +impl From for OrderBy { + fn from(val: torii_grpc::types::OrderBy) -> Self { OrderBy { model: CString::new(val.model.clone()).unwrap().into_raw(), member: CString::new(val.member.clone()).unwrap().into_raw(), - direction: (&val.direction).into(), + direction: val.direction.into(), } } } @@ -430,8 +479,8 @@ pub enum OrderDirection { Desc, } -impl From<&OrderDirection> for torii_grpc::types::OrderDirection { - fn from(val: &OrderDirection) -> Self { +impl From for torii_grpc::types::OrderDirection { + fn from(val: OrderDirection) -> Self { match val { OrderDirection::Asc => torii_grpc::types::OrderDirection::Asc, OrderDirection::Desc => torii_grpc::types::OrderDirection::Desc, @@ -439,8 +488,8 @@ impl From<&OrderDirection> for torii_grpc::types::OrderDirection { } } -impl From<&torii_grpc::types::OrderDirection> for OrderDirection { - fn from(val: &torii_grpc::types::OrderDirection) -> Self { +impl From for OrderDirection { + fn from(val: torii_grpc::types::OrderDirection) -> Self { match val { torii_grpc::types::OrderDirection::Asc => OrderDirection::Asc, torii_grpc::types::OrderDirection::Desc => OrderDirection::Desc, @@ -493,19 +542,19 @@ pub enum MemberValue { List(CArray), } -impl From<&MemberValue> for torii_grpc::types::MemberValue { - fn from(val: &MemberValue) -> Self { +impl From for torii_grpc::types::MemberValue { + fn from(val: MemberValue) -> Self { match val { MemberValue::PrimitiveValue(primitive) => { - torii_grpc::types::MemberValue::Primitive((&primitive.clone()).into()) + torii_grpc::types::MemberValue::Primitive(primitive.into()) } MemberValue::String(string) => torii_grpc::types::MemberValue::String(unsafe { - CStr::from_ptr(*string).to_string_lossy().to_string() + CStr::from_ptr(string).to_string_lossy().to_string() }), MemberValue::List(list) => { let values: Vec = list.into(); let values = values - .iter() + .into_iter() .map(|v| v.into()) .collect::>(); torii_grpc::types::MemberValue::List(values) @@ -514,17 +563,17 @@ impl From<&MemberValue> for torii_grpc::types::MemberValue { } } -impl From<&torii_grpc::types::MemberValue> for MemberValue { - fn from(val: &torii_grpc::types::MemberValue) -> Self { +impl From for MemberValue { + fn from(val: torii_grpc::types::MemberValue) -> Self { match val { torii_grpc::types::MemberValue::Primitive(primitive) => { - MemberValue::PrimitiveValue((&primitive.clone()).into()) + MemberValue::PrimitiveValue(primitive.into()) } torii_grpc::types::MemberValue::String(string) => { MemberValue::String(CString::new(string.clone()).unwrap().into_raw()) } torii_grpc::types::MemberValue::List(list) => { - let values = list.iter().map(|v| v.into()).collect::>(); + let values = list.into_iter().map(|v| v.into()).collect::>(); MemberValue::List(values.into()) } } @@ -591,33 +640,33 @@ pub struct Entity { pub models: CArray, } -impl From<&Entity> for torii_grpc::types::schema::Entity { - fn from(val: &Entity) -> Self { - let models: Vec = (&val.models).into(); - let models = models.iter().map(|m| (&m.clone()).into()).collect(); +impl From for torii_grpc::types::schema::Entity { + fn from(val: Entity) -> Self { + let models: Vec = val.models.into(); + let models = models.into_iter().map(|m| m.into()).collect(); - torii_grpc::types::schema::Entity { hashed_keys: (&val.hashed_keys.clone()).into(), models } + torii_grpc::types::schema::Entity { hashed_keys: val.hashed_keys.into(), models } } } -impl From<&torii_grpc::types::schema::Entity> for Entity { - fn from(val: &torii_grpc::types::schema::Entity) -> Self { - let models = val.models.iter().map(|m| (&m.clone()).into()).collect::>(); +impl From for Entity { + fn from(val: torii_grpc::types::schema::Entity) -> Self { + let models = val.models.into_iter().map(|m| m.into()).collect::>(); - Entity { hashed_keys: (&val.hashed_keys.clone()).into(), models: models.into() } + Entity { hashed_keys: val.hashed_keys.into(), models: models.into() } } } -impl From<&ValueType> for torii_grpc::types::ValueType { - fn from(value: &ValueType) -> Self { +impl From for torii_grpc::types::ValueType { + fn from(value: ValueType) -> Self { match value { ValueType::String(v) => { - let v = unsafe { CStr::from_ptr(*v).to_string_lossy().into_owned() }; + let v = unsafe { CStr::from_ptr(v).to_string_lossy().into_owned() }; torii_grpc::types::ValueType::String(v) } - ValueType::Int(v) => torii_grpc::types::ValueType::Int(*v), - ValueType::UInt(v) => torii_grpc::types::ValueType::UInt(*v), - ValueType::VBool(v) => torii_grpc::types::ValueType::Bool(*v), + ValueType::Int(v) => torii_grpc::types::ValueType::Int(v), + ValueType::UInt(v) => torii_grpc::types::ValueType::UInt(v), + ValueType::VBool(v) => torii_grpc::types::ValueType::Bool(v), ValueType::Bytes(v) => { let v = v.into(); torii_grpc::types::ValueType::Bytes(v) @@ -626,16 +675,16 @@ impl From<&ValueType> for torii_grpc::types::ValueType { } } -impl From<&torii_grpc::types::ValueType> for ValueType { - fn from(value: &torii_grpc::types::ValueType) -> Self { +impl From for ValueType { + fn from(value: torii_grpc::types::ValueType) -> Self { match value { torii_grpc::types::ValueType::String(v) => { let v = CString::new(v.clone()).unwrap().into_raw(); ValueType::String(v) } - torii_grpc::types::ValueType::Int(v) => ValueType::Int(*v), - torii_grpc::types::ValueType::UInt(v) => ValueType::UInt(*v), - torii_grpc::types::ValueType::Bool(v) => ValueType::VBool(*v), + torii_grpc::types::ValueType::Int(v) => ValueType::Int(v), + torii_grpc::types::ValueType::UInt(v) => ValueType::UInt(v), + torii_grpc::types::ValueType::Bool(v) => ValueType::VBool(v), torii_grpc::types::ValueType::Bytes(v) => { let v = v.clone().into(); ValueType::Bytes(v) @@ -656,26 +705,18 @@ pub enum Ty { ByteArray(*const c_char), } -impl From<&dojo_types::schema::Ty> for Ty { - fn from(value: &dojo_types::schema::Ty) -> Self { +impl From for Ty { + fn from(value: dojo_types::schema::Ty) -> Self { match value { dojo_types::schema::Ty::Primitive(primitive) => { let primitive = primitive.into(); Ty::Primitive_(primitive) } - dojo_types::schema::Ty::Struct(struct_) => Ty::Struct_((&struct_.clone()).into()), - dojo_types::schema::Ty::Enum(enum_) => Ty::Enum_((&enum_.clone()).into()), - dojo_types::schema::Ty::Tuple(tuple) => { - let children = tuple.iter().map(|c| (&c.clone()).into()).collect::>(); - - Ty::Tuple_(children.into()) - } - dojo_types::schema::Ty::Array(array) => { - let children = array.iter().map(|c| (&c.clone()).into()).collect::>(); - - Ty::Array_(children.into()) - } + dojo_types::schema::Ty::Struct(struct_) => Ty::Struct_(struct_.into()), + dojo_types::schema::Ty::Enum(enum_) => Ty::Enum_(enum_.into()), + dojo_types::schema::Ty::Tuple(tuple) => Ty::Tuple_(tuple.into()), + dojo_types::schema::Ty::Array(array) => Ty::Array_(array.into()), dojo_types::schema::Ty::ByteArray(array) => { let array = CString::new(array.clone()).unwrap().into_raw(); Ty::ByteArray(array) @@ -686,36 +727,16 @@ impl From<&dojo_types::schema::Ty> for Ty { // Implement opposite conversion // use CString and other alike types to destruct the data -impl From<&Ty> for dojo_types::schema::Ty { - fn from(value: &Ty) -> Self { +impl From for dojo_types::schema::Ty { + fn from(value: Ty) -> Self { match value { - Ty::Primitive_(primitive) => { - dojo_types::schema::Ty::Primitive((&primitive.clone()).into()) - } - Ty::Struct_(struct_) => dojo_types::schema::Ty::Struct((&struct_.clone()).into()), - Ty::Enum_(enum_) => dojo_types::schema::Ty::Enum((&enum_.clone()).into()), - Ty::Tuple_(tuple) => { - let children = unsafe { - std::slice::from_raw_parts(tuple.data, tuple.data_len) - .iter() - .map(|c| (&c.clone()).into()) - .collect::>() - }; - - dojo_types::schema::Ty::Tuple(children) - } - Ty::Array_(array) => { - let children = unsafe { - std::slice::from_raw_parts(array.data, array.data_len) - .iter() - .map(|c| (&c.clone()).into()) - .collect::>() - }; - - dojo_types::schema::Ty::Array(children) - } + Ty::Primitive_(primitive) => dojo_types::schema::Ty::Primitive(primitive.into()), + Ty::Struct_(struct_) => dojo_types::schema::Ty::Struct(struct_.into()), + Ty::Enum_(enum_) => dojo_types::schema::Ty::Enum(enum_.into()), + Ty::Tuple_(tuple) => dojo_types::schema::Ty::Tuple(tuple.into()), + Ty::Array_(array) => dojo_types::schema::Ty::Array(array.into()), Ty::ByteArray(array) => { - let array = unsafe { CStr::from_ptr(*array).to_string_lossy().to_string() }; + let array = unsafe { CStr::from_ptr(array).to_string_lossy().to_string() }; dojo_types::schema::Ty::ByteArray(array) } } @@ -730,10 +751,10 @@ pub struct Enum { pub options: CArray, } -impl From<&Enum> for dojo_types::schema::Enum { - fn from(value: &Enum) -> Self { - let options: Vec = (&value.options).into(); - let options = options.iter().map(|o| (&o.clone()).into()).collect(); +impl From for dojo_types::schema::Enum { + fn from(value: Enum) -> Self { + let options: Vec = value.options.into(); + let options = options.into_iter().map(|o| o.into()).collect(); dojo_types::schema::Enum { name: unsafe { CString::from_raw(value.name as *mut c_char).into_string().unwrap() }, @@ -743,10 +764,9 @@ impl From<&Enum> for dojo_types::schema::Enum { } } -impl From<&dojo_types::schema::Enum> for Enum { - fn from(value: &dojo_types::schema::Enum) -> Self { - let options = - value.options.iter().map(|o| (&o.clone()).into()).collect::>(); +impl From for Enum { + fn from(value: dojo_types::schema::Enum) -> Self { + let options = value.options.into_iter().map(|o| o.into()).collect::>(); Enum { name: CString::new(value.name.clone()).unwrap().into_raw(), @@ -763,20 +783,20 @@ pub struct EnumOption { pub ty: *mut Ty, } -impl From<&EnumOption> for dojo_types::schema::EnumOption { - fn from(value: &EnumOption) -> Self { +impl From for dojo_types::schema::EnumOption { + fn from(value: EnumOption) -> Self { dojo_types::schema::EnumOption { name: unsafe { CString::from_raw(value.name as *mut c_char).into_string().unwrap() }, - ty: unsafe { (&*Box::::from_raw(value.ty)).into() }, + ty: unsafe { (*Box::::from_raw(value.ty)).into() }, } } } -impl From<&dojo_types::schema::EnumOption> for EnumOption { - fn from(value: &dojo_types::schema::EnumOption) -> Self { +impl From for EnumOption { + fn from(value: dojo_types::schema::EnumOption) -> Self { EnumOption { name: CString::new(value.name.clone()).unwrap().into_raw(), - ty: Box::into_raw(Box::new((&value.ty.clone()).into())), + ty: Box::into_raw(Box::new(value.ty.into())), } } } @@ -788,10 +808,10 @@ pub struct Struct { pub children: CArray, } -impl From<&Struct> for dojo_types::schema::Struct { - fn from(value: &Struct) -> Self { - let children: Vec = (&value.children).into(); - let children = children.iter().map(|c| (&c.clone()).into()).collect(); +impl From for dojo_types::schema::Struct { + fn from(value: Struct) -> Self { + let children: Vec = value.children.into(); + let children = children.into_iter().map(|c| c.into()).collect(); dojo_types::schema::Struct { name: unsafe { CString::from_raw(value.name as *mut c_char).into_string().unwrap() }, @@ -800,9 +820,9 @@ impl From<&Struct> for dojo_types::schema::Struct { } } -impl From<&dojo_types::schema::Struct> for Struct { - fn from(value: &dojo_types::schema::Struct) -> Self { - let children = value.children.iter().map(|c| (&c.clone()).into()).collect::>(); +impl From for Struct { + fn from(value: dojo_types::schema::Struct) -> Self { + let children = value.children.into_iter().map(|c| c.into()).collect::>(); Struct { name: CString::new(value.name.clone()).unwrap().into_raw(), @@ -819,21 +839,21 @@ pub struct Member { pub key: bool, } -impl From<&Member> for dojo_types::schema::Member { - fn from(value: &Member) -> Self { +impl From for dojo_types::schema::Member { + fn from(value: Member) -> Self { dojo_types::schema::Member { name: unsafe { CString::from_raw(value.name as *mut c_char).into_string().unwrap() }, - ty: unsafe { (&*Box::::from_raw(value.ty)).into() }, + ty: unsafe { (*Box::::from_raw(value.ty)).into() }, key: value.key, } } } -impl From<&dojo_types::schema::Member> for Member { - fn from(value: &dojo_types::schema::Member) -> Self { +impl From for Member { + fn from(value: dojo_types::schema::Member) -> Self { Member { name: CString::new(value.name.clone()).unwrap().into_raw(), - ty: Box::into_raw(Box::new((&value.ty.clone()).into())), + ty: Box::into_raw(Box::new(value.ty.into())), key: value.key, } } @@ -862,43 +882,39 @@ pub enum Primitive { EthAddress(FieldElement), } -impl From<&Primitive> for dojo_types::primitive::Primitive { - fn from(value: &Primitive) -> Self { +impl From for dojo_types::primitive::Primitive { + fn from(value: Primitive) -> Self { match value { - Primitive::I8(v) => dojo_types::primitive::Primitive::I8(Some(*v)), - Primitive::I16(v) => dojo_types::primitive::Primitive::I16(Some(*v)), - Primitive::I32(v) => dojo_types::primitive::Primitive::I32(Some(*v)), - Primitive::I64(v) => dojo_types::primitive::Primitive::I64(Some(*v)), + Primitive::I8(v) => dojo_types::primitive::Primitive::I8(Some(v)), + Primitive::I16(v) => dojo_types::primitive::Primitive::I16(Some(v)), + Primitive::I32(v) => dojo_types::primitive::Primitive::I32(Some(v)), + Primitive::I64(v) => dojo_types::primitive::Primitive::I64(Some(v)), Primitive::I128(v) => { - dojo_types::primitive::Primitive::I128(Some(i128::from_be_bytes(*v))) + dojo_types::primitive::Primitive::I128(Some(i128::from_be_bytes(v))) } - Primitive::U8(v) => dojo_types::primitive::Primitive::U8(Some(*v)), - Primitive::U16(v) => dojo_types::primitive::Primitive::U16(Some(*v)), - Primitive::U32(v) => dojo_types::primitive::Primitive::U32(Some(*v)), - Primitive::U64(v) => dojo_types::primitive::Primitive::U64(Some(*v)), + Primitive::U8(v) => dojo_types::primitive::Primitive::U8(Some(v)), + Primitive::U16(v) => dojo_types::primitive::Primitive::U16(Some(v)), + Primitive::U32(v) => dojo_types::primitive::Primitive::U32(Some(v)), + Primitive::U64(v) => dojo_types::primitive::Primitive::U64(Some(v)), Primitive::U128(v) => { - dojo_types::primitive::Primitive::U128(Some(u128::from_be_bytes(*v))) + dojo_types::primitive::Primitive::U128(Some(u128::from_be_bytes(v))) } Primitive::U256_(v) => dojo_types::primitive::Primitive::U256(Some(v.into())), - Primitive::Bool(v) => dojo_types::primitive::Primitive::Bool(Some(*v)), - Primitive::Felt252(v) => { - dojo_types::primitive::Primitive::Felt252(Some((&v.clone()).into())) - } - Primitive::ClassHash(v) => { - dojo_types::primitive::Primitive::ClassHash(Some((&v.clone()).into())) - } + Primitive::Bool(v) => dojo_types::primitive::Primitive::Bool(Some(v)), + Primitive::Felt252(v) => dojo_types::primitive::Primitive::Felt252(Some(v.into())), + Primitive::ClassHash(v) => dojo_types::primitive::Primitive::ClassHash(Some(v.into())), Primitive::ContractAddress(v) => { - dojo_types::primitive::Primitive::ContractAddress(Some((&v.clone()).into())) + dojo_types::primitive::Primitive::ContractAddress(Some(v.into())) } Primitive::EthAddress(v) => { - dojo_types::primitive::Primitive::EthAddress(Some((&v.clone()).into())) + dojo_types::primitive::Primitive::EthAddress(Some(v.into())) } } } } -impl From<&dojo_types::primitive::Primitive> for Primitive { - fn from(value: &dojo_types::primitive::Primitive) -> Self { +impl From for Primitive { + fn from(value: dojo_types::primitive::Primitive) -> Self { match value { dojo_types::primitive::Primitive::I8(v) => Primitive::I8(v.unwrap_or(0)), dojo_types::primitive::Primitive::I16(v) => Primitive::I16(v.unwrap_or(0)), @@ -932,28 +948,28 @@ impl From<&dojo_types::primitive::Primitive> for Primitive { dojo_types::primitive::Primitive::Bool(v) => Primitive::Bool(v.unwrap_or(false)), dojo_types::primitive::Primitive::Felt252(v) => { if let Some(v) = v { - Primitive::Felt252((&v.clone()).into()) + Primitive::Felt252(v.into()) } else { Primitive::Felt252(FieldElement { data: [0; 32] }) } } dojo_types::primitive::Primitive::ClassHash(v) => { if let Some(v) = v { - Primitive::Felt252((&v.clone()).into()) + Primitive::Felt252(v.into()) } else { Primitive::Felt252(FieldElement { data: [0; 32] }) } } dojo_types::primitive::Primitive::ContractAddress(v) => { if let Some(v) = v { - Primitive::Felt252((&v.clone()).into()) + Primitive::Felt252(v.into()) } else { Primitive::Felt252(FieldElement { data: [0; 32] }) } } dojo_types::primitive::Primitive::EthAddress(v) => { if let Some(v) = v { - Primitive::EthAddress((&v.clone()).into()) + Primitive::EthAddress(v.into()) } else { Primitive::EthAddress(FieldElement { data: [0; 32] }) } @@ -962,14 +978,12 @@ impl From<&dojo_types::primitive::Primitive> for Primitive { } } -impl From<&Query> for torii_grpc::types::Query { - fn from(val: &Query) -> Self { - let order_by: Vec = (&val.order_by).into(); - let order_by = order_by.iter().map(|o| o.into()).collect(); +impl From for torii_grpc::types::Query { + fn from(val: Query) -> Self { + let order_by: Vec = val.order_by.into(); + let entity_models: Vec = CStringArray(val.entity_models).into(); - let entity_models: Vec = (&val.entity_models).into(); - - match &val.clause { + match val.clause { COption::Some(clause) => { let clause = clause.into(); torii_grpc::types::Query { @@ -995,12 +1009,12 @@ impl From<&Query> for torii_grpc::types::Query { } } -impl From<&torii_grpc::types::Query> for Query { - fn from(val: &torii_grpc::types::Query) -> Self { - let order_by = val.order_by.iter().map(|o| o.into()).collect::>(); - let entity_models = val.clone().entity_models.into(); +impl From for Query { + fn from(val: torii_grpc::types::Query) -> Self { + let order_by = val.order_by.into_iter().map(|o| o.into()).collect::>(); + let entity_models = StringVec(val.entity_models).into(); - match &val.clause { + match val.clause { Option::Some(clause) => { let clause = clause.into(); Query { @@ -1026,8 +1040,8 @@ impl From<&torii_grpc::types::Query> for Query { } } -impl From<&Clause> for torii_grpc::types::Clause { - fn from(val: &Clause) -> Self { +impl From for torii_grpc::types::Clause { + fn from(val: Clause) -> Self { match val { Clause::Keys(keys) => torii_grpc::types::Clause::Keys(keys.into()), Clause::CMember(member) => torii_grpc::types::Clause::Member(member.into()), @@ -1036,8 +1050,8 @@ impl From<&Clause> for torii_grpc::types::Clause { } } -impl From<&torii_grpc::types::Clause> for Clause { - fn from(val: &torii_grpc::types::Clause) -> Self { +impl From for Clause { + fn from(val: torii_grpc::types::Clause) -> Self { match val { torii_grpc::types::Clause::Keys(keys) => Clause::Keys(keys.into()), torii_grpc::types::Clause::Member(member) => Clause::CMember(member.into()), @@ -1046,8 +1060,8 @@ impl From<&torii_grpc::types::Clause> for Clause { } } -impl From<&PatternMatching> for torii_grpc::types::PatternMatching { - fn from(val: &PatternMatching) -> Self { +impl From for torii_grpc::types::PatternMatching { + fn from(val: PatternMatching) -> Self { match val { PatternMatching::FixedLen => torii_grpc::types::PatternMatching::FixedLen, PatternMatching::VariableLen => torii_grpc::types::PatternMatching::VariableLen, @@ -1055,8 +1069,8 @@ impl From<&PatternMatching> for torii_grpc::types::PatternMatching { } } -impl From<&torii_grpc::types::PatternMatching> for PatternMatching { - fn from(val: &torii_grpc::types::PatternMatching) -> Self { +impl From for PatternMatching { + fn from(val: torii_grpc::types::PatternMatching) -> Self { match val { torii_grpc::types::PatternMatching::FixedLen => PatternMatching::FixedLen, torii_grpc::types::PatternMatching::VariableLen => PatternMatching::VariableLen, @@ -1064,12 +1078,12 @@ impl From<&torii_grpc::types::PatternMatching> for PatternMatching { } } -impl From<&EntityKeysClause> for torii_grpc::types::EntityKeysClause { - fn from(val: &EntityKeysClause) -> Self { +impl From for torii_grpc::types::EntityKeysClause { + fn from(val: EntityKeysClause) -> Self { match val { EntityKeysClause::HashedKeys(keys) => { let keys: Vec = keys.into(); - let keys = keys.iter().map(|k| k.into()).collect(); + let keys = keys.into_iter().map(|k| k.into()).collect(); torii_grpc::types::EntityKeysClause::HashedKeys(keys) } @@ -1080,11 +1094,11 @@ impl From<&EntityKeysClause> for torii_grpc::types::EntityKeysClause { } } -impl From<&torii_grpc::types::EntityKeysClause> for EntityKeysClause { - fn from(val: &torii_grpc::types::EntityKeysClause) -> Self { +impl From for EntityKeysClause { + fn from(val: torii_grpc::types::EntityKeysClause) -> Self { match val { torii_grpc::types::EntityKeysClause::HashedKeys(keys) => { - let keys = keys.iter().map(|k| k.into()).collect::>(); + let keys = keys.into_iter().map(|k| k.into()).collect::>(); EntityKeysClause::HashedKeys(keys.into()) } torii_grpc::types::EntityKeysClause::Keys(keys) => { @@ -1094,66 +1108,43 @@ impl From<&torii_grpc::types::EntityKeysClause> for EntityKeysClause { } } -impl From<&KeysClause> for torii_grpc::types::KeysClause { - fn from(val: &KeysClause) -> Self { - let keys: Vec<_> = (&val.keys).into(); - let keys = std::mem::ManuallyDrop::new(keys); - - let models: Vec<*const c_char> = (&val.models).into(); - let models = std::mem::ManuallyDrop::new(models); +impl From for torii_grpc::types::KeysClause { + fn from(val: KeysClause) -> Self { + let keys: Vec> = COptionArray(val.keys).into(); + let models: Vec = CStringArray(val.models).into(); torii_grpc::types::KeysClause { - keys: keys - .iter() - .map(|o| o.clone().into()) - .map(|o: Option| o.as_ref().map(Into::into)) - .collect(), - pattern_matching: (&val.pattern_matching).into(), - models: models - .iter() - .map(|m| unsafe { CStr::from_ptr(*m).to_string_lossy().to_string() }) - .collect(), + keys, + pattern_matching: val.pattern_matching.into(), + models, } } } -impl From<&torii_grpc::types::KeysClause> for KeysClause { - fn from(val: &torii_grpc::types::KeysClause) -> Self { - let keys = val - .keys - .iter() - .map(|o| (*o).into()) - .map(|o: COption<_>| o.as_ref().map(Into::into)) - .collect::>>(); - let models = val - .models - .iter() - .map(|m| CString::new(m.clone()).unwrap().into_raw() as *const c_char) - .collect::>(); - +impl From for KeysClause { + fn from(val: torii_grpc::types::KeysClause) -> Self { KeysClause { - models: models.into(), - keys: keys.into(), - pattern_matching: (&val.pattern_matching).into(), + models: StringVec(val.models).into(), + keys: val.keys.into(), + pattern_matching: val.pattern_matching.into(), } } } -impl From<&ModelKeysClause> for torii_grpc::types::ModelKeysClause { - fn from(val: &ModelKeysClause) -> Self { - let keys: Vec = (&val.keys).into(); - let keys = std::mem::ManuallyDrop::new(keys); +impl From for torii_grpc::types::ModelKeysClause { + fn from(val: ModelKeysClause) -> Self { + let keys: Vec<_> = val.keys.into(); torii_grpc::types::ModelKeysClause { model: unsafe { CStr::from_ptr(val.model).to_string_lossy().to_string() }, - keys: keys.iter().map(Into::into).collect(), + keys, } } } -impl From<&torii_grpc::types::ModelKeysClause> for ModelKeysClause { - fn from(val: &torii_grpc::types::ModelKeysClause) -> Self { - let keys = val.keys.iter().map(|k| k.into()).collect::>(); +impl From for ModelKeysClause { + fn from(val: torii_grpc::types::ModelKeysClause) -> Self { + let keys = val.keys.into_iter().map(|k| k.into()).collect::>(); ModelKeysClause { model: CString::new(val.model.clone()).unwrap().into_raw(), @@ -1162,49 +1153,48 @@ impl From<&torii_grpc::types::ModelKeysClause> for ModelKeysClause { } } -impl From<&MemberClause> for torii_grpc::types::MemberClause { - fn from(val: &MemberClause) -> Self { +impl From for torii_grpc::types::MemberClause { + fn from(val: MemberClause) -> Self { torii_grpc::types::MemberClause { member: unsafe { CString::from_raw(val.member as *mut c_char).into_string().unwrap() }, model: unsafe { CString::from_raw(val.model as *mut c_char).into_string().unwrap() }, - operator: (&val.operator.clone()).into(), - value: (&val.value.clone()).into(), + operator: val.operator.into(), + value: val.value.into(), } } } -impl From<&torii_grpc::types::MemberClause> for MemberClause { - fn from(val: &torii_grpc::types::MemberClause) -> Self { +impl From for MemberClause { + fn from(val: torii_grpc::types::MemberClause) -> Self { MemberClause { model: CString::new(val.model.clone()).unwrap().into_raw(), member: CString::new(val.member.clone()).unwrap().into_raw(), - operator: (&val.operator.clone()).into(), - value: (&val.value.clone()).into(), + operator: val.operator.into(), + value: val.value.into(), } } } -impl From<&CompositeClause> for torii_grpc::types::CompositeClause { - fn from(val: &CompositeClause) -> Self { - let operator = &val.operator.clone(); - let clauses = unsafe { std::slice::from_raw_parts(val.clauses.data, val.clauses.data_len) }; - let clauses = clauses.iter().map(|c| c.into()).collect::>(); +impl From for torii_grpc::types::CompositeClause { + fn from(val: CompositeClause) -> Self { + let operator = val.operator.into(); + let clauses = val.clauses.into(); - torii_grpc::types::CompositeClause { operator: operator.into(), clauses } + torii_grpc::types::CompositeClause { operator, clauses } } } -impl From<&torii_grpc::types::CompositeClause> for CompositeClause { - fn from(val: &torii_grpc::types::CompositeClause) -> Self { - let operator = &val.operator.clone(); - let clauses = val.clauses.iter().map(|c| (&c.clone()).into()).collect::>(); +impl From for CompositeClause { + fn from(val: torii_grpc::types::CompositeClause) -> Self { + let operator = val.operator.into(); + let clauses = val.clauses.into(); - CompositeClause { operator: operator.into(), clauses: clauses.into() } + CompositeClause { operator, clauses } } } -impl From<&LogicalOperator> for torii_grpc::types::LogicalOperator { - fn from(val: &LogicalOperator) -> Self { +impl From for torii_grpc::types::LogicalOperator { + fn from(val: LogicalOperator) -> Self { match val { LogicalOperator::And => torii_grpc::types::LogicalOperator::And, LogicalOperator::Or => torii_grpc::types::LogicalOperator::Or, @@ -1212,8 +1202,8 @@ impl From<&LogicalOperator> for torii_grpc::types::LogicalOperator { } } -impl From<&torii_grpc::types::LogicalOperator> for LogicalOperator { - fn from(val: &torii_grpc::types::LogicalOperator) -> Self { +impl From for LogicalOperator { + fn from(val: torii_grpc::types::LogicalOperator) -> Self { match val { torii_grpc::types::LogicalOperator::And => LogicalOperator::And, torii_grpc::types::LogicalOperator::Or => LogicalOperator::Or, @@ -1221,8 +1211,8 @@ impl From<&torii_grpc::types::LogicalOperator> for LogicalOperator { } } -impl From<&ComparisonOperator> for torii_grpc::types::ComparisonOperator { - fn from(val: &ComparisonOperator) -> Self { +impl From for torii_grpc::types::ComparisonOperator { + fn from(val: ComparisonOperator) -> Self { match val { ComparisonOperator::Eq => torii_grpc::types::ComparisonOperator::Eq, ComparisonOperator::Neq => torii_grpc::types::ComparisonOperator::Neq, @@ -1236,8 +1226,8 @@ impl From<&ComparisonOperator> for torii_grpc::types::ComparisonOperator { } } -impl From<&torii_grpc::types::ComparisonOperator> for ComparisonOperator { - fn from(val: &torii_grpc::types::ComparisonOperator) -> Self { +impl From for ComparisonOperator { + fn from(val: torii_grpc::types::ComparisonOperator) -> Self { match val { torii_grpc::types::ComparisonOperator::Eq => ComparisonOperator::Eq, torii_grpc::types::ComparisonOperator::Neq => ComparisonOperator::Neq, @@ -1251,18 +1241,18 @@ impl From<&torii_grpc::types::ComparisonOperator> for ComparisonOperator { } } -impl From<&Value> for torii_grpc::types::Value { - fn from(val: &Value) -> Self { +impl From for torii_grpc::types::Value { + fn from(val: Value) -> Self { torii_grpc::types::Value { - primitive_type: (&val.primitive_type).into(), - value_type: (&val.value_type).into(), + primitive_type: val.primitive_type.into(), + value_type: val.value_type.into(), } } } -impl From<&torii_grpc::types::Value> for Value { - fn from(val: &torii_grpc::types::Value) -> Self { - Value { primitive_type: (&val.primitive_type).into(), value_type: (&val.value_type).into() } +impl From for Value { + fn from(val: torii_grpc::types::Value) -> Self { + Value { primitive_type: val.primitive_type.into(), value_type: val.value_type.into() } } } @@ -1273,35 +1263,32 @@ pub struct WorldMetadata { pub models: CArray>, } -impl From<&dojo_types::WorldMetadata> for WorldMetadata { - fn from(value: &dojo_types::WorldMetadata) -> Self { +impl From for WorldMetadata { + fn from(value: dojo_types::WorldMetadata) -> Self { let models = value .models - .iter() + .into_iter() .map(|(k, v)| CHashItem { key: k.into(), value: v.into() }) .collect::>>(); - WorldMetadata { - world_address: (&value.world_address.clone()).into(), - models: models.into(), - } + WorldMetadata { world_address: value.world_address.into(), models: models.into() } } } -impl From<&WorldMetadata> for dojo_types::WorldMetadata { - fn from(value: &WorldMetadata) -> Self { - let models: Vec> = (&value.models).into(); +impl From for dojo_types::WorldMetadata { + fn from(value: WorldMetadata) -> Self { + let models: Vec> = value.models.into(); let models = models - .iter() + .into_iter() .map(|m| { - let k = (&m.key).into(); - let v: dojo_types::schema::ModelMetadata = (&m.value).into(); + let k = m.key.into(); + let v: dojo_types::schema::ModelMetadata = m.value.into(); (k, v) }) .collect(); - dojo_types::WorldMetadata { world_address: (&value.world_address.clone()).into(), models } + dojo_types::WorldMetadata { world_address: value.world_address.into(), models } } } @@ -1318,41 +1305,40 @@ pub struct ModelMetadata { pub layout: CArray, } -impl From<&dojo_types::schema::ModelMetadata> for ModelMetadata { - fn from(value: &dojo_types::schema::ModelMetadata) -> Self { - let layout = - value.layout.iter().map(|v| (&v.clone()).into()).collect::>(); +impl From for ModelMetadata { + fn from(value: dojo_types::schema::ModelMetadata) -> Self { + let layout = value.layout.into_iter().map(|v| v.into()).collect::>(); ModelMetadata { - schema: (&value.schema.clone()).into(), + schema: value.schema.into(), name: CString::new(value.name.clone()).unwrap().into_raw(), namespace: CString::new(value.namespace.clone()).unwrap().into_raw(), packed_size: value.packed_size, unpacked_size: value.unpacked_size, - class_hash: (&value.class_hash.clone()).into(), - contract_address: (&value.contract_address.clone()).into(), + class_hash: value.class_hash.into(), + contract_address: value.contract_address.into(), layout: layout.into(), } } } -impl From<&ModelMetadata> for dojo_types::schema::ModelMetadata { - fn from(value: &ModelMetadata) -> Self { - let layout: Vec = (&value.layout).into(); +impl From for dojo_types::schema::ModelMetadata { + fn from(value: ModelMetadata) -> Self { + let layout: Vec = value.layout.into(); let layout: Vec = - layout.iter().map(|v| (&v.clone()).into()).collect(); + layout.into_iter().map(|v| v.into()).collect(); dojo_types::schema::ModelMetadata { - schema: (&value.schema).into(), + schema: value.schema.into(), namespace: unsafe { CString::from_raw(value.namespace as *mut c_char).into_string().unwrap() }, name: unsafe { CString::from_raw(value.name as *mut c_char).into_string().unwrap() }, packed_size: value.packed_size, unpacked_size: value.unpacked_size, - class_hash: (&value.class_hash).into(), - contract_address: (&value.contract_address).into(), + class_hash: value.class_hash.into(), + contract_address: value.contract_address.into(), layout, } } @@ -1366,31 +1352,24 @@ pub struct Event { pub transaction_hash: FieldElement, } -impl From<&Event> for torii_grpc::types::Event { - fn from(val: &Event) -> Self { - let keys: Vec = (&val.keys).into(); - let keys = std::mem::ManuallyDrop::new(keys); +impl From for torii_grpc::types::Event { + fn from(val: Event) -> Self { + let keys: Vec<_> = val.keys.into(); + let data: Vec<_> = val.data.into(); - let data: Vec = (&val.data).into(); - let data = std::mem::ManuallyDrop::new(data); - - torii_grpc::types::Event { - keys: keys.iter().map(Into::into).collect(), - data: data.iter().map(Into::into).collect(), - transaction_hash: (&val.transaction_hash).into(), - } + torii_grpc::types::Event { keys, data, transaction_hash: val.transaction_hash.into() } } } -impl From<&torii_grpc::types::Event> for Event { - fn from(val: &torii_grpc::types::Event) -> Self { - let keys = val.keys.iter().map(|k| k.into()).collect::>(); - let data = val.data.iter().map(|k| k.into()).collect::>(); +impl From for Event { + fn from(val: torii_grpc::types::Event) -> Self { + let keys = val.keys.into_iter().map(|k| k.into()).collect::>(); + let data = val.data.into_iter().map(|k| k.into()).collect::>(); Event { keys: keys.into(), data: data.into(), - transaction_hash: (&val.transaction_hash).into(), + transaction_hash: val.transaction_hash.into(), } } } diff --git a/src/wasm/mod.rs b/src/wasm/mod.rs index f0c156a..d15e81c 100644 --- a/src/wasm/mod.rs +++ b/src/wasm/mod.rs @@ -41,8 +41,8 @@ mod types; use types::{ BlockId, Call, Calls, ClientConfig, Controller, Controllers, Entities, Entity, IndexerUpdate, - KeysClause, KeysClauses, Model, Query, Signature, Token, TokenBalance, TokenBalances, Tokens, - WasmU256, + KeysClause, KeysClauses, Model, Page, Query, Signature, Token, TokenBalance, TokenBalances, + Tokens, WasmU256, }; const JSON_COMPAT_SERIALIZER: serde_wasm_bindgen::Serializer = @@ -114,7 +114,7 @@ pub fn signing_key_sign(private_key: &str, hash: &str) -> Result Result::Ok(Signature::from(&sig)), + Ok(sig) => Result::Ok(Signature::from(sig)), Err(e) => Err(JsValue::from(format!("failed to sign: {e}"))), } } @@ -167,9 +167,7 @@ pub fn verifying_key_verify( let hash = &hash.unwrap(); - let signature = &starknet::core::crypto::Signature::from(&signature); - - match verifying_key.verify(hash, signature) { + match verifying_key.verify(hash, &signature.into()) { Ok(result) => Result::Ok(result), Err(e) => Err(JsValue::from(format!("failed to verify: {e}"))), } @@ -256,10 +254,7 @@ impl Provider { pub async unsafe fn call(&self, call: Call, block_id: BlockId) -> Result { let result = self .0 - .call::( - (&call).into(), - (&block_id).into(), - ) + .call::(call.into(), block_id.into()) .await; match result { @@ -334,7 +329,7 @@ impl Account { /// Result containing transaction hash as hex string or error #[wasm_bindgen(js_name = executeRaw)] pub async unsafe fn execute_raw(&self, calldata: Calls) -> Result { - let calldata = calldata.iter().map(|c| c.into()).collect(); + let calldata = calldata.into_iter().map(|c| c.into()).collect(); let call = self.0.execute_v3(calldata); @@ -614,7 +609,7 @@ impl ToriiClient { .await .map_err(|e| JsValue::from(format!("failed to get controllers: {e}")))?; - Ok(Controllers(controllers.iter().map(|c| c.into()).collect())) + Ok(Controllers(controllers.into_iter().map(|c| c.into()).collect())) } /// Gets token information for the given contract addresses @@ -629,6 +624,9 @@ impl ToriiClient { &self, contract_addresses: Vec, token_ids: Vec, + limit: Option, + offset: Option, + cursor: Option, ) -> Result { let contract_addresses = contract_addresses .iter() @@ -636,15 +634,15 @@ impl ToriiClient { .collect::, _>>() .map_err(|e| JsValue::from(format!("failed to parse contract addresses: {e}")))?; - let token_ids = token_ids.iter().map(|t| t.into()).collect::>(); + let token_ids = token_ids.into_iter().map(|t| t.into()).collect::>(); let tokens = self .inner - .tokens(contract_addresses, token_ids) + .tokens(contract_addresses, token_ids, limit, offset, cursor) .await .map_err(|e| JsValue::from(format!("failed to get tokens: {e}")))?; - Ok(Tokens(tokens.iter().map(|t| t.into()).collect())) + Ok(Tokens(tokens.into())) } /// Subscribes to token updates @@ -674,7 +672,7 @@ impl ToriiClient { }) .collect::, _>>()?; - let token_ids = token_ids.iter().map(|t| t.into()).collect::>(); + let token_ids = token_ids.into_iter().map(|t| t.into()).collect::>(); let subscription_id = Arc::new(AtomicU64::new(0)); let (trigger, tripwire) = Tripwire::new(); @@ -698,7 +696,7 @@ impl ToriiClient { while let Some(Ok((id, token))) = stream.next().await { subscription_id_clone.store(id, Ordering::SeqCst); - let token: Token = (&token).into(); + let token: Token = token.into(); let _ = callback.call1( &JsValue::null(), @@ -734,6 +732,9 @@ impl ToriiClient { contract_addresses: Vec, account_addresses: Vec, token_ids: Vec, + limit: Option, + offset: Option, + cursor: Option, ) -> Result { let account_addresses = account_addresses .iter() @@ -747,15 +748,15 @@ impl ToriiClient { .collect::, _>>() .map_err(|e| JsValue::from(format!("failed to parse contract addresses: {e}")))?; - let token_ids = token_ids.iter().map(|t| t.into()).collect::>(); + let token_ids = token_ids.into_iter().map(|t| t.into()).collect::>(); let token_balances = self .inner - .token_balances(account_addresses, contract_addresses, token_ids) + .token_balances(account_addresses, contract_addresses, token_ids, limit, offset, cursor) .await .map_err(|e| JsValue::from(format!("failed to get token balances: {e}")))?; - Ok(TokenBalances(token_balances.iter().map(|t| t.into()).collect())) + Ok(TokenBalances(token_balances.into())) } /// Queries entities based on the provided query parameters @@ -770,10 +771,10 @@ impl ToriiClient { #[cfg(feature = "console-error-panic")] console_error_panic_hook::set_once(); - let results = self.inner.entities((&query).into(), historical).await; + let results = self.inner.entities(query.into(), historical).await; match results { - Ok(entities) => Ok((&entities).into()), + Ok(entities) => Ok(entities.into()), Err(err) => Err(JsValue::from(format!("failed to get entities: {err}"))), } } @@ -813,7 +814,7 @@ impl ToriiClient { .await; match results { - Ok(entities) => Ok((&entities).into()), + Ok(entities) => Ok(entities.into()), Err(err) => Err(JsValue::from(format!("failed to get entities: {err}"))), } } @@ -835,10 +836,10 @@ impl ToriiClient { #[cfg(feature = "console-error-panic")] console_error_panic_hook::set_once(); - let results = self.inner.event_messages((&query).into(), historical).await; + let results = self.inner.event_messages(query.into(), historical).await; match results { - Ok(event_messages) => Ok((&event_messages).into()), + Ok(event_messages) => Ok(event_messages.into()), Err(err) => Err(JsValue::from(format!("failed to get event_messages: {err}"))), } } @@ -860,7 +861,7 @@ impl ToriiClient { #[cfg(feature = "console-error-panic")] console_error_panic_hook::set_once(); - let clauses: Vec<_> = clauses.iter().map(|c| c.into()).collect(); + let clauses: Vec<_> = clauses.into_iter().map(|c| c.into()).collect(); let subscription_id = Arc::new(AtomicU64::new(0)); let (trigger, tripwire) = Tripwire::new(); @@ -881,11 +882,12 @@ impl ToriiClient { while let Some(Ok((id, entity))) = stream.next().await { subscription_id_clone.store(id, Ordering::SeqCst); - let models: Entity = (&entity).into(); + let hashed_keys = entity.hashed_keys.clone(); + let models: Entity = entity.into(); let _ = callback.call2( &JsValue::null(), - &JsValue::from_str(&format!("{:#x}", entity.hashed_keys)), + &JsValue::from_str(&format!("{:#x}", hashed_keys)), &models.serialize(&JSON_COMPAT_SERIALIZER).unwrap(), ); } @@ -918,7 +920,7 @@ impl ToriiClient { subscription: &Subscription, clauses: KeysClauses, ) -> Result<(), JsValue> { - let clauses = clauses.iter().map(|c| c.into()).collect(); + let clauses = clauses.into_iter().map(|c| c.into()).collect(); self.inner .update_entity_subscription(subscription.id.load(Ordering::SeqCst), clauses) .await @@ -942,7 +944,7 @@ impl ToriiClient { #[cfg(feature = "console-error-panic")] console_error_panic_hook::set_once(); - let clauses: Vec<_> = clauses.iter().map(|c| c.into()).collect(); + let clauses: Vec<_> = clauses.into_iter().map(|c| c.into()).collect(); let subscription_id = Arc::new(AtomicU64::new(0)); let (trigger, tripwire) = Tripwire::new(); @@ -963,11 +965,12 @@ impl ToriiClient { while let Some(Ok((id, entity))) = stream.next().await { subscription_id_clone.store(id, Ordering::SeqCst); - let models: Entity = (&entity).into(); + let hashed_keys = entity.hashed_keys.clone(); + let models: Entity = entity.into(); let _ = callback.call2( &JsValue::null(), - &JsValue::from_str(&format!("{:#x}", entity.hashed_keys)), + &JsValue::from_str(&format!("{:#x}", hashed_keys)), &models.serialize(&JSON_COMPAT_SERIALIZER).unwrap(), ); } @@ -1000,7 +1003,7 @@ impl ToriiClient { subscription: &Subscription, clauses: KeysClauses, ) -> Result<(), JsValue> { - let clauses = clauses.iter().map(|c| c.into()).collect(); + let clauses = clauses.into_iter().map(|c| c.into()).collect(); self.inner .update_event_message_subscription(subscription.id.load(Ordering::SeqCst), clauses) .await @@ -1024,7 +1027,7 @@ impl ToriiClient { #[cfg(feature = "console-error-panic")] console_error_panic_hook::set_once(); - let clauses: Vec<_> = clauses.iter().map(|c| c.into()).collect(); + let clauses: Vec<_> = clauses.into_iter().map(|c| c.into()).collect(); let subscription_id = Arc::new(AtomicU64::new(0)); let (trigger, tripwire) = Tripwire::new(); @@ -1106,7 +1109,7 @@ impl ToriiClient { let mut stream = stream.take_until_if(tripwire.clone()); while let Some(Ok(update)) = stream.next().await { - let update: IndexerUpdate = (&update).into(); + let update: IndexerUpdate = update.into(); let _ = callback.call1( &JsValue::null(), @@ -1165,7 +1168,7 @@ impl ToriiClient { }) .collect::, _>>()?; - let token_ids = token_ids.iter().map(|t| t.into()).collect::>(); + let token_ids = token_ids.into_iter().map(|t| t.into()).collect::>(); let subscription_id = Arc::new(AtomicU64::new(0)); let (trigger, tripwire) = Tripwire::new(); @@ -1194,7 +1197,7 @@ impl ToriiClient { while let Some(Ok((id, balance))) = stream.next().await { subscription_id_clone.store(id, Ordering::SeqCst); - let balance: TokenBalance = (&balance).into(); + let balance: TokenBalance = balance.into(); let _ = callback.call1( &JsValue::null(), @@ -1250,7 +1253,7 @@ impl ToriiClient { }) .collect::, _>>()?; - let token_ids = token_ids.iter().map(|t| t.into()).collect::>(); + let token_ids = token_ids.into_iter().map(|t| t.into()).collect::>(); self.inner .update_token_balance_subscription( diff --git a/src/wasm/types.rs b/src/wasm/types.rs index 4dca595..3ee696b 100644 --- a/src/wasm/types.rs +++ b/src/wasm/types.rs @@ -12,12 +12,31 @@ use wasm_bindgen::prelude::*; use super::utils::{pad_to_hex, parse_ty_as_json_str}; +#[derive(Tsify, Serialize, Deserialize, Debug)] +#[tsify(into_wasm_abi, from_wasm_abi)] +pub struct Page { + pub items: Vec, + pub next_cursor: Option, +} + +impl From> for Page +where + U: From, +{ + fn from(value: torii_grpc::types::Page) -> Self { + Self { + items: value.items.into_iter().map(|t| t.into()).collect(), + next_cursor: if value.next_cursor.is_empty() { None } else { Some(value.next_cursor) }, + } + } +} + #[derive(Tsify, Serialize, Deserialize, Debug)] #[tsify(into_wasm_abi, from_wasm_abi)] pub struct WasmU256(pub String); -impl From<&WasmU256> for U256 { - fn from(value: &WasmU256) -> Self { +impl From for U256 { + fn from(value: WasmU256) -> Self { U256::from_be_hex(pad_to_hex(value.0.as_str()).unwrap().as_str()) } } @@ -34,8 +53,8 @@ pub struct Controller { pub deployed_at_timestamp: u64, } -impl From<&torii_grpc::types::Controller> for Controller { - fn from(value: &torii_grpc::types::Controller) -> Self { +impl From for Controller { + fn from(value: torii_grpc::types::Controller) -> Self { Self { address: format!("{:#x}", value.address), username: value.username.clone(), @@ -46,11 +65,11 @@ impl From<&torii_grpc::types::Controller> for Controller { #[derive(Tsify, Serialize, Deserialize, Debug)] #[tsify(into_wasm_abi, from_wasm_abi)] -pub struct Tokens(pub Vec); +pub struct Tokens(pub Page); #[derive(Tsify, Serialize, Deserialize, Debug)] #[tsify(into_wasm_abi, from_wasm_abi)] -pub struct TokenBalances(pub Vec); +pub struct TokenBalances(pub Page); #[derive(Tsify, Serialize, Deserialize, Debug)] #[tsify(into_wasm_abi, from_wasm_abi)] @@ -63,8 +82,8 @@ pub struct Token { pub metadata: String, } -impl From<&torii_grpc::types::Token> for Token { - fn from(value: &torii_grpc::types::Token) -> Self { +impl From for Token { + fn from(value: torii_grpc::types::Token) -> Self { Self { contract_address: format!("{:#x}", value.contract_address), token_id: format!("0x{:x}", value.token_id), @@ -85,8 +104,8 @@ pub struct TokenBalance { pub token_id: String, } -impl From<&torii_grpc::types::TokenBalance> for TokenBalance { - fn from(value: &torii_grpc::types::TokenBalance) -> Self { +impl From for TokenBalance { + fn from(value: torii_grpc::types::TokenBalance) -> Self { Self { balance: format!("0x{:x}", value.balance), account_address: format!("{:#x}", value.account_address), @@ -105,8 +124,8 @@ pub struct IndexerUpdate { pub contract_address: String, } -impl From<&IndexerUpdate> for torii_grpc::types::IndexerUpdate { - fn from(value: &IndexerUpdate) -> Self { +impl From for torii_grpc::types::IndexerUpdate { + fn from(value: IndexerUpdate) -> Self { Self { head: value.head, tps: value.tps, @@ -116,8 +135,8 @@ impl From<&IndexerUpdate> for torii_grpc::types::IndexerUpdate { } } -impl From<&torii_grpc::types::IndexerUpdate> for IndexerUpdate { - fn from(value: &torii_grpc::types::IndexerUpdate) -> Self { +impl From for IndexerUpdate { + fn from(value: torii_grpc::types::IndexerUpdate) -> Self { Self { head: value.head, tps: value.tps, @@ -173,14 +192,14 @@ pub struct Signature { pub s: String, } -impl From<&starknet::core::crypto::Signature> for Signature { - fn from(value: &starknet::core::crypto::Signature) -> Self { +impl From for Signature { + fn from(value: starknet::core::crypto::Signature) -> Self { Self { r: format!("{:#x}", value.r), s: format!("{:#x}", value.s) } } } -impl From<&Signature> for starknet::core::crypto::Signature { - fn from(value: &Signature) -> Self { +impl From for starknet::core::crypto::Signature { + fn from(value: Signature) -> Self { Self { r: Felt::from_str(value.r.as_str()).unwrap(), s: Felt::from_str(value.s.as_str()).unwrap(), @@ -195,8 +214,8 @@ pub type Calls = Vec; #[tsify(into_wasm_abi, from_wasm_abi, hashmap_as_object)] pub struct Model(pub HashMap); -impl From<&Struct> for Model { - fn from(value: &Struct) -> Self { +impl From for Model { + fn from(value: Struct) -> Self { Self( value .children @@ -211,13 +230,13 @@ impl From<&Struct> for Model { #[tsify(into_wasm_abi, from_wasm_abi, hashmap_as_object)] pub struct Entity(pub HashMap); -impl From<&torii_grpc::types::schema::Entity> for Entity { - fn from(value: &torii_grpc::types::schema::Entity) -> Self { +impl From for Entity { + fn from(value: torii_grpc::types::schema::Entity) -> Self { let mut seen_models = HashMap::new(); Self( value .models - .iter() + .into_iter() .map(|m| { let count = seen_models.entry(m.name.clone()).or_insert(0); let name = @@ -234,11 +253,11 @@ impl From<&torii_grpc::types::schema::Entity> for Entity { #[tsify(into_wasm_abi, from_wasm_abi, hashmap_as_object)] pub struct Entities(pub HashMap); -impl From<&Vec> for Entities { - fn from(value: &Vec) -> Self { +impl From> for Entities { + fn from(value: Vec) -> Self { Self( value - .iter() + .into_iter() .enumerate() .map(|(i, e)| { ( @@ -263,8 +282,8 @@ pub struct Call { pub calldata: Vec, } -impl From<&Call> for starknet::core::types::Call { - fn from(value: &Call) -> Self { +impl From for starknet::core::types::Call { + fn from(value: Call) -> Self { Self { to: Felt::from_str(value.to.as_str()).unwrap(), selector: get_selector_from_name(value.selector.as_str()).unwrap(), @@ -273,8 +292,8 @@ impl From<&Call> for starknet::core::types::Call { } } -impl From<&Call> for FunctionCall { - fn from(value: &Call) -> Self { +impl From for FunctionCall { + fn from(value: Call) -> Self { Self { contract_address: Felt::from_str(value.to.as_str()).unwrap(), entry_point_selector: get_selector_from_name(value.selector.as_str()).unwrap(), @@ -290,8 +309,8 @@ pub enum BlockTag { Pending, } -impl From<&BlockTag> for starknet::core::types::BlockTag { - fn from(value: &BlockTag) -> Self { +impl From for starknet::core::types::BlockTag { + fn from(value: BlockTag) -> Self { match value { BlockTag::Latest => starknet::core::types::BlockTag::Latest, BlockTag::Pending => starknet::core::types::BlockTag::Pending, @@ -307,13 +326,13 @@ pub enum BlockId { BlockTag(BlockTag), } -impl From<&BlockId> for starknet::core::types::BlockId { - fn from(value: &BlockId) -> Self { +impl From for starknet::core::types::BlockId { + fn from(value: BlockId) -> Self { match value { BlockId::Hash(hash) => { starknet::core::types::BlockId::Hash(Felt::from_str(hash.as_str()).unwrap()) } - BlockId::Number(number) => starknet::core::types::BlockId::Number(*number), + BlockId::Number(number) => starknet::core::types::BlockId::Number(number), BlockId::BlockTag(tag) => starknet::core::types::BlockId::Tag(tag.into()), } } @@ -339,12 +358,12 @@ pub struct OrderBy { pub direction: OrderDirection, } -impl From<&OrderBy> for torii_grpc::types::OrderBy { - fn from(value: &OrderBy) -> Self { +impl From for torii_grpc::types::OrderBy { + fn from(value: OrderBy) -> Self { Self { model: value.model.clone(), member: value.member.clone(), - direction: (&value.direction).into(), + direction: value.direction.into(), } } } @@ -356,8 +375,8 @@ pub enum OrderDirection { Desc, } -impl From<&OrderDirection> for torii_grpc::types::OrderDirection { - fn from(value: &OrderDirection) -> Self { +impl From for torii_grpc::types::OrderDirection { + fn from(value: OrderDirection) -> Self { match value { OrderDirection::Asc => Self::Asc, OrderDirection::Desc => Self::Desc, @@ -365,14 +384,14 @@ impl From<&OrderDirection> for torii_grpc::types::OrderDirection { } } -impl From<&Query> for torii_grpc::types::Query { - fn from(value: &Query) -> Self { +impl From for torii_grpc::types::Query { + fn from(value: Query) -> Self { Self { limit: value.limit, offset: value.offset, - clause: value.clause.as_ref().map(|c| c.into()), + clause: value.clause.map(|c| c.into()), dont_include_hashed_keys: value.dont_include_hashed_keys, - order_by: value.order_by.iter().map(|o| o.into()).collect(), + order_by: value.order_by.into_iter().map(|o| o.into()).collect(), entity_models: value.entity_models.iter().map(|m| m.to_string()).collect(), entity_updated_after: value.entity_updated_after, } @@ -407,8 +426,8 @@ pub enum PatternMatching { VariableLen = 1, } -impl From<&PatternMatching> for torii_grpc::types::PatternMatching { - fn from(value: &PatternMatching) -> Self { +impl From for torii_grpc::types::PatternMatching { + fn from(value: PatternMatching) -> Self { match value { PatternMatching::FixedLen => Self::FixedLen, PatternMatching::VariableLen => Self::VariableLen, @@ -423,8 +442,8 @@ pub enum EntityKeysClause { Keys(KeysClause), } -impl From<&EntityKeysClause> for torii_grpc::types::EntityKeysClause { - fn from(value: &EntityKeysClause) -> Self { +impl From for torii_grpc::types::EntityKeysClause { + fn from(value: EntityKeysClause) -> Self { match value { EntityKeysClause::HashedKeys(keys) => { Self::HashedKeys(keys.iter().map(|k| Felt::from_str(k.as_str()).unwrap()).collect()) @@ -450,16 +469,18 @@ pub enum MemberValue { List(Vec), } -impl From<&MemberValue> for torii_grpc::types::MemberValue { - fn from(value: &MemberValue) -> Self { +impl From for torii_grpc::types::MemberValue { + fn from(value: MemberValue) -> Self { match value { MemberValue::Primitive(primitive) => { torii_grpc::types::MemberValue::Primitive(primitive.into()) } MemberValue::String(string) => torii_grpc::types::MemberValue::String(string.clone()), MemberValue::List(list) => { - let values = - list.iter().map(|v| v.into()).collect::>(); + let values = list + .into_iter() + .map(|v| v.into()) + .collect::>(); torii_grpc::types::MemberValue::List(values) } } @@ -482,8 +503,8 @@ pub struct CompositeClause { pub clauses: Vec, } -impl From<&ModelKeysClause> for torii_grpc::types::ModelKeysClause { - fn from(value: &ModelKeysClause) -> Self { +impl From for torii_grpc::types::ModelKeysClause { + fn from(value: ModelKeysClause) -> Self { Self { model: value.model.to_string(), keys: value.keys.iter().map(|k| Felt::from_str(k.as_str()).unwrap()).collect(), @@ -491,8 +512,8 @@ impl From<&ModelKeysClause> for torii_grpc::types::ModelKeysClause { } } -impl From<&KeysClause> for torii_grpc::types::KeysClause { - fn from(value: &KeysClause) -> Self { +impl From for torii_grpc::types::KeysClause { + fn from(value: KeysClause) -> Self { Self { keys: value .keys @@ -500,33 +521,33 @@ impl From<&KeysClause> for torii_grpc::types::KeysClause { .map(|o| o.as_ref().map(|k| Felt::from_str(k.as_str()).unwrap())) .collect(), models: value.models.iter().map(|m| m.to_string()).collect(), - pattern_matching: (&value.pattern_matching).into(), + pattern_matching: value.pattern_matching.into(), } } } -impl From<&MemberClause> for torii_grpc::types::MemberClause { - fn from(value: &MemberClause) -> Self { +impl From for torii_grpc::types::MemberClause { + fn from(value: MemberClause) -> Self { Self { model: value.model.to_string(), member: value.member.to_string(), - operator: (&value.operator).into(), - value: (&value.value).into(), + operator: value.operator.into(), + value: value.value.into(), } } } -impl From<&CompositeClause> for torii_grpc::types::CompositeClause { - fn from(value: &CompositeClause) -> Self { +impl From for torii_grpc::types::CompositeClause { + fn from(value: CompositeClause) -> Self { Self { - operator: (&value.operator).into(), - clauses: value.clauses.iter().map(|c| c.into()).collect(), + operator: value.operator.into(), + clauses: value.clauses.into_iter().map(|c| c.into()).collect(), } } } -impl From<&Clause> for torii_grpc::types::Clause { - fn from(value: &Clause) -> Self { +impl From for torii_grpc::types::Clause { + fn from(value: Clause) -> Self { match value { Clause::Keys(keys) => Self::Keys(keys.into()), Clause::Member(member) => Self::Member(member.into()), @@ -542,8 +563,8 @@ pub enum LogicalOperator { Or, } -impl From<&LogicalOperator> for torii_grpc::types::LogicalOperator { - fn from(value: &LogicalOperator) -> Self { +impl From for torii_grpc::types::LogicalOperator { + fn from(value: LogicalOperator) -> Self { match value { LogicalOperator::And => Self::And, LogicalOperator::Or => Self::Or, @@ -564,8 +585,8 @@ pub enum ComparisonOperator { NotIn, } -impl From<&ComparisonOperator> for torii_grpc::types::ComparisonOperator { - fn from(value: &ComparisonOperator) -> Self { +impl From for torii_grpc::types::ComparisonOperator { + fn from(value: ComparisonOperator) -> Self { match value { ComparisonOperator::Eq => Self::Eq, ComparisonOperator::Neq => Self::Neq, @@ -586,12 +607,9 @@ pub struct Value { pub value_type: ValueType, } -impl From<&Value> for torii_grpc::types::Value { - fn from(value: &Value) -> Self { - Self { - primitive_type: (&value.primitive_type).into(), - value_type: (&value.value_type).into(), - } +impl From for torii_grpc::types::Value { + fn from(value: Value) -> Self { + Self { primitive_type: value.primitive_type.into(), value_type: value.value_type.into() } } } @@ -605,8 +623,8 @@ pub enum ValueType { Bytes(Vec), } -impl From<&ValueType> for torii_grpc::types::ValueType { - fn from(value: &ValueType) -> Self { +impl From for torii_grpc::types::ValueType { + fn from(value: ValueType) -> Self { match &value { ValueType::String(s) => Self::String(s.to_string()), ValueType::Int(i) => Self::Int(*i), @@ -638,30 +656,36 @@ pub enum Primitive { EthAddress(Option), } -impl From<&Primitive> for dojo_types::primitive::Primitive { - fn from(value: &Primitive) -> Self { +impl From for dojo_types::primitive::Primitive { + fn from(value: Primitive) -> Self { match value { - Primitive::I8(Some(value)) => Self::I8(Some(*value)), - Primitive::I16(Some(value)) => Self::I16(Some(*value)), - Primitive::I32(Some(value)) => Self::I32(Some(*value)), - Primitive::I64(Some(value)) => Self::I64(Some(*value)), - Primitive::I128(Some(value)) => Self::I128(Some(i128::from_str(value).unwrap())), - Primitive::U8(Some(value)) => Self::U8(Some(*value)), - Primitive::U16(Some(value)) => Self::U16(Some(*value)), - Primitive::U32(Some(value)) => Self::U32(Some(*value)), - Primitive::U64(Some(value)) => Self::U64(Some(*value)), - Primitive::U128(Some(value)) => Self::U128(Some(u128::from_str(value).unwrap())), + Primitive::I8(Some(value)) => Self::I8(Some(value)), + Primitive::I16(Some(value)) => Self::I16(Some(value)), + Primitive::I32(Some(value)) => Self::I32(Some(value)), + Primitive::I64(Some(value)) => Self::I64(Some(value)), + Primitive::I128(Some(value)) => { + Self::I128(Some(i128::from_str(value.as_str()).unwrap())) + } + Primitive::U8(Some(value)) => Self::U8(Some(value)), + Primitive::U16(Some(value)) => Self::U16(Some(value)), + Primitive::U32(Some(value)) => Self::U32(Some(value)), + Primitive::U64(Some(value)) => Self::U64(Some(value)), + Primitive::U128(Some(value)) => { + Self::U128(Some(u128::from_str(value.as_str()).unwrap())) + } Primitive::U256(Some(value)) => Self::U256(Some(U256::from_be_hex(value.as_str()))), - Primitive::Bool(Some(value)) => Self::Bool(Some(*value)), - Primitive::Felt252(Some(value)) => Self::Felt252(Some(Felt::from_str(value).unwrap())), + Primitive::Bool(Some(value)) => Self::Bool(Some(value)), + Primitive::Felt252(Some(value)) => { + Self::Felt252(Some(Felt::from_str(value.as_str()).unwrap())) + } Primitive::ClassHash(Some(value)) => { - Self::ClassHash(Some(Felt::from_str(value).unwrap())) + Self::ClassHash(Some(Felt::from_str(value.as_str()).unwrap())) } Primitive::ContractAddress(Some(value)) => { - Self::ContractAddress(Some(Felt::from_str(value).unwrap())) + Self::ContractAddress(Some(Felt::from_str(value.as_str()).unwrap())) } Primitive::EthAddress(Some(value)) => { - Self::EthAddress(Some(Felt::from_str(value).unwrap())) + Self::EthAddress(Some(Felt::from_str(value.as_str()).unwrap())) } _ => unimplemented!(), }