viem 0.0.0-w-20230818210302 → 0.0.0-w-20230820223631

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/cjs/actions/getContract.js.map +1 -1
  2. package/dist/cjs/actions/public/multicall.js.map +1 -1
  3. package/dist/cjs/actions/public/readContract.js.map +1 -1
  4. package/dist/cjs/clients/decorators/public.js.map +1 -1
  5. package/dist/cjs/index.js.map +1 -1
  6. package/dist/esm/actions/getContract.js.map +1 -1
  7. package/dist/esm/actions/public/multicall.js.map +1 -1
  8. package/dist/esm/actions/public/readContract.js.map +1 -1
  9. package/dist/esm/clients/decorators/public.js.map +1 -1
  10. package/dist/esm/index.js.map +1 -1
  11. package/dist/types/actions/getContract.d.ts +9 -9
  12. package/dist/types/actions/getContract.d.ts.map +1 -1
  13. package/dist/types/actions/public/multicall.d.ts +4 -3
  14. package/dist/types/actions/public/multicall.d.ts.map +1 -1
  15. package/dist/types/actions/public/readContract.d.ts +4 -4
  16. package/dist/types/actions/public/readContract.d.ts.map +1 -1
  17. package/dist/types/clients/decorators/public.d.ts +2 -2
  18. package/dist/types/clients/decorators/public.d.ts.map +1 -1
  19. package/dist/types/index.d.ts +2 -2
  20. package/dist/types/index.d.ts.map +1 -1
  21. package/dist/types/types/contract.d.ts +15 -8
  22. package/dist/types/types/contract.d.ts.map +1 -1
  23. package/dist/types/types/multicall.d.ts +18 -35
  24. package/dist/types/types/multicall.d.ts.map +1 -1
  25. package/dist/types/types/utils.d.ts +1 -1
  26. package/dist/types/types/utils.d.ts.map +1 -1
  27. package/package.json +1 -1
  28. package/src/actions/getContract.ts +185 -177
  29. package/src/actions/public/multicall.ts +4 -5
  30. package/src/actions/public/readContract.ts +29 -11
  31. package/src/clients/decorators/public.ts +7 -4
  32. package/src/index.ts +4 -1
  33. package/src/types/contract.ts +80 -27
  34. package/src/types/multicall.ts +67 -121
  35. package/src/types/utils.ts +1 -1
@@ -17,6 +17,7 @@ import type { Transport } from '../clients/transports/createTransport.js'
17
17
  import type { Chain } from '../types/chain.js'
18
18
  import type {
19
19
  AbiEventParametersToPrimitiveTypes,
20
+ ContractFunctionName,
20
21
  MaybeExtractEventArgsFromAbi,
21
22
  } from '../types/contract.js'
22
23
  import type {
@@ -119,81 +120,17 @@ export type GetContractReturnType<
119
120
  : string,
120
121
  _Narrowable extends boolean = IsNarrowable<TAbi, Abi>,
121
122
  > = Prettify<
122
- (TPublicClient extends PublicClient
123
- ? (IsNever<_ReadFunctionNames> extends true
124
- ? unknown
125
- : {
126
- /**
127
- * Calls a read-only function on a contract, and returns the response.
128
- *
129
- * A "read-only" function (constant function) on a Solidity contract is denoted by a `view` or `pure` keyword. They can only read the state of the contract, and cannot make any changes to it. Since read-only methods do not change the state of the contract, they do not require any gas to be executed, and can be called by any user without the need to pay for gas.
130
- *
131
- * Internally, `read` uses a [Public Client](https://viem.sh/docs/clients/public.html) to call the [`call` action](https://viem.sh/docs/actions/public/call.html) with [ABI-encoded `data`](https://viem.sh/docs/contract/encodeFunctionData.html).
132
- *
133
- * @example
134
- * import { createPublicClient, getContract, http, parseAbi } from 'viem'
135
- * import { mainnet } from 'viem/chains'
136
- *
137
- * const publicClient = createPublicClient({
138
- * chain: mainnet,
139
- * transport: http(),
140
- * })
141
- * const contract = getContract({
142
- * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
143
- * abi: parseAbi([
144
- * 'function balanceOf(address owner) view returns (uint256)',
145
- * ]),
146
- * publicClient,
147
- * })
148
- * const result = await contract.read.balanceOf(['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'])
149
- * // 424122n
150
- */
151
- read: {
152
- [FunctionName in _ReadFunctionNames]: GetReadFunction<
153
- _Narrowable,
154
- TAbi,
155
- FunctionName
156
- >
157
- }
158
- }) &
159
- (IsNever<_WriteFunctionNames> extends true
123
+ Prettify<
124
+ (TPublicClient extends PublicClient
125
+ ? (IsNever<_ReadFunctionNames> extends true
160
126
  ? unknown
161
127
  : {
162
128
  /**
163
- * Estimates the gas necessary to complete a transaction without submitting it to the network.
164
- *
165
- * @example
166
- * import { createPublicClient, getContract, http, parseAbi } from 'viem'
167
- * import { mainnet } from 'viem/chains'
129
+ * Calls a read-only function on a contract, and returns the response.
168
130
  *
169
- * const publicClient = createPublicClient({
170
- * chain: mainnet,
171
- * transport: http(),
172
- * })
173
- * const contract = getContract({
174
- * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
175
- * abi: parseAbi(['function mint() public']),
176
- * publicClient,
177
- * })
178
- * const gas = await contract.estimateGas.mint({
179
- * account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
180
- * })
181
- */
182
- estimateGas: {
183
- [FunctionName in _WriteFunctionNames]: GetEstimateFunction<
184
- _Narrowable,
185
- TPublicClient['chain'],
186
- undefined,
187
- TAbi,
188
- FunctionName
189
- >
190
- }
191
- /**
192
- * Simulates/validates a contract interaction. This is useful for retrieving return data and revert reasons of contract write functions.
131
+ * A "read-only" function (constant function) on a Solidity contract is denoted by a `view` or `pure` keyword. They can only read the state of the contract, and cannot make any changes to it. Since read-only methods do not change the state of the contract, they do not require any gas to be executed, and can be called by any user without the need to pay for gas.
193
132
  *
194
- * This function does not require gas to execute and does not change the state of the blockchain. It is almost identical to [`readContract`](https://viem.sh/docs/contract/readContract.html), but also supports contract write functions.
195
- *
196
- * Internally, `simulate` uses a [Public Client](https://viem.sh/docs/clients/public.html) to call the [`call` action](https://viem.sh/docs/actions/public/call.html) with [ABI-encoded `data`](https://viem.sh/docs/contract/encodeFunctionData.html).
133
+ * Internally, `read` uses a [Public Client](https://viem.sh/docs/clients/public.html) to call the [`call` action](https://viem.sh/docs/actions/public/call.html) with [ABI-encoded `data`](https://viem.sh/docs/contract/encodeFunctionData.html).
197
134
  *
198
135
  * @example
199
136
  * import { createPublicClient, getContract, http, parseAbi } from 'viem'
@@ -205,156 +142,227 @@ export type GetContractReturnType<
205
142
  * })
206
143
  * const contract = getContract({
207
144
  * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
208
- * abi: parseAbi(['function mint() public']),
145
+ * abi: parseAbi([
146
+ * 'function balanceOf(address owner) view returns (uint256)',
147
+ * ]),
209
148
  * publicClient,
210
149
  * })
211
- * const result = await contract.simulate.mint({
212
- * account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
213
- * })
150
+ * const result = await contract.read.balanceOf(['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'])
151
+ * // 424122n
214
152
  */
215
- simulate: {
216
- [FunctionName in _WriteFunctionNames]: GetSimulateFunction<
153
+ read: {
154
+ [functionName in _ReadFunctionNames]: GetReadFunction<
217
155
  _Narrowable,
218
- TPublicClient['chain'],
219
156
  TAbi,
220
- FunctionName
157
+ functionName extends ContractFunctionName<
158
+ TAbi,
159
+ 'pure' | 'view'
160
+ >
161
+ ? functionName
162
+ : never
221
163
  >
222
164
  }
223
165
  }) &
224
- (IsNever<_EventNames> extends true
166
+ (IsNever<_WriteFunctionNames> extends true
167
+ ? unknown
168
+ : {
169
+ /**
170
+ * Estimates the gas necessary to complete a transaction without submitting it to the network.
171
+ *
172
+ * @example
173
+ * import { createPublicClient, getContract, http, parseAbi } from 'viem'
174
+ * import { mainnet } from 'viem/chains'
175
+ *
176
+ * const publicClient = createPublicClient({
177
+ * chain: mainnet,
178
+ * transport: http(),
179
+ * })
180
+ * const contract = getContract({
181
+ * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
182
+ * abi: parseAbi(['function mint() public']),
183
+ * publicClient,
184
+ * })
185
+ * const gas = await contract.estimateGas.mint({
186
+ * account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
187
+ * })
188
+ */
189
+ estimateGas: {
190
+ [functionName in _WriteFunctionNames]: GetEstimateFunction<
191
+ _Narrowable,
192
+ TPublicClient['chain'],
193
+ undefined,
194
+ TAbi,
195
+ functionName
196
+ >
197
+ }
198
+ /**
199
+ * Simulates/validates a contract interaction. This is useful for retrieving return data and revert reasons of contract write functions.
200
+ *
201
+ * This function does not require gas to execute and does not change the state of the blockchain. It is almost identical to [`readContract`](https://viem.sh/docs/contract/readContract.html), but also supports contract write functions.
202
+ *
203
+ * Internally, `simulate` uses a [Public Client](https://viem.sh/docs/clients/public.html) to call the [`call` action](https://viem.sh/docs/actions/public/call.html) with [ABI-encoded `data`](https://viem.sh/docs/contract/encodeFunctionData.html).
204
+ *
205
+ * @example
206
+ * import { createPublicClient, getContract, http, parseAbi } from 'viem'
207
+ * import { mainnet } from 'viem/chains'
208
+ *
209
+ * const publicClient = createPublicClient({
210
+ * chain: mainnet,
211
+ * transport: http(),
212
+ * })
213
+ * const contract = getContract({
214
+ * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
215
+ * abi: parseAbi(['function mint() public']),
216
+ * publicClient,
217
+ * })
218
+ * const result = await contract.simulate.mint({
219
+ * account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
220
+ * })
221
+ */
222
+ simulate: {
223
+ [functionName in _WriteFunctionNames]: GetSimulateFunction<
224
+ _Narrowable,
225
+ TPublicClient['chain'],
226
+ TAbi,
227
+ functionName
228
+ >
229
+ }
230
+ }) &
231
+ (IsNever<_EventNames> extends true
232
+ ? unknown
233
+ : {
234
+ /**
235
+ * Creates a Filter to retrieve event logs that can be used with [`getFilterChanges`](https://viem.sh/docs/actions/public/getFilterChanges.html) or [`getFilterLogs`](https://viem.sh/docs/actions/public/getFilterLogs.html).
236
+ *
237
+ * @example
238
+ * import { createPublicClient, getContract, http, parseAbi } from 'viem'
239
+ * import { mainnet } from 'viem/chains'
240
+ *
241
+ * const publicClient = createPublicClient({
242
+ * chain: mainnet,
243
+ * transport: http(),
244
+ * })
245
+ * const contract = getContract({
246
+ * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
247
+ * abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
248
+ * publicClient,
249
+ * })
250
+ * const filter = await contract.createEventFilter.Transfer()
251
+ */
252
+ createEventFilter: {
253
+ [EventName in _EventNames]: GetEventFilter<
254
+ _Narrowable,
255
+ TAbi,
256
+ EventName
257
+ >
258
+ }
259
+ /**
260
+ * Watches and returns emitted contract event logs.
261
+ *
262
+ * This Action will batch up all the event logs found within the [`pollingInterval`](https://viem.sh/docs/contract/watchContractEvent.html#pollinginterval-optional), and invoke them via [`onLogs`](https://viem.sh/docs/contract/watchContractEvent.html#onLogs).
263
+ *
264
+ * `watchEvent` will attempt to create an [Event Filter](https://viem.sh/docs/contract/createContractEventFilter.html) and listen to changes to the Filter per polling interval, however, if the RPC Provider does not support Filters (e.g. `eth_newFilter`), then `watchEvent` will fall back to using [`getLogs`](https://viem.sh/docs/actions/public/getLogs.html) instead.
265
+ *
266
+ * @example
267
+ * import { createPublicClient, getContract, http, parseAbi } from 'viem'
268
+ * import { mainnet } from 'viem/chains'
269
+ *
270
+ * const publicClient = createPublicClient({
271
+ * chain: mainnet,
272
+ * transport: http(),
273
+ * })
274
+ * const contract = getContract({
275
+ * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
276
+ * abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
277
+ * publicClient,
278
+ * })
279
+ * const filter = await contract.createEventFilter.Transfer()
280
+ * const unwatch = contract.watchEvent.Transfer(
281
+ * { from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b' },
282
+ * { onLogs: (logs) => console.log(logs) },
283
+ * )
284
+ */
285
+ watchEvent: {
286
+ [EventName in _EventNames]: GetWatchEvent<
287
+ _Narrowable,
288
+ TAbi,
289
+ EventName
290
+ >
291
+ }
292
+ })
293
+ : unknown) &
294
+ (TWalletClient extends WalletClient
295
+ ? IsNever<_WriteFunctionNames> extends true
225
296
  ? unknown
226
297
  : {
227
298
  /**
228
- * Creates a Filter to retrieve event logs that can be used with [`getFilterChanges`](https://viem.sh/docs/actions/public/getFilterChanges.html) or [`getFilterLogs`](https://viem.sh/docs/actions/public/getFilterLogs.html).
299
+ * Estimates the gas necessary to complete a transaction without submitting it to the network.
229
300
  *
230
301
  * @example
231
- * import { createPublicClient, getContract, http, parseAbi } from 'viem'
302
+ * import { createWalletClient, getContract, http, parseAbi } from 'viem'
232
303
  * import { mainnet } from 'viem/chains'
233
304
  *
234
- * const publicClient = createPublicClient({
305
+ * const walletClient = createWalletClient({
235
306
  * chain: mainnet,
236
307
  * transport: http(),
237
308
  * })
238
309
  * const contract = getContract({
239
310
  * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
240
- * abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
241
- * publicClient,
311
+ * abi: parseAbi(['function mint() public']),
312
+ * walletClient,
313
+ * })
314
+ * const gas = await contract.estimateGas.mint({
315
+ * account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
242
316
  * })
243
- * const filter = await contract.createEventFilter.Transfer()
244
317
  */
245
- createEventFilter: {
246
- [EventName in _EventNames]: GetEventFilter<
318
+ estimateGas: {
319
+ [functionName in _WriteFunctionNames]: GetEstimateFunction<
247
320
  _Narrowable,
321
+ TWalletClient['chain'],
322
+ TWalletClient['account'],
248
323
  TAbi,
249
- EventName
324
+ functionName
250
325
  >
251
326
  }
252
327
  /**
253
- * Watches and returns emitted contract event logs.
328
+ * Executes a write function on a contract.
254
329
  *
255
- * This Action will batch up all the event logs found within the [`pollingInterval`](https://viem.sh/docs/contract/watchContractEvent.html#pollinginterval-optional), and invoke them via [`onLogs`](https://viem.sh/docs/contract/watchContractEvent.html#onLogs).
330
+ * A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a [Transaction](https://viem.sh/docs/glossary/terms.html) is needed to be broadcast in order to change the state.
256
331
  *
257
- * `watchEvent` will attempt to create an [Event Filter](https://viem.sh/docs/contract/createContractEventFilter.html) and listen to changes to the Filter per polling interval, however, if the RPC Provider does not support Filters (e.g. `eth_newFilter`), then `watchEvent` will fall back to using [`getLogs`](https://viem.sh/docs/actions/public/getLogs.html) instead.
332
+ * Internally, `write` uses a [Wallet Client](https://viem.sh/docs/clients/wallet.html) to call the [`sendTransaction` action](https://viem.sh/docs/actions/wallet/sendTransaction.html) with [ABI-encoded `data`](https://viem.sh/docs/contract/encodeFunctionData.html).
333
+ *
334
+ * __Warning: The `write` internally sends a transaction – it does not validate if the contract write will succeed (the contract may throw an error). It is highly recommended to [simulate the contract write with `contract.simulate`](https://viem.sh/docs/contract/writeContract.html#usage) before you execute it.__
258
335
  *
259
336
  * @example
260
- * import { createPublicClient, getContract, http, parseAbi } from 'viem'
337
+ * import { createWalletClient, getContract, http, parseAbi } from 'viem'
261
338
  * import { mainnet } from 'viem/chains'
262
339
  *
263
- * const publicClient = createPublicClient({
340
+ * const walletClient = createWalletClient({
264
341
  * chain: mainnet,
265
342
  * transport: http(),
266
343
  * })
267
344
  * const contract = getContract({
268
345
  * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
269
- * abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
270
- * publicClient,
346
+ * abi: parseAbi(['function mint(uint32 tokenId) nonpayable']),
347
+ * walletClient,
348
+ * })
349
+ * const hash = await contract.write.min([69420], {
350
+ * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
271
351
  * })
272
- * const filter = await contract.createEventFilter.Transfer()
273
- * const unwatch = contract.watchEvent.Transfer(
274
- * { from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b' },
275
- * { onLogs: (logs) => console.log(logs) },
276
- * )
277
352
  */
278
- watchEvent: {
279
- [EventName in _EventNames]: GetWatchEvent<
353
+ write: {
354
+ [functionName in _WriteFunctionNames]: GetWriteFunction<
280
355
  _Narrowable,
356
+ TWalletClient['chain'],
357
+ TWalletClient['account'],
281
358
  TAbi,
282
- EventName
359
+ functionName
283
360
  >
284
361
  }
285
- })
286
- : unknown) &
287
- (TWalletClient extends WalletClient
288
- ? IsNever<_WriteFunctionNames> extends true
289
- ? unknown
290
- : {
291
- /**
292
- * Estimates the gas necessary to complete a transaction without submitting it to the network.
293
- *
294
- * @example
295
- * import { createWalletClient, getContract, http, parseAbi } from 'viem'
296
- * import { mainnet } from 'viem/chains'
297
- *
298
- * const walletClient = createWalletClient({
299
- * chain: mainnet,
300
- * transport: http(),
301
- * })
302
- * const contract = getContract({
303
- * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
304
- * abi: parseAbi(['function mint() public']),
305
- * walletClient,
306
- * })
307
- * const gas = await contract.estimateGas.mint({
308
- * account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
309
- * })
310
- */
311
- estimateGas: {
312
- [FunctionName in _WriteFunctionNames]: GetEstimateFunction<
313
- _Narrowable,
314
- TWalletClient['chain'],
315
- TWalletClient['account'],
316
- TAbi,
317
- FunctionName
318
- >
319
- }
320
- /**
321
- * Executes a write function on a contract.
322
- *
323
- * A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, and hence a [Transaction](https://viem.sh/docs/glossary/terms.html) is needed to be broadcast in order to change the state.
324
- *
325
- * Internally, `write` uses a [Wallet Client](https://viem.sh/docs/clients/wallet.html) to call the [`sendTransaction` action](https://viem.sh/docs/actions/wallet/sendTransaction.html) with [ABI-encoded `data`](https://viem.sh/docs/contract/encodeFunctionData.html).
326
- *
327
- * __Warning: The `write` internally sends a transaction – it does not validate if the contract write will succeed (the contract may throw an error). It is highly recommended to [simulate the contract write with `contract.simulate`](https://viem.sh/docs/contract/writeContract.html#usage) before you execute it.__
328
- *
329
- * @example
330
- * import { createWalletClient, getContract, http, parseAbi } from 'viem'
331
- * import { mainnet } from 'viem/chains'
332
- *
333
- * const walletClient = createWalletClient({
334
- * chain: mainnet,
335
- * transport: http(),
336
- * })
337
- * const contract = getContract({
338
- * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
339
- * abi: parseAbi(['function mint(uint32 tokenId) nonpayable']),
340
- * walletClient,
341
- * })
342
- * const hash = await contract.write.min([69420], {
343
- * address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
344
- * })
345
- */
346
- write: {
347
- [FunctionName in _WriteFunctionNames]: GetWriteFunction<
348
- _Narrowable,
349
- TWalletClient['chain'],
350
- TWalletClient['account'],
351
- TAbi,
352
- FunctionName
353
- >
354
362
  }
355
- }
356
- : unknown)
357
- > & { address: TAddress; abi: TAbi }
363
+ : unknown)
364
+ > & { address: TAddress; abi: TAbi }
365
+ >
358
366
 
359
367
  /**
360
368
  * Gets type-safe interface for performing contract-related actions with a specific `abi` and `address`.
@@ -443,7 +451,7 @@ export function getContract<
443
451
  get(_, functionName: string) {
444
452
  return (
445
453
  ...parameters: [
446
- args?: readonly unknown[],
454
+ args?: readonly unknown[] | undefined,
447
455
  options?: UnionOmit<
448
456
  ReadContractParameters,
449
457
  'abi' | 'address' | 'functionName' | 'args'
@@ -671,7 +679,7 @@ export function getEventParameters(
671
679
  type GetReadFunction<
672
680
  Narrowable extends boolean,
673
681
  TAbi extends Abi | readonly unknown[],
674
- TFunctionName extends string,
682
+ TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>,
675
683
  TAbiFunction extends AbiFunction = TAbi extends Abi
676
684
  ? ExtractAbiFunction<TAbi, TFunctionName>
677
685
  : AbiFunction,
@@ -7,9 +7,9 @@ import { AbiDecodingZeroDataError } from '../../errors/abi.js'
7
7
  import type { BaseError } from '../../errors/base.js'
8
8
  import { RawContractError } from '../../errors/contract.js'
9
9
  import type { Chain } from '../../types/chain.js'
10
+ import type { ContractFunctionParameters } from '../../types/contract.js'
10
11
  import type { Hex } from '../../types/misc.js'
11
12
  import type {
12
- MulticallContract,
13
13
  MulticallContracts,
14
14
  MulticallResults,
15
15
  } from '../../types/multicall.js'
@@ -17,12 +17,11 @@ import { decodeFunctionResult } from '../../utils/abi/decodeFunctionResult.js'
17
17
  import { encodeFunctionData } from '../../utils/abi/encodeFunctionData.js'
18
18
  import { getChainContractAddress } from '../../utils/chain.js'
19
19
  import { getContractError } from '../../utils/errors/getContractError.js'
20
-
21
20
  import type { CallParameters } from './call.js'
22
21
  import { readContract } from './readContract.js'
23
22
 
24
23
  export type MulticallParameters<
25
- contracts extends readonly unknown[] = readonly MulticallContract[],
24
+ contracts extends readonly unknown[] = readonly ContractFunctionParameters[],
26
25
  allowFailure extends boolean = true,
27
26
  options extends {
28
27
  optional?: boolean
@@ -39,7 +38,7 @@ export type MulticallParameters<
39
38
  }
40
39
 
41
40
  export type MulticallReturnType<
42
- contracts extends readonly unknown[] = readonly MulticallContract[],
41
+ contracts extends readonly unknown[] = readonly ContractFunctionParameters[],
43
42
  allowFailure extends boolean = true,
44
43
  options extends {
45
44
  error?: Error
@@ -104,7 +103,7 @@ export async function multicall<
104
103
  blockTag,
105
104
  multicallAddress: multicallAddress_,
106
105
  } = parameters
107
- const contracts = parameters.contracts as MulticallContract[]
106
+ const contracts = parameters.contracts as ContractFunctionParameters[]
108
107
 
109
108
  const batchSize =
110
109
  batchSize_ ??
@@ -5,8 +5,10 @@ import type { Transport } from '../../clients/transports/createTransport.js'
5
5
  import type { BaseError } from '../../errors/base.js'
6
6
  import type { Chain } from '../../types/chain.js'
7
7
  import type {
8
- ContractFunctionConfig,
9
- ContractFunctionResult,
8
+ ContractFunctionArgs,
9
+ ContractFunctionName,
10
+ ContractFunctionParameters,
11
+ ContractFunctionReturnType,
10
12
  } from '../../types/contract.js'
11
13
  import {
12
14
  type DecodeFunctionResultParameters,
@@ -17,19 +19,34 @@ import {
17
19
  encodeFunctionData,
18
20
  } from '../../utils/abi/encodeFunctionData.js'
19
21
  import { getContractError } from '../../utils/errors/getContractError.js'
20
-
21
22
  import { type CallParameters, call } from './call.js'
22
23
 
23
24
  export type ReadContractParameters<
24
25
  abi extends Abi | readonly unknown[] = Abi,
25
- functionName extends string = string,
26
+ functionName extends ContractFunctionName<
27
+ abi,
28
+ 'pure' | 'view'
29
+ > = ContractFunctionName<abi, 'pure' | 'view'>,
30
+ args extends ContractFunctionArgs<
31
+ abi,
32
+ 'pure' | 'view',
33
+ functionName
34
+ > = ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
26
35
  > = Pick<CallParameters, 'account' | 'blockNumber' | 'blockTag'> &
27
- ContractFunctionConfig<abi, functionName, 'view' | 'pure'>
36
+ ContractFunctionParameters<abi, 'pure' | 'view', functionName, args>
28
37
 
29
38
  export type ReadContractReturnType<
30
- TAbi extends Abi | readonly unknown[] = Abi,
31
- TFunctionName extends string = string,
32
- > = ContractFunctionResult<TAbi, TFunctionName>
39
+ abi extends Abi | readonly unknown[] = Abi,
40
+ functionName extends ContractFunctionName<
41
+ abi,
42
+ 'pure' | 'view'
43
+ > = ContractFunctionName<abi, 'pure' | 'view'>,
44
+ args extends ContractFunctionArgs<
45
+ abi,
46
+ 'pure' | 'view',
47
+ functionName
48
+ > = ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
49
+ > = ContractFunctionReturnType<abi, 'pure' | 'view', functionName, args>
33
50
 
34
51
  /**
35
52
  * Calls a read-only function on a contract, and returns the response.
@@ -65,11 +82,12 @@ export type ReadContractReturnType<
65
82
  export async function readContract<
66
83
  chain extends Chain | undefined,
67
84
  const abi extends Abi | readonly unknown[],
68
- functionName extends string,
85
+ functionName extends ContractFunctionName<abi, 'pure' | 'view'>,
86
+ args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
69
87
  >(
70
88
  client: Client<Transport, chain>,
71
- parameters: ReadContractParameters<abi, functionName>,
72
- ): Promise<ReadContractReturnType<abi, functionName>> {
89
+ parameters: ReadContractParameters<abi, functionName, args>,
90
+ ): Promise<ReadContractReturnType<abi, functionName, args>> {
73
91
  const { abi, address, args, functionName, ...callRequest } = parameters
74
92
  const calldata = encodeFunctionData({
75
93
  abi,
@@ -200,6 +200,8 @@ import type { Account } from '../../types/account.js'
200
200
  import type { BlockNumber, BlockTag } from '../../types/block.js'
201
201
  import type { Chain } from '../../types/chain.js'
202
202
  import type {
203
+ ContractFunctionArgs,
204
+ ContractFunctionName,
203
205
  MaybeAbiEventName,
204
206
  MaybeExtractEventArgsFromAbi,
205
207
  } from '../../types/contract.js'
@@ -1169,11 +1171,12 @@ export type PublicActions<
1169
1171
  * // 424122n
1170
1172
  */
1171
1173
  readContract: <
1172
- const TAbi extends Abi | readonly unknown[],
1173
- TFunctionName extends string,
1174
+ const abi extends Abi | readonly unknown[],
1175
+ functionName extends ContractFunctionName<abi, 'pure' | 'view'>,
1176
+ args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
1174
1177
  >(
1175
- args: ReadContractParameters<TAbi, TFunctionName>,
1176
- ) => Promise<ReadContractReturnType<TAbi, TFunctionName>>
1178
+ args: ReadContractParameters<abi, functionName, args>,
1179
+ ) => Promise<ReadContractReturnType<abi, functionName, args>>
1177
1180
  /**
1178
1181
  * Simulates/validates a contract interaction. This is useful for retrieving **return data** and **revert reasons** of contract write functions.
1179
1182
  *
package/src/index.ts CHANGED
@@ -515,6 +515,10 @@ export { SizeExceedsPaddingSizeError } from './errors/data.js'
515
515
  export { UrlRequiredError } from './errors/transport.js'
516
516
  export type {
517
517
  AbiItem,
518
+ ContractFunctionParameters,
519
+ ContractFunctionReturnType,
520
+ ContractFunctionName,
521
+ ContractFunctionArgs,
518
522
  ContractParameters,
519
523
  ContractReturnType,
520
524
  ContractFunctionConfig,
@@ -636,7 +640,6 @@ export type { GetTransportConfig } from './types/transport.js'
636
640
  export type { HDKey } from '@scure/bip32'
637
641
  export type { Log } from './types/log.js'
638
642
  export type {
639
- MulticallContract,
640
643
  MulticallContracts,
641
644
  MulticallResponse,
642
645
  MulticallResults,