Skip to content

Commit

Permalink
fix: waiting for tx receipt with retries (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsters authored Aug 28, 2024
1 parent 485d879 commit fc40ba3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
19 changes: 13 additions & 6 deletions composables/transaction/useAllowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
14 changes: 8 additions & 6 deletions composables/zksync/useWithdrawalFinalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ export default (transactionInfo: ComputedRef<TransactionInfo>) => {
});

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,
Expand Down
8 changes: 5 additions & 3 deletions store/zksync/transactionStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
9 changes: 7 additions & 2 deletions utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(func: () => Promise<T>, options: RetryOptions = {}): Promise<T> {
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;
}
Expand Down

0 comments on commit fc40ba3

Please sign in to comment.