-
Notifications
You must be signed in to change notification settings - Fork 193
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 keySchema/valueSchema helpers (#1443)
Co-authored-by: alvarius <[email protected]>
- Loading branch information
Showing
31 changed files
with
216 additions
and
81 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/store": minor | ||
--- | ||
|
||
Moved `KeySchema`, `ValueSchema`, `SchemaToPrimitives` and `TableRecord` types into `@latticexyz/protocol-parser` |
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,5 @@ | ||
--- | ||
"@latticexyz/protocol-parser": minor | ||
--- | ||
|
||
Adds `decodeKey`, `decodeValue`, `encodeKey`, and `encodeValue` helpers to decode/encode from key/value schemas. Deprecates previous methods that use a schema object with static/dynamic field arrays, originally attempting to model our on-chain behavior but ended up not very ergonomic when working with table configs. |
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 |
---|---|---|
@@ -1,11 +1,26 @@ | ||
import { DynamicAbiType, StaticAbiType } from "@latticexyz/schema-type"; | ||
import { DynamicAbiType, SchemaAbiType, SchemaAbiTypeToPrimitiveType, StaticAbiType } from "@latticexyz/schema-type"; | ||
|
||
/** @deprecated use `KeySchema` or `ValueSchema` instead */ | ||
export type Schema = { | ||
readonly staticFields: readonly StaticAbiType[]; | ||
readonly dynamicFields: readonly DynamicAbiType[]; | ||
}; | ||
|
||
/** @deprecated use `KeySchema` and `ValueSchema` instead */ | ||
export type TableSchema = { | ||
readonly keySchema: Schema; // TODO: refine to set dynamicFields to [] | ||
readonly valueSchema: Schema; | ||
}; | ||
|
||
export type KeySchema = Record<string, StaticAbiType>; | ||
export type ValueSchema = Record<string, SchemaAbiType>; | ||
|
||
/** Map a table schema like `{ value: "uint256" }` to its primitive types like `{ value: bigint }` */ | ||
export type SchemaToPrimitives<TSchema extends ValueSchema> = { | ||
[key in keyof TSchema]: SchemaAbiTypeToPrimitiveType<TSchema[key]>; | ||
}; | ||
|
||
export type TableRecord<TKeySchema extends KeySchema = KeySchema, TValueSchema extends ValueSchema = ValueSchema> = { | ||
key: SchemaToPrimitives<TKeySchema>; | ||
value: SchemaToPrimitives<TValueSchema>; | ||
}; |
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,15 @@ | ||
import { Hex } from "viem"; | ||
import { SchemaToPrimitives, KeySchema } from "./common"; | ||
import { decodeKeyTuple } from "./decodeKeyTuple"; | ||
|
||
export function decodeKey<TSchema extends KeySchema>( | ||
keySchema: TSchema, | ||
data: readonly Hex[] | ||
): SchemaToPrimitives<TSchema> { | ||
// TODO: refactor and move all decodeKeyTuple logic into this method so we can delete decodeKeyTuple | ||
const keyValues = decodeKeyTuple({ staticFields: Object.values(keySchema), dynamicFields: [] }, data); | ||
|
||
return Object.fromEntries( | ||
Object.keys(keySchema).map((name, i) => [name, keyValues[i]]) | ||
) as SchemaToPrimitives<TSchema>; | ||
} |
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
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,16 @@ | ||
import { isStaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type"; | ||
import { Hex } from "viem"; | ||
import { SchemaToPrimitives, ValueSchema } from "./common"; | ||
import { decodeRecord } from "./decodeRecord"; | ||
|
||
export function decodeValue<TSchema extends ValueSchema>(valueSchema: TSchema, data: Hex): SchemaToPrimitives<TSchema> { | ||
const staticFields = Object.values(valueSchema).filter(isStaticAbiType); | ||
const dynamicFields = Object.values(valueSchema).filter(isDynamicAbiType); | ||
|
||
// TODO: refactor and move all decodeRecord logic into this method so we can delete decodeRecord | ||
const valueTuple = decodeRecord({ staticFields, dynamicFields }, data); | ||
|
||
return Object.fromEntries( | ||
Object.keys(valueSchema).map((name, i) => [name, valueTuple[i]]) | ||
) as SchemaToPrimitives<TSchema>; | ||
} |
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,10 @@ | ||
import { isStaticAbiType } from "@latticexyz/schema-type"; | ||
import { Hex } from "viem"; | ||
import { SchemaToPrimitives, KeySchema } from "./common"; | ||
import { encodeKeyTuple } from "./encodeKeyTuple"; | ||
|
||
export function encodeKey<TSchema extends KeySchema>(keySchema: TSchema, key: SchemaToPrimitives<TSchema>): Hex[] { | ||
const staticFields = Object.values(keySchema).filter(isStaticAbiType); | ||
// TODO: refactor and move all encodeKeyTuple logic into this method so we can delete encodeKeyTuple | ||
return encodeKeyTuple({ staticFields, dynamicFields: [] }, Object.values(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
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
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,18 @@ | ||
import { isStaticAbiType, isDynamicAbiType } from "@latticexyz/schema-type"; | ||
import { Hex } from "viem"; | ||
import { SchemaToPrimitives, ValueSchema } from "./common"; | ||
import { encodeRecord } from "./encodeRecord"; | ||
|
||
export function encodeValue<TSchema extends ValueSchema>( | ||
valueSchema: TSchema, | ||
value: SchemaToPrimitives<TSchema> | ||
): Hex { | ||
const staticFields = Object.values(valueSchema).filter(isStaticAbiType); | ||
const dynamicFields = Object.values(valueSchema).filter(isDynamicAbiType); | ||
|
||
// TODO: refactor and move all encodeRecord logic into this method so we can delete encodeRecord | ||
|
||
// This currently assumes fields/values are ordered by static, dynamic | ||
// TODO: make sure we preserve ordering based on schema definition | ||
return encodeRecord({ staticFields, dynamicFields }, Object.values(value)); | ||
} |
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
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,8 @@ | ||
import { isStaticAbiType } from "@latticexyz/schema-type"; | ||
import { Hex } from "viem"; | ||
import { KeySchema } from "./common"; | ||
import { schemaToHex } from "./schemaToHex"; | ||
|
||
export function keySchemaToHex(schema: KeySchema): Hex { | ||
return schemaToHex({ staticFields: Object.values(schema).filter(isStaticAbiType), dynamicFields: [] }); | ||
} |
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,15 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { readHex } from "./readHex"; | ||
|
||
describe("readHex", () => { | ||
it("can slice empty hex", () => { | ||
expect(readHex("0x", 6)).toBe("0x"); | ||
expect(readHex("0x", 6, 10)).toBe("0x00000000"); | ||
}); | ||
it("can slice hex out of bounds", () => { | ||
expect(readHex("0x000100", 1)).toBe("0x0100"); | ||
expect(readHex("0x000100", 1, 4)).toBe("0x010000"); | ||
expect(readHex("0x000100", 3)).toBe("0x"); | ||
expect(readHex("0x000100", 3, 4)).toBe("0x00"); | ||
}); | ||
}); |
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,15 @@ | ||
import { Hex } from "viem"; | ||
|
||
/** | ||
* Get the hex value at start/end positions. This will always return a valid hex string. | ||
* | ||
* If `start` is out of range, this returns `"0x"`. | ||
* | ||
* If `end` is specified and out of range, the result is right zero-padded to the desired length (`end - start`). | ||
*/ | ||
export function readHex(data: Hex, start: number, end?: number): Hex { | ||
return `0x${data | ||
.replace(/^0x/, "") | ||
.slice(start * 2, end != null ? end * 2 : undefined) | ||
.padEnd(((end ?? start) - start) * 2, "0")}`; | ||
} |
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,11 @@ | ||
import { isDynamicAbiType, isStaticAbiType } from "@latticexyz/schema-type"; | ||
import { Hex } from "viem"; | ||
import { ValueSchema } from "./common"; | ||
import { schemaToHex } from "./schemaToHex"; | ||
|
||
export function valueSchemaToHex(schema: ValueSchema): Hex { | ||
return schemaToHex({ | ||
staticFields: Object.values(schema).filter(isStaticAbiType), | ||
dynamicFields: Object.values(schema).filter(isDynamicAbiType), | ||
}); | ||
} |
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
Oops, something went wrong.