-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): add getRecord and getStaticDataLocation helpers (#3430)
- Loading branch information
Showing
5 changed files
with
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/store": patch | ||
--- | ||
|
||
Added internal `getRecord` and `getStaticDataLocation` helpers. |
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,78 @@ | ||
import { Address, Client, Hex } from "viem"; | ||
import { Table } from "@latticexyz/config"; | ||
import { | ||
decodeValueArgs, | ||
getKeySchema, | ||
getKeyTuple, | ||
getSchemaPrimitives, | ||
getSchemaTypes, | ||
getValueSchema, | ||
} from "@latticexyz/protocol-parser/internal"; | ||
import { readContract } from "viem/actions"; | ||
import { getAction } from "viem/utils"; | ||
|
||
export type GetRecordOptions<table extends Table> = { | ||
address: Address; | ||
table: table; | ||
key: getSchemaPrimitives<getKeySchema<table>>; | ||
blockTag?: "latest" | "pending"; | ||
}; | ||
|
||
export async function getRecord<table extends Table>( | ||
client: Client, | ||
{ address, table, key, blockTag }: GetRecordOptions<table>, | ||
): Promise<getSchemaPrimitives<table["schema"]>> { | ||
const [staticData, encodedLengths, dynamicData] = await getAction( | ||
client, | ||
readContract, | ||
"readContract", | ||
)({ | ||
address, | ||
abi, | ||
functionName: "getRecord", | ||
args: [table.tableId, getKeyTuple(table, key) as readonly Hex[]], | ||
blockTag, | ||
}); | ||
|
||
return { | ||
...key, | ||
...decodeValueArgs(getSchemaTypes(getValueSchema(table)), { staticData, encodedLengths, dynamicData }), | ||
}; | ||
} | ||
|
||
const abi = [ | ||
{ | ||
type: "function", | ||
name: "getRecord", | ||
inputs: [ | ||
{ | ||
name: "tableId", | ||
type: "bytes32", | ||
internalType: "ResourceId", | ||
}, | ||
{ | ||
name: "keyTuple", | ||
type: "bytes32[]", | ||
internalType: "bytes32[]", | ||
}, | ||
], | ||
outputs: [ | ||
{ | ||
name: "staticData", | ||
type: "bytes", | ||
internalType: "bytes", | ||
}, | ||
{ | ||
name: "encodedLengths", | ||
type: "bytes32", | ||
internalType: "EncodedLengths", | ||
}, | ||
{ | ||
name: "dynamicData", | ||
type: "bytes", | ||
internalType: "bytes", | ||
}, | ||
], | ||
stateMutability: "view", | ||
}, | ||
] as const; |
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 { Hex, concatHex, hexToBigInt, keccak256, numberToHex, toBytes } from "viem"; | ||
|
||
// TODO: move to protocol-parser? | ||
// equivalent of StoreCore._getStaticDataLocation | ||
|
||
const SLOT = hexToBigInt(keccak256(toBytes("mud.store"))); | ||
|
||
export function getStaticDataLocation(tableId: Hex, keyTuple: readonly Hex[]): Hex { | ||
return numberToHex(SLOT ^ hexToBigInt(keccak256(concatHex([tableId, ...keyTuple]))), { size: 32 }); | ||
} |