Skip to content

Commit

Permalink
Merge pull request #64 from dojoengine/rc0
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo authored Nov 1, 2024
2 parents 94670fc + 6dbd658 commit 6b98fa2
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 2,633 deletions.
2,902 changes: 308 additions & 2,594 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 10 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
[package]
edition = "2021"
name = "dojo-c"
version = "1.0.0-alpha.18"
version = "1.0.0-rc.0"

[lib]
crate-type = ["cdylib", "rlib", "staticlib"]

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
dojo-world = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.18", features = [
dojo-world = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-rc.0", features = [
"metadata",
] }

[dependencies]
dojo-types = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.18" }
torii-client = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.18" }
dojo-types = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-rc.0" }
torii-client = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-rc.0" }
torii-grpc = { git = "https://github.com/dojoengine/dojo", features = [
"client",
], tag = "v1.0.0-alpha.18" }
torii-relay = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.18" }
], tag = "v1.0.0-rc.0" }
torii-relay = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-rc.0" }

starknet = "0.11.0"
starknet = "0.12.0"
starknet-crypto = "0.7.2"
starknet-types-core = { version = "0.1.6", features = [ "arbitrary" ] }

parking_lot = "0.12.1"
tokio = { version = "1.39.2", default-features = false, features = ["rt"] }
Expand All @@ -37,7 +38,7 @@ cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.3.2" }

# WASM
[target.'cfg(target_arch = "wasm32")'.dependencies]
dojo-world = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.18", features = [
dojo-world = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-rc.0", features = [
] }
serde-wasm-bindgen = "0.6.3"
wasm-bindgen-futures = "0.4.39"
Expand All @@ -55,8 +56,4 @@ instant = { version = "0.1.13", features = ["wasm-bindgen"] }
gloo-timers = { version = "0.3.0", features = ["futures"] }

[build-dependencies]
cbindgen = "0.26.0"

[patch.crates-io]
# Matching the same rev that `cainome` is using. Mainly because `starknet-rs` hasn't create a new release yet.
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "2ddc694" }
cbindgen = "0.26.0"
7 changes: 5 additions & 2 deletions dojo.h
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,8 @@ struct ResultCArrayu8 client_publish_message(struct ToriiClient *client,
struct ResultCArrayEntity client_entities(struct ToriiClient *client, const struct Query *query);

struct ResultCArrayEntity client_event_messages(struct ToriiClient *client,
const struct Query *query);
const struct Query *query,
bool historical);

struct WorldMetadata client_metadata(struct ToriiClient *client);

Expand All @@ -650,13 +651,15 @@ struct Resultbool client_update_entity_subscription(struct ToriiClient *client,
struct ResultSubscription client_on_event_message_update(struct ToriiClient *client,
const struct EntityKeysClause *clauses,
uintptr_t clauses_len,
bool historical,
void (*callback)(struct FieldElement,
struct CArrayStruct));

struct Resultbool client_update_event_message_subscription(struct ToriiClient *client,
struct Subscription *subscription,
const struct EntityKeysClause *clauses,
uintptr_t clauses_len);
uintptr_t clauses_len,
bool historical);

struct ResultSubscription client_on_starknet_event(struct ToriiClient *client,
const struct EntityKeysClause *clauses,
Expand Down
8 changes: 6 additions & 2 deletions dojo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,9 @@ Result<CArray<uint8_t>> client_publish_message(ToriiClient *client,

Result<CArray<Entity>> client_entities(ToriiClient *client, const Query *query);

Result<CArray<Entity>> client_event_messages(ToriiClient *client, const Query *query);
Result<CArray<Entity>> client_event_messages(ToriiClient *client,
const Query *query,
bool historical);

WorldMetadata client_metadata(ToriiClient *client);

Expand All @@ -907,12 +909,14 @@ Result<bool> client_update_entity_subscription(ToriiClient *client,
Result<Subscription*> client_on_event_message_update(ToriiClient *client,
const EntityKeysClause *clauses,
uintptr_t clauses_len,
bool historical,
void (*callback)(FieldElement, CArray<Struct>));

Result<bool> client_update_event_message_subscription(ToriiClient *client,
Subscription *subscription,
const EntityKeysClause *clauses,
uintptr_t clauses_len);
uintptr_t clauses_len,
bool historical);

Result<Subscription*> client_on_starknet_event(ToriiClient *client,
const EntityKeysClause *clauses,
Expand Down
8 changes: 6 additions & 2 deletions dojo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ cdef extern from *:

ResultCArrayEntity client_entities(ToriiClient *client, const Query *query);

ResultCArrayEntity client_event_messages(ToriiClient *client, const Query *query);
ResultCArrayEntity client_event_messages(ToriiClient *client,
const Query *query,
bool historical);

WorldMetadata client_metadata(ToriiClient *client);

Expand All @@ -414,12 +416,14 @@ cdef extern from *:
ResultSubscription client_on_event_message_update(ToriiClient *client,
const EntityKeysClause *clauses,
uintptr_t clauses_len,
bool historical,
void (*callback)(FieldElement, CArrayStruct));

Resultbool client_update_event_message_subscription(ToriiClient *client,
Subscription *subscription,
const EntityKeysClause *clauses,
uintptr_t clauses_len);
uintptr_t clauses_len,
bool historical);

ResultSubscription client_on_starknet_event(ToriiClient *client,
const EntityKeysClause *clauses,
Expand Down
18 changes: 11 additions & 7 deletions src/c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ pub unsafe extern "C" fn client_entities(
pub unsafe extern "C" fn client_event_messages(
client: *mut ToriiClient,
query: &Query,
historical: bool,
) -> Result<CArray<Entity>> {
let event_messages_future = unsafe { (*client).inner.event_messages(query.into()) };
let event_messages_future = unsafe { (*client).inner.event_messages(query.into(), historical) };

match (*client).runtime.block_on(event_messages_future) {
Ok(event_messages) => {
Expand Down Expand Up @@ -223,6 +224,7 @@ pub unsafe extern "C" fn client_on_event_message_update(
client: *mut ToriiClient,
clauses: *const EntityKeysClause,
clauses_len: usize,
historical: bool,
callback: unsafe extern "C" fn(types::FieldElement, CArray<Struct>),
) -> Result<*mut Subscription> {
let client = Arc::from_raw(client);
Expand All @@ -242,7 +244,8 @@ 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(clauses.clone(), historical).await;
if let Ok(rcv) = rcv {
backoff = Duration::from_secs(1); // Reset backoff on successful connection

Expand Down Expand Up @@ -277,15 +280,16 @@ pub unsafe extern "C" fn client_update_event_message_subscription(
subscription: *mut Subscription,
clauses: *const EntityKeysClause,
clauses_len: usize,
historical: bool,
) -> Result<bool> {
let clauses = unsafe { std::slice::from_raw_parts(clauses, clauses_len) };
let clauses = clauses.iter().map(|c| c.into()).collect::<Vec<_>>();

match (*client).runtime.block_on(
(*client)
.inner
.update_event_message_subscription((*subscription).id.load(Ordering::SeqCst), clauses),
) {
match (*client).runtime.block_on((*client).inner.update_event_message_subscription(
(*subscription).id.load(Ordering::SeqCst),
clauses,
historical,
)) {
Ok(_) => Result::Ok(true),
Err(e) => Result::Err(e.into()),
}
Expand Down
20 changes: 16 additions & 4 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,15 @@ impl ToriiClient {
}

#[wasm_bindgen(js_name = getEventMessages)]
pub async fn get_event_messages(&self, query: Query) -> Result<Entities, JsValue> {
pub async fn get_event_messages(
&self,
query: Query,
historical: bool,
) -> Result<Entities, JsValue> {
#[cfg(feature = "console-error-panic")]
console_error_panic_hook::set_once();

let results = self.inner.event_messages((&query).into()).await;
let results = self.inner.event_messages((&query).into(), historical).await;

match results {
Ok(event_messages) => Ok((&event_messages).into()),
Expand Down Expand Up @@ -535,6 +539,7 @@ impl ToriiClient {
pub async fn on_event_message_updated(
&self,
clauses: KeysClauses,
historical: bool,
callback: js_sys::Function,
) -> Result<Subscription, JsValue> {
#[cfg(feature = "console-error-panic")]
Expand All @@ -554,7 +559,9 @@ 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(clauses.clone(), historical).await
{
backoff = 1000; // Reset backoff on successful connection

let mut stream = stream.take_until_if(tripwire.clone());
Expand Down Expand Up @@ -589,10 +596,15 @@ impl ToriiClient {
&self,
subscription: &Subscription,
clauses: KeysClauses,
historical: bool,
) -> Result<(), JsValue> {
let clauses = clauses.iter().map(|c| c.into()).collect();
self.inner
.update_event_message_subscription(subscription.id.load(Ordering::SeqCst), clauses)
.update_event_message_subscription(
subscription.id.load(Ordering::SeqCst),
clauses,
historical,
)
.await
.map_err(|err| JsValue::from(format!("failed to update subscription: {err}")))
}
Expand Down
15 changes: 14 additions & 1 deletion src/wasm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,20 @@ pub struct Entity(pub HashMap<String, Model>);

impl From<&torii_grpc::types::schema::Entity> for Entity {
fn from(value: &torii_grpc::types::schema::Entity) -> Self {
Self(value.models.iter().map(|m| (m.name.clone(), m.into())).collect())
let mut seen_models = HashMap::new();
Self(
value
.models
.iter()
.map(|m| {
let count = seen_models.entry(m.name.clone()).or_insert(0);
let name =
if *count == 0 { m.name.clone() } else { format!("{}-{}", m.name, count) };
*count += 1;
(name, m.into())
})
.collect(),
)
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/wasm/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ fn primitive_value_json(primitive: Primitive) -> JsValue {
Primitive::U16(Some(value)) => JsValue::from_f64(value.into()),
Primitive::U32(Some(value)) => JsValue::from_f64(value.into()),
Primitive::USize(Some(value)) => JsValue::from_f64(value.into()),
Primitive::I64(Some(value)) => JsValue::from_str(&format!("{value:#x}")),
Primitive::U64(Some(value)) => JsValue::from_str(&format!("{value:#x}")),
Primitive::I128(Some(value)) => JsValue::from_str(&format!("{value:#x}")),
Primitive::U128(Some(value)) => JsValue::from_str(&format!("{value:#x}")),
Primitive::U256(Some(value)) => JsValue::from_str(&format!("{value:#x}")),
Primitive::Felt252(Some(value)) => JsValue::from_str(&format!("{value:#x}")),
Primitive::ClassHash(Some(value)) => JsValue::from_str(&format!("{value:#x}")),
Primitive::ContractAddress(Some(value)) => JsValue::from_str(&format!("{value:#x}")),
Primitive::I64(Some(value)) => JsValue::from_str(&format!("0x{value:064x}")),
Primitive::U64(Some(value)) => JsValue::from_str(&format!("0x{value:064x}")),
Primitive::I128(Some(value)) => JsValue::from_str(&format!("0x{value:064x}")),
Primitive::U128(Some(value)) => JsValue::from_str(&format!("0x{value:064x}")),
Primitive::U256(Some(value)) => JsValue::from_str(&format!("0x{value:064x}")),
Primitive::Felt252(Some(value)) => JsValue::from_str(&format!("0x{value:064x}")),
Primitive::ClassHash(Some(value)) => JsValue::from_str(&format!("0x{value:064x}")),
Primitive::ContractAddress(Some(value)) => JsValue::from_str(&format!("0x{value:064x}")),
_ => JsValue::NULL,
}
}
Expand Down

0 comments on commit 6b98fa2

Please sign in to comment.