Skip to content

Commit 5970df9

Browse files
fix useReadContract type hinting
1 parent a0835f7 commit 5970df9

File tree

4 files changed

+55
-45
lines changed

4 files changed

+55
-45
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Abi, AbiFunction } from "abitype";
2+
import type { ThirdwebContract } from "./contract.js";
3+
4+
export type AbiOfLength<TLength> = { length: TLength };
5+
6+
export type AsyncGetAbiFunctionFromContract<TAbi extends Abi> = (
7+
contract: ThirdwebContract<TAbi>,
8+
) => Promise<AbiFunction>;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { Abi } from "abitype";
2+
import type { BaseTransactionOptions } from "../transaction/types.js";
3+
4+
export type Extension<TAbi extends Abi, TParams extends object, TResult> = (
5+
options: BaseTransactionOptions<TParams, TAbi>,
6+
) => Promise<TResult>;

packages/thirdweb/src/react/core/hooks/contract/useReadContract.ts

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
useQuery,
55
} from "@tanstack/react-query";
66
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";
89
import {
910
type ReadContractOptions,
1011
type ReadContractResult,
@@ -17,17 +18,13 @@ import type {
1718
import type { PreparedMethod } from "../../../../utils/abi/prepare-method.js";
1819
import { getFunctionId } from "../../../../utils/function-id.js";
1920
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";
2622

2723
/**
2824
* A hook to read state from a contract that automatically updates when the contract changes.
2925
*
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.
3128
*
3229
* @param options - The options for reading from a contract
3330
* @returns a UseQueryResult object.
@@ -52,20 +49,19 @@ type PickedQueryOptions = {
5249
* @contract
5350
*/
5451
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>
5754
? AbiFunction | string
58-
: ExtractAbiFunctionNames<abi>,
55+
: ExtractAbiFunctionNames<TAbi>,
5956
>(
60-
options: ReadContractOptions<abi, method> & {
61-
queryOptions?: PickedQueryOptions;
62-
},
57+
options: WithPickedOnceQueryOptions<ReadContractOptions<TAbi, TMethod>>,
6358
): UseQueryResult<
64-
ReadContractResult<PreparedMethod<ParseMethod<abi, method>>[2]>
59+
ReadContractResult<PreparedMethod<ParseMethod<TAbi, TMethod>>[2]>
6560
>;
6661
/**
6762
* 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.
6965
*
7066
* @param extension - An extension to call.
7167
* @param options - The read extension params.
@@ -82,37 +78,29 @@ export function useReadContract<
8278
* ```
8379
*/
8480
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,
8884
>(
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>;
9488

9589
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>
10092
?
10193
| AbiFunction
10294
| `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,
10799
>(
108100
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>>,
116104
) {
117105
// extension case
118106
if (typeof extensionOrOptions === "function") {
@@ -131,13 +119,10 @@ export function useReadContract<
131119
getFunctionId(extensionOrOptions),
132120
stringify(params),
133121
] 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 }),
136123
...queryOptions,
137124
});
138125

139-
// TODO - FIX LATER
140-
// biome-ignore lint/correctness/useHookAtTopLevel: <explanation>
141126
return useQuery(query);
142127
}
143128
// raw tx case
@@ -156,8 +141,6 @@ export function useReadContract<
156141
...queryOptions,
157142
});
158143

159-
// TODO - FIX LATER
160-
// biome-ignore lint/correctness/useHookAtTopLevel: <explanation>
161144
return useQuery(query);
162145
}
163146

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Prettify } from "../../../utils/type-utils.js";
2+
3+
type BasePickedQueryOptions<T = {}> = T & {
4+
enabled?: boolean;
5+
};
6+
7+
type PickedOnceQueryOptions = Prettify<
8+
BasePickedQueryOptions & { refetchInterval?: number; retry?: number }
9+
>;
10+
11+
export type WithPickedOnceQueryOptions<T> = T & {
12+
queryOptions?: PickedOnceQueryOptions;
13+
};

0 commit comments

Comments
 (0)