-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseScaffoldContractRead.ts
More file actions
46 lines (44 loc) · 1.66 KB
/
useScaffoldContractRead.ts
File metadata and controls
46 lines (44 loc) · 1.66 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
import type { ExtractAbiFunctionNames } from "abitype";
import { useContractRead } from "wagmi";
import { useDeployedContractInfo } from "~~/hooks/scaffold-eth";
import { getTargetNetwork } from "~~/utils/scaffold-eth";
import {
AbiFunctionReturnType,
ContractAbi,
ContractName,
UseScaffoldReadConfig,
} from "~~/utils/scaffold-eth/contract";
/**
* @dev wrapper for wagmi's useContractRead hook which loads in deployed contract contract abi, 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 - args to be passed to the function call
*/
export const useScaffoldContractRead = <
TContractName extends ContractName,
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, "pure" | "view">,
>({
contractName,
functionName,
args,
...readConfig
}: UseScaffoldReadConfig<TContractName, TFunctionName>) => {
const { data: deployedContract } = useDeployedContractInfo(contractName);
return useContractRead({
chainId: getTargetNetwork().id,
functionName,
address: deployedContract?.address,
abi: deployedContract?.abi,
watch: true,
args,
enabled: !Array.isArray(args) || !args.some(arg => arg === undefined),
...(readConfig as any),
}) as Omit<ReturnType<typeof useContractRead>, "data" | "refetch"> & {
data: AbiFunctionReturnType<ContractAbi, TFunctionName> | undefined;
refetch: (options?: {
throwOnError: boolean;
cancelRefetch: boolean;
}) => Promise<AbiFunctionReturnType<ContractAbi, TFunctionName>>;
};
};