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

fix: waiting for tx receipt with retries #190

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading