Skip to content

Commit 0c5a6ec

Browse files
authored
Merge pull request #382 from dojoengine/feat/experimental-events
fix: parseEntities returns array instead of record<string, parsedenti…
2 parents c842833 + b4dc1e2 commit 0c5a6ec

File tree

6 files changed

+117
-146
lines changed

6 files changed

+117
-146
lines changed

.changeset/curvy-queens-whisper.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@dojoengine/sdk": patch
3+
"template-vite-ts": patch
4+
"@dojoengine/core": patch
5+
"@dojoengine/create-burner": patch
6+
"@dojoengine/create-dojo": patch
7+
"@dojoengine/predeployed-connector": patch
8+
"@dojoengine/react": patch
9+
"@dojoengine/state": patch
10+
"@dojoengine/torii-client": patch
11+
"@dojoengine/torii-wasm": patch
12+
"@dojoengine/utils": patch
13+
"@dojoengine/utils-wasm": patch
14+
---
15+
16+
parseEntities now returns Array<ParsedEntity<T>> instead of Record<string, ParsedEntity<T>>

examples/example-vite-react-phaser-recs/tsconfig.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
// "noUnusedLocals": true,
1818
// "noUnusedParameters": true,
1919
"noFallthroughCasesInSwitch": true,
20-
"allowSyntheticDefaultImports": true
20+
"allowSyntheticDefaultImports": true,
21+
"baseUrl": ".",
22+
"paths": {
23+
"@/*": ["./src/*"]
24+
}
2125
},
2226
"include": ["src"],
23-
"baseUrl": ".",
24-
"paths": {
25-
"@/*": ["./src/*"]
26-
},
2727
"references": [
2828
{
2929
"path": "./tsconfig.node.json"

packages/sdk/src/__tests__/parseEntities.test.ts

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -140,52 +140,50 @@ describe("parseEntities", () => {
140140

141141
const result = parseEntities(mockEntities);
142142

143-
expect(result).toEqual({
144-
"0x14c362c17947ef1d40152d6e3bedd859c98bebfad41f75ef3f26798556a4c85":
145-
{
146-
entityId:
147-
"0x14c362c17947ef1d40152d6e3bedd859c98bebfad41f75ef3f26798556a4c85",
148-
models: {
149-
dojo_starter: {
150-
Position: {
151-
player: "0x7f7e355d3ae20c34de26c21b46612622f4e4012e7debc10f0300cf193a46366",
152-
vec: {
153-
x: 6,
154-
y: 10,
155-
},
156-
},
157-
Moves: {
158-
last_direction: "Left",
159-
remaining: 98,
160-
can_move: true,
161-
player: "0x7f7e355d3ae20c34de26c21b46612622f4e4012e7debc10f0300cf193a46366",
143+
expect(result).toEqual([
144+
{
145+
entityId:
146+
"0x14c362c17947ef1d40152d6e3bedd859c98bebfad41f75ef3f26798556a4c85",
147+
models: {
148+
dojo_starter: {
149+
Position: {
150+
player: "0x7f7e355d3ae20c34de26c21b46612622f4e4012e7debc10f0300cf193a46366",
151+
vec: {
152+
x: 6,
153+
y: 10,
162154
},
163155
},
156+
Moves: {
157+
last_direction: "Left",
158+
remaining: 98,
159+
can_move: true,
160+
player: "0x7f7e355d3ae20c34de26c21b46612622f4e4012e7debc10f0300cf193a46366",
161+
},
164162
},
165163
},
166-
"0x144c128b8ead7d0da39c6a150abbfdd38f572ba9418d3e36929eb6107b4ce4d":
167-
{
168-
entityId:
169-
"0x144c128b8ead7d0da39c6a150abbfdd38f572ba9418d3e36929eb6107b4ce4d",
170-
models: {
171-
dojo_starter: {
172-
Moves: {
173-
last_direction: "Left",
174-
remaining: 99,
175-
can_move: true,
176-
player: "0x70c774f8d061323ada4e4924c12c894f39b5874b71147af254b3efae07e68c0",
177-
},
178-
Position: {
179-
player: "0x70c774f8d061323ada4e4924c12c894f39b5874b71147af254b3efae07e68c0",
180-
vec: {
181-
x: 6,
182-
y: 10,
183-
},
164+
},
165+
{
166+
entityId:
167+
"0x144c128b8ead7d0da39c6a150abbfdd38f572ba9418d3e36929eb6107b4ce4d",
168+
models: {
169+
dojo_starter: {
170+
Moves: {
171+
last_direction: "Left",
172+
remaining: 99,
173+
can_move: true,
174+
player: "0x70c774f8d061323ada4e4924c12c894f39b5874b71147af254b3efae07e68c0",
175+
},
176+
Position: {
177+
player: "0x70c774f8d061323ada4e4924c12c894f39b5874b71147af254b3efae07e68c0",
178+
vec: {
179+
x: 6,
180+
y: 10,
184181
},
185182
},
186183
},
187184
},
188-
});
185+
},
186+
]);
189187
});
190188

191189
it("should parse Options", () => {
@@ -224,12 +222,9 @@ describe("parseEntities", () => {
224222
};
225223
const res = parseEntities(toriiResult);
226224
const expected = new CairoOption(CairoOptionVariant.Some, 1734537235);
227-
// @ts-ignore can be undefined
228-
expect(
229-
res[
230-
"0x43ebbfee0476dcc36cae36dfa9b47935cc20c36cb4dc7d014076e5f875cf164"
231-
].models.onchain_dash.CallerCounter.timestamp
232-
).toEqual(expected);
225+
expect(res[0]?.models?.onchain_dash?.CallerCounter?.timestamp).toEqual(
226+
expected
227+
);
233228
});
234229
it("should parse complex enums", () => {
235230
const toriiResult: torii.Entities = {
@@ -281,12 +276,7 @@ describe("parseEntities", () => {
281276
};
282277
const res = parseEntities<SchemaType>(toriiResult);
283278
const expected = new CairoCustomEnum({ Predefined: "Dojo" });
284-
// @ts-ignore can be undefined
285-
expect(
286-
res[
287-
"0x5248d30cafd7af5e7f9255ed9bef2bd7aa0f191669a4c1e3a03b8c64ea5a9d8"
288-
].models.onchain_dash.Theme.value
289-
).toEqual(expected);
279+
expect(res[0]?.models?.onchain_dash?.Theme?.value).toEqual(expected);
290280
});
291281

292282
it("should parse enum with nested struct", () => {
@@ -343,11 +333,6 @@ describe("parseEntities", () => {
343333
"0x0000000000000000000000000000000000000000637573746f6d5f636c617373",
344334
},
345335
});
346-
// @ts-ignore can be undefined
347-
expect(
348-
res[
349-
"0x5248d30cafd7af5e7f9255ed9bef2bd7aa0f191669a4c1e3a03b8c64ea5a9d8"
350-
].models.onchain_dash.Theme.value
351-
).toEqual(expected);
336+
expect(res[0]?.models?.onchain_dash?.Theme?.value).toEqual(expected);
352337
});
353338
});

packages/sdk/src/getEntities.ts

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,50 +45,38 @@ export async function getEntities<T extends SchemaType>({
4545
const clause = convertQueryToClause(query, schema);
4646

4747
let cursor = offset;
48-
let continueFetching = true;
49-
let allEntities: torii.Entities = {};
5048

51-
while (continueFetching) {
52-
const toriiQuery: torii.Query = {
53-
limit,
54-
offset: cursor,
55-
order_by: orderBy,
56-
entity_models: entityModels,
57-
clause,
58-
dont_include_hashed_keys: dontIncludeHashedKeys,
59-
entity_updated_after: entityUpdatedAfter,
60-
};
49+
const toriiQuery: torii.Query = {
50+
limit,
51+
offset: cursor,
52+
order_by: orderBy,
53+
entity_models: entityModels,
54+
clause,
55+
dont_include_hashed_keys: dontIncludeHashedKeys,
56+
entity_updated_after: entityUpdatedAfter,
57+
};
6158

62-
try {
63-
const entities = await client.getEntities(toriiQuery);
59+
try {
60+
const entities = await client.getEntities(toriiQuery);
6461

65-
if (options?.logging) {
66-
console.log("Clause", clause, "Query", query);
67-
console.log(`Fetched entities at offset ${cursor}:`, entities);
68-
}
69-
70-
Object.assign(allEntities, entities);
62+
if (options?.logging) {
63+
console.log("Clause", clause, "Query", query);
64+
console.log(`Fetched entities at offset ${cursor}:`, entities);
65+
}
7166

72-
const parsedEntities = parseEntities<T>(allEntities);
67+
const parsedEntities = parseEntities<T>(entities);
7368

74-
callback({ data: parsedEntities });
69+
callback({ data: parsedEntities });
7570

76-
if (Object.keys(entities).length < limit) {
77-
continueFetching = false;
78-
} else {
79-
cursor += limit;
80-
}
81-
} catch (error) {
82-
if (options?.logging) {
83-
console.error("Error fetching entities:", error);
84-
}
85-
callback({ error: error as Error });
86-
throw error;
71+
if (options?.logging) {
72+
console.log("All fetched entities:", parsedEntities);
8773
}
74+
return parsedEntities;
75+
} catch (error) {
76+
if (options?.logging) {
77+
console.error("Error fetching entities:", error);
78+
}
79+
callback({ error: error as Error });
80+
throw error;
8881
}
89-
90-
if (options?.logging) {
91-
console.log("All fetched entities:", allEntities);
92-
}
93-
return parseEntities<T>(allEntities);
9482
}

packages/sdk/src/getEventMessages.ts

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,57 +47,39 @@ export async function getEventMessages<T extends SchemaType>({
4747
const clause = convertQueryToClause(query, schema);
4848

4949
let cursor = offset;
50-
let continueFetching = true;
51-
let allEntities: torii.Entities = {};
5250

53-
while (continueFetching) {
54-
const toriiQuery: torii.Query = {
55-
limit,
56-
offset: cursor,
57-
order_by: orderBy,
58-
entity_models: entityModels,
59-
clause,
60-
dont_include_hashed_keys: dontIncludeHashedKeys,
61-
entity_updated_after: entityUpdatedAfter,
62-
};
51+
const toriiQuery: torii.Query = {
52+
limit,
53+
offset: cursor,
54+
order_by: orderBy,
55+
entity_models: entityModels,
56+
clause,
57+
dont_include_hashed_keys: dontIncludeHashedKeys,
58+
entity_updated_after: entityUpdatedAfter,
59+
};
6360

64-
try {
65-
const entities = await client.getEventMessages(
66-
toriiQuery,
67-
historical
68-
);
61+
try {
62+
const entities = await client.getEventMessages(toriiQuery, historical);
6963

70-
if (options?.logging) {
71-
console.log(`Fetched entities at offset ${cursor}:`, entities);
72-
}
73-
74-
Object.assign(allEntities, entities);
64+
if (options?.logging) {
65+
console.log(`Fetched entities at offset ${cursor}:`, entities);
66+
}
7567

76-
const parsedEntities = historical
77-
? parseHistoricalEvents<T>(allEntities, options)
78-
: parseEntities<T>(allEntities, options);
68+
const parsedEntities = historical
69+
? parseHistoricalEvents<T>(entities, options)
70+
: parseEntities<T>(entities, options);
7971

80-
callback({ data: parsedEntities });
72+
callback({ data: parsedEntities });
8173

82-
if (Object.keys(entities).length < limit) {
83-
continueFetching = false;
84-
} else {
85-
cursor += limit;
86-
}
87-
} catch (error) {
88-
if (options?.logging) {
89-
console.error("Error fetching entities:", error);
90-
}
91-
callback({ error: error as Error });
92-
throw error;
74+
if (options?.logging) {
75+
console.log("All fetched entities:", parsedEntities);
9376
}
77+
return parsedEntities;
78+
} catch (error) {
79+
if (options?.logging) {
80+
console.error("Error fetching entities:", error);
81+
}
82+
callback({ error: error as Error });
83+
throw error;
9484
}
95-
96-
if (options?.logging) {
97-
console.log("All fetched entities:", allEntities);
98-
}
99-
100-
return historical
101-
? parseHistoricalEvents<T>(allEntities, options)
102-
: parseEntities<T>(allEntities, options);
10385
}

packages/sdk/src/parseEntities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function parseEntities<T extends SchemaType>(
88
options?: { logging?: boolean }
99
): StandardizedQueryResult<T> {
1010
// @ts-ignore
11-
const result: StandardizedQueryResult<T> = entities;
11+
const result: Record<string, ParsedEntity> = {};
1212
const entityIds = Object.keys(entities);
1313

1414
entityIds.forEach((entityId) => {
@@ -52,7 +52,7 @@ export function parseEntities<T extends SchemaType>(
5252
console.log("Parsed result:", result);
5353
}
5454

55-
return result;
55+
return Object.values(result);
5656
}
5757

5858
/**

0 commit comments

Comments
 (0)