4
4
useQuery ,
5
5
} from "@tanstack/react-query" ;
6
6
import type { Abi , AbiFunction , ExtractAbiFunctionNames } from "abitype" ;
7
- import type { ThirdwebContract } from "../../../../contract/contract.js" ;
7
+ import type { AbiOfLength , AsyncGetAbiFunctionFromContract } from "../../../../contract/types.js" ;
8
+ import type { Extension } from "../../../../extensions/types.js" ;
8
9
import {
9
10
type ReadContractOptions ,
10
11
type ReadContractResult ,
@@ -17,17 +18,13 @@ import type {
17
18
import type { PreparedMethod } from "../../../../utils/abi/prepare-method.js" ;
18
19
import { getFunctionId } from "../../../../utils/function-id.js" ;
19
20
import { stringify } from "../../../../utils/json.js" ;
20
-
21
- type PickedQueryOptions = {
22
- enabled ?: boolean ;
23
- refetchInterval ?: number ;
24
- retry ?: number ;
25
- } ;
21
+ import type { WithPickedOnceQueryOptions } from "../types.js" ;
26
22
27
23
/**
28
24
* A hook to read state from a contract that automatically updates when the contract changes.
29
25
*
30
- * You can use raw read calls or read [extensions](https://portal.thirdweb.com/react/v5/extensions) to read from a contract.
26
+ * You can use raw read calls or read [extensions](https://portal.thirdweb.com/react/v5/extensions) to read from a
27
+ * contract.
31
28
*
32
29
* @param options - The options for reading from a contract
33
30
* @returns a UseQueryResult object.
@@ -52,20 +49,19 @@ type PickedQueryOptions = {
52
49
* @contract
53
50
*/
54
51
export function useReadContract <
55
- const abi extends Abi ,
56
- const method extends abi extends { length : 0 }
52
+ const TAbi extends Abi ,
53
+ const TMethod extends TAbi extends AbiOfLength < 0 >
57
54
? AbiFunction | string
58
- : ExtractAbiFunctionNames < abi > ,
55
+ : ExtractAbiFunctionNames < TAbi > ,
59
56
> (
60
- options : ReadContractOptions < abi , method > & {
61
- queryOptions ?: PickedQueryOptions ;
62
- } ,
57
+ options : WithPickedOnceQueryOptions < ReadContractOptions < TAbi , TMethod > > ,
63
58
) : UseQueryResult <
64
- ReadContractResult < PreparedMethod < ParseMethod < abi , method > > [ 2 ] >
59
+ ReadContractResult < PreparedMethod < ParseMethod < TAbi , TMethod > > [ 2 ] >
65
60
> ;
66
61
/**
67
62
* A hook to read state from a contract that automatically updates when the contract changes.
68
- * You can use raw read calls or read [extensions](https://portal.thirdweb.com/react/v5/extensions) to read from a contract.
63
+ * You can use raw read calls or read [extensions](https://portal.thirdweb.com/react/v5/extensions) to read from a
64
+ * contract.
69
65
*
70
66
* @param extension - An extension to call.
71
67
* @param options - The read extension params.
@@ -82,37 +78,29 @@ export function useReadContract<
82
78
* ```
83
79
*/
84
80
export function useReadContract <
85
- const abi extends Abi ,
86
- const params extends object ,
87
- result ,
81
+ const TAbi extends Abi ,
82
+ const TParams extends object ,
83
+ TResult ,
88
84
> (
89
- extension : ( options : BaseTransactionOptions < params , abi > ) => Promise < result > ,
90
- options : BaseTransactionOptions < params , abi > & {
91
- queryOptions ?: PickedQueryOptions ;
92
- } ,
93
- ) : UseQueryResult < result > ;
85
+ extension : Extension < TAbi , TParams , TResult > ,
86
+ options : WithPickedOnceQueryOptions < BaseTransactionOptions < TParams , TAbi > > ,
87
+ ) : UseQueryResult < TResult > ;
94
88
95
89
export function useReadContract <
96
- const abi extends Abi ,
97
- const method extends abi extends {
98
- length : 0 ;
99
- }
90
+ const TAbi extends Abi ,
91
+ const TMethod extends TAbi extends AbiOfLength < 0 >
100
92
?
101
93
| AbiFunction
102
94
| `function ${string } `
103
- | ( ( contract : ThirdwebContract < abi > ) => Promise < AbiFunction > )
104
- : ExtractAbiFunctionNames < abi > ,
105
- const params extends object ,
106
- result ,
95
+ | AsyncGetAbiFunctionFromContract < TAbi >
96
+ : ExtractAbiFunctionNames < TAbi > ,
97
+ const TParams extends object ,
98
+ TResult ,
107
99
> (
108
100
extensionOrOptions :
109
- | ( ( options : BaseTransactionOptions < params , abi > ) => Promise < result > )
110
- | ( ReadContractOptions < abi , method > & {
111
- queryOptions ?: PickedQueryOptions ;
112
- } ) ,
113
- options ?: BaseTransactionOptions < params , abi > & {
114
- queryOptions ?: PickedQueryOptions ;
115
- } ,
101
+ | Extension < TAbi , TParams , TResult >
102
+ | WithPickedOnceQueryOptions < ReadContractOptions < TAbi , TMethod > > ,
103
+ options ?: WithPickedOnceQueryOptions < BaseTransactionOptions < TParams , TAbi > > ,
116
104
) {
117
105
// extension case
118
106
if ( typeof extensionOrOptions === "function" ) {
@@ -131,13 +119,10 @@ export function useReadContract<
131
119
getFunctionId ( extensionOrOptions ) ,
132
120
stringify ( params ) ,
133
121
] as const ,
134
- // @ts -expect-error - TODO: clean up the type issues here
135
- queryFn : ( ) => extensionOrOptions ( { ...params , contract } ) ,
122
+ queryFn : ( ) => extensionOrOptions ( { ...params as TParams , contract } ) ,
136
123
...queryOptions ,
137
124
} ) ;
138
125
139
- // TODO - FIX LATER
140
- // biome-ignore lint/correctness/useHookAtTopLevel: <explanation>
141
126
return useQuery ( query ) ;
142
127
}
143
128
// raw tx case
@@ -156,8 +141,6 @@ export function useReadContract<
156
141
...queryOptions ,
157
142
} ) ;
158
143
159
- // TODO - FIX LATER
160
- // biome-ignore lint/correctness/useHookAtTopLevel: <explanation>
161
144
return useQuery ( query ) ;
162
145
}
163
146
0 commit comments