From 7a0bd74140356ec3b70f8e7688262d3d6cd3f776 Mon Sep 17 00:00:00 2001 From: alvarius Date: Wed, 5 Jul 2023 23:43:44 +0100 Subject: [PATCH] feat(protocol-parser): add encodeKeyTuple (#1099) --- .../src/decodeKeyTuple.test.ts | 1 - .../src/encodeKeyTuple.test.ts | 39 +++++++++++++++++++ .../protocol-parser/src/encodeKeyTuple.ts | 7 ++++ packages/protocol-parser/src/index.ts | 1 + 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 packages/protocol-parser/src/encodeKeyTuple.test.ts create mode 100644 packages/protocol-parser/src/encodeKeyTuple.ts diff --git a/packages/protocol-parser/src/decodeKeyTuple.test.ts b/packages/protocol-parser/src/decodeKeyTuple.test.ts index 7b47ac731d..362118332d 100644 --- a/packages/protocol-parser/src/decodeKeyTuple.test.ts +++ b/packages/protocol-parser/src/decodeKeyTuple.test.ts @@ -1,6 +1,5 @@ import { describe, expect, it } from "vitest"; import { decodeKeyTuple } from "./decodeKeyTuple"; -import { Schema } from "./common"; describe("decodeKeyTuple", () => { it("can decode bool key tuple", () => { diff --git a/packages/protocol-parser/src/encodeKeyTuple.test.ts b/packages/protocol-parser/src/encodeKeyTuple.test.ts new file mode 100644 index 0000000000..048e0d2921 --- /dev/null +++ b/packages/protocol-parser/src/encodeKeyTuple.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from "vitest"; +import { encodeKeyTuple } from "./encodeKeyTuple"; + +describe("encodeKeyTuple", () => { + it("can encode bool key tuple", () => { + expect(encodeKeyTuple({ staticFields: ["bool"], dynamicFields: [] }, [false])).toStrictEqual([ + "0x0000000000000000000000000000000000000000000000000000000000000000", + ]); + expect( + encodeKeyTuple( + { + staticFields: ["bool"], + dynamicFields: [], + }, + [true] + ) + ).toStrictEqual(["0x0000000000000000000000000000000000000000000000000000000000000001"]); + }); + + it("can encode complex key tuple", () => { + expect( + encodeKeyTuple({ staticFields: ["uint256", "int32", "bytes16", "address", "bool", "int8"], dynamicFields: [] }, [ + 42n, + -42, + "0x12340000000000000000000000000000", + "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF", + true, + 3, + ]) + ).toStrictEqual([ + "0x000000000000000000000000000000000000000000000000000000000000002a", + "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd6", + "0x1234000000000000000000000000000000000000000000000000000000000000", + "0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff", + "0x0000000000000000000000000000000000000000000000000000000000000001", + "0x0000000000000000000000000000000000000000000000000000000000000003", + ]); + }); +}); diff --git a/packages/protocol-parser/src/encodeKeyTuple.ts b/packages/protocol-parser/src/encodeKeyTuple.ts new file mode 100644 index 0000000000..c52ffae0ac --- /dev/null +++ b/packages/protocol-parser/src/encodeKeyTuple.ts @@ -0,0 +1,7 @@ +import { StaticPrimitiveType } from "@latticexyz/schema-type"; +import { Hex, encodeAbiParameters } from "viem"; +import { Schema } from "./common"; + +export function encodeKeyTuple(keySchema: Schema, keyTuple: StaticPrimitiveType[]): Hex[] { + return keyTuple.map((key, index) => encodeAbiParameters([{ type: keySchema.staticFields[index] }], [key])); +} diff --git a/packages/protocol-parser/src/index.ts b/packages/protocol-parser/src/index.ts index 5889d6e581..bec5944af2 100644 --- a/packages/protocol-parser/src/index.ts +++ b/packages/protocol-parser/src/index.ts @@ -4,6 +4,7 @@ export * from "./decodeKeyTuple"; export * from "./decodeRecord"; export * from "./decodeStaticField"; export * from "./encodeField"; +export * from "./encodeKeyTuple"; export * from "./encodeRecord"; export * from "./errors"; export * from "./hexToPackedCounter";