diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 979b8cb1..888f4540 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,7 +20,7 @@ jobs: run: git submodule update --init --recursive - run: curl -L https://install.dojoengine.org | bash - - run: /home/runner/.config/.dojo/bin/dojoup -v v1.0.0-alpha.3 + - run: /home/runner/.config/.dojo/bin/dojoup -v v1.0.0-alpha.5 - run: | cd examples/dojo-starter /home/runner/.config/.dojo/bin/sozo build diff --git a/examples/dojo-starter b/examples/dojo-starter index 7c01064e..beaf9a95 160000 --- a/examples/dojo-starter +++ b/examples/dojo-starter @@ -1 +1 @@ -Subproject commit 7c01064e352a5798848090a150a3384409d336ba +Subproject commit beaf9a95206dc9da3868c185cb3274a7df013547 diff --git a/examples/react/react-app/src/App.tsx b/examples/react/react-app/src/App.tsx index fd523d5a..da43171f 100644 --- a/examples/react/react-app/src/App.tsx +++ b/examples/react/react-app/src/App.tsx @@ -17,19 +17,7 @@ function App() { account, } = useDojo(); - useQuerySync(toriiClient, contractComponents as any, [ - { - Keys: { - keys: [BigInt(account?.account.address).toString()], - models: [ - "dojo_starter-Position", - "dojo_starter-Moves", - "dojo_starter-DirectionsAvailable", - ], - pattern_matching: "FixedLen", - }, - }, - ]); + useQuerySync(toriiClient, contractComponents as any, []); const [clipboardStatus, setClipboardStatus] = useState({ message: "", diff --git a/examples/react/react-app/src/dojo/createSystemCalls.ts b/examples/react/react-app/src/dojo/createSystemCalls.ts index 71825390..4240ba17 100644 --- a/examples/react/react-app/src/dojo/createSystemCalls.ts +++ b/examples/react/react-app/src/dojo/createSystemCalls.ts @@ -82,28 +82,28 @@ export function createSystemCalls( ]) as Entity; // Update the state before the transaction - const positionId = uuid(); - Position.addOverride(positionId, { - entity: entityId, - value: { - player: BigInt(entityId), - vec: updatePositionWithDirection( - direction, - getComponentValue(Position, entityId) as any - ).vec, - }, - }); + // const positionId = uuid(); + // Position.addOverride(positionId, { + // entity: entityId, + // value: { + // player: BigInt(entityId), + // vec: updatePositionWithDirection( + // direction, + // getComponentValue(Position, entityId) as any + // ).vec, + // }, + // }); - // Update the state before the transaction - const movesId = uuid(); - Moves.addOverride(movesId, { - entity: entityId, - value: { - player: BigInt(entityId), - remaining: - (getComponentValue(Moves, entityId)?.remaining || 0) - 1, - }, - }); + // // Update the state before the transaction + // const movesId = uuid(); + // Moves.addOverride(movesId, { + // entity: entityId, + // value: { + // player: BigInt(entityId), + // remaining: + // (getComponentValue(Moves, entityId)?.remaining || 0) - 1, + // }, + // }); try { await client.actions.move({ @@ -127,11 +127,11 @@ export function createSystemCalls( }); } catch (e) { console.log(e); - Position.removeOverride(positionId); - Moves.removeOverride(movesId); + // Position.removeOverride(positionId); + // Moves.removeOverride(movesId); } finally { - Position.removeOverride(positionId); - Moves.removeOverride(movesId); + // Position.removeOverride(positionId); + // Moves.removeOverride(movesId); } }; diff --git a/examples/react/react-app/src/dojo/generated/setup.ts b/examples/react/react-app/src/dojo/generated/setup.ts index f4c1f093..98c64040 100644 --- a/examples/react/react-app/src/dojo/generated/setup.ts +++ b/examples/react/react-app/src/dojo/generated/setup.ts @@ -72,6 +72,5 @@ export async function setup({ ...config }: DojoConfig) { dojoProvider, burnerManager, toriiClient, - // sync, }; } diff --git a/packages/react/src/useQuerySync.ts b/packages/react/src/useQuerySync.ts index 3eca4fcd..19f79faf 100644 --- a/packages/react/src/useQuerySync.ts +++ b/packages/react/src/useQuerySync.ts @@ -1,7 +1,7 @@ import { Component, Metadata, Schema } from "@dojoengine/recs"; import { useCallback, useEffect } from "react"; import { - Client, + ToriiClient, EntityKeysClause, Subscription, } from "@dojoengine/torii-client"; @@ -28,7 +28,7 @@ import { getSyncEntities } from "@dojoengine/state"; * ]); */ export function useQuerySync( - toriiClient: Client, + toriiClient: ToriiClient, components: Component[], entityKeyClause: EntityKeysClause[] ) { diff --git a/packages/state/src/recs/index.ts b/packages/state/src/recs/index.ts index 1c6ebce8..eefbd2e8 100644 --- a/packages/state/src/recs/index.ts +++ b/packages/state/src/recs/index.ts @@ -8,7 +8,7 @@ import { } from "@dojoengine/recs"; import { Clause, - Client, + ToriiClient, EntityKeysClause, PatternMatching, } from "@dojoengine/torii-client"; @@ -40,7 +40,7 @@ import { convertValues } from "../utils"; * the batch size of each request. */ export const getSyncEntities = async ( - client: Client, + client: ToriiClient, components: Component[], entityKeyClause: EntityKeysClause[], limit: number = 100 @@ -62,7 +62,7 @@ export const getSyncEntities = async ( * This function performs paginated queries to fetch all entities and their components. */ export const getEntities = async ( - client: Client, + client: ToriiClient, components: Component[], limit: number = 100 ) => { @@ -109,7 +109,7 @@ export const getEntities = async ( * to control the batch size of each request. */ export const getEntitiesQuery = async ( - client: Client, + client: ToriiClient, components: Component[], entityKeyClause: EntityKeysClause, patternMatching: PatternMatching = "FixedLen", @@ -137,7 +137,7 @@ export const getEntitiesQuery = async ( const fetchedEntities = await client.getEntities({ limit, offset: cursor, - clause, + clause: clause || undefined, }); setEntities(fetchedEntities, components); @@ -163,14 +163,14 @@ export const getEntitiesQuery = async ( * sync.cancel(); // cancel the subscription */ export const syncEntities = async ( - client: Client, + client: ToriiClient, components: Component[], entityKeyClause: EntityKeysClause[] ) => { return await client.onEntityUpdated( entityKeyClause, - (fetchedEntities: any) => { - setEntities(fetchedEntities, components); + (fetchedEntities: any, data: any) => { + setEntities({ [fetchedEntities]: data }, components); } ); }; diff --git a/packages/state/src/utils/index.ts b/packages/state/src/utils/index.ts index 476c3f0d..1cbcddaf 100644 --- a/packages/state/src/utils/index.ts +++ b/packages/state/src/utils/index.ts @@ -1,83 +1,88 @@ -import { Type as RecsType, Schema, ComponentValue } from "@dojoengine/recs"; +import { Type as RecsType, Schema } from "@dojoengine/recs"; -export function convertValues(schema: Schema, values: any): ComponentValue { - return Object.keys(schema).reduce( - (acc: ComponentValue, key: string) => { - if (!acc) { - acc = {}; // Ensure acc is initialized - } - const schemaType = schema[key]; - const value = values[key]; +export function convertValues(schema: Schema, values: any) { + return Object.keys(schema).reduce((acc, key) => { + if (!acc) { + acc = {}; + } + const schemaType = schema[key]; + const value = values[key]; - if (value === null || value === undefined) { - acc[key] = value; - return acc; - } + if (value === null || value === undefined) { + acc[key] = value; + return acc; + } - if (value.type === "enum") { - acc[key] = value.value.option; - return acc; - } + if (value.type === "enum") { + acc[key] = value.value.option; + return acc; + } - switch (schemaType) { - case RecsType.StringArray: - if ( - value.type === "array" && - value.value[0].type === "enum" - ) { - acc[key] = value.value.map( - (item: any) => item.value.option - ); - } else { - acc[key] = value.value.map((a: any) => { - try { - return BigInt(a.value); - } catch (error) { - console.warn( - `Failed to convert ${a.value} to BigInt. Using string value instead.` - ); - return a.value; - } - }); - } - break; + switch (schemaType) { + case RecsType.StringArray: + if (value.type === "array" && value.value[0].type === "enum") { + acc[key] = value.value.map( + (item: any) => item.value.option + ); + } else { + acc[key] = value.value.map((a: any) => { + try { + return BigInt(a.value); + } catch (error) { + console.warn( + `Failed to convert ${a.value} to BigInt. Using string value instead.` + ); + return a.value; + } + }); + } + break; - case RecsType.String: - acc[key] = value.value; - break; + case RecsType.String: + acc[key] = value.value; + break; - case RecsType.BigInt: - try { - acc[key] = BigInt(value.value); - } catch (error) { - console.warn( - `Failed to convert ${value.value} to BigInt. Using string value instead.` - ); + case RecsType.BigInt: + try { + acc[key] = BigInt(value.value); + } catch (error) { + console.warn( + `Failed to convert ${value.value} to BigInt. Using string value instead.` + ); - acc[key] = BigInt(`0x${value.value}`); - } - break; + acc[key] = BigInt(`0x${value.value}`); + } + break; - case RecsType.Boolean: - acc[key] = value.value; - break; + case RecsType.Boolean: + acc[key] = value.value; + break; - case RecsType.Number: - acc[key] = Number(value.value); - break; + case RecsType.Number: + acc[key] = Number(value.value); + break; - default: - if ( - typeof schemaType === "object" && - typeof value === "object" - ) { + default: + if (typeof schemaType === "object" && value.type === "struct") { + if (value.value instanceof Map) { + const structValues = Object.fromEntries(value.value); + acc[key] = convertValues(schemaType, structValues); + } else { acc[key] = convertValues(schemaType, value.value); } - break; - } + } else if ( + Array.isArray(schemaType) && + value.type === "array" + ) { + acc[key] = value.value.map((item: any) => + convertValues(schemaType[0], item) + ); + } else { + acc[key] = value.value; + } + break; + } - return acc; - }, - {} - ); + return acc; + }, {}); }