From 842fd419eb8b53e45abd6bd73190f66954928b87 Mon Sep 17 00:00:00 2001 From: romsters Date: Wed, 28 Aug 2024 01:19:18 +0300 Subject: [PATCH 1/3] fix: use retry for waiting for transaction --- composables/transaction/useAllowance.ts | 14 ++++++++------ composables/zksync/useWithdrawalFinalization.ts | 14 ++++++++------ store/zksync/transactionStatus.ts | 8 +++++--- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/composables/transaction/useAllowance.ts b/composables/transaction/useAllowance.ts index 7c84f7f61..22259967a 100644 --- a/composables/transaction/useAllowance.ts +++ b/composables/transaction/useAllowance.ts @@ -75,12 +75,14 @@ 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; + }, + }) + ); 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({ From caaa63119f0f249a86edf10e3b66f4055ba9290a Mon Sep 17 00:00:00 2001 From: romsters Date: Wed, 28 Aug 2024 01:38:08 +0300 Subject: [PATCH 2/3] fix: add delay option for retry --- composables/transaction/useAllowance.ts | 19 ++++++++++++------- utils/helpers.ts | 9 +++++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/composables/transaction/useAllowance.ts b/composables/transaction/useAllowance.ts index 22259967a..3629be334 100644 --- a/composables/transaction/useAllowance.ts +++ b/composables/transaction/useAllowance.ts @@ -75,13 +75,18 @@ export default ( }); setAllowanceStatus.value = "sending"; - const receipt = await retry(() => - 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: 2, + delay: 3_000, + } ); await requestAllowance(); 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; } From e1808c707be3c14f65f1118eb3aecc6242a9ecb9 Mon Sep 17 00:00:00 2001 From: romsters Date: Wed, 28 Aug 2024 01:44:31 +0300 Subject: [PATCH 3/3] fix: update allowance wait for tx retry config --- composables/transaction/useAllowance.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composables/transaction/useAllowance.ts b/composables/transaction/useAllowance.ts index 3629be334..ae983b1cf 100644 --- a/composables/transaction/useAllowance.ts +++ b/composables/transaction/useAllowance.ts @@ -84,8 +84,8 @@ export default ( }, }), { - retries: 2, - delay: 3_000, + retries: 3, + delay: 5_000, } ); await requestAllowance();