From fc40ba3d42ad8dd8760eed617c24bd397394d5e3 Mon Sep 17 00:00:00 2001 From: Roman Petriv Date: Wed, 28 Aug 2024 12:47:33 +0300 Subject: [PATCH] fix: waiting for tx receipt with retries (#190) --- composables/transaction/useAllowance.ts | 19 +++++++++++++------ .../zksync/useWithdrawalFinalization.ts | 14 ++++++++------ store/zksync/transactionStatus.ts | 8 +++++--- utils/helpers.ts | 9 +++++++-- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/composables/transaction/useAllowance.ts b/composables/transaction/useAllowance.ts index 7c84f7f61..ae983b1cf 100644 --- a/composables/transaction/useAllowance.ts +++ b/composables/transaction/useAllowance.ts @@ -75,12 +75,19 @@ export default ( }); setAllowanceStatus.value = "sending"; - const receipt = await getPublicClient().waitForTransactionReceipt({ - hash: setAllowanceTransactionHash.value!, - onReplaced: (replacement) => { - setAllowanceTransactionHash.value = replacement.transaction.hash; - }, - }); + const receipt = await retry( + () => + getPublicClient().waitForTransactionReceipt({ + hash: setAllowanceTransactionHash.value!, + onReplaced: (replacement) => { + setAllowanceTransactionHash.value = replacement.transaction.hash; + }, + }), + { + retries: 3, + delay: 5_000, + } + ); await requestAllowance(); setAllowanceStatus.value = "done"; diff --git a/composables/zksync/useWithdrawalFinalization.ts b/composables/zksync/useWithdrawalFinalization.ts index 5eb7186a3..a7ba59c7f 100644 --- a/composables/zksync/useWithdrawalFinalization.ts +++ b/composables/zksync/useWithdrawalFinalization.ts @@ -130,12 +130,14 @@ export default (transactionInfo: ComputedRef) => { }); status.value = "sending"; - const receipt = await onboardStore.getPublicClient().waitForTransactionReceipt({ - hash: transactionHash.value!, - onReplaced: (replacement) => { - transactionHash.value = replacement.transaction.hash; - }, - }); + const receipt = await retry(() => + onboardStore.getPublicClient().waitForTransactionReceipt({ + hash: transactionHash.value!, + onReplaced: (replacement) => { + transactionHash.value = replacement.transaction.hash; + }, + }) + ); trackEvent("withdrawal-finalized", { token: transactionInfo.value!.token.symbol, diff --git a/store/zksync/transactionStatus.ts b/store/zksync/transactionStatus.ts index 0d165e898..18535e239 100644 --- a/store/zksync/transactionStatus.ts +++ b/store/zksync/transactionStatus.ts @@ -52,9 +52,11 @@ export const useZkSyncTransactionStatusStore = defineStore("zkSyncTransactionSta const getDepositL2TransactionHash = async (l1TransactionHash: string) => { const publicClient = onboardStore.getPublicClient(); - const transaction = await publicClient.waitForTransactionReceipt({ - hash: l1TransactionHash as Hash, - }); + const transaction = await retry(() => + publicClient.waitForTransactionReceipt({ + hash: l1TransactionHash as Hash, + }) + ); for (const log of transaction.logs) { try { const { args, eventName } = decodeEventLog({ diff --git a/utils/helpers.ts b/utils/helpers.ts index 447191211..6993af0dd 100644 --- a/utils/helpers.ts +++ b/utils/helpers.ts @@ -36,17 +36,22 @@ export const silentRouterChange = (location: string, mode: "push" | "replace" = interface RetryOptions { retries?: number; + delay?: number; } const DEFAULT_RETRY_OPTIONS: RetryOptions = { retries: 2, + delay: 0, }; export async function retry(func: () => Promise, options: RetryOptions = {}): Promise { - const { retries } = Object.assign({}, DEFAULT_RETRY_OPTIONS, options); + const { retries, delay } = Object.assign({}, DEFAULT_RETRY_OPTIONS, options); try { return await func(); } catch (error) { if (retries && retries > 0) { - return retry(func, { retries: retries - 1 }); + if (delay) { + await new Promise((resolve) => setTimeout(resolve, delay)); + } + return retry(func, { retries: retries - 1, delay }); } else { throw error; }