Skip to content

Commit

Permalink
show blockexplorer link when transaction is reverted (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 committed Aug 19, 2024
1 parent b6d73d2 commit a132cce
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/nextjs/hooks/scaffold-eth/useTransactor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getPublicClient } from "@wagmi/core";
import { Hash, SendTransactionParameters, WalletClient } from "viem";
import { Hash, SendTransactionParameters, TransactionReceipt, WalletClient } from "viem";
import { Config, useWalletClient } from "wagmi";
import { SendTransactionMutate } from "wagmi/query";
import { wagmiConfig } from "~~/services/web3/wagmiConfig";
Expand Down Expand Up @@ -48,6 +48,8 @@ export const useTransactor = (_walletClient?: WalletClient): TransactionFunc =>

let notificationId = null;
let transactionHash: Hash | undefined = undefined;
let transactionReceipt: TransactionReceipt | undefined;
let blockExplorerTxURL = "";
try {
const network = await walletClient.getChainId();
// Get full transaction from public client
Expand All @@ -65,13 +67,13 @@ export const useTransactor = (_walletClient?: WalletClient): TransactionFunc =>
}
notification.remove(notificationId);

const blockExplorerTxURL = network ? getBlockExplorerTxLink(network, transactionHash) : "";
blockExplorerTxURL = network ? getBlockExplorerTxLink(network, transactionHash) : "";

notificationId = notification.loading(
<TxnNotification message="Waiting for transaction to complete." blockExplorerLink={blockExplorerTxURL} />,
);

const transactionReceipt = await publicClient.waitForTransactionReceipt({
transactionReceipt = await publicClient.waitForTransactionReceipt({
hash: transactionHash,
confirmations: options?.blockConfirmations,
});
Expand All @@ -93,6 +95,13 @@ export const useTransactor = (_walletClient?: WalletClient): TransactionFunc =>
}
console.error("⚡️ ~ file: useTransactor.ts ~ error", error);
const message = getParsedError(error);

// if receipt was reverted, show notification with block explorer link and return error
if (transactionReceipt?.status === "reverted") {
notification.error(<TxnNotification message={message} blockExplorerLink={blockExplorerTxURL} />);
throw error;
}

notification.error(message);
throw error;
}
Expand Down

0 comments on commit a132cce

Please sign in to comment.