Skip to content

Commit

Permalink
feat(protocol-parser): add encodeKeyTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Jul 4, 2023
1 parent 086be4e commit 7d565fa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/protocol-parser/src/decodeKeyTuple.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
39 changes: 39 additions & 0 deletions packages/protocol-parser/src/encodeKeyTuple.test.ts
Original file line number Diff line number Diff line change
@@ -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",
]);
});
});
7 changes: 7 additions & 0 deletions packages/protocol-parser/src/encodeKeyTuple.ts
Original file line number Diff line number Diff line change
@@ -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]));
}

0 comments on commit 7d565fa

Please sign in to comment.