Skip to content

Commit

Permalink
use fetchBlockLogs to determine is world deployed
Browse files Browse the repository at this point in the history
  • Loading branch information
karooolis committed Oct 31, 2024
1 parent 722f4b4 commit 398db02
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"dependencies": {
"@hookform/resolvers": "^3.9.0",
"@latticexyz/block-logs-stream": "workspace:*",
"@latticexyz/common": "workspace:*",
"@latticexyz/config": "workspace:*",
"@latticexyz/protocol-parser": "workspace:*",
Expand Down
15 changes: 9 additions & 6 deletions packages/explorer/src/app/(explorer)/api/world-abi/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Address, Hex, createWalletClient, http, parseAbi } from "viem";
import { getBlockNumber, getLogs } from "viem/actions";
import { getBlockNumber } from "viem/actions";
import { fetchBlockLogs } from "@latticexyz/block-logs-stream";
import { helloStoreEvent } from "@latticexyz/store";
import { helloWorldEvent } from "@latticexyz/world";
import { getWorldAbi } from "@latticexyz/world/internal";
Expand All @@ -20,16 +21,18 @@ async function getClient(chainId: supportedChainId) {
async function getParameters(chainId: supportedChainId, worldAddress: Address) {
const client = await getClient(chainId);
const toBlock = await getBlockNumber(client);
const logs = await getLogs(client, {
strict: true,
const logs = await fetchBlockLogs({
fromBlock: 0n,
toBlock,
maxBlockRange: 100_000n,
publicClient: client,
address: worldAddress,
events: parseAbi([helloStoreEvent, helloWorldEvent] as const),
fromBlock: "earliest",
toBlock,
});

const fromBlock = logs[0]?.blockNumber ?? 0n;
// world is considered loaded when both events are emitted
const isWorldDeployed = logs.length === 2;
const isWorldDeployed = logs[0]?.logs.length === 2;

return { fromBlock, toBlock, isWorldDeployed };
}
Expand Down

0 comments on commit 398db02

Please sign in to comment.