-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
useReadContract.ts
99 lines (91 loc) · 3.03 KB
/
useReadContract.ts
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
'use client'
import type {
Config,
ReadContractErrorType,
ResolvedRegister,
} from '@wagmi/core'
import type { UnionCompute } from '@wagmi/core/internal'
import {
type ReadContractData,
type ReadContractOptions,
type ReadContractQueryFnData,
type ReadContractQueryKey,
readContractQueryOptions,
structuralSharing,
} from '@wagmi/core/query'
import type { Abi, ContractFunctionArgs, ContractFunctionName, Hex } from 'viem'
import type { ConfigParameter, QueryParameter } from '../types/properties.js'
import { type UseQueryReturnType, useQuery } from '../utils/query.js'
import { useChainId } from './useChainId.js'
import { useConfig } from './useConfig.js'
export type UseReadContractParameters<
abi extends Abi | readonly unknown[] = Abi,
functionName extends ContractFunctionName<
abi,
'pure' | 'view'
> = ContractFunctionName<abi, 'pure' | 'view'>,
args extends ContractFunctionArgs<
abi,
'pure' | 'view',
functionName
> = ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
config extends Config = Config,
selectData = ReadContractData<abi, functionName, args>,
> = UnionCompute<
ReadContractOptions<abi, functionName, args, config> &
ConfigParameter<config> &
QueryParameter<
ReadContractQueryFnData<abi, functionName, args>,
ReadContractErrorType,
selectData,
ReadContractQueryKey<abi, functionName, args, config>
>
>
export type UseReadContractReturnType<
abi extends Abi | readonly unknown[] = Abi,
functionName extends ContractFunctionName<
abi,
'pure' | 'view'
> = ContractFunctionName<abi, 'pure' | 'view'>,
args extends ContractFunctionArgs<
abi,
'pure' | 'view',
functionName
> = ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
selectData = ReadContractData<abi, functionName, args>,
> = UseQueryReturnType<selectData, ReadContractErrorType>
/** https://wagmi.sh/react/api/hooks/useReadContract */
export function useReadContract<
const abi extends Abi | readonly unknown[],
functionName extends ContractFunctionName<abi, 'pure' | 'view'>,
args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
config extends Config = ResolvedRegister['config'],
selectData = ReadContractData<abi, functionName, args>,
>(
parameters: UseReadContractParameters<
abi,
functionName,
args,
config,
selectData
> = {} as any,
): UseReadContractReturnType<abi, functionName, args, selectData> {
const { abi, address, functionName, query = {} } = parameters
// @ts-ignore
const code = parameters.code as Hex | undefined
const config = useConfig(parameters)
const chainId = useChainId({ config })
const options = readContractQueryOptions<config, abi, functionName, args>(
config,
{ ...(parameters as any), chainId: parameters.chainId ?? chainId },
)
const enabled = Boolean(
(address || code) && abi && functionName && (query.enabled ?? true),
)
return useQuery({
...query,
...options,
enabled,
structuralSharing: query.structuralSharing ?? structuralSharing,
})
}