Skip to content

Commit

Permalink
feat(protocol-parser): add abiTypesToSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Jul 5, 2023
1 parent ca50fef commit dd564e0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
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 };
}

0 comments on commit dd564e0

Please sign in to comment.