From b4dc1e271dcceb56953f8bc1ee90765ce94e5e6f Mon Sep 17 00:00:00 2001 From: Valentin Dosimont Date: Sat, 25 Jan 2025 10:33:51 +0100 Subject: [PATCH] fix: parseEntities returns array instead of record> --- .changeset/curvy-queens-whisper.md | 16 +++ .../tsconfig.json | 10 +- .../sdk/src/__tests__/parseEntities.test.ts | 97 ++++++++----------- packages/sdk/src/getEntities.ts | 64 +++++------- packages/sdk/src/getEventMessages.ts | 72 ++++++-------- packages/sdk/src/parseEntities.ts | 4 +- 6 files changed, 117 insertions(+), 146 deletions(-) create mode 100644 .changeset/curvy-queens-whisper.md diff --git a/.changeset/curvy-queens-whisper.md b/.changeset/curvy-queens-whisper.md new file mode 100644 index 00000000..7caaf65f --- /dev/null +++ b/.changeset/curvy-queens-whisper.md @@ -0,0 +1,16 @@ +--- +"@dojoengine/sdk": patch +"template-vite-ts": patch +"@dojoengine/core": patch +"@dojoengine/create-burner": patch +"@dojoengine/create-dojo": patch +"@dojoengine/predeployed-connector": patch +"@dojoengine/react": patch +"@dojoengine/state": patch +"@dojoengine/torii-client": patch +"@dojoengine/torii-wasm": patch +"@dojoengine/utils": patch +"@dojoengine/utils-wasm": patch +--- + +parseEntities now returns Array> instead of Record> diff --git a/examples/example-vite-react-phaser-recs/tsconfig.json b/examples/example-vite-react-phaser-recs/tsconfig.json index 03701768..e6e01a68 100644 --- a/examples/example-vite-react-phaser-recs/tsconfig.json +++ b/examples/example-vite-react-phaser-recs/tsconfig.json @@ -17,13 +17,13 @@ // "noUnusedLocals": true, // "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, - "allowSyntheticDefaultImports": true + "allowSyntheticDefaultImports": true, + "baseUrl": ".", + "paths": { + "@/*": ["./src/*"] + } }, "include": ["src"], - "baseUrl": ".", - "paths": { - "@/*": ["./src/*"] - }, "references": [ { "path": "./tsconfig.node.json" diff --git a/packages/sdk/src/__tests__/parseEntities.test.ts b/packages/sdk/src/__tests__/parseEntities.test.ts index 2b003a58..38be6795 100644 --- a/packages/sdk/src/__tests__/parseEntities.test.ts +++ b/packages/sdk/src/__tests__/parseEntities.test.ts @@ -140,52 +140,50 @@ describe("parseEntities", () => { const result = parseEntities(mockEntities); - expect(result).toEqual({ - "0x14c362c17947ef1d40152d6e3bedd859c98bebfad41f75ef3f26798556a4c85": - { - entityId: - "0x14c362c17947ef1d40152d6e3bedd859c98bebfad41f75ef3f26798556a4c85", - models: { - dojo_starter: { - Position: { - player: "0x7f7e355d3ae20c34de26c21b46612622f4e4012e7debc10f0300cf193a46366", - vec: { - x: 6, - y: 10, - }, - }, - Moves: { - last_direction: "Left", - remaining: 98, - can_move: true, - player: "0x7f7e355d3ae20c34de26c21b46612622f4e4012e7debc10f0300cf193a46366", + expect(result).toEqual([ + { + entityId: + "0x14c362c17947ef1d40152d6e3bedd859c98bebfad41f75ef3f26798556a4c85", + models: { + dojo_starter: { + Position: { + player: "0x7f7e355d3ae20c34de26c21b46612622f4e4012e7debc10f0300cf193a46366", + vec: { + x: 6, + y: 10, }, }, + Moves: { + last_direction: "Left", + remaining: 98, + can_move: true, + player: "0x7f7e355d3ae20c34de26c21b46612622f4e4012e7debc10f0300cf193a46366", + }, }, }, - "0x144c128b8ead7d0da39c6a150abbfdd38f572ba9418d3e36929eb6107b4ce4d": - { - entityId: - "0x144c128b8ead7d0da39c6a150abbfdd38f572ba9418d3e36929eb6107b4ce4d", - models: { - dojo_starter: { - Moves: { - last_direction: "Left", - remaining: 99, - can_move: true, - player: "0x70c774f8d061323ada4e4924c12c894f39b5874b71147af254b3efae07e68c0", - }, - Position: { - player: "0x70c774f8d061323ada4e4924c12c894f39b5874b71147af254b3efae07e68c0", - vec: { - x: 6, - y: 10, - }, + }, + { + entityId: + "0x144c128b8ead7d0da39c6a150abbfdd38f572ba9418d3e36929eb6107b4ce4d", + models: { + dojo_starter: { + Moves: { + last_direction: "Left", + remaining: 99, + can_move: true, + player: "0x70c774f8d061323ada4e4924c12c894f39b5874b71147af254b3efae07e68c0", + }, + Position: { + player: "0x70c774f8d061323ada4e4924c12c894f39b5874b71147af254b3efae07e68c0", + vec: { + x: 6, + y: 10, }, }, }, }, - }); + }, + ]); }); it("should parse Options", () => { @@ -224,12 +222,9 @@ describe("parseEntities", () => { }; const res = parseEntities(toriiResult); const expected = new CairoOption(CairoOptionVariant.Some, 1734537235); - // @ts-ignore can be undefined - expect( - res[ - "0x43ebbfee0476dcc36cae36dfa9b47935cc20c36cb4dc7d014076e5f875cf164" - ].models.onchain_dash.CallerCounter.timestamp - ).toEqual(expected); + expect(res[0]?.models?.onchain_dash?.CallerCounter?.timestamp).toEqual( + expected + ); }); it("should parse complex enums", () => { const toriiResult: torii.Entities = { @@ -281,12 +276,7 @@ describe("parseEntities", () => { }; const res = parseEntities(toriiResult); const expected = new CairoCustomEnum({ Predefined: "Dojo" }); - // @ts-ignore can be undefined - expect( - res[ - "0x5248d30cafd7af5e7f9255ed9bef2bd7aa0f191669a4c1e3a03b8c64ea5a9d8" - ].models.onchain_dash.Theme.value - ).toEqual(expected); + expect(res[0]?.models?.onchain_dash?.Theme?.value).toEqual(expected); }); it("should parse enum with nested struct", () => { @@ -343,11 +333,6 @@ describe("parseEntities", () => { "0x0000000000000000000000000000000000000000637573746f6d5f636c617373", }, }); - // @ts-ignore can be undefined - expect( - res[ - "0x5248d30cafd7af5e7f9255ed9bef2bd7aa0f191669a4c1e3a03b8c64ea5a9d8" - ].models.onchain_dash.Theme.value - ).toEqual(expected); + expect(res[0]?.models?.onchain_dash?.Theme?.value).toEqual(expected); }); }); diff --git a/packages/sdk/src/getEntities.ts b/packages/sdk/src/getEntities.ts index e1a92a07..748cabd7 100644 --- a/packages/sdk/src/getEntities.ts +++ b/packages/sdk/src/getEntities.ts @@ -45,50 +45,38 @@ export async function getEntities({ const clause = convertQueryToClause(query, schema); let cursor = offset; - let continueFetching = true; - let allEntities: torii.Entities = {}; - while (continueFetching) { - const toriiQuery: torii.Query = { - limit, - offset: cursor, - order_by: orderBy, - entity_models: entityModels, - clause, - dont_include_hashed_keys: dontIncludeHashedKeys, - entity_updated_after: entityUpdatedAfter, - }; + const toriiQuery: torii.Query = { + limit, + offset: cursor, + order_by: orderBy, + entity_models: entityModels, + clause, + dont_include_hashed_keys: dontIncludeHashedKeys, + entity_updated_after: entityUpdatedAfter, + }; - try { - const entities = await client.getEntities(toriiQuery); + try { + const entities = await client.getEntities(toriiQuery); - if (options?.logging) { - console.log("Clause", clause, "Query", query); - console.log(`Fetched entities at offset ${cursor}:`, entities); - } - - Object.assign(allEntities, entities); + if (options?.logging) { + console.log("Clause", clause, "Query", query); + console.log(`Fetched entities at offset ${cursor}:`, entities); + } - const parsedEntities = parseEntities(allEntities); + const parsedEntities = parseEntities(entities); - callback({ data: parsedEntities }); + callback({ data: parsedEntities }); - if (Object.keys(entities).length < limit) { - continueFetching = false; - } else { - cursor += limit; - } - } catch (error) { - if (options?.logging) { - console.error("Error fetching entities:", error); - } - callback({ error: error as Error }); - throw error; + if (options?.logging) { + console.log("All fetched entities:", parsedEntities); } + return parsedEntities; + } catch (error) { + if (options?.logging) { + console.error("Error fetching entities:", error); + } + callback({ error: error as Error }); + throw error; } - - if (options?.logging) { - console.log("All fetched entities:", allEntities); - } - return parseEntities(allEntities); } diff --git a/packages/sdk/src/getEventMessages.ts b/packages/sdk/src/getEventMessages.ts index 83f1869f..3c716585 100644 --- a/packages/sdk/src/getEventMessages.ts +++ b/packages/sdk/src/getEventMessages.ts @@ -47,57 +47,39 @@ export async function getEventMessages({ const clause = convertQueryToClause(query, schema); let cursor = offset; - let continueFetching = true; - let allEntities: torii.Entities = {}; - while (continueFetching) { - const toriiQuery: torii.Query = { - limit, - offset: cursor, - order_by: orderBy, - entity_models: entityModels, - clause, - dont_include_hashed_keys: dontIncludeHashedKeys, - entity_updated_after: entityUpdatedAfter, - }; + const toriiQuery: torii.Query = { + limit, + offset: cursor, + order_by: orderBy, + entity_models: entityModels, + clause, + dont_include_hashed_keys: dontIncludeHashedKeys, + entity_updated_after: entityUpdatedAfter, + }; - try { - const entities = await client.getEventMessages( - toriiQuery, - historical - ); + try { + const entities = await client.getEventMessages(toriiQuery, historical); - if (options?.logging) { - console.log(`Fetched entities at offset ${cursor}:`, entities); - } - - Object.assign(allEntities, entities); + if (options?.logging) { + console.log(`Fetched entities at offset ${cursor}:`, entities); + } - const parsedEntities = historical - ? parseHistoricalEvents(allEntities, options) - : parseEntities(allEntities, options); + const parsedEntities = historical + ? parseHistoricalEvents(entities, options) + : parseEntities(entities, options); - callback({ data: parsedEntities }); + callback({ data: parsedEntities }); - if (Object.keys(entities).length < limit) { - continueFetching = false; - } else { - cursor += limit; - } - } catch (error) { - if (options?.logging) { - console.error("Error fetching entities:", error); - } - callback({ error: error as Error }); - throw error; + if (options?.logging) { + console.log("All fetched entities:", parsedEntities); } + return parsedEntities; + } catch (error) { + if (options?.logging) { + console.error("Error fetching entities:", error); + } + callback({ error: error as Error }); + throw error; } - - if (options?.logging) { - console.log("All fetched entities:", allEntities); - } - - return historical - ? parseHistoricalEvents(allEntities, options) - : parseEntities(allEntities, options); } diff --git a/packages/sdk/src/parseEntities.ts b/packages/sdk/src/parseEntities.ts index 9ae4e745..8e9a4ba4 100644 --- a/packages/sdk/src/parseEntities.ts +++ b/packages/sdk/src/parseEntities.ts @@ -8,7 +8,7 @@ export function parseEntities( options?: { logging?: boolean } ): StandardizedQueryResult { // @ts-ignore - const result: StandardizedQueryResult = entities; + const result: Record = {}; const entityIds = Object.keys(entities); entityIds.forEach((entityId) => { @@ -52,7 +52,7 @@ export function parseEntities( console.log("Parsed result:", result); } - return result; + return Object.values(result); } /**