Skip to content

Commit

Permalink
feat(protocol-parser): add abiTypesToSchema (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs authored Jul 5, 2023
1 parent ca50fef commit b98e518
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-ears-hug.md
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions packages/protocol-parser/src/abiTypesToSchema.test.ts
Original file line number Diff line number Diff line change
@@ -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",
],
}
`);
});
});
12 changes: 12 additions & 0 deletions packages/protocol-parser/src/abiTypesToSchema.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
1 change: 1 addition & 0 deletions packages/protocol-parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./abiTypesToSchema";
export * from "./common";
export * from "./decodeDynamicField";
export * from "./decodeKeyTuple";
Expand Down

0 comments on commit b98e518

Please sign in to comment.