-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(protocol-parser): add encodeKeyTuple (#1099)
- Loading branch information
Showing
4 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters