diff --git a/.changeset/soft-rings-grab.md b/.changeset/soft-rings-grab.md new file mode 100644 index 0000000000..0459025933 --- /dev/null +++ b/.changeset/soft-rings-grab.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/world": patch +--- + +`getFunctions` now internally uses `fetchLogs` for better handling of block range errors. diff --git a/packages/world/package.json b/packages/world/package.json index ed2f7ab488..32e808297c 100644 --- a/packages/world/package.json +++ b/packages/world/package.json @@ -56,6 +56,7 @@ }, "dependencies": { "@ark/util": "catalog:", + "@latticexyz/block-logs-stream": "workspace:*", "@latticexyz/common": "workspace:*", "@latticexyz/config": "workspace:*", "@latticexyz/protocol-parser": "workspace:*", diff --git a/packages/world/ts/getFunctions.ts b/packages/world/ts/getFunctions.ts index 34dc39363a..cbdb2c2eb8 100644 --- a/packages/world/ts/getFunctions.ts +++ b/packages/world/ts/getFunctions.ts @@ -1,8 +1,6 @@ -import { Client, parseAbiItem, Address } from "viem"; +import { Client, Address } from "viem"; import { WorldFunction } from "./common"; import { debug } from "./debug"; -import { storeSetRecordEvent } from "@latticexyz/store"; -import { getLogs } from "viem/actions"; import { decodeKey, decodeValueArgs, @@ -11,6 +9,8 @@ import { getValueSchema, } from "@latticexyz/protocol-parser/internal"; import worldConfig from "../mud.config"; +import { fetchBlockLogs } from "@latticexyz/block-logs-stream"; +import { flattenStoreLogs, getStoreLogs } from "@latticexyz/store/internal"; export async function getFunctions({ client, @@ -25,14 +25,21 @@ export async function getFunctions({ }): Promise { // This assumes we only use `FunctionSelectors._set(...)`, which is true as of this writing. debug("looking up function selectors for", worldAddress); - const selectorLogs = await getLogs(client, { - strict: true, + + const selectorBlocks = await fetchBlockLogs({ fromBlock, toBlock, - address: worldAddress, - event: parseAbiItem(storeSetRecordEvent), - args: { tableId: worldConfig.namespaces.world.tables.FunctionSelectors.tableId }, + maxBlockRange: 100_000n, + async getLogs({ fromBlock, toBlock }) { + return getStoreLogs(client, { + address: worldAddress, + fromBlock, + toBlock, + tableId: worldConfig.namespaces.world.tables.FunctionSelectors.tableId, + }); + }, }); + const selectorLogs = flattenStoreLogs(selectorBlocks.flatMap((block) => block.logs)); const selectors = selectorLogs.map((log) => { return { @@ -50,14 +57,21 @@ export async function getFunctions({ // This assumes we only use `FunctionSignatures._set(...)`, which is true as of this writing. debug("looking up function signatures for", worldAddress); - const signatureLogs = await getLogs(client, { - strict: true, + + const signatureBlocks = await fetchBlockLogs({ fromBlock, toBlock, - address: worldAddress, - event: parseAbiItem(storeSetRecordEvent), - args: { tableId: worldConfig.namespaces.world.tables.FunctionSignatures.tableId }, + maxBlockRange: 100_000n, + async getLogs({ fromBlock, toBlock }) { + return getStoreLogs(client, { + address: worldAddress, + fromBlock, + toBlock, + tableId: worldConfig.namespaces.world.tables.FunctionSignatures.tableId, + }); + }, }); + const signatureLogs = flattenStoreLogs(signatureBlocks.flatMap((block) => block.logs)); const selectorToSignature = Object.fromEntries( signatureLogs.map((log) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7c622f31c2..725a834cd1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1237,6 +1237,9 @@ importers: '@ark/util': specifier: 'catalog:' version: 0.2.2 + '@latticexyz/block-logs-stream': + specifier: workspace:* + version: link:../block-logs-stream '@latticexyz/common': specifier: workspace:* version: link:../common