Skip to content

Commit 84d936d

Browse files
committed
fix: utils - change way to compute selector hash
1 parent 988730c commit 84d936d

File tree

7 files changed

+16089
-17007
lines changed

7 files changed

+16089
-17007
lines changed

packages/state/src/recs/index.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const getEntities = async <S extends Schema>(
7171

7272
while (continueFetching) {
7373
const entities = await client.getAllEntities(limit, cursor);
74+
console.log(entities, components);
7475

7576
setEntities(entities, components);
7677

@@ -184,26 +185,34 @@ export const setEntities = async <S extends Schema>(
184185
entities: any,
185186
components: Component<S, Metadata, undefined>[]
186187
) => {
187-
console.log(entities, components);
188188
for (let key in entities) {
189-
if (entities.hasOwnProperty(key)) {
190-
for (let componentName in entities[key]) {
191-
if (entities[key].hasOwnProperty(componentName)) {
192-
let recsComponent = Object.values(components).find(
193-
(component) =>
194-
component.metadata?.name === componentName
195-
);
189+
if (!entities.hasOwnProperty(key)) {
190+
continue;
191+
}
192+
193+
for (let componentName in entities[key]) {
194+
if (!entities[key].hasOwnProperty(componentName)) {
195+
continue;
196+
}
197+
let recsComponent = Object.values(components).find(
198+
(component) => component.metadata?.name === componentName
199+
);
196200

197-
if (recsComponent) {
198-
setComponent(
199-
recsComponent,
200-
key as Entity,
201-
convertValues(
202-
recsComponent.schema,
203-
entities[key][componentName]
204-
) as ComponentValue
205-
);
206-
}
201+
if (recsComponent) {
202+
try {
203+
setComponent(
204+
recsComponent,
205+
key as Entity,
206+
convertValues(
207+
recsComponent.schema,
208+
entities[key][componentName]
209+
) as ComponentValue
210+
);
211+
} catch (error) {
212+
console.warn(
213+
`Failed to set component ${recsComponent.metadata?.name} on ${key}`,
214+
error
215+
);
207216
}
208217
}
209218
}

packages/state/src/utils/index.ts

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,83 @@
1-
import { Type as RecsType, Schema } from "@dojoengine/recs";
1+
import { Type as RecsType, Schema, ComponentValue } from "@dojoengine/recs";
22

3-
export function convertValues(schema: Schema, values: any) {
4-
return Object.keys(schema).reduce<any>((acc, key) => {
5-
if (!acc) {
6-
acc = {}; // Ensure acc is initialized
7-
}
8-
const schemaType = schema[key];
9-
const value = values[key];
3+
export function convertValues(schema: Schema, values: any): ComponentValue {
4+
return Object.keys(schema).reduce<any>(
5+
(acc: ComponentValue, key: string) => {
6+
if (!acc) {
7+
acc = {}; // Ensure acc is initialized
8+
}
9+
const schemaType = schema[key];
10+
const value = values[key];
1011

11-
if (value === null || value === undefined) {
12-
acc[key] = value;
13-
return acc;
14-
}
12+
if (value === null || value === undefined) {
13+
acc[key] = value;
14+
return acc;
15+
}
1516

16-
if (value.type === "enum") {
17-
acc[key] = value.value.option;
18-
return acc;
19-
}
17+
if (value.type === "enum") {
18+
acc[key] = value.value.option;
19+
return acc;
20+
}
2021

21-
switch (schemaType) {
22-
case RecsType.StringArray:
23-
if (value.type === "array" && value.value[0].type === "enum") {
24-
acc[key] = value.value.map(
25-
(item: any) => item.value.option
26-
);
27-
} else {
28-
acc[key] = value.value.map((a: any) => {
29-
try {
30-
return BigInt(a.value);
31-
} catch (error) {
32-
console.warn(
33-
`Failed to convert ${a.value} to BigInt. Using string value instead.`
34-
);
35-
return a.value;
36-
}
37-
});
38-
}
39-
break;
22+
switch (schemaType) {
23+
case RecsType.StringArray:
24+
if (
25+
value.type === "array" &&
26+
value.value[0].type === "enum"
27+
) {
28+
acc[key] = value.value.map(
29+
(item: any) => item.value.option
30+
);
31+
} else {
32+
acc[key] = value.value.map((a: any) => {
33+
try {
34+
return BigInt(a.value);
35+
} catch (error) {
36+
console.warn(
37+
`Failed to convert ${a.value} to BigInt. Using string value instead.`
38+
);
39+
return a.value;
40+
}
41+
});
42+
}
43+
break;
4044

41-
case RecsType.String:
42-
acc[key] = value.value;
43-
break;
45+
case RecsType.String:
46+
acc[key] = value.value;
47+
break;
4448

45-
case RecsType.BigInt:
46-
try {
47-
acc[key] = BigInt(value.value);
48-
} catch (error) {
49-
console.warn(
50-
`Failed to convert ${value.value} to BigInt. Using string value instead.`
51-
);
49+
case RecsType.BigInt:
50+
try {
51+
acc[key] = BigInt(value.value);
52+
} catch (error) {
53+
console.warn(
54+
`Failed to convert ${value.value} to BigInt. Using string value instead.`
55+
);
5256

53-
acc[key] = BigInt(`0x${value.value}`);
54-
}
55-
break;
57+
acc[key] = BigInt(`0x${value.value}`);
58+
}
59+
break;
5660

57-
case RecsType.Boolean:
58-
acc[key] = value.value;
59-
break;
61+
case RecsType.Boolean:
62+
acc[key] = value.value;
63+
break;
6064

61-
case RecsType.Number:
62-
acc[key] = Number(value.value);
63-
break;
65+
case RecsType.Number:
66+
acc[key] = Number(value.value);
67+
break;
6468

65-
default:
66-
if (
67-
typeof schemaType === "object" &&
68-
typeof value === "object"
69-
) {
70-
acc[key] = convertValues(schemaType, value.value);
71-
}
72-
break;
73-
}
69+
default:
70+
if (
71+
typeof schemaType === "object" &&
72+
typeof value === "object"
73+
) {
74+
acc[key] = convertValues(schemaType, value.value);
75+
}
76+
break;
77+
}
7478

75-
return acc;
76-
}, {});
79+
return acc;
80+
},
81+
{}
82+
);
7783
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"result": [
3+
{
4+
"scriptId": "576",
5+
"url": "file:///Users/valentindosimont/www/dojo.js/packages/utils/src/_test_/utils/index.test.ts",
6+
"functions": [
7+
{
8+
"functionName": "",
9+
"ranges": [
10+
{ "startOffset": 0, "endOffset": 1213, "count": 1 }
11+
],
12+
"isBlockCoverage": true
13+
},
14+
{
15+
"functionName": "",
16+
"ranges": [
17+
{ "startOffset": 13, "endOffset": 1213, "count": 1 }
18+
],
19+
"isBlockCoverage": true
20+
},
21+
{
22+
"functionName": "",
23+
"ranges": [
24+
{ "startOffset": 339, "endOffset": 467, "count": 1 }
25+
],
26+
"isBlockCoverage": true
27+
},
28+
{
29+
"functionName": "",
30+
"ranges": [
31+
{ "startOffset": 400, "endOffset": 463, "count": 1 }
32+
],
33+
"isBlockCoverage": true
34+
}
35+
]
36+
}
37+
]
38+
}

packages/utils/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"type": "module",
99
"scripts": {
1010
"build": "tsup --dts-resolve",
11-
"test": "jest"
11+
"test": "vitest run",
12+
"coverage": "vitest run --coverage"
1213
},
1314
"exports": {
1415
".": {
@@ -19,7 +20,9 @@
1920
"devDependencies": {
2021
"@types/elliptic": "^6.4.14",
2122
"tsup": "^8.0.1",
22-
"typescript": "^5.0.3"
23+
"typescript": "^5.0.3",
24+
"@vitest/coverage-v8": "^1.3.0",
25+
"vitest": "^1.1.0"
2326
},
2427
"peerDependencies": {
2528
"starknet": "6.11.0"
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { describe, it, expect } from "vitest";
2+
import {
3+
computeByteArrayHash,
4+
getSelectorFromTag,
5+
getComponentNameFromEvent,
6+
splitEventTag,
7+
} from "../../utils/index";
8+
import { byteArray } from "starknet";
9+
10+
describe("utils", () => {
11+
it("should getComponentFromEvent", () => {
12+
const f = (actions: string[], event: string[], action: string) => {
13+
expect(getComponentNameFromEvent(actions, event)).toBe(action);
14+
};
15+
16+
// Working tests
17+
f(
18+
[
19+
"dojo_starter-Moves",
20+
"dojo_starter-Moved",
21+
"dojo_starter-Position",
22+
],
23+
[
24+
"0x2a29373f1af8348bd366a990eb3a342ef2cbe5e85160539eaca3441a673f468",
25+
"0x1",
26+
"0x434962bc189d79bb549fa55f492780831b0852acf26c6d345f47f520d518589",
27+
"0x3",
28+
"0x64",
29+
"0x0",
30+
"0x1",
31+
],
32+
"dojo_starter-Moves"
33+
);
34+
f(
35+
[
36+
"dojo_starter-Moves",
37+
"dojo_starter-Moved",
38+
"dojo_starter-Position",
39+
],
40+
[
41+
"0x2ac8b4c190f7031b9fc44312e6b047a1dce0b3f2957c33a935ca7846a46dd5b",
42+
"0x1",
43+
"0x434962bc189d79bb549fa55f492780831b0852acf26c6d345f47f520d518589",
44+
"0x3",
45+
"0x64",
46+
"0x0",
47+
"0x1",
48+
],
49+
"dojo_starter-Position"
50+
);
51+
});
52+
53+
it("should throw an exception when the action is not found", () => {
54+
const f = (actions: string[], event: string[]) => {
55+
expect(() =>
56+
getComponentNameFromEvent(actions, event)
57+
).toThrowError();
58+
};
59+
// Should throw an exception
60+
f(
61+
["dojo_starter-Invalid"],
62+
[
63+
"0x2ac8b4c190f7031b9fc44312e6b047a1dce0b3f2957c33a935ca7846a46dd5b",
64+
"0x1",
65+
"0x434962bc189d79bb549fa55f492780831b0852acf26c6d345f47f520d518589",
66+
"0x3",
67+
"0x64",
68+
"0x0",
69+
"0x1",
70+
]
71+
);
72+
});
73+
it("should splitEventTag", () => {
74+
const f = (tag: string, expected: string[]) => {
75+
expect(splitEventTag(tag)).toEqual(expected);
76+
};
77+
78+
f("dojo_starter-Moves", ["dojo_starter", "Moves"]);
79+
f("dojo_starter-Moved", ["dojo_starter", "Moved"]);
80+
f("dojo_starter-Position", ["dojo_starter", "Position"]);
81+
});
82+
83+
it("should computeByteArrayHash", () => {
84+
const t = computeByteArrayHash("test");
85+
expect("0x" + t.toString(16)).toBe(
86+
"0x2ca96bf6e71766195fa290b97c50f073b218d4e8c6948c899e3b07d754d6760"
87+
);
88+
});
89+
90+
it("should get the proper event name", () => {
91+
const f = (namespace: string, event: string, expected: string) => {
92+
expect(getSelectorFromTag(namespace, event)).toBe(expected);
93+
};
94+
95+
f(
96+
"dojo_starter",
97+
"Moves",
98+
"0x2a29373f1af8348bd366a990eb3a342ef2cbe5e85160539eaca3441a673f468"
99+
);
100+
f(
101+
"dojo_starter",
102+
"Position",
103+
"0x2ac8b4c190f7031b9fc44312e6b047a1dce0b3f2957c33a935ca7846a46dd5b"
104+
);
105+
});
106+
});

0 commit comments

Comments
 (0)