diff --git a/Cargo.lock b/Cargo.lock index ed5538a..003c289 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -857,9 +857,9 @@ dependencies = [ [[package]] name = "cairo-lang-casm" -version = "2.9.4" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e5568d33e98e2af9530d7dc2353699cb2a0cd0d7c32b105114615371d6ee030" +checksum = "e3670c7c84c310dfc96667b6f10c37e275ed750761058e8052cace1d1853a03b" dependencies = [ "cairo-lang-utils", "indoc", @@ -871,9 +871,9 @@ dependencies = [ [[package]] name = "cairo-lang-eq-solver" -version = "2.9.4" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40f01748564e51d96074f1ce6cd85c46a525954637143c083aaeaa5cbe969257" +checksum = "d81fe837084f841398225eba8ae50759d7ff64fb64a5af0b4b42f1fed4e2c351" dependencies = [ "cairo-lang-utils", "good_lp", @@ -881,9 +881,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra" -version = "2.9.4" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95ce5b707226e42868dc8da9a63c6c5baef6a13c54be22b70080361e686d5e0d" +checksum = "8daba1806b75e032be1c5d5975f975a7c11777d6d9f5a8e2df61dab290bd08c6" dependencies = [ "anyhow", "cairo-lang-utils", @@ -908,9 +908,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-ap-change" -version = "2.9.4" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78c228f0ad5bb6ae8d8086b53fca1c64d5298dc33cd590e9ab15dc85d4e800ce" +checksum = "dc9fdbd7418950ebc9781c93ad4b435ee4ed0f920d6a9b44063359da957bd8cd" dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", @@ -924,9 +924,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-gas" -version = "2.9.4" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "241ba43dc88925635dbae05fccdb471e3bcdb44a6284b6499993ee63517d4171" +checksum = "2e15d759696fdff96d7fbebf34a4dfb520791cd1cddb5805804e1493110e4443" dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", @@ -940,9 +940,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-to-casm" -version = "2.9.4" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ebcfc50dfc0f926ba87a51931b75ff234aa8e8e965a9dbbdb2776fa38cfb903" +checksum = "5dd3e7e61a3af0bc8a26a2134707878bcc974c5b69151247f16744d97f45dd88" dependencies = [ "assert_matches", "cairo-lang-casm", @@ -961,9 +961,9 @@ dependencies = [ [[package]] name = "cairo-lang-sierra-type-size" -version = "2.9.4" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "403e2524c27f28423405ecc160a8000f45e5a450c1d3015989e20fb4c5e55df9" +checksum = "04e94db2e8faaab9fcfcf658e33743017b4e548cb93103b3a106605fb8693de6" dependencies = [ "cairo-lang-sierra", "cairo-lang-utils", @@ -971,9 +971,9 @@ dependencies = [ [[package]] name = "cairo-lang-starknet-classes" -version = "2.9.4" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3295bc0e3ead98195d5e280dbaf280bc458ee42ef2dccf77d5a1c8ce71a40953" +checksum = "c31a18f0718547e2ea2dfac3bf394b822e81cac9fd3b2585abd429a7ba45d607" dependencies = [ "cairo-lang-casm", "cairo-lang-sierra", @@ -994,9 +994,9 @@ dependencies = [ [[package]] name = "cairo-lang-utils" -version = "2.9.4" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34609db9f2589340e217cd02254548d6e65dad1d0306fa17af1afba949509692" +checksum = "cc13c3f9b93451df0011bf5ae11804ebaac4b85c8555aa01679a25d4a3e64da5" dependencies = [ "hashbrown 0.14.5", "indexmap 2.9.0", @@ -1634,6 +1634,7 @@ dependencies = [ "torii-client", "torii-grpc-client", "torii-libp2p-types", + "torii-proto", "torii-typed-data", "tower-http 0.6.2", "tsify-next", @@ -1646,8 +1647,8 @@ dependencies = [ [[package]] name = "dojo-types" -version = "1.4.1" -source = "git+https://github.com/dojoengine/dojo?tag=v1.4.1#0fab6ed063a56ca6049b51117e46e3e8acf237fa" +version = "1.5.0-alpha.2" +source = "git+https://github.com/dojoengine/dojo?rev=4145801#4145801c2a1da8e413af2c57615869a4ccf19e76" dependencies = [ "anyhow", "cainome 0.5.1", @@ -1668,8 +1669,8 @@ dependencies = [ [[package]] name = "dojo-world" -version = "1.4.1" -source = "git+https://github.com/dojoengine/dojo?tag=v1.4.1#0fab6ed063a56ca6049b51117e46e3e8acf237fa" +version = "1.5.0-alpha.2" +source = "git+https://github.com/dojoengine/dojo?rev=4145801#4145801c2a1da8e413af2c57615869a4ccf19e76" dependencies = [ "anyhow", "async-trait", @@ -6648,8 +6649,8 @@ dependencies = [ [[package]] name = "torii-client" -version = "1.5.0-alpha.0" -source = "git+https://github.com/dojoengine/torii?rev=4dbb084#4dbb0848737d6bb96d9e774f7975a4c58d622da6" +version = "1.5.0-alpha.1" +source = "git+https://github.com/dojoengine/torii?rev=3ae9b23#3ae9b231b17fd22c211317c242555b31b5322c81" dependencies = [ "async-trait", "crypto-bigint", @@ -6674,8 +6675,8 @@ dependencies = [ [[package]] name = "torii-grpc-client" -version = "1.5.0-alpha.0" -source = "git+https://github.com/dojoengine/torii?rev=4dbb084#4dbb0848737d6bb96d9e774f7975a4c58d622da6" +version = "1.5.0-alpha.1" +source = "git+https://github.com/dojoengine/torii?rev=3ae9b23#3ae9b231b17fd22c211317c242555b31b5322c81" dependencies = [ "crypto-bigint", "dojo-types", @@ -6700,8 +6701,8 @@ dependencies = [ [[package]] name = "torii-libp2p-client" -version = "1.5.0-alpha.0" -source = "git+https://github.com/dojoengine/torii?rev=4dbb084#4dbb0848737d6bb96d9e774f7975a4c58d622da6" +version = "1.5.0-alpha.1" +source = "git+https://github.com/dojoengine/torii?rev=3ae9b23#3ae9b231b17fd22c211317c242555b31b5322c81" dependencies = [ "anyhow", "futures", @@ -6723,8 +6724,8 @@ dependencies = [ [[package]] name = "torii-libp2p-types" -version = "1.5.0-alpha.0" -source = "git+https://github.com/dojoengine/torii?rev=4dbb084#4dbb0848737d6bb96d9e774f7975a4c58d622da6" +version = "1.5.0-alpha.1" +source = "git+https://github.com/dojoengine/torii?rev=3ae9b23#3ae9b231b17fd22c211317c242555b31b5322c81" dependencies = [ "serde", "starknet 0.12.0", @@ -6733,8 +6734,8 @@ dependencies = [ [[package]] name = "torii-proto" -version = "1.5.0-alpha.0" -source = "git+https://github.com/dojoengine/torii?rev=4dbb084#4dbb0848737d6bb96d9e774f7975a4c58d622da6" +version = "1.5.0-alpha.1" +source = "git+https://github.com/dojoengine/torii?rev=3ae9b23#3ae9b231b17fd22c211317c242555b31b5322c81" dependencies = [ "crypto-bigint", "dojo-types", @@ -6755,8 +6756,8 @@ dependencies = [ [[package]] name = "torii-typed-data" -version = "1.5.0-alpha.0" -source = "git+https://github.com/dojoengine/torii?rev=4dbb084#4dbb0848737d6bb96d9e774f7975a4c58d622da6" +version = "1.5.0-alpha.1" +source = "git+https://github.com/dojoengine/torii?rev=3ae9b23#3ae9b231b17fd22c211317c242555b31b5322c81" dependencies = [ "cainome 0.5.1", "crypto-bigint", diff --git a/Cargo.toml b/Cargo.toml index 4c28033..e2834e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,12 +7,13 @@ version = "1.5.0" crate-type = ["cdylib", "rlib", "staticlib"] [dependencies] -dojo-world = { git = "https://github.com/dojoengine/dojo", tag = "v1.4.1" } -dojo-types = { git = "https://github.com/dojoengine/dojo", tag = "v1.4.1" } -torii-client = { git = "https://github.com/dojoengine/torii", rev = "4dbb084" } -torii-grpc-client = { git = "https://github.com/dojoengine/torii", rev = "4dbb084" } -torii-typed-data = { git = "https://github.com/dojoengine/torii", rev = "4dbb084" } -torii-libp2p-types = { git = "https://github.com/dojoengine/torii", rev = "4dbb084" } +dojo-world = { git = "https://github.com/dojoengine/dojo", rev = "4145801" } +dojo-types = { git = "https://github.com/dojoengine/dojo", rev = "4145801" } +torii-proto = { git = "https://github.com/dojoengine/torii", rev = "3ae9b23" } +torii-client = { git = "https://github.com/dojoengine/torii", rev = "3ae9b23" } +torii-grpc-client = { git = "https://github.com/dojoengine/torii", rev = "3ae9b23" } +torii-typed-data = { git = "https://github.com/dojoengine/torii", rev = "3ae9b23" } +torii-libp2p-types = { git = "https://github.com/dojoengine/torii", rev = "3ae9b23" } starknet = "0.13.0" starknet-crypto = "0.7.2" diff --git a/dojo.h b/dojo.h index 2cdc231..b0d971c 100644 --- a/dojo.h +++ b/dojo.h @@ -17,7 +17,6 @@ struct OrderBy; struct COptionFieldElement; struct CHashItemFieldElementModelMetadata; struct Subscription; -struct EntityKeysClause; struct Struct; struct Token; struct TokenBalance; @@ -236,6 +235,11 @@ typedef struct Pagination { struct CArrayOrderBy order_by; } Pagination; +typedef struct CArrayFieldElement { + struct FieldElement *data; + uintptr_t data_len; +} CArrayFieldElement; + typedef struct CArrayCOptionFieldElement { struct COptionFieldElement *data; uintptr_t data_len; @@ -373,6 +377,7 @@ typedef struct CompositeClause { } CompositeClause; typedef enum Clause_Tag { + HashedKeys, Keys, CMember, Composite, @@ -381,6 +386,9 @@ typedef enum Clause_Tag { typedef struct Clause { Clause_Tag tag; union { + struct { + struct CArrayFieldElement hashed_keys; + }; struct { struct KeysClause keys; }; @@ -464,11 +472,6 @@ typedef struct CArrayStruct { uintptr_t data_len; } CArrayStruct; -typedef struct CArrayFieldElement { - struct FieldElement *data; - uintptr_t data_len; -} CArrayFieldElement; - typedef struct Event { struct CArrayFieldElement keys; struct CArrayFieldElement data; @@ -790,23 +793,6 @@ typedef struct CHashItemFieldElementModelMetadata { struct ModelMetadata value; } CHashItemFieldElementModelMetadata; -typedef enum EntityKeysClause_Tag { - HashedKeys, - EntityKeys, -} EntityKeysClause_Tag; - -typedef struct EntityKeysClause { - EntityKeysClause_Tag tag; - union { - struct { - struct CArrayFieldElement hashed_keys; - }; - struct { - struct KeysClause entity_keys; - }; - }; -} EntityKeysClause; - typedef struct Member { const char *name; struct Ty *ty; @@ -1073,8 +1059,7 @@ struct ResultWorldMetadata client_metadata(struct ToriiClient *client); * Result containing pointer to Subscription or error */ struct ResultSubscription client_on_entity_state_update(struct ToriiClient *client, - const struct EntityKeysClause *clauses, - uintptr_t clauses_len, + struct COptionClause clause, void (*callback)(struct FieldElement, struct CArrayStruct)); @@ -1092,8 +1077,7 @@ struct ResultSubscription client_on_entity_state_update(struct ToriiClient *clie */ struct Resultbool client_update_entity_subscription(struct ToriiClient *client, struct Subscription *subscription, - const struct EntityKeysClause *clauses, - uintptr_t clauses_len); + struct COptionClause clause); /** * Subscribes to event message updates @@ -1108,8 +1092,7 @@ struct Resultbool client_update_entity_subscription(struct ToriiClient *client, * Result containing pointer to Subscription or error */ struct ResultSubscription client_on_event_message_update(struct ToriiClient *client, - const struct EntityKeysClause *clauses, - uintptr_t clauses_len, + struct COptionClause clause, void (*callback)(struct FieldElement, struct CArrayStruct)); @@ -1127,8 +1110,7 @@ struct ResultSubscription client_on_event_message_update(struct ToriiClient *cli */ struct Resultbool client_update_event_message_subscription(struct ToriiClient *client, struct Subscription *subscription, - const struct EntityKeysClause *clauses, - uintptr_t clauses_len); + struct COptionClause clause); /** * Subscribes to Starknet events @@ -1143,7 +1125,7 @@ struct Resultbool client_update_event_message_subscription(struct ToriiClient *c * Result containing pointer to Subscription or error */ struct ResultSubscription client_on_starknet_event(struct ToriiClient *client, - const struct EntityKeysClause *clauses, + const struct KeysClause *clauses, uintptr_t clauses_len, void (*callback)(struct Event)); diff --git a/dojo.hpp b/dojo.hpp index fd78852..4bebd10 100644 --- a/dojo.hpp +++ b/dojo.hpp @@ -12,7 +12,6 @@ struct ControllerAccount; struct Call; struct Ty; struct Subscription; -struct EntityKeysClause; struct Provider; struct Account; @@ -558,11 +557,16 @@ struct CompositeClause { struct Clause { enum class Tag { + HashedKeys, Keys, CMember, Composite, }; + struct HashedKeys_Body { + CArray _0; + }; + struct Keys_Body { KeysClause _0; }; @@ -577,11 +581,23 @@ struct Clause { Tag tag; union { + HashedKeys_Body hashed_keys; Keys_Body keys; CMember_Body c_member; Composite_Body composite; }; + static Clause HashedKeys(const CArray &_0) { + Clause result; + ::new (&result.hashed_keys._0) (CArray)(_0); + result.tag = Tag::HashedKeys; + return result; + } + + bool IsHashedKeys() const { + return tag == Tag::HashedKeys; + } + static Clause Keys(const KeysClause &_0) { Clause result; ::new (&result.keys._0) (KeysClause)(_0); @@ -877,49 +893,6 @@ struct Policy { const char *description; }; -struct EntityKeysClause { - enum class Tag { - HashedKeys, - EntityKeys, - }; - - struct HashedKeys_Body { - CArray _0; - }; - - struct EntityKeys_Body { - KeysClause _0; - }; - - Tag tag; - union { - HashedKeys_Body hashed_keys; - EntityKeys_Body entity_keys; - }; - - static EntityKeysClause HashedKeys(const CArray &_0) { - EntityKeysClause result; - ::new (&result.hashed_keys._0) (CArray)(_0); - result.tag = Tag::HashedKeys; - return result; - } - - bool IsHashedKeys() const { - return tag == Tag::HashedKeys; - } - - static EntityKeysClause EntityKeys(const KeysClause &_0) { - EntityKeysClause result; - ::new (&result.entity_keys._0) (KeysClause)(_0); - result.tag = Tag::EntityKeys; - return result; - } - - bool IsEntityKeys() const { - return tag == Tag::EntityKeys; - } -}; - extern "C" { /// Creates a new Torii client instance @@ -1139,8 +1112,7 @@ Result client_metadata(ToriiClient *client); /// # Returns /// Result containing pointer to Subscription or error Result client_on_entity_state_update(ToriiClient *client, - const EntityKeysClause *clauses, - uintptr_t clauses_len, + COption clause, void (*callback)(FieldElement, CArray)); /// Updates an existing entity subscription with new clauses @@ -1155,8 +1127,7 @@ Result client_on_entity_state_update(ToriiClient *client, /// Result containing success boolean or error Result client_update_entity_subscription(ToriiClient *client, Subscription *subscription, - const EntityKeysClause *clauses, - uintptr_t clauses_len); + COption clause); /// Subscribes to event message updates /// @@ -1169,8 +1140,7 @@ Result client_update_entity_subscription(ToriiClient *client, /// # Returns /// Result containing pointer to Subscription or error Result client_on_event_message_update(ToriiClient *client, - const EntityKeysClause *clauses, - uintptr_t clauses_len, + COption clause, void (*callback)(FieldElement, CArray)); /// Updates an existing event message subscription @@ -1185,8 +1155,7 @@ Result client_on_event_message_update(ToriiClient *client, /// Result containing success boolean or error Result client_update_event_message_subscription(ToriiClient *client, Subscription *subscription, - const EntityKeysClause *clauses, - uintptr_t clauses_len); + COption clause); /// Subscribes to Starknet events /// @@ -1199,7 +1168,7 @@ Result client_update_event_message_subscription(ToriiClient *client, /// # Returns /// Result containing pointer to Subscription or error Result client_on_starknet_event(ToriiClient *client, - const EntityKeysClause *clauses, + const KeysClause *clauses, uintptr_t clauses_len, void (*callback)(Event)); diff --git a/dojo.pyx b/dojo.pyx index 274e40f..22012c4 100644 --- a/dojo.pyx +++ b/dojo.pyx @@ -154,6 +154,10 @@ cdef extern from *: PaginationDirection direction; CArrayOrderBy order_by; + cdef struct CArrayFieldElement: + FieldElement *data; + uintptr_t data_len; + cdef struct CArrayCOptionFieldElement: COptionFieldElement *data; uintptr_t data_len; @@ -237,12 +241,14 @@ cdef extern from *: CArrayClause clauses; cdef enum Clause_Tag: + HashedKeys, Keys, CMember, Composite, cdef struct Clause: Clause_Tag tag; + CArrayFieldElement hashed_keys; KeysClause keys; MemberClause c_member; CompositeClause composite; @@ -292,10 +298,6 @@ cdef extern from *: Struct *data; uintptr_t data_len; - cdef struct CArrayFieldElement: - FieldElement *data; - uintptr_t data_len; - cdef struct Event: CArrayFieldElement keys; CArrayFieldElement data; @@ -502,15 +504,6 @@ cdef extern from *: FieldElement key; ModelMetadata value; - cdef enum EntityKeysClause_Tag: - HashedKeys, - EntityKeys, - - cdef struct EntityKeysClause: - EntityKeysClause_Tag tag; - CArrayFieldElement hashed_keys; - KeysClause entity_keys; - cdef struct Member: const char *name; Ty *ty; @@ -737,8 +730,7 @@ cdef extern from *: # # Returns # Result containing pointer to Subscription or error ResultSubscription client_on_entity_state_update(ToriiClient *client, - const EntityKeysClause *clauses, - uintptr_t clauses_len, + COptionClause clause, void (*callback)(FieldElement, CArrayStruct)); # Updates an existing entity subscription with new clauses @@ -753,8 +745,7 @@ cdef extern from *: # Result containing success boolean or error Resultbool client_update_entity_subscription(ToriiClient *client, Subscription *subscription, - const EntityKeysClause *clauses, - uintptr_t clauses_len); + COptionClause clause); # Subscribes to event message updates # @@ -767,8 +758,7 @@ cdef extern from *: # # Returns # Result containing pointer to Subscription or error ResultSubscription client_on_event_message_update(ToriiClient *client, - const EntityKeysClause *clauses, - uintptr_t clauses_len, + COptionClause clause, void (*callback)(FieldElement, CArrayStruct)); # Updates an existing event message subscription @@ -783,8 +773,7 @@ cdef extern from *: # Result containing success boolean or error Resultbool client_update_event_message_subscription(ToriiClient *client, Subscription *subscription, - const EntityKeysClause *clauses, - uintptr_t clauses_len); + COptionClause clause); # Subscribes to Starknet events # @@ -797,7 +786,7 @@ cdef extern from *: # # Returns # Result containing pointer to Subscription or error ResultSubscription client_on_starknet_event(ToriiClient *client, - const EntityKeysClause *clauses, + const KeysClause *clauses, uintptr_t clauses_len, void (*callback)(Event)); diff --git a/src/c/mod.rs b/src/c/mod.rs index 47cb274..e90682a 100644 --- a/src/c/mod.rs +++ b/src/c/mod.rs @@ -52,9 +52,9 @@ use torii_libp2p_types::Message; use torii_typed_data::TypedData; use tower_http::cors::{AllowOrigin, CorsLayer}; use types::{ - BlockId, CArray, COption, Call, Controller, Entity, EntityKeysClause, Error, Event, - IndexerUpdate, Page, Policy, Query, Result, Signature, Struct, Token, TokenBalance, - ToriiClient, Ty, WorldMetadata, + BlockId, CArray, COption, Call, Clause, Controller, Entity, Error, Event, IndexerUpdate, + KeysClause, Page, Policy, Query, Result, Signature, Struct, Token, TokenBalance, ToriiClient, + Ty, WorldMetadata, }; use url::Url; @@ -831,22 +831,15 @@ pub unsafe extern "C" fn client_metadata(client: *mut ToriiClient) -> Result, callback: unsafe extern "C" fn(types::FieldElement, CArray), ) -> Result<*mut Subscription> { let client = Arc::new(unsafe { &*client }); - let clauses = if clauses.is_null() || clauses_len == 0 { - Vec::new() - } else { - let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) }; - clauses.iter().map(|c| c.clone().into()).collect::>() - }; - let subscription_id = Arc::new(AtomicU64::new(0)); let (trigger, tripwire) = Tripwire::new(); let subscription = Subscription { id: Arc::clone(&subscription_id), trigger }; + let clause: Option = clause.map(|c| c.into()).into(); // Spawn a new thread to handle the stream and reconnections let client_clone = client.clone(); @@ -856,7 +849,7 @@ pub unsafe extern "C" fn client_on_entity_state_update( let max_backoff = Duration::from_secs(60); loop { - let rcv = client_clone.inner.on_entity_updated(clauses.clone()).await; + let rcv = client_clone.inner.on_entity_updated(clause.clone()).await; if let Ok(rcv) = rcv { backoff = Duration::from_secs(1); // Reset backoff on successful connection @@ -897,20 +890,14 @@ pub unsafe extern "C" fn client_on_entity_state_update( pub unsafe extern "C" fn client_update_entity_subscription( client: *mut ToriiClient, subscription: *mut Subscription, - clauses: *const EntityKeysClause, - clauses_len: usize, + clause: COption, ) -> Result { - let clauses = if clauses.is_null() || clauses_len == 0 { - Vec::new() - } else { - let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) }; - clauses.iter().map(|c| c.clone().into()).collect::>() - }; + let clause: Option = clause.map(|c| c.into()).into(); match RUNTIME.block_on( (*client) .inner - .update_entity_subscription((*subscription).id.load(Ordering::SeqCst), clauses), + .update_entity_subscription((*subscription).id.load(Ordering::SeqCst), clause), ) { Ok(_) => Result::Ok(true), Err(e) => Result::Err(e.into()), @@ -930,17 +917,11 @@ pub unsafe extern "C" fn client_update_entity_subscription( #[no_mangle] pub unsafe extern "C" fn client_on_event_message_update( client: *mut ToriiClient, - clauses: *const EntityKeysClause, - clauses_len: usize, + clause: COption, callback: unsafe extern "C" fn(types::FieldElement, CArray), ) -> Result<*mut Subscription> { let client = Arc::new(unsafe { &*client }); - let clauses = if clauses.is_null() || clauses_len == 0 { - Vec::new() - } else { - let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) }; - clauses.iter().map(|c| c.clone().into()).collect::>() - }; + let clause: Option = clause.map(|c| c.into()).into(); let subscription_id = Arc::new(AtomicU64::new(0)); let (trigger, tripwire) = Tripwire::new(); @@ -955,7 +936,7 @@ pub unsafe extern "C" fn client_on_event_message_update( let max_backoff = Duration::from_secs(60); loop { - let rcv = client_clone.inner.on_event_message_updated(clauses.clone()).await; + let rcv = client_clone.inner.on_event_message_updated(clause.clone()).await; if let Ok(rcv) = rcv { backoff = Duration::from_secs(1); // Reset backoff on successful connection @@ -996,20 +977,14 @@ pub unsafe extern "C" fn client_on_event_message_update( pub unsafe extern "C" fn client_update_event_message_subscription( client: *mut ToriiClient, subscription: *mut Subscription, - clauses: *const EntityKeysClause, - clauses_len: usize, + clause: COption, ) -> Result { - let clauses = if clauses.is_null() || clauses_len == 0 { - Vec::new() - } else { - let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) }; - clauses.iter().map(|c| c.clone().into()).collect::>() - }; + let clause: Option = clause.map(|c| c.into()).into(); match RUNTIME.block_on( (*client) .inner - .update_event_message_subscription((*subscription).id.load(Ordering::SeqCst), clauses), + .update_event_message_subscription((*subscription).id.load(Ordering::SeqCst), clause), ) { Ok(_) => Result::Ok(true), Err(e) => Result::Err(e.into()), @@ -1029,7 +1004,7 @@ pub unsafe extern "C" fn client_update_event_message_subscription( #[no_mangle] pub unsafe extern "C" fn client_on_starknet_event( client: *mut ToriiClient, - clauses: *const EntityKeysClause, + clauses: *const KeysClause, clauses_len: usize, callback: unsafe extern "C" fn(Event), ) -> Result<*mut Subscription> { @@ -2116,7 +2091,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_client::types::schema::Entity = (*Box::::from_raw(entity)).into(); + let _: torii_proto::schema::Entity = (*Box::::from_raw(entity)).into(); } } diff --git a/src/c/types.rs b/src/c/types.rs index c8af6c0..2e397bc 100644 --- a/src/c/types.rs +++ b/src/c/types.rs @@ -11,11 +11,11 @@ pub struct Page { pub next_cursor: COption<*const c_char>, } -impl From> for Page +impl From> for Page where U: From, { - fn from(val: torii_grpc_client::types::Page) -> Self { + fn from(val: torii_proto::Page) -> Self { let items = val.items.into_iter().map(|t| t.into()).collect::>(); Page { items: items.into(), @@ -117,8 +117,8 @@ pub struct Controller { pub deployed_at_timestamp: u64, } -impl From for Controller { - fn from(val: torii_grpc_client::types::Controller) -> Self { +impl From for Controller { + fn from(val: torii_proto::Controller) -> Self { Controller { address: val.address.into(), username: CString::new(val.username.clone()).unwrap().into_raw(), @@ -138,8 +138,8 @@ pub struct Token { pub metadata: *const c_char, } -impl From for Token { - fn from(val: torii_grpc_client::types::Token) -> Self { +impl From for Token { + fn from(val: torii_proto::Token) -> Self { Token { token_id: val.token_id.into(), contract_address: val.contract_address.into(), @@ -160,8 +160,8 @@ pub struct TokenBalance { pub token_id: U256, } -impl From for TokenBalance { - fn from(val: torii_grpc_client::types::TokenBalance) -> Self { +impl From for TokenBalance { + fn from(val: torii_proto::TokenBalance) -> Self { TokenBalance { balance: val.balance.into(), account_address: val.account_address.into(), @@ -180,9 +180,9 @@ pub struct IndexerUpdate { pub contract_address: FieldElement, } -impl From for torii_grpc_client::types::IndexerUpdate { +impl From for torii_proto::IndexerUpdate { fn from(val: IndexerUpdate) -> Self { - torii_grpc_client::types::IndexerUpdate { + torii_proto::IndexerUpdate { head: val.head, tps: val.tps, last_block_timestamp: val.last_block_timestamp, @@ -191,8 +191,8 @@ impl From for torii_grpc_client::types::IndexerUpdate { } } -impl From for IndexerUpdate { - fn from(val: torii_grpc_client::types::IndexerUpdate) -> Self { +impl From for IndexerUpdate { + fn from(val: torii_proto::IndexerUpdate) -> Self { IndexerUpdate { head: val.head, tps: val.tps, @@ -450,9 +450,9 @@ pub struct Pagination { pub order_by: CArray, } -impl From for torii_grpc_client::types::Pagination { +impl From for torii_proto::Pagination { fn from(val: Pagination) -> Self { - torii_grpc_client::types::Pagination { + torii_proto::Pagination { cursor: val .cursor .map(|c| unsafe { CStr::from_ptr(c).to_string_lossy().to_string() }) @@ -464,8 +464,8 @@ impl From for torii_grpc_client::types::Pagination { } } -impl From for Pagination { - fn from(val: torii_grpc_client::types::Pagination) -> Self { +impl From for Pagination { + fn from(val: torii_proto::Pagination) -> Self { Pagination { cursor: val.cursor.map(|c| CString::new(c).unwrap().into_raw() as *const c_char).into(), limit: val.limit, @@ -482,24 +482,20 @@ pub enum PaginationDirection { Backward, } -impl From for torii_grpc_client::types::PaginationDirection { +impl From for torii_proto::PaginationDirection { fn from(val: PaginationDirection) -> Self { match val { - PaginationDirection::Forward => torii_grpc_client::types::PaginationDirection::Forward, - PaginationDirection::Backward => { - torii_grpc_client::types::PaginationDirection::Backward - } + PaginationDirection::Forward => torii_proto::PaginationDirection::Forward, + PaginationDirection::Backward => torii_proto::PaginationDirection::Backward, } } } -impl From for PaginationDirection { - fn from(val: torii_grpc_client::types::PaginationDirection) -> Self { +impl From for PaginationDirection { + fn from(val: torii_proto::PaginationDirection) -> Self { match val { - torii_grpc_client::types::PaginationDirection::Forward => PaginationDirection::Forward, - torii_grpc_client::types::PaginationDirection::Backward => { - PaginationDirection::Backward - } + torii_proto::PaginationDirection::Forward => PaginationDirection::Forward, + torii_proto::PaginationDirection::Backward => PaginationDirection::Backward, } } } @@ -511,9 +507,9 @@ pub struct OrderBy { pub direction: OrderDirection, } -impl From for torii_grpc_client::types::OrderBy { +impl From for torii_proto::OrderBy { fn from(val: OrderBy) -> Self { - torii_grpc_client::types::OrderBy { + torii_proto::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(), @@ -521,8 +517,8 @@ impl From for torii_grpc_client::types::OrderBy { } } -impl From for OrderBy { - fn from(val: torii_grpc_client::types::OrderBy) -> Self { +impl From for OrderBy { + fn from(val: torii_proto::OrderBy) -> Self { OrderBy { model: CString::new(val.model.clone()).unwrap().into_raw(), member: CString::new(val.member.clone()).unwrap().into_raw(), @@ -538,20 +534,20 @@ pub enum OrderDirection { Desc, } -impl From for torii_grpc_client::types::OrderDirection { +impl From for torii_proto::OrderDirection { fn from(val: OrderDirection) -> Self { match val { - OrderDirection::Asc => torii_grpc_client::types::OrderDirection::Asc, - OrderDirection::Desc => torii_grpc_client::types::OrderDirection::Desc, + OrderDirection::Asc => torii_proto::OrderDirection::Asc, + OrderDirection::Desc => torii_proto::OrderDirection::Desc, } } } -impl From for OrderDirection { - fn from(val: torii_grpc_client::types::OrderDirection) -> Self { +impl From for OrderDirection { + fn from(val: torii_proto::OrderDirection) -> Self { match val { - torii_grpc_client::types::OrderDirection::Asc => OrderDirection::Asc, - torii_grpc_client::types::OrderDirection::Desc => OrderDirection::Desc, + torii_proto::OrderDirection::Asc => OrderDirection::Asc, + torii_proto::OrderDirection::Desc => OrderDirection::Desc, } } } @@ -559,18 +555,12 @@ impl From for OrderDirection { #[derive(Clone, Debug)] #[repr(C)] pub enum Clause { + HashedKeys(CArray), Keys(KeysClause), CMember(MemberClause), Composite(CompositeClause), } -#[derive(Clone, Debug)] -#[repr(C)] -pub enum EntityKeysClause { - HashedKeys(CArray), - EntityKeys(KeysClause), -} - #[derive(Clone, Debug)] #[repr(C)] pub enum PatternMatching { @@ -594,37 +584,35 @@ pub enum MemberValue { List(CArray), } -impl From for torii_grpc_client::types::MemberValue { +impl From for torii_proto::MemberValue { fn from(val: MemberValue) -> Self { match val { MemberValue::PrimitiveValue(primitive) => { - torii_grpc_client::types::MemberValue::Primitive(primitive.into()) + torii_proto::MemberValue::Primitive(primitive.into()) } - MemberValue::String(string) => torii_grpc_client::types::MemberValue::String(unsafe { + MemberValue::String(string) => torii_proto::MemberValue::String(unsafe { CStr::from_ptr(string).to_string_lossy().to_string() }), MemberValue::List(list) => { let values: Vec = list.into(); - let values = values - .into_iter() - .map(|v| v.into()) - .collect::>(); - torii_grpc_client::types::MemberValue::List(values) + let values = + values.into_iter().map(|v| v.into()).collect::>(); + torii_proto::MemberValue::List(values) } } } } -impl From for MemberValue { - fn from(val: torii_grpc_client::types::MemberValue) -> Self { +impl From for MemberValue { + fn from(val: torii_proto::MemberValue) -> Self { match val { - torii_grpc_client::types::MemberValue::Primitive(primitive) => { + torii_proto::MemberValue::Primitive(primitive) => { MemberValue::PrimitiveValue(primitive.into()) } - torii_grpc_client::types::MemberValue::String(string) => { + torii_proto::MemberValue::String(string) => { MemberValue::String(CString::new(string.clone()).unwrap().into_raw()) } - torii_grpc_client::types::MemberValue::List(list) => { + torii_proto::MemberValue::List(list) => { let values = list.into_iter().map(|v| v.into()).collect::>(); MemberValue::List(values.into()) } @@ -692,52 +680,52 @@ pub struct Entity { pub models: CArray, } -impl From for torii_grpc_client::types::schema::Entity { +impl From for torii_proto::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_client::types::schema::Entity { hashed_keys: val.hashed_keys.into(), models } + torii_proto::schema::Entity { hashed_keys: val.hashed_keys.into(), models } } } -impl From for Entity { - fn from(val: torii_grpc_client::types::schema::Entity) -> Self { +impl From for Entity { + fn from(val: torii_proto::schema::Entity) -> Self { let models = val.models.into_iter().map(|m| m.into()).collect::>(); Entity { hashed_keys: val.hashed_keys.into(), models: models.into() } } } -impl From for torii_grpc_client::types::ValueType { +impl From for torii_proto::ValueType { fn from(value: ValueType) -> Self { match value { ValueType::String(v) => { let v = unsafe { CStr::from_ptr(v).to_string_lossy().into_owned() }; - torii_grpc_client::types::ValueType::String(v) + torii_proto::ValueType::String(v) } - ValueType::Int(v) => torii_grpc_client::types::ValueType::Int(v), - ValueType::UInt(v) => torii_grpc_client::types::ValueType::UInt(v), - ValueType::VBool(v) => torii_grpc_client::types::ValueType::Bool(v), + ValueType::Int(v) => torii_proto::ValueType::Int(v), + ValueType::UInt(v) => torii_proto::ValueType::UInt(v), + ValueType::VBool(v) => torii_proto::ValueType::Bool(v), ValueType::Bytes(v) => { let v = v.into(); - torii_grpc_client::types::ValueType::Bytes(v) + torii_proto::ValueType::Bytes(v) } } } } -impl From for ValueType { - fn from(value: torii_grpc_client::types::ValueType) -> Self { +impl From for ValueType { + fn from(value: torii_proto::ValueType) -> Self { match value { - torii_grpc_client::types::ValueType::String(v) => { + torii_proto::ValueType::String(v) => { let v = CString::new(v.clone()).unwrap().into_raw(); ValueType::String(v) } - torii_grpc_client::types::ValueType::Int(v) => ValueType::Int(v), - torii_grpc_client::types::ValueType::UInt(v) => ValueType::UInt(v), - torii_grpc_client::types::ValueType::Bool(v) => ValueType::VBool(v), - torii_grpc_client::types::ValueType::Bytes(v) => { + torii_proto::ValueType::Int(v) => ValueType::Int(v), + torii_proto::ValueType::UInt(v) => ValueType::UInt(v), + torii_proto::ValueType::Bool(v) => ValueType::VBool(v), + torii_proto::ValueType::Bytes(v) => { let v = v.clone().into(); ValueType::Bytes(v) } @@ -1030,12 +1018,12 @@ impl From for Primitive { } } -impl From for torii_grpc_client::types::Query { +impl From for torii_proto::Query { fn from(val: Query) -> Self { let models: Vec = CStringArray(val.models).into(); let clause = val.clause.map(|c| c.into()).into(); - torii_grpc_client::types::Query { + torii_proto::Query { pagination: val.pagination.into(), clause, models, @@ -1045,8 +1033,8 @@ impl From for torii_grpc_client::types::Query { } } -impl From for Query { - fn from(val: torii_grpc_client::types::Query) -> Self { +impl From for Query { + fn from(val: torii_proto::Query) -> Self { let models = StringVec(val.models).into(); Query { @@ -1059,93 +1047,57 @@ impl From for Query { } } -impl From for torii_grpc_client::types::Clause { +impl From for torii_proto::Clause { fn from(val: Clause) -> Self { match val { - Clause::Keys(keys) => torii_grpc_client::types::Clause::Keys(keys.into()), - Clause::CMember(member) => torii_grpc_client::types::Clause::Member(member.into()), - Clause::Composite(composite) => { - torii_grpc_client::types::Clause::Composite(composite.into()) - } + Clause::HashedKeys(keys) => torii_proto::Clause::HashedKeys(keys.into()), + Clause::Keys(keys) => torii_proto::Clause::Keys(keys.into()), + Clause::CMember(member) => torii_proto::Clause::Member(member.into()), + Clause::Composite(composite) => torii_proto::Clause::Composite(composite.into()), } } } -impl From for Clause { - fn from(val: torii_grpc_client::types::Clause) -> Self { +impl From for Clause { + fn from(val: torii_proto::Clause) -> Self { match val { - torii_grpc_client::types::Clause::Keys(keys) => Clause::Keys(keys.into()), - torii_grpc_client::types::Clause::Member(member) => Clause::CMember(member.into()), - torii_grpc_client::types::Clause::Composite(composite) => { - Clause::Composite(composite.into()) - } + torii_proto::Clause::HashedKeys(keys) => Clause::HashedKeys(keys.into()), + torii_proto::Clause::Keys(keys) => Clause::Keys(keys.into()), + torii_proto::Clause::Member(member) => Clause::CMember(member.into()), + torii_proto::Clause::Composite(composite) => Clause::Composite(composite.into()), } } } -impl From for torii_grpc_client::types::PatternMatching { +impl From for torii_proto::PatternMatching { fn from(val: PatternMatching) -> Self { match val { - PatternMatching::FixedLen => torii_grpc_client::types::PatternMatching::FixedLen, - PatternMatching::VariableLen => torii_grpc_client::types::PatternMatching::VariableLen, - } - } -} - -impl From for PatternMatching { - fn from(val: torii_grpc_client::types::PatternMatching) -> Self { - match val { - torii_grpc_client::types::PatternMatching::FixedLen => PatternMatching::FixedLen, - torii_grpc_client::types::PatternMatching::VariableLen => PatternMatching::VariableLen, + PatternMatching::FixedLen => torii_proto::PatternMatching::FixedLen, + PatternMatching::VariableLen => torii_proto::PatternMatching::VariableLen, } } } -impl From for torii_grpc_client::types::EntityKeysClause { - fn from(val: EntityKeysClause) -> Self { +impl From for PatternMatching { + fn from(val: torii_proto::PatternMatching) -> Self { match val { - EntityKeysClause::HashedKeys(keys) => { - let keys: Vec = keys.into(); - let keys = keys.into_iter().map(|k| k.into()).collect(); - - torii_grpc_client::types::EntityKeysClause::HashedKeys(keys) - } - EntityKeysClause::EntityKeys(keys) => { - torii_grpc_client::types::EntityKeysClause::Keys(keys.into()) - } + torii_proto::PatternMatching::FixedLen => PatternMatching::FixedLen, + torii_proto::PatternMatching::VariableLen => PatternMatching::VariableLen, } } } -impl From for EntityKeysClause { - fn from(val: torii_grpc_client::types::EntityKeysClause) -> Self { - match val { - torii_grpc_client::types::EntityKeysClause::HashedKeys(keys) => { - let keys = keys.into_iter().map(|k| k.into()).collect::>(); - EntityKeysClause::HashedKeys(keys.into()) - } - torii_grpc_client::types::EntityKeysClause::Keys(keys) => { - EntityKeysClause::EntityKeys(keys.into()) - } - } - } -} - -impl From for torii_grpc_client::types::KeysClause { +impl From for torii_proto::KeysClause { fn from(val: KeysClause) -> Self { let keys: Vec> = COptionArray(val.keys).into(); let models: Vec = CStringArray(val.models).into(); - torii_grpc_client::types::KeysClause { - keys, - pattern_matching: val.pattern_matching.into(), - models, - } + torii_proto::KeysClause { keys, pattern_matching: val.pattern_matching.into(), models } } } -impl From for KeysClause { - fn from(val: torii_grpc_client::types::KeysClause) -> Self { +impl From for KeysClause { + fn from(val: torii_proto::KeysClause) -> Self { KeysClause { models: StringVec(val.models).into(), keys: val.keys.into(), @@ -1154,9 +1106,9 @@ impl From for KeysClause { } } -impl From for torii_grpc_client::types::MemberClause { +impl From for torii_proto::MemberClause { fn from(val: MemberClause) -> Self { - torii_grpc_client::types::MemberClause { + torii_proto::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.into(), @@ -1165,8 +1117,8 @@ impl From for torii_grpc_client::types::MemberClause { } } -impl From for MemberClause { - fn from(val: torii_grpc_client::types::MemberClause) -> Self { +impl From for MemberClause { + fn from(val: torii_proto::MemberClause) -> Self { MemberClause { model: CString::new(val.model.clone()).unwrap().into_raw(), member: CString::new(val.member.clone()).unwrap().into_raw(), @@ -1176,17 +1128,17 @@ impl From for MemberClause { } } -impl From for torii_grpc_client::types::CompositeClause { +impl From for torii_proto::CompositeClause { fn from(val: CompositeClause) -> Self { let operator = val.operator.into(); let clauses = val.clauses.into(); - torii_grpc_client::types::CompositeClause { operator, clauses } + torii_proto::CompositeClause { operator, clauses } } } -impl From for CompositeClause { - fn from(val: torii_grpc_client::types::CompositeClause) -> Self { +impl From for CompositeClause { + fn from(val: torii_proto::CompositeClause) -> Self { let operator = val.operator.into(); let clauses = val.clauses.into(); @@ -1194,65 +1146,65 @@ impl From for CompositeClause { } } -impl From for torii_grpc_client::types::LogicalOperator { +impl From for torii_proto::LogicalOperator { fn from(val: LogicalOperator) -> Self { match val { - LogicalOperator::And => torii_grpc_client::types::LogicalOperator::And, - LogicalOperator::Or => torii_grpc_client::types::LogicalOperator::Or, + LogicalOperator::And => torii_proto::LogicalOperator::And, + LogicalOperator::Or => torii_proto::LogicalOperator::Or, } } } -impl From for LogicalOperator { - fn from(val: torii_grpc_client::types::LogicalOperator) -> Self { +impl From for LogicalOperator { + fn from(val: torii_proto::LogicalOperator) -> Self { match val { - torii_grpc_client::types::LogicalOperator::And => LogicalOperator::And, - torii_grpc_client::types::LogicalOperator::Or => LogicalOperator::Or, + torii_proto::LogicalOperator::And => LogicalOperator::And, + torii_proto::LogicalOperator::Or => LogicalOperator::Or, } } } -impl From for torii_grpc_client::types::ComparisonOperator { +impl From for torii_proto::ComparisonOperator { fn from(val: ComparisonOperator) -> Self { match val { - ComparisonOperator::Eq => torii_grpc_client::types::ComparisonOperator::Eq, - ComparisonOperator::Neq => torii_grpc_client::types::ComparisonOperator::Neq, - ComparisonOperator::Gt => torii_grpc_client::types::ComparisonOperator::Gt, - ComparisonOperator::Gte => torii_grpc_client::types::ComparisonOperator::Gte, - ComparisonOperator::Lt => torii_grpc_client::types::ComparisonOperator::Lt, - ComparisonOperator::Lte => torii_grpc_client::types::ComparisonOperator::Lte, - ComparisonOperator::In => torii_grpc_client::types::ComparisonOperator::In, - ComparisonOperator::NotIn => torii_grpc_client::types::ComparisonOperator::NotIn, + ComparisonOperator::Eq => torii_proto::ComparisonOperator::Eq, + ComparisonOperator::Neq => torii_proto::ComparisonOperator::Neq, + ComparisonOperator::Gt => torii_proto::ComparisonOperator::Gt, + ComparisonOperator::Gte => torii_proto::ComparisonOperator::Gte, + ComparisonOperator::Lt => torii_proto::ComparisonOperator::Lt, + ComparisonOperator::Lte => torii_proto::ComparisonOperator::Lte, + ComparisonOperator::In => torii_proto::ComparisonOperator::In, + ComparisonOperator::NotIn => torii_proto::ComparisonOperator::NotIn, } } } -impl From for ComparisonOperator { - fn from(val: torii_grpc_client::types::ComparisonOperator) -> Self { +impl From for ComparisonOperator { + fn from(val: torii_proto::ComparisonOperator) -> Self { match val { - torii_grpc_client::types::ComparisonOperator::Eq => ComparisonOperator::Eq, - torii_grpc_client::types::ComparisonOperator::Neq => ComparisonOperator::Neq, - torii_grpc_client::types::ComparisonOperator::Gt => ComparisonOperator::Gt, - torii_grpc_client::types::ComparisonOperator::Gte => ComparisonOperator::Gte, - torii_grpc_client::types::ComparisonOperator::Lt => ComparisonOperator::Lt, - torii_grpc_client::types::ComparisonOperator::Lte => ComparisonOperator::Lte, - torii_grpc_client::types::ComparisonOperator::In => ComparisonOperator::In, - torii_grpc_client::types::ComparisonOperator::NotIn => ComparisonOperator::NotIn, + torii_proto::ComparisonOperator::Eq => ComparisonOperator::Eq, + torii_proto::ComparisonOperator::Neq => ComparisonOperator::Neq, + torii_proto::ComparisonOperator::Gt => ComparisonOperator::Gt, + torii_proto::ComparisonOperator::Gte => ComparisonOperator::Gte, + torii_proto::ComparisonOperator::Lt => ComparisonOperator::Lt, + torii_proto::ComparisonOperator::Lte => ComparisonOperator::Lte, + torii_proto::ComparisonOperator::In => ComparisonOperator::In, + torii_proto::ComparisonOperator::NotIn => ComparisonOperator::NotIn, } } } -impl From for torii_grpc_client::types::Value { +impl From for torii_proto::Value { fn from(val: Value) -> Self { - torii_grpc_client::types::Value { + torii_proto::Value { primitive_type: val.primitive_type.into(), value_type: val.value_type.into(), } } } -impl From for Value { - fn from(val: torii_grpc_client::types::Value) -> Self { +impl From for Value { + fn from(val: torii_proto::Value) -> Self { Value { primitive_type: val.primitive_type.into(), value_type: val.value_type.into() } } } @@ -1353,21 +1305,17 @@ pub struct Event { pub transaction_hash: FieldElement, } -impl From for torii_grpc_client::types::Event { +impl From for torii_proto::Event { fn from(val: Event) -> Self { let keys: Vec<_> = val.keys.into(); let data: Vec<_> = val.data.into(); - torii_grpc_client::types::Event { - keys, - data, - transaction_hash: val.transaction_hash.into(), - } + torii_proto::Event { keys, data, transaction_hash: val.transaction_hash.into() } } } -impl From for Event { - fn from(val: torii_grpc_client::types::Event) -> Self { +impl From for Event { + fn from(val: torii_proto::Event) -> Self { let keys = val.keys.into_iter().map(|k| k.into()).collect::>(); let data = val.data.into_iter().map(|k| k.into()).collect::>(); diff --git a/src/wasm/mod.rs b/src/wasm/mod.rs index 2de9f6f..429b706 100644 --- a/src/wasm/mod.rs +++ b/src/wasm/mod.rs @@ -38,9 +38,9 @@ use crate::utils::watch_tx; mod types; use types::{ - BlockId, Call, Calls, ClientConfig, Controller, Controllers, Entities, Entity, IndexerUpdate, - KeysClause, KeysClauses, Model, Page, Query, Signature, Token, TokenBalance, TokenBalances, - Tokens, WasmU256, + BlockId, Call, Calls, Clause, ClientConfig, Controller, Controllers, Entities, Entity, + IndexerUpdate, KeysClause, KeysClauses, Model, Page, Query, Signature, Token, TokenBalance, + TokenBalances, Tokens, WasmU256, }; const JSON_COMPAT_SERIALIZER: serde_wasm_bindgen::Serializer = @@ -917,11 +917,11 @@ impl ToriiClient { let results = self .inner - .entities(torii_grpc_client::types::Query { - pagination: torii_grpc_client::types::Pagination { + .entities(torii_proto::Query { + pagination: torii_proto::Pagination { limit, cursor, - direction: torii_grpc_client::types::PaginationDirection::Forward, + direction: torii_proto::PaginationDirection::Forward, order_by: vec![], }, no_hashed_keys: false, @@ -968,13 +968,13 @@ impl ToriiClient { #[wasm_bindgen(js_name = onEntityUpdated)] pub fn on_entity_updated( &self, - clauses: KeysClauses, + clause: Option, callback: js_sys::Function, ) -> Result { #[cfg(feature = "console-error-panic")] console_error_panic_hook::set_once(); - let clauses: Vec<_> = clauses.into_iter().map(|c| c.into()).collect(); + let clause = clause.map(|c| c.into()); let subscription_id = Arc::new(AtomicU64::new(0)); let (trigger, tripwire) = Tripwire::new(); @@ -988,7 +988,7 @@ impl ToriiClient { let max_backoff = 60000; loop { - if let Ok(stream) = client.on_entity_updated(clauses.clone()).await { + if let Ok(stream) = client.on_entity_updated(clause.clone()).await { backoff = 1000; // Reset backoff on successful connection let mut stream = stream.take_until_if(tripwire.clone()); @@ -1031,11 +1031,11 @@ impl ToriiClient { pub async fn update_entity_subscription( &self, subscription: &Subscription, - clauses: KeysClauses, + clause: Option, ) -> Result<(), JsValue> { - let clauses = clauses.into_iter().map(|c| c.into()).collect(); + let clause = clause.map(|c| c.into()); self.inner - .update_entity_subscription(subscription.id.load(Ordering::SeqCst), clauses) + .update_entity_subscription(subscription.id.load(Ordering::SeqCst), clause) .await .map_err(|err| JsValue::from(format!("failed to update subscription: {err}"))) } @@ -1051,13 +1051,13 @@ impl ToriiClient { #[wasm_bindgen(js_name = onEventMessageUpdated)] pub fn on_event_message_updated( &self, - clauses: KeysClauses, + clause: Option, callback: js_sys::Function, ) -> Result { #[cfg(feature = "console-error-panic")] console_error_panic_hook::set_once(); - let clauses: Vec<_> = clauses.into_iter().map(|c| c.into()).collect(); + let clause = clause.map(|c| c.into()); let subscription_id = Arc::new(AtomicU64::new(0)); let (trigger, tripwire) = Tripwire::new(); @@ -1071,7 +1071,7 @@ impl ToriiClient { let max_backoff = 60000; loop { - if let Ok(stream) = client.on_event_message_updated(clauses.clone()).await { + if let Ok(stream) = client.on_event_message_updated(clause.clone()).await { backoff = 1000; // Reset backoff on successful connection let mut stream = stream.take_until_if(tripwire.clone()); @@ -1114,11 +1114,11 @@ impl ToriiClient { pub async fn update_event_message_subscription( &self, subscription: &Subscription, - clauses: KeysClauses, + clause: Option, ) -> Result<(), JsValue> { - let clauses = clauses.into_iter().map(|c| c.into()).collect(); + let clause = clause.map(|c| c.into()); self.inner - .update_event_message_subscription(subscription.id.load(Ordering::SeqCst), clauses) + .update_event_message_subscription(subscription.id.load(Ordering::SeqCst), clause) .await .map_err(|err| JsValue::from(format!("failed to update subscription: {err}"))) } diff --git a/src/wasm/types.rs b/src/wasm/types.rs index fba92f2..eced59b 100644 --- a/src/wasm/types.rs +++ b/src/wasm/types.rs @@ -19,11 +19,11 @@ pub struct Page { pub next_cursor: Option, } -impl From> for Page +impl From> for Page where U: From, { - fn from(value: torii_grpc_client::types::Page) -> Self { + fn from(value: torii_proto::Page) -> Self { Self { items: value.items.into_iter().map(|t| t.into()).collect(), next_cursor: value.next_cursor, @@ -53,8 +53,8 @@ pub struct Controller { pub deployed_at_timestamp: u64, } -impl From for Controller { - fn from(value: torii_grpc_client::types::Controller) -> Self { +impl From for Controller { + fn from(value: torii_proto::Controller) -> Self { Self { address: format!("{:#x}", value.address), username: value.username.clone(), @@ -82,8 +82,8 @@ pub struct Token { pub metadata: String, } -impl From for Token { - fn from(value: torii_grpc_client::types::Token) -> Self { +impl From for Token { + fn from(value: torii_proto::Token) -> Self { Self { contract_address: format!("{:#x}", value.contract_address), token_id: format!("0x{:x}", value.token_id), @@ -104,8 +104,8 @@ pub struct TokenBalance { pub token_id: String, } -impl From for TokenBalance { - fn from(value: torii_grpc_client::types::TokenBalance) -> Self { +impl From for TokenBalance { + fn from(value: torii_proto::TokenBalance) -> Self { Self { balance: format!("0x{:x}", value.balance), account_address: format!("{:#x}", value.account_address), @@ -124,7 +124,7 @@ pub struct IndexerUpdate { pub contract_address: String, } -impl From for torii_grpc_client::types::IndexerUpdate { +impl From for torii_proto::IndexerUpdate { fn from(value: IndexerUpdate) -> Self { Self { head: value.head, @@ -135,8 +135,8 @@ impl From for torii_grpc_client::types::IndexerUpdate { } } -impl From for IndexerUpdate { - fn from(value: torii_grpc_client::types::IndexerUpdate) -> Self { +impl From for IndexerUpdate { + fn from(value: torii_proto::IndexerUpdate) -> Self { Self { head: value.head, tps: value.tps, @@ -225,8 +225,8 @@ pub struct Entity { pub models: HashMap, } -impl From for Entity { - fn from(value: torii_grpc_client::types::schema::Entity) -> Self { +impl From for Entity { + fn from(value: torii_proto::schema::Entity) -> Self { Self { hashed_keys: format!("{:#x}", value.hashed_keys), models: value.models.into_iter().map(|m| (m.name.clone(), m.into())).collect(), @@ -321,7 +321,7 @@ pub struct Pagination { pub order_by: Vec, } -impl From for torii_grpc_client::types::Pagination { +impl From for torii_proto::Pagination { fn from(value: Pagination) -> Self { Self { limit: value.limit, @@ -339,7 +339,7 @@ pub enum PaginationDirection { Backward, } -impl From for torii_grpc_client::types::PaginationDirection { +impl From for torii_proto::PaginationDirection { fn from(value: PaginationDirection) -> Self { match value { PaginationDirection::Forward => Self::Forward, @@ -356,7 +356,7 @@ pub struct OrderBy { pub direction: OrderDirection, } -impl From for torii_grpc_client::types::OrderBy { +impl From for torii_proto::OrderBy { fn from(value: OrderBy) -> Self { Self { model: value.model.clone(), @@ -373,7 +373,7 @@ pub enum OrderDirection { Desc, } -impl From for torii_grpc_client::types::OrderDirection { +impl From for torii_proto::OrderDirection { fn from(value: OrderDirection) -> Self { match value { OrderDirection::Asc => Self::Asc, @@ -382,7 +382,7 @@ impl From for torii_grpc_client::types::OrderDirection { } } -impl From for torii_grpc_client::types::Query { +impl From for torii_proto::Query { fn from(value: Query) -> Self { Self { pagination: value.pagination.into(), @@ -397,14 +397,12 @@ impl From for torii_grpc_client::types::Query { #[derive(Tsify, Serialize, Deserialize, Debug)] #[tsify(into_wasm_abi, from_wasm_abi)] pub enum Clause { + HashedKeys(Vec), Keys(KeysClause), Member(MemberClause), Composite(CompositeClause), } -#[declare] -pub type KeysClauses = Vec; - #[derive(Tsify, Serialize, Deserialize, Debug)] #[tsify(into_wasm_abi, from_wasm_abi)] pub enum PatternMatching { @@ -412,7 +410,7 @@ pub enum PatternMatching { VariableLen = 1, } -impl From for torii_grpc_client::types::PatternMatching { +impl From for torii_proto::PatternMatching { fn from(value: PatternMatching) -> Self { match value { PatternMatching::FixedLen => Self::FixedLen, @@ -421,24 +419,6 @@ impl From for torii_grpc_client::types::PatternMatching { } } -#[derive(Tsify, Serialize, Deserialize, Debug)] -#[tsify(into_wasm_abi, from_wasm_abi)] -pub enum EntityKeysClause { - HashedKeys(Vec), - Keys(KeysClause), -} - -impl From for torii_grpc_client::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()) - } - EntityKeysClause::Keys(keys) => Self::Keys(keys.into()), - } - } -} - #[derive(Tsify, Serialize, Deserialize, Debug)] #[tsify(into_wasm_abi, from_wasm_abi)] pub struct KeysClause { @@ -447,6 +427,9 @@ pub struct KeysClause { pub models: Vec, } +#[declare] +pub type KeysClauses = Vec; + #[derive(Tsify, Serialize, Deserialize, Debug)] #[tsify(into_wasm_abi, from_wasm_abi)] pub enum MemberValue { @@ -455,21 +438,17 @@ pub enum MemberValue { List(Vec), } -impl From for torii_grpc_client::types::MemberValue { +impl From for torii_proto::MemberValue { fn from(value: MemberValue) -> Self { match value { MemberValue::Primitive(primitive) => { - torii_grpc_client::types::MemberValue::Primitive(primitive.into()) - } - MemberValue::String(string) => { - torii_grpc_client::types::MemberValue::String(string.clone()) + torii_proto::MemberValue::Primitive(primitive.into()) } + MemberValue::String(string) => torii_proto::MemberValue::String(string.clone()), MemberValue::List(list) => { - let values = list - .into_iter() - .map(|v| v.into()) - .collect::>(); - torii_grpc_client::types::MemberValue::List(values) + let values = + list.into_iter().map(|v| v.into()).collect::>(); + torii_proto::MemberValue::List(values) } } } @@ -491,7 +470,7 @@ pub struct CompositeClause { pub clauses: Vec, } -impl From for torii_grpc_client::types::KeysClause { +impl From for torii_proto::KeysClause { fn from(value: KeysClause) -> Self { Self { keys: value @@ -505,7 +484,7 @@ impl From for torii_grpc_client::types::KeysClause { } } -impl From for torii_grpc_client::types::MemberClause { +impl From for torii_proto::MemberClause { fn from(value: MemberClause) -> Self { Self { model: value.model.to_string(), @@ -516,7 +495,7 @@ impl From for torii_grpc_client::types::MemberClause { } } -impl From for torii_grpc_client::types::CompositeClause { +impl From for torii_proto::CompositeClause { fn from(value: CompositeClause) -> Self { Self { operator: value.operator.into(), @@ -525,9 +504,12 @@ impl From for torii_grpc_client::types::CompositeClause { } } -impl From for torii_grpc_client::types::Clause { +impl From for torii_proto::Clause { fn from(value: Clause) -> Self { match value { + Clause::HashedKeys(keys) => { + Self::HashedKeys(keys.iter().map(|k| Felt::from_str(k.as_str()).unwrap()).collect()) + } Clause::Keys(keys) => Self::Keys(keys.into()), Clause::Member(member) => Self::Member(member.into()), Clause::Composite(composite) => Self::Composite(composite.into()), @@ -542,7 +524,7 @@ pub enum LogicalOperator { Or, } -impl From for torii_grpc_client::types::LogicalOperator { +impl From for torii_proto::LogicalOperator { fn from(value: LogicalOperator) -> Self { match value { LogicalOperator::And => Self::And, @@ -564,7 +546,7 @@ pub enum ComparisonOperator { NotIn, } -impl From for torii_grpc_client::types::ComparisonOperator { +impl From for torii_proto::ComparisonOperator { fn from(value: ComparisonOperator) -> Self { match value { ComparisonOperator::Eq => Self::Eq, @@ -586,7 +568,7 @@ pub struct Value { pub value_type: ValueType, } -impl From for torii_grpc_client::types::Value { +impl From for torii_proto::Value { fn from(value: Value) -> Self { Self { primitive_type: value.primitive_type.into(), value_type: value.value_type.into() } } @@ -602,7 +584,7 @@ pub enum ValueType { Bytes(Vec), } -impl From for torii_grpc_client::types::ValueType { +impl From for torii_proto::ValueType { fn from(value: ValueType) -> Self { match &value { ValueType::String(s) => Self::String(s.to_string()),