diff --git a/e2e/packages/sync-test/data/encodeTestData.test.ts b/e2e/packages/sync-test/data/encodeTestData.test.ts index 90507dc4660..a9442fe7714 100644 --- a/e2e/packages/sync-test/data/encodeTestData.test.ts +++ b/e2e/packages/sync-test/data/encodeTestData.test.ts @@ -13,4 +13,15 @@ describe("encodeTestData", () => { ], }); }); + + it("should encode number arrays", () => { + expect(encodeTestData({ NumberList: [{ key: {}, value: { value: [1, 2, 3] } }] })).toStrictEqual({ + NumberList: [ + { + key: [], + value: "0x0000000000000c000000000c0000000000000000000000000000000000000000000000010000000200000003", + }, + ], + }); + }); }); diff --git a/e2e/packages/sync-test/data/encodeTestData.ts b/e2e/packages/sync-test/data/encodeTestData.ts index f4ba27153b3..c7b6c272d91 100644 --- a/e2e/packages/sync-test/data/encodeTestData.ts +++ b/e2e/packages/sync-test/data/encodeTestData.ts @@ -1,7 +1,9 @@ import { mapObject } from "@latticexyz/utils"; import { Data, EncodedData } from "./types"; -import { encodeAbiParameters, toHex, encodePacked, numberToHex } from "viem"; +import { encodeAbiParameters, toHex, numberToHex } from "viem"; +import { Schema, encodeRecord } from "@latticexyz/protocol-parser"; import config from "../../contracts/mud.config"; +import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type"; /** * Turns the typed data into encoded data in the format expected by `world.setRecord` @@ -9,16 +11,31 @@ import config from "../../contracts/mud.config"; export function encodeTestData(testData: Data) { return mapObject(testData, (records, table) => records - ? records.map((record) => ({ - key: Object.entries(record.key).map(([keyName, keyValue]) => { + ? records.map((record) => { + const valueSchema = abiTypeArrayToSchema(Object.values(config.tables[table].schema)); + const value = encodeRecord(valueSchema, Object.values(record.value)); + const key = Object.entries(record.key).map(([keyName, keyValue]) => { const signed = config.tables[table].keySchema[keyName]?.startsWith("int"); return encodeAbiParameters( [{ type: "bytes32" }], [signed ? numberToHex(keyValue as any, { size: 32, signed }) : toHex(keyValue as any, { size: 32 })] ); - }), - value: encodePacked(Object.values(config.tables[table].schema), Object.values(record.value)), - })) + }); + return { + key, + value, + }; + }) : undefined ) as EncodedData; } + +function abiTypeArrayToSchema(abiTypes: SchemaAbiType[]): Schema { + const staticFields: StaticAbiType[] = []; + const dynamicFields: DynamicAbiType[] = []; + for (const abiType of abiTypes) { + if (isDynamicAbiType(abiType)) dynamicFields.push(abiType); + else staticFields.push(abiType); + } + return { staticFields, dynamicFields }; +} diff --git a/e2e/packages/sync-test/data/testData.ts b/e2e/packages/sync-test/data/testData.ts index 9c24b99fe68..d3a4837af6b 100644 --- a/e2e/packages/sync-test/data/testData.ts +++ b/e2e/packages/sync-test/data/testData.ts @@ -13,6 +13,7 @@ export const testData1 = { value: { num: BigInt(1337), value: true }, }, ], + NumberList: [{ key: {}, value: { value: [1, 2, 3] } }], } satisfies Data; export const testData2 = { @@ -34,4 +35,5 @@ export const testData2 = { value: { num: BigInt(0), value: true }, }, ], + NumberList: [{ key: {}, value: { value: [1, 2, 3] } }], } satisfies Data; diff --git a/e2e/packages/sync-test/package.json b/e2e/packages/sync-test/package.json index 89be9b80f54..3f1021261a4 100644 --- a/e2e/packages/sync-test/package.json +++ b/e2e/packages/sync-test/package.json @@ -9,6 +9,7 @@ "devDependencies": { "@latticexyz/cli": "link:../../../packages/cli", "@latticexyz/network": "link:../../../packages/network", + "@latticexyz/protocol-parser": "link:../../../packages/protocol-parser", "@latticexyz/schema-type": "link:../../../packages/schema-type", "@latticexyz/store": "link:../../../packages/store", "@latticexyz/utils": "link:../../../packages/utils", diff --git a/e2e/pnpm-lock.yaml b/e2e/pnpm-lock.yaml index f14ab1281f5..e8d3f5b25f3 100644 --- a/e2e/pnpm-lock.yaml +++ b/e2e/pnpm-lock.yaml @@ -152,6 +152,9 @@ importers: '@latticexyz/network': specifier: link:../../../packages/network version: link:../../../packages/network + '@latticexyz/protocol-parser': + specifier: link:../../../packages/protocol-parser + version: link:../../../packages/protocol-parser '@latticexyz/schema-type': specifier: link:../../../packages/schema-type version: link:../../../packages/schema-type