Skip to content

Commit

Permalink
move polling interval to waitForTransactions, add better retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 18, 2024
1 parent d79ef96 commit 37b868a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
10 changes: 2 additions & 8 deletions packages/cli/src/deploy/deployWorld.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Account, Chain, Client, Hex, Transport } from "viem";
import { waitForTransactionReceipt } from "viem/actions";
import { ensureWorldFactory } from "./ensureWorldFactory";
import WorldFactoryAbi from "@latticexyz/world/out/WorldFactory.sol/WorldFactory.abi.json" assert { type: "json" };
import { writeContract } from "@latticexyz/common";
import { debug } from "./debug";
import { logsToWorldDeploy } from "./logsToWorldDeploy";
import { WorldDeploy } from "./common";
import { waitForTransactions } from "./waitForTransactions";

export async function deployWorld(
client: Client<Transport, Chain | undefined, Account>,
Expand All @@ -24,13 +24,7 @@ export async function deployWorld(
args: [salt],
});

debug("waiting for world deploy");
const receipt = await waitForTransactionReceipt(client, { hash: tx });
if (receipt.status !== "success") {
console.error("world deploy failed", receipt);
throw new Error("world deploy failed");
}

const [receipt] = await waitForTransactions({ client, hashes: [tx], debugLabel: "world deploy" });
const deploy = logsToWorldDeploy(receipt.logs);
debug("deployed world to", deploy.address, "at block", deploy.deployBlock);

Expand Down
16 changes: 12 additions & 4 deletions packages/cli/src/deploy/waitForTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, Transport, Chain, Account, Hex } from "viem";
import { Client, Transport, Chain, Account, Hex, TransactionReceipt } from "viem";
import { debug } from "./debug";
import { waitForTransactionReceipt } from "viem/actions";

Expand All @@ -10,15 +10,23 @@ export async function waitForTransactions({
readonly client: Client<Transport, Chain | undefined, Account>;
readonly hashes: readonly Hex[];
readonly debugLabel: string;
}): Promise<void> {
if (!hashes.length) return;
}): Promise<TransactionReceipt[]> {
if (!hashes.length) return [];

debug(`waiting for ${debugLabel} to confirm`);
const receipts: TransactionReceipt[] = [];
// wait for each tx separately/serially, because parallelizing results in RPC errors
for (const hash of hashes) {
const receipt = await waitForTransactionReceipt(client, { hash });
const receipt = await waitForTransactionReceipt(client, {
hash,
pollingInterval: 100,
retryDelay: ({ count }) => 2 ** count * 200,
});
if (receipt.status === "reverted") {
throw new Error(`Transaction reverted: ${hash}`);
}
receipts.push(receipt);
}

return receipts;
}
1 change: 0 additions & 1 deletion packages/cli/src/runDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {
: undefined,
}),
account,
pollingInterval: 100,
});

console.log("Deploying from", client.account.address);
Expand Down

0 comments on commit 37b868a

Please sign in to comment.