-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseScaffoldContractWrite.ts
More file actions
99 lines (93 loc) · 3.38 KB
/
Copy pathuseScaffoldContractWrite.ts
File metadata and controls
99 lines (93 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { useState } from "react";
import { Abi, ExtractAbiFunctionNames } from "abitype";
import { parseEther } from "viem";
import { useContractWrite, useNetwork } from "wagmi";
import { getParsedError } from "~~/components/scaffold-eth";
import { useDeployedContractInfo, useTransactor } from "~~/hooks/scaffold-eth";
import { getTargetNetwork, notification } from "~~/utils/scaffold-eth";
import { ContractAbi, ContractName, UseScaffoldWriteConfig } from "~~/utils/scaffold-eth/contract";
type UpdatedArgs = Parameters<ReturnType<typeof useContractWrite<Abi, string, undefined>>["writeAsync"]>[0];
/**
* @dev wrapper for wagmi's useContractWrite hook(with config prepared by usePrepareContractWrite hook) which loads in deployed contract abi and address automatically
* @param config - The config settings, including extra wagmi configuration
* @param config.contractName - deployed contract name
* @param config.functionName - name of the function to be called
* @param config.args - arguments for the function
* @param config.value - value in ETH that will be sent with transaction
*/
export const useScaffoldContractWrite = <
TContractName extends ContractName,
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, "nonpayable" | "payable">,
>({
contractName,
functionName,
args,
value,
onBlockConfirmation,
blockConfirmations,
...writeConfig
}: UseScaffoldWriteConfig<TContractName, TFunctionName>) => {
const { data: deployedContractData } = useDeployedContractInfo(contractName);
const { chain } = useNetwork();
const writeTx = useTransactor();
const [isMining, setIsMining] = useState(false);
const configuredNetwork = getTargetNetwork();
const wagmiContractWrite = useContractWrite({
chainId: configuredNetwork.id,
address: deployedContractData?.address,
abi: deployedContractData?.abi as Abi,
functionName: functionName as any,
args: args as unknown[],
value: value ? parseEther(value) : undefined,
...writeConfig,
});
const sendContractWriteTx = async ({
args: newArgs,
value: newValue,
...otherConfig
}: {
args?: UseScaffoldWriteConfig<TContractName, TFunctionName>["args"];
value?: UseScaffoldWriteConfig<TContractName, TFunctionName>["value"];
} & UpdatedArgs = {}) => {
if (!deployedContractData) {
notification.error("Target Contract is not deployed, did you forgot to run `yarn deploy`?");
return;
}
if (!chain?.id) {
notification.error("Please connect your wallet");
return;
}
if (chain?.id !== configuredNetwork.id) {
notification.error("You on the wrong network");
return;
}
if (wagmiContractWrite.writeAsync) {
try {
setIsMining(true);
await writeTx(
() =>
wagmiContractWrite.writeAsync({
args: newArgs ?? args,
value: newValue ? parseEther(newValue) : value && parseEther(value),
...otherConfig,
}),
{ onBlockConfirmation, blockConfirmations },
);
} catch (e: any) {
const message = getParsedError(e);
notification.error(message);
} finally {
setIsMining(false);
}
} else {
notification.error("Contract writer error. Try again.");
return;
}
};
return {
...wagmiContractWrite,
isMining,
// Overwrite wagmi's write async
writeAsync: sendContractWriteTx,
};
};