diff --git a/composables/zksync/useWithdrawalFinalization.ts b/composables/zksync/useWithdrawalFinalization.ts index a7ba59c7f..c69592eb0 100644 --- a/composables/zksync/useWithdrawalFinalization.ts +++ b/composables/zksync/useWithdrawalFinalization.ts @@ -122,6 +122,13 @@ export default (transactionInfo: ComputedRef) => { } const wallet = await onboardStore.getWallet(); const { transactionParams, gasLimit, gasPrice } = (await estimateFee())!; + + // Check if the batch has been processed before attempting finalization + const l1BatchNumber = finalizeWithdrawalParams.value?.l1BatchNumber; + if (l1BatchNumber && !(await isBatchProcessed(l1BatchNumber))) { + throw new Error("Batch not yet processed. Please wait and try again later."); + } + status.value = "waiting-for-signature"; transactionHash.value = await wallet.writeContract({ ...transactionParams, @@ -130,13 +137,16 @@ export default (transactionInfo: ComputedRef) => { }); status.value = "sending"; - const receipt = await retry(() => - onboardStore.getPublicClient().waitForTransactionReceipt({ - hash: transactionHash.value!, - onReplaced: (replacement) => { - transactionHash.value = replacement.transaction.hash; - }, - }) + const receipt = await retry( + async () => { + return await onboardStore.getPublicClient().waitForTransactionReceipt({ + hash: transactionHash.value!, + onReplaced: (replacement) => { + transactionHash.value = replacement.transaction.hash; + }, + }); + }, + { retries: 3, delay: 10000 } ); trackEvent("withdrawal-finalized", { @@ -166,3 +176,16 @@ export default (transactionInfo: ComputedRef) => { commitTransaction, }; }; + +const isBatchProcessed = async (batchNumber: any) => { + const providerStore = useZkSyncProviderStore(); + try { + const totalBatchesExecuted = await providerStore.requestProvider().getTotalBatchesExecuted(); + + return batchNumber <= BigNumber.from(totalBatchesExecuted).toNumber(); + } catch (error) { + // eslint-disable-next-line no-console + console.error("Error fetching total batches executed:", error); + return false; + } +}; diff --git a/package.json b/package.json index 742334ac4..079b00063 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "vite": "^3.0.0", "vue-tippy": "^6.0.0", "web3-avatar-vue": "^1.0.0", - "zksync-ethers": "^5.9.1" + "zksync-ethers": "../zksync-ethers/build" }, "overrides": { "vue": "latest"