From d21c1d1817ec2394007b28c90fec5a81f1fdd3d0 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Fri, 20 Sep 2024 09:45:24 +0000 Subject: [PATCH] refactor: waitForStateChange -> waitForTransaction (#3210) --- .changeset/thick-countries-double.md | 11 ++++++ .../packages/client/src/mud/setupNetwork.ts | 36 ++++++++----------- packages/explorer/src/exports/observer.ts | 2 +- packages/explorer/src/observer/decorator.ts | 14 ++++---- packages/explorer/src/observer/messages.ts | 4 +-- packages/store-sync/src/stash/syncToStash.ts | 6 ++-- 6 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 .changeset/thick-countries-double.md diff --git a/.changeset/thick-countries-double.md b/.changeset/thick-countries-double.md new file mode 100644 index 0000000000..190496b165 --- /dev/null +++ b/.changeset/thick-countries-double.md @@ -0,0 +1,11 @@ +--- +"@latticexyz/explorer": patch +--- + +Renamed optional `waitForStateChange` param in `observer()` decorator to `waitForTransaction` to better align with `@latticexyz/store-sync` packages. + +```diff + const { waitForTransaction } = syncToZustand(...); +-observer({ waitForStateChange: waitForTransaction }); ++observer({ waitForTransaction }); +``` diff --git a/examples/local-explorer/packages/client/src/mud/setupNetwork.ts b/examples/local-explorer/packages/client/src/mud/setupNetwork.ts index 2ff379e41e..f99b4b21ed 100644 --- a/examples/local-explorer/packages/client/src/mud/setupNetwork.ts +++ b/examples/local-explorer/packages/client/src/mud/setupNetwork.ts @@ -18,7 +18,7 @@ import { getNetworkConfig } from "./getNetworkConfig"; import IWorldAbi from "contracts/out/IWorld.sol/IWorld.abi.json"; import { createBurnerAccount, transportObserver } from "@latticexyz/common"; import { transactionQueue } from "@latticexyz/common/actions"; -import { observer, type WaitForStateChange } from "@latticexyz/explorer/observer"; +import { observer } from "@latticexyz/explorer/observer"; /* * Import our MUD config, which includes strong types for @@ -34,7 +34,6 @@ export type SetupNetworkResult = Awaited>; export async function setupNetwork() { const networkConfig = await getNetworkConfig(); - const waitForStateChange = Promise.withResolvers(); /* * Create a viem public (read only) client @@ -48,6 +47,19 @@ export async function setupNetwork() { const publicClient = createPublicClient(clientOptions); + /* + * Sync on-chain state into RECS and keeps our client in sync. + * Uses the MUD indexer if available, otherwise falls back + * to the viem publicClient to make RPC calls to fetch MUD + * events from the chain. + */ + const { tables, useStore, latestBlock$, storedBlockLogs$, waitForTransaction } = await syncToZustand({ + config: mudConfig, + address: networkConfig.worldAddress as Hex, + publicClient, + startBlock: BigInt(networkConfig.initialBlockNumber), + }); + /* * Create a temporary wallet and a viem client for it * (see https://viem.sh/docs/clients/wallet.html). @@ -58,11 +70,7 @@ export async function setupNetwork() { account: burnerAccount, }) .extend(transactionQueue()) - .extend( - observer({ - waitForStateChange: (hash) => waitForStateChange.promise.then((fn) => fn(hash)), - }), - ); + .extend(observer({ waitForTransaction })); /* * Create an object for communicating with the deployed World. @@ -73,20 +81,6 @@ export async function setupNetwork() { client: { public: publicClient, wallet: burnerWalletClient }, }); - /* - * Sync on-chain state into RECS and keeps our client in sync. - * Uses the MUD indexer if available, otherwise falls back - * to the viem publicClient to make RPC calls to fetch MUD - * events from the chain. - */ - const { tables, useStore, latestBlock$, storedBlockLogs$, waitForTransaction } = await syncToZustand({ - config: mudConfig, - address: networkConfig.worldAddress as Hex, - publicClient, - startBlock: BigInt(networkConfig.initialBlockNumber), - }); - waitForStateChange.resolve(waitForTransaction); - return { tables, useStore, diff --git a/packages/explorer/src/exports/observer.ts b/packages/explorer/src/exports/observer.ts index 84c6797b40..8dd50fdfe4 100644 --- a/packages/explorer/src/exports/observer.ts +++ b/packages/explorer/src/exports/observer.ts @@ -1,3 +1,3 @@ export { createBridge, type CreateBridgeOpts } from "../observer/bridge"; export type { Messages, MessageType, EmitMessage } from "../observer/messages"; -export { observer, type ObserverOptions, type WaitForStateChange } from "../observer/decorator"; +export { observer, type ObserverOptions, type WaitForTransaction } from "../observer/decorator"; diff --git a/packages/explorer/src/observer/decorator.ts b/packages/explorer/src/observer/decorator.ts index c54394589e..43077b4ada 100644 --- a/packages/explorer/src/observer/decorator.ts +++ b/packages/explorer/src/observer/decorator.ts @@ -4,16 +4,16 @@ import { formatAbiItem, getAction } from "viem/utils"; import { createBridge } from "./bridge"; import { ReceiptSummary } from "./common"; -export type WaitForStateChange = (hash: Hex) => Promise; +export type WaitForTransaction = (hash: Hex) => Promise; export type ObserverOptions = { explorerUrl?: string; - waitForStateChange?: WaitForStateChange; + waitForTransaction?: WaitForTransaction; }; let writeCounter = 0; -export function observer({ explorerUrl = "http://localhost:13690", waitForStateChange }: ObserverOptions = {}): < +export function observer({ explorerUrl = "http://localhost:13690", waitForTransaction }: ObserverOptions = {}): < transport extends Transport, chain extends Chain | undefined = Chain | undefined, account extends Account | undefined = Account | undefined, @@ -52,12 +52,12 @@ export function observer({ explorerUrl = "http://localhost:13690", waitForStateC }); }); - if (waitForStateChange) { + if (waitForTransaction) { write.then((hash) => { - const receipt = waitForStateChange(hash); - emit("waitForStateChange", { writeId }); + const receipt = waitForTransaction(hash); + emit("waitForTransaction", { writeId }); Promise.allSettled([receipt]).then(([result]) => { - emit("waitForStateChange:result", { ...result, writeId }); + emit("waitForTransaction:result", { ...result, writeId }); }); }); } diff --git a/packages/explorer/src/observer/messages.ts b/packages/explorer/src/observer/messages.ts index 4c37d7c32d..a906c9f615 100644 --- a/packages/explorer/src/observer/messages.ts +++ b/packages/explorer/src/observer/messages.ts @@ -18,10 +18,10 @@ export type Messages = { "waitForTransactionReceipt:result": PromiseSettledResult & { writeId: string; }; - waitForStateChange: { + waitForTransaction: { writeId: string; }; - "waitForStateChange:result": PromiseSettledResult & { + "waitForTransaction:result": PromiseSettledResult & { writeId: string; }; }; diff --git a/packages/store-sync/src/stash/syncToStash.ts b/packages/store-sync/src/stash/syncToStash.ts index 40ca66fed6..d9000e4e0c 100644 --- a/packages/store-sync/src/stash/syncToStash.ts +++ b/packages/store-sync/src/stash/syncToStash.ts @@ -35,8 +35,7 @@ export type SyncToStashOptions = { startSync?: boolean; }; -export type SyncToStashResult = Omit & { - waitForStateChange: SyncResult["waitForTransaction"]; +export type SyncToStashResult = SyncResult & { stopSync: () => void; }; @@ -50,7 +49,7 @@ export async function syncToStash({ const storageAdapter = createStorageAdapter({ stash }); - const { waitForTransaction: waitForStateChange, ...sync } = await createStoreSync({ + const sync = await createStoreSync({ storageAdapter, publicClient: client.extend(publicActions) as never, address, @@ -70,7 +69,6 @@ export async function syncToStash({ return { ...sync, - waitForStateChange, stopSync, }; }