Skip to content

Commit

Permalink
add dynamic test data
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Jul 3, 2023
1 parent 3631c5c commit 58a757b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
11 changes: 11 additions & 0 deletions e2e/packages/sync-test/data/encodeTestData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
});
});
});
29 changes: 23 additions & 6 deletions e2e/packages/sync-test/data/encodeTestData.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
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`
*/
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<typeof testData>;
}

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 };
}
2 changes: 2 additions & 0 deletions e2e/packages/sync-test/data/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -34,4 +35,5 @@ export const testData2 = {
value: { num: BigInt(0), value: true },
},
],
NumberList: [{ key: {}, value: { value: [1, 2, 3] } }],
} satisfies Data;
1 change: 1 addition & 0 deletions e2e/packages/sync-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions e2e/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58a757b

Please sign in to comment.