Skip to content

Commit

Permalink
fix(world): use fetchLogs in getFunctions (#3338)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Oct 28, 2024
1 parent cdbd3a5 commit 22674ad
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-rings-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/world": patch
---

`getFunctions` now internally uses `fetchLogs` for better handling of block range errors.
1 change: 1 addition & 0 deletions packages/world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
},
"dependencies": {
"@ark/util": "catalog:",
"@latticexyz/block-logs-stream": "workspace:*",
"@latticexyz/common": "workspace:*",
"@latticexyz/config": "workspace:*",
"@latticexyz/protocol-parser": "workspace:*",
Expand Down
40 changes: 27 additions & 13 deletions packages/world/ts/getFunctions.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -25,14 +25,21 @@ export async function getFunctions({
}): Promise<readonly WorldFunction[]> {
// 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 {
Expand All @@ -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) => {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 22674ad

Please sign in to comment.