Skip to content

Commit

Permalink
Allow to use useTransactor hook with contract.write.method (#623)
Browse files Browse the repository at this point in the history
Co-authored-by: Shiv Bhonde <[email protected]>
  • Loading branch information
damianmarti and technophile-04 authored Nov 25, 2023
1 parent 6053ad5 commit 55ae5a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const useScaffoldContractWrite = <
if (wagmiContractWrite.writeAsync) {
try {
setIsMining(true);
await writeTx(
const writeTxResult = await writeTx(
() =>
wagmiContractWrite.writeAsync({
args: newArgs ?? args,
Expand All @@ -76,6 +76,8 @@ export const useScaffoldContractWrite = <
}),
{ onBlockConfirmation, blockConfirmations },
);

return writeTxResult;
} catch (e: any) {
const message = getParsedError(e);
notification.error(message);
Expand Down
9 changes: 7 additions & 2 deletions packages/nextjs/hooks/scaffold-eth/useTransactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getParsedError } from "~~/components/scaffold-eth";
import { getBlockExplorerTxLink, notification } from "~~/utils/scaffold-eth";

type TransactionFunc = (
tx: (() => Promise<WriteContractResult>) | SendTransactionParameters,
tx: (() => Promise<WriteContractResult>) | (() => Promise<Hash>) | SendTransactionParameters,
options?: {
onBlockConfirmation?: (txnReceipt: TransactionReceipt) => void;
blockConfirmations?: number;
Expand Down Expand Up @@ -57,7 +57,12 @@ export const useTransactor = (_walletClient?: WalletClient): TransactionFunc =>
notificationId = notification.loading(<TxnNotification message="Awaiting for user confirmation" />);
if (typeof tx === "function") {
// Tx is already prepared by the caller
transactionHash = (await tx()).hash;
const result = await tx();
if (typeof result === "string") {
transactionHash = result;
} else {
transactionHash = result.hash;
}
} else if (tx != null) {
transactionHash = await walletClient.sendTransaction(tx);
} else {
Expand Down

0 comments on commit 55ae5a4

Please sign in to comment.