Skip to content

Commit

Permalink
feat: catch error if tx hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
yum0e committed Jul 22, 2023
1 parent 24d9d7e commit 31b2285
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions front/src/utils/useContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,27 @@ export default function useContract({
hash: `0x${string}`
): Promise<TransactionReceipt | undefined> {
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;
}
Expand Down

0 comments on commit 31b2285

Please sign in to comment.