Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/curvy-queens-whisper.md
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +2 to +13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Update version bump type to reflect breaking changes.

All packages are currently marked for patch updates, but changing the return type of parseEntities from Record<string, ParsedEntity<T>> to Array<ParsedEntity<T>> is a breaking change that will affect API consumers. This should be reflected in the version numbers.

Apply this diff to update the version bump type:

-"@dojoengine/sdk": patch
+"@dojoengine/sdk": major

Committable suggestion skipped: line range outside the PR's diff.

---

parseEntities now returns Array<ParsedEntity<T>> instead of Record<string, ParsedEntity<T>>
10 changes: 5 additions & 5 deletions examples/example-vite-react-phaser-recs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
97 changes: 41 additions & 56 deletions packages/sdk/src/__tests__/parseEntities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -281,12 +276,7 @@ describe("parseEntities", () => {
};
const res = parseEntities<SchemaType>(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", () => {
Expand Down Expand Up @@ -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);
});
});
64 changes: 26 additions & 38 deletions packages/sdk/src/getEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,50 +45,38 @@ export async function getEntities<T extends SchemaType>({
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<T>(allEntities);
const parsedEntities = parseEntities<T>(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<T>(allEntities);
}
72 changes: 27 additions & 45 deletions packages/sdk/src/getEventMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,57 +47,39 @@ export async function getEventMessages<T extends SchemaType>({
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<T>(allEntities, options)
: parseEntities<T>(allEntities, options);
const parsedEntities = historical
? parseHistoricalEvents<T>(entities, options)
: parseEntities<T>(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<T>(allEntities, options)
: parseEntities<T>(allEntities, options);
}
4 changes: 2 additions & 2 deletions packages/sdk/src/parseEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function parseEntities<T extends SchemaType>(
options?: { logging?: boolean }
): StandardizedQueryResult<T> {
// @ts-ignore
const result: StandardizedQueryResult<T> = entities;
const result: Record<string, ParsedEntity> = {};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Type declaration mismatch with return type.

The function's return type is StandardizedQueryResult<T> but the implementation returns an array. This should be updated to match the new array-based return type.

-): StandardizedQueryResult<T> {
+): ParsedEntity<T>[] {

Committable suggestion skipped: line range outside the PR's diff.

const entityIds = Object.keys(entities);

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

return result;
return Object.values(result);
}

/**
Expand Down
Loading