diff --git a/.changeset/fast-ears-hug.md b/.changeset/fast-ears-hug.md new file mode 100644 index 0000000000..635e8bc42c --- /dev/null +++ b/.changeset/fast-ears-hug.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/protocol-parser": minor +--- + +feat: add abiTypesToSchema, a util to turn a list of abi types into a Schema by separating static and dynamic types diff --git a/packages/protocol-parser/src/abiTypesToSchema.test.ts b/packages/protocol-parser/src/abiTypesToSchema.test.ts new file mode 100644 index 0000000000..a4f9ea77e8 --- /dev/null +++ b/packages/protocol-parser/src/abiTypesToSchema.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from "vitest"; +import { abiTypesToSchema } from "./abiTypesToSchema"; + +describe("hexToSchema", () => { + it("converts hex to schema", () => { + expect(abiTypesToSchema(["bool"])).toMatchInlineSnapshot(` + { + "dynamicFields": [], + "staticFields": [ + "bool", + ], + } + `); + expect(abiTypesToSchema(["bool", "bool[]"])).toMatchInlineSnapshot(` + { + "dynamicFields": [ + "bool[]", + ], + "staticFields": [ + "bool", + ], + } + `); + expect(abiTypesToSchema(["bytes32", "int32", "uint256[]", "address[]", "bytes", "string"])).toMatchInlineSnapshot(` + { + "dynamicFields": [ + "uint256[]", + "address[]", + "bytes", + "string", + ], + "staticFields": [ + "bytes32", + "int32", + ], + } + `); + }); +}); diff --git a/packages/protocol-parser/src/abiTypesToSchema.ts b/packages/protocol-parser/src/abiTypesToSchema.ts new file mode 100644 index 0000000000..1dc2077ac9 --- /dev/null +++ b/packages/protocol-parser/src/abiTypesToSchema.ts @@ -0,0 +1,12 @@ +import { DynamicAbiType, SchemaAbiType, StaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type"; +import { Schema } from "./common"; + +export function abiTypesToSchema(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/packages/protocol-parser/src/index.ts b/packages/protocol-parser/src/index.ts index bec5944af2..6217f74de0 100644 --- a/packages/protocol-parser/src/index.ts +++ b/packages/protocol-parser/src/index.ts @@ -1,3 +1,4 @@ +export * from "./abiTypesToSchema"; export * from "./common"; export * from "./decodeDynamicField"; export * from "./decodeKeyTuple";