From 31b2285f23a713ab59e6264cb85bfec1c8765972 Mon Sep 17 00:00:00 2001 From: bigq Date: Sat, 22 Jul 2023 23:24:58 +0200 Subject: [PATCH] feat: catch error if tx hangs --- front/src/utils/useContract.tsx | 38 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/front/src/utils/useContract.tsx b/front/src/utils/useContract.tsx index 300dc16..4a5f6d3 100644 --- a/front/src/utils/useContract.tsx +++ b/front/src/utils/useContract.tsx @@ -75,23 +75,27 @@ export default function useContract({ hash: `0x${string}` ): Promise { let txReceipt: TransactionReceipt | undefined; - if (chain.id === 5151111) { - const timeout = new Promise((_, reject) => - setTimeout( - () => - reject( - new Error( - "Transaction timed-out: If you are running a local fork on Anvil please make sure to reset your wallet nonce. In metamask: Go to settings > advanced > clear activity and nonce data" - ) - ), - 10000 - ) - ); - const txReceiptPromise = hash && waitForTransaction({ hash: hash }); - const race = await Promise.race([txReceiptPromise, timeout]); - txReceipt = race as TransactionReceipt; - } else { - txReceipt = hash && (await waitForTransaction({ hash: hash })); + try { + if (chain.id === 5151111) { + const timeout = new Promise((_, reject) => + setTimeout( + () => + reject( + new Error( + "Transaction timed-out: If you are running a local fork on Anvil please make sure to reset your wallet nonce. In metamask: Go to settings > advanced > clear activity and nonce data" + ) + ), + 10000 + ) + ); + const txReceiptPromise = hash && waitForTransaction({ hash: hash }); + const race = await Promise.race([txReceiptPromise, timeout]); + txReceipt = race as TransactionReceipt; + } else { + txReceipt = hash && (await waitForTransaction({ hash: hash })); + } + } catch (e: any) { + setError(formatError(e)); } return txReceipt; }