Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: waitForStateChange -> waitForTransaction #3210

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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, type WaitForTransaction } from "@latticexyz/explorer/observer";

/*
* Import our MUD config, which includes strong types for
Expand All @@ -34,7 +34,7 @@ export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;

export async function setupNetwork() {
const networkConfig = await getNetworkConfig();
const waitForStateChange = Promise.withResolvers<WaitForStateChange>();
const waitForTx = Promise.withResolvers<WaitForTransaction>();

/*
* Create a viem public (read only) client
Expand All @@ -60,7 +60,7 @@ export async function setupNetwork() {
.extend(transactionQueue())
.extend(
observer({
waitForStateChange: (hash) => waitForStateChange.promise.then((fn) => fn(hash)),
waitForTransaction: (hash) => waitForTx.promise.then((fn) => fn(hash)),
}),
);

Expand All @@ -85,7 +85,7 @@ export async function setupNetwork() {
publicClient,
startBlock: BigInt(networkConfig.initialBlockNumber),
});
waitForStateChange.resolve(waitForTransaction);
waitForTx.resolve(waitForTransaction);

return {
tables,
Expand Down
2 changes: 1 addition & 1 deletion packages/explorer/src/exports/observer.ts
Original file line number Diff line number Diff line change
@@ -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";
14 changes: 7 additions & 7 deletions packages/explorer/src/observer/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { formatAbiItem, getAction } from "viem/utils";
import { createBridge } from "./bridge";
import { ReceiptSummary } from "./common";

export type WaitForStateChange = (hash: Hex) => Promise<ReceiptSummary>;
export type WaitForTransaction = (hash: Hex) => Promise<ReceiptSummary>;

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,
Expand Down Expand Up @@ -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 });
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/explorer/src/observer/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export type Messages = {
"waitForTransactionReceipt:result": PromiseSettledResult<ReceiptSummary> & {
writeId: string;
};
waitForStateChange: {
waitForTransaction: {
writeId: string;
};
"waitForStateChange:result": PromiseSettledResult<ReceiptSummary> & {
"waitForTransaction:result": PromiseSettledResult<ReceiptSummary> & {
writeId: string;
};
};
Expand Down
6 changes: 2 additions & 4 deletions packages/store-sync/src/stash/syncToStash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export type SyncToStashOptions = {
startSync?: boolean;
};

export type SyncToStashResult = Omit<SyncResult, "waitForTransaction"> & {
waitForStateChange: SyncResult["waitForTransaction"];
export type SyncToStashResult = SyncResult & {
stopSync: () => void;
};

Expand All @@ -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,
Expand All @@ -70,7 +69,6 @@ export async function syncToStash({

return {
...sync,
waitForStateChange,
stopSync,
};
}
Loading