Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

block confirmation options for useScaffoldContractWrite #348

Merged
merged 4 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ const { writeAsync, isLoading, isMining } = useScaffoldContractWrite({
args: ["The value to set"],
// For payable functions, expressed in ETH
value: "0.01",
// The number of block confirmations to wait for before considering transaction to be confirmed (default : 1).
blockConfirmations: 1,
// The callback function to execute when the transaction is confirmed.
onBlockConfirmation: (txnReceipt) => {
console.log("Transaction blockHash", txnReceipt.blockHash);
},
});
```

Expand Down
3 changes: 3 additions & 0 deletions packages/nextjs/components/example-ui/ContractInteraction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const ContractInteraction = () => {
functionName: "setGreeting",
args: [newGreeting],
value: "0.01",
onBlockConfirmation: txnReceipt => {
console.log("📦 Transaction blockHash", txnReceipt.blockHash);
},
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const useScaffoldContractWrite = <
functionName,
args,
value,
onBlockConfirmation,
blockConfirmations,
...writeConfig
}: UseScaffoldWriteConfig<TContractName, TFunctionName>) => {
const { data: deployedContractData } = useDeployedContractInfo(contractName);
Expand Down Expand Up @@ -61,7 +63,7 @@ export const useScaffoldContractWrite = <
if (wagmiContractWrite.writeAsync) {
try {
setIsMining(true);
await writeTx(wagmiContractWrite.writeAsync());
await writeTx(wagmiContractWrite.writeAsync(), { onBlockConfirmation, blockConfirmations });
} catch (e: any) {
const message = getParsedEthersError(e);
notification.error(message);
Expand Down
17 changes: 8 additions & 9 deletions packages/nextjs/hooks/scaffold-eth/useTransactor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { getBlockExplorerTxLink, notification } from "~~/utils/scaffold-eth";

type TTransactionFunc = (
tx: Promise<SendTransactionResult> | Deferrable<TransactionRequest> | undefined,
callback?: ((_param: any) => void) | undefined,
options?: {
onBlockConfirmation?: (txnReceipt: TransactionReceipt) => void;
blockConfirmations?: number;
},
) => Promise<Record<string, any> | undefined>;

/**
Expand Down Expand Up @@ -39,15 +42,14 @@ export const useTransactor = (_signer?: Signer): TTransactionFunc => {
signer = data;
}

const result: TTransactionFunc = async (tx, callback) => {
const result: TTransactionFunc = async (tx, options) => {
if (!signer) {
notification.error("Wallet/Signer not connected");
console.error("⚡️ ~ file: useTransactor.tsx ~ error");
return;
}

let notificationId = null;
let transactionReceipt: TransactionReceipt | undefined;
let transactionResponse: SendTransactionResult | TransactionResponse | undefined;
try {
const provider = signer.provider;
Expand All @@ -69,7 +71,8 @@ export const useTransactor = (_signer?: Signer): TTransactionFunc => {
notificationId = notification.loading(
<TxnNotification message="Waiting for transaction to complete." blockExplorerLink={blockExplorerTxURL} />,
);
transactionReceipt = await transactionResponse.wait();

const transactionReceipt = await transactionResponse.wait(options?.blockConfirmations);
carletex marked this conversation as resolved.
Show resolved Hide resolved
notification.remove(notificationId);

notification.success(
Expand All @@ -79,11 +82,7 @@ export const useTransactor = (_signer?: Signer): TTransactionFunc => {
},
);

if (transactionReceipt) {
if (callback != null && transactionReceipt.blockHash != null && transactionReceipt.confirmations >= 1) {
callback({ ...transactionResponse, ...transactionReceipt });
}
}
if (options?.onBlockConfirmation) options.onBlockConfirmation(transactionReceipt);
} catch (error: any) {
if (notificationId) {
notification.remove(notificationId);
Expand Down
3 changes: 3 additions & 0 deletions packages/nextjs/utils/scaffold-eth/contract.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TransactionReceipt } from "@ethersproject/abstract-provider";
import {
Abi,
AbiParameterToPrimitiveType,
Expand Down Expand Up @@ -151,6 +152,8 @@ export type UseScaffoldWriteConfig<
> = {
contractName: TContractName;
value?: string;
onBlockConfirmation?: (txnReceipt: TransactionReceipt) => void;
blockConfirmations?: number;
} & IsContractsFileMissing<
Partial<UseContractWriteConfig> & { args?: unknown[] },
{
Expand Down