viem 0.0.0-w-20230818230619 → 0.0.0-w-20230821160922
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.
- package/dist/cjs/actions/getContract.js.map +1 -1
- package/dist/cjs/actions/public/estimateContractGas.js +7 -14
- package/dist/cjs/actions/public/estimateContractGas.js.map +1 -1
- package/dist/cjs/actions/public/multicall.js.map +1 -1
- package/dist/cjs/actions/public/readContract.js +6 -10
- package/dist/cjs/actions/public/readContract.js.map +1 -1
- package/dist/cjs/actions/public/simulateContract.js +6 -9
- package/dist/cjs/actions/public/simulateContract.js.map +1 -1
- package/dist/cjs/actions/wallet/writeContract.js +1 -5
- package/dist/cjs/actions/wallet/writeContract.js.map +1 -1
- package/dist/cjs/clients/decorators/public.js.map +1 -1
- package/dist/cjs/clients/decorators/wallet.js.map +1 -1
- package/dist/esm/actions/getContract.js.map +1 -1
- package/dist/esm/actions/public/estimateContractGas.js +8 -15
- package/dist/esm/actions/public/estimateContractGas.js.map +1 -1
- package/dist/esm/actions/public/multicall.js.map +1 -1
- package/dist/esm/actions/public/readContract.js +8 -12
- package/dist/esm/actions/public/readContract.js.map +1 -1
- package/dist/esm/actions/public/simulateContract.js +8 -11
- package/dist/esm/actions/public/simulateContract.js.map +1 -1
- package/dist/esm/actions/wallet/writeContract.js +2 -6
- package/dist/esm/actions/wallet/writeContract.js.map +1 -1
- package/dist/esm/clients/decorators/public.js.map +1 -1
- package/dist/esm/clients/decorators/wallet.js +1 -0
- package/dist/esm/clients/decorators/wallet.js.map +1 -1
- package/dist/esm/types/contract.js +1 -1
- package/dist/esm/types/contract.js.map +1 -1
- package/dist/types/actions/getContract.d.ts +13 -13
- package/dist/types/actions/getContract.d.ts.map +1 -1
- package/dist/types/actions/public/estimateContractGas.d.ts +3 -3
- package/dist/types/actions/public/estimateContractGas.d.ts.map +1 -1
- package/dist/types/actions/public/multicall.d.ts +4 -3
- package/dist/types/actions/public/multicall.d.ts.map +1 -1
- package/dist/types/actions/public/readContract.d.ts +4 -4
- package/dist/types/actions/public/readContract.d.ts.map +1 -1
- package/dist/types/actions/public/simulateContract.d.ts +11 -11
- package/dist/types/actions/public/simulateContract.d.ts.map +1 -1
- package/dist/types/actions/wallet/writeContract.d.ts +3 -3
- package/dist/types/actions/wallet/writeContract.d.ts.map +1 -1
- package/dist/types/clients/decorators/public.d.ts +4 -4
- package/dist/types/clients/decorators/public.d.ts.map +1 -1
- package/dist/types/clients/decorators/wallet.d.ts +2 -1
- package/dist/types/clients/decorators/wallet.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/contract.d.ts +15 -28
- package/dist/types/types/contract.d.ts.map +1 -1
- package/dist/types/types/multicall.d.ts +18 -35
- package/dist/types/types/multicall.d.ts.map +1 -1
- package/dist/types/types/utils.d.ts +4 -0
- package/dist/types/types/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/actions/getContract.ts +251 -191
- package/src/actions/public/estimateContractGas.ts +48 -38
- package/src/actions/public/multicall.ts +4 -5
- package/src/actions/public/readContract.ts +39 -33
- package/src/actions/public/simulateContract.ts +91 -59
- package/src/actions/wallet/writeContract.ts +28 -15
- package/src/clients/decorators/public.ts +32 -12
- package/src/clients/decorators/wallet.ts +10 -4
- package/src/index.ts +4 -4
- package/src/types/contract.ts +98 -117
- package/src/types/multicall.ts +67 -121
- package/src/types/utils.ts +9 -0
@@ -17,6 +17,8 @@ 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
|
+
ContractFunctionArgs,
|
21
|
+
ContractFunctionName,
|
20
22
|
MaybeExtractEventArgsFromAbi,
|
21
23
|
} from '../types/contract.js'
|
22
24
|
import type {
|
@@ -119,81 +121,17 @@ export type GetContractReturnType<
|
|
119
121
|
: string,
|
120
122
|
_Narrowable extends boolean = IsNarrowable<TAbi, Abi>,
|
121
123
|
> = Prettify<
|
122
|
-
|
123
|
-
|
124
|
-
|
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
|
124
|
+
Prettify<
|
125
|
+
(TPublicClient extends PublicClient
|
126
|
+
? (IsNever<_ReadFunctionNames> extends true
|
160
127
|
? unknown
|
161
128
|
: {
|
162
129
|
/**
|
163
|
-
*
|
130
|
+
* Calls a read-only function on a contract, and returns the response.
|
164
131
|
*
|
165
|
-
*
|
166
|
-
* import { createPublicClient, getContract, http, parseAbi } from 'viem'
|
167
|
-
* import { mainnet } from 'viem/chains'
|
132
|
+
* 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.
|
168
133
|
*
|
169
|
-
*
|
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.
|
193
|
-
*
|
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).
|
134
|
+
* 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
135
|
*
|
198
136
|
* @example
|
199
137
|
* import { createPublicClient, getContract, http, parseAbi } from 'viem'
|
@@ -205,156 +143,247 @@ export type GetContractReturnType<
|
|
205
143
|
* })
|
206
144
|
* const contract = getContract({
|
207
145
|
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
208
|
-
* abi: parseAbi([
|
146
|
+
* abi: parseAbi([
|
147
|
+
* 'function balanceOf(address owner) view returns (uint256)',
|
148
|
+
* ]),
|
209
149
|
* publicClient,
|
210
150
|
* })
|
211
|
-
* const result = await contract.
|
212
|
-
*
|
213
|
-
* })
|
151
|
+
* const result = await contract.read.balanceOf(['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'])
|
152
|
+
* // 424122n
|
214
153
|
*/
|
215
|
-
|
216
|
-
[
|
154
|
+
read: {
|
155
|
+
[functionName in _ReadFunctionNames]: GetReadFunction<
|
217
156
|
_Narrowable,
|
218
|
-
TPublicClient['chain'],
|
219
157
|
TAbi,
|
220
|
-
|
158
|
+
functionName extends ContractFunctionName<
|
159
|
+
TAbi,
|
160
|
+
'pure' | 'view'
|
161
|
+
>
|
162
|
+
? functionName
|
163
|
+
: never
|
221
164
|
>
|
222
165
|
}
|
223
166
|
}) &
|
224
|
-
|
167
|
+
(IsNever<_WriteFunctionNames> extends true
|
168
|
+
? unknown
|
169
|
+
: {
|
170
|
+
/**
|
171
|
+
* Estimates the gas necessary to complete a transaction without submitting it to the network.
|
172
|
+
*
|
173
|
+
* @example
|
174
|
+
* import { createPublicClient, getContract, http, parseAbi } from 'viem'
|
175
|
+
* import { mainnet } from 'viem/chains'
|
176
|
+
*
|
177
|
+
* const publicClient = createPublicClient({
|
178
|
+
* chain: mainnet,
|
179
|
+
* transport: http(),
|
180
|
+
* })
|
181
|
+
* const contract = getContract({
|
182
|
+
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
183
|
+
* abi: parseAbi(['function mint() public']),
|
184
|
+
* publicClient,
|
185
|
+
* })
|
186
|
+
* const gas = await contract.estimateGas.mint({
|
187
|
+
* account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
|
188
|
+
* })
|
189
|
+
*/
|
190
|
+
estimateGas: {
|
191
|
+
[functionName in _WriteFunctionNames]: GetEstimateFunction<
|
192
|
+
_Narrowable,
|
193
|
+
TPublicClient['chain'],
|
194
|
+
undefined,
|
195
|
+
TAbi,
|
196
|
+
functionName extends ContractFunctionName<
|
197
|
+
TAbi,
|
198
|
+
'nonpayable' | 'payable'
|
199
|
+
>
|
200
|
+
? functionName
|
201
|
+
: never
|
202
|
+
>
|
203
|
+
}
|
204
|
+
/**
|
205
|
+
* Simulates/validates a contract interaction. This is useful for retrieving return data and revert reasons of contract write functions.
|
206
|
+
*
|
207
|
+
* 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.
|
208
|
+
*
|
209
|
+
* 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).
|
210
|
+
*
|
211
|
+
* @example
|
212
|
+
* import { createPublicClient, getContract, http, parseAbi } from 'viem'
|
213
|
+
* import { mainnet } from 'viem/chains'
|
214
|
+
*
|
215
|
+
* const publicClient = createPublicClient({
|
216
|
+
* chain: mainnet,
|
217
|
+
* transport: http(),
|
218
|
+
* })
|
219
|
+
* const contract = getContract({
|
220
|
+
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
221
|
+
* abi: parseAbi(['function mint() public']),
|
222
|
+
* publicClient,
|
223
|
+
* })
|
224
|
+
* const result = await contract.simulate.mint({
|
225
|
+
* account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
|
226
|
+
* })
|
227
|
+
*/
|
228
|
+
simulate: {
|
229
|
+
[functionName in _WriteFunctionNames]: GetSimulateFunction<
|
230
|
+
_Narrowable,
|
231
|
+
TPublicClient['chain'],
|
232
|
+
TAbi,
|
233
|
+
functionName extends ContractFunctionName<
|
234
|
+
TAbi,
|
235
|
+
'nonpayable' | 'payable'
|
236
|
+
>
|
237
|
+
? functionName
|
238
|
+
: never
|
239
|
+
>
|
240
|
+
}
|
241
|
+
}) &
|
242
|
+
(IsNever<_EventNames> extends true
|
243
|
+
? unknown
|
244
|
+
: {
|
245
|
+
/**
|
246
|
+
* 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).
|
247
|
+
*
|
248
|
+
* @example
|
249
|
+
* import { createPublicClient, getContract, http, parseAbi } from 'viem'
|
250
|
+
* import { mainnet } from 'viem/chains'
|
251
|
+
*
|
252
|
+
* const publicClient = createPublicClient({
|
253
|
+
* chain: mainnet,
|
254
|
+
* transport: http(),
|
255
|
+
* })
|
256
|
+
* const contract = getContract({
|
257
|
+
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
258
|
+
* abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
|
259
|
+
* publicClient,
|
260
|
+
* })
|
261
|
+
* const filter = await contract.createEventFilter.Transfer()
|
262
|
+
*/
|
263
|
+
createEventFilter: {
|
264
|
+
[EventName in _EventNames]: GetEventFilter<
|
265
|
+
_Narrowable,
|
266
|
+
TAbi,
|
267
|
+
EventName
|
268
|
+
>
|
269
|
+
}
|
270
|
+
/**
|
271
|
+
* Watches and returns emitted contract event logs.
|
272
|
+
*
|
273
|
+
* 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).
|
274
|
+
*
|
275
|
+
* `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.
|
276
|
+
*
|
277
|
+
* @example
|
278
|
+
* import { createPublicClient, getContract, http, parseAbi } from 'viem'
|
279
|
+
* import { mainnet } from 'viem/chains'
|
280
|
+
*
|
281
|
+
* const publicClient = createPublicClient({
|
282
|
+
* chain: mainnet,
|
283
|
+
* transport: http(),
|
284
|
+
* })
|
285
|
+
* const contract = getContract({
|
286
|
+
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
287
|
+
* abi: parseAbi(['event Transfer(address indexed, address indexed, uint256)']),
|
288
|
+
* publicClient,
|
289
|
+
* })
|
290
|
+
* const filter = await contract.createEventFilter.Transfer()
|
291
|
+
* const unwatch = contract.watchEvent.Transfer(
|
292
|
+
* { from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b' },
|
293
|
+
* { onLogs: (logs) => console.log(logs) },
|
294
|
+
* )
|
295
|
+
*/
|
296
|
+
watchEvent: {
|
297
|
+
[EventName in _EventNames]: GetWatchEvent<
|
298
|
+
_Narrowable,
|
299
|
+
TAbi,
|
300
|
+
EventName
|
301
|
+
>
|
302
|
+
}
|
303
|
+
})
|
304
|
+
: unknown) &
|
305
|
+
(TWalletClient extends WalletClient
|
306
|
+
? IsNever<_WriteFunctionNames> extends true
|
225
307
|
? unknown
|
226
308
|
: {
|
227
309
|
/**
|
228
|
-
*
|
310
|
+
* Estimates the gas necessary to complete a transaction without submitting it to the network.
|
229
311
|
*
|
230
312
|
* @example
|
231
|
-
* import {
|
313
|
+
* import { createWalletClient, getContract, http, parseAbi } from 'viem'
|
232
314
|
* import { mainnet } from 'viem/chains'
|
233
315
|
*
|
234
|
-
* const
|
316
|
+
* const walletClient = createWalletClient({
|
235
317
|
* chain: mainnet,
|
236
318
|
* transport: http(),
|
237
319
|
* })
|
238
320
|
* const contract = getContract({
|
239
321
|
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
240
|
-
* abi: parseAbi(['
|
241
|
-
*
|
322
|
+
* abi: parseAbi(['function mint() public']),
|
323
|
+
* walletClient,
|
324
|
+
* })
|
325
|
+
* const gas = await contract.estimateGas.mint({
|
326
|
+
* account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
|
242
327
|
* })
|
243
|
-
* const filter = await contract.createEventFilter.Transfer()
|
244
328
|
*/
|
245
|
-
|
246
|
-
[
|
329
|
+
estimateGas: {
|
330
|
+
[functionName in _WriteFunctionNames]: GetEstimateFunction<
|
247
331
|
_Narrowable,
|
332
|
+
TWalletClient['chain'],
|
333
|
+
TWalletClient['account'],
|
248
334
|
TAbi,
|
249
|
-
|
335
|
+
functionName extends ContractFunctionName<
|
336
|
+
TAbi,
|
337
|
+
'nonpayable' | 'payable'
|
338
|
+
>
|
339
|
+
? functionName
|
340
|
+
: never
|
250
341
|
>
|
251
342
|
}
|
252
343
|
/**
|
253
|
-
*
|
344
|
+
* Executes a write function on a contract.
|
345
|
+
*
|
346
|
+
* 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.
|
254
347
|
*
|
255
|
-
*
|
348
|
+
* 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).
|
256
349
|
*
|
257
|
-
* `
|
350
|
+
* __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
351
|
*
|
259
352
|
* @example
|
260
|
-
* import {
|
353
|
+
* import { createWalletClient, getContract, http, parseAbi } from 'viem'
|
261
354
|
* import { mainnet } from 'viem/chains'
|
262
355
|
*
|
263
|
-
* const
|
356
|
+
* const walletClient = createWalletClient({
|
264
357
|
* chain: mainnet,
|
265
358
|
* transport: http(),
|
266
359
|
* })
|
267
360
|
* const contract = getContract({
|
268
361
|
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
269
|
-
* abi: parseAbi(['
|
270
|
-
*
|
362
|
+
* abi: parseAbi(['function mint(uint32 tokenId) nonpayable']),
|
363
|
+
* walletClient,
|
364
|
+
* })
|
365
|
+
* const hash = await contract.write.min([69420], {
|
366
|
+
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
271
367
|
* })
|
272
|
-
* const filter = await contract.createEventFilter.Transfer()
|
273
|
-
* const unwatch = contract.watchEvent.Transfer(
|
274
|
-
* { from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b' },
|
275
|
-
* { onLogs: (logs) => console.log(logs) },
|
276
|
-
* )
|
277
368
|
*/
|
278
|
-
|
279
|
-
[
|
369
|
+
write: {
|
370
|
+
[functionName in _WriteFunctionNames]: GetWriteFunction<
|
280
371
|
_Narrowable,
|
372
|
+
TWalletClient['chain'],
|
373
|
+
TWalletClient['account'],
|
281
374
|
TAbi,
|
282
|
-
|
375
|
+
functionName extends ContractFunctionName<
|
376
|
+
TAbi,
|
377
|
+
'nonpayable' | 'payable'
|
378
|
+
>
|
379
|
+
? functionName
|
380
|
+
: never
|
283
381
|
>
|
284
382
|
}
|
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
383
|
}
|
320
|
-
|
321
|
-
|
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
|
-
}
|
355
|
-
}
|
356
|
-
: unknown)
|
357
|
-
> & { address: TAddress; abi: TAbi }
|
384
|
+
: unknown)
|
385
|
+
> & { address: TAddress; abi: TAbi }
|
386
|
+
>
|
358
387
|
|
359
388
|
/**
|
360
389
|
* Gets type-safe interface for performing contract-related actions with a specific `abi` and `address`.
|
@@ -443,7 +472,7 @@ export function getContract<
|
|
443
472
|
get(_, functionName: string) {
|
444
473
|
return (
|
445
474
|
...parameters: [
|
446
|
-
args?: readonly unknown[],
|
475
|
+
args?: readonly unknown[] | undefined,
|
447
476
|
options?: UnionOmit<
|
448
477
|
ReadContractParameters,
|
449
478
|
'abi' | 'address' | 'functionName' | 'args'
|
@@ -577,13 +606,8 @@ export function getContract<
|
|
577
606
|
address,
|
578
607
|
functionName,
|
579
608
|
args,
|
580
|
-
...options,
|
581
|
-
}
|
582
|
-
TAbi,
|
583
|
-
typeof functionName,
|
584
|
-
TChain,
|
585
|
-
TAccount
|
586
|
-
>)
|
609
|
+
...(options as any),
|
610
|
+
})
|
587
611
|
}
|
588
612
|
},
|
589
613
|
},
|
@@ -671,14 +695,19 @@ export function getEventParameters(
|
|
671
695
|
type GetReadFunction<
|
672
696
|
Narrowable extends boolean,
|
673
697
|
TAbi extends Abi | readonly unknown[],
|
674
|
-
TFunctionName extends
|
698
|
+
TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>,
|
699
|
+
TArgs extends ContractFunctionArgs<
|
700
|
+
TAbi,
|
701
|
+
'pure' | 'view',
|
702
|
+
TFunctionName
|
703
|
+
> = ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName>,
|
675
704
|
TAbiFunction extends AbiFunction = TAbi extends Abi
|
676
705
|
? ExtractAbiFunction<TAbi, TFunctionName>
|
677
706
|
: AbiFunction,
|
678
707
|
Args = AbiParametersToPrimitiveTypes<TAbiFunction['inputs']>,
|
679
708
|
Options = Prettify<
|
680
709
|
UnionOmit<
|
681
|
-
ReadContractParameters<TAbi, TFunctionName>,
|
710
|
+
ReadContractParameters<TAbi, TFunctionName, TArgs>,
|
682
711
|
'abi' | 'address' | 'args' | 'functionName'
|
683
712
|
>
|
684
713
|
>,
|
@@ -687,7 +716,7 @@ type GetReadFunction<
|
|
687
716
|
...parameters: Args extends readonly []
|
688
717
|
? [options?: Options]
|
689
718
|
: [args: Args, options?: Options]
|
690
|
-
) => Promise<ReadContractReturnType<TAbi, TFunctionName>>
|
719
|
+
) => Promise<ReadContractReturnType<TAbi, TFunctionName, TArgs>>
|
691
720
|
: (
|
692
721
|
...parameters:
|
693
722
|
| [options?: Options]
|
@@ -699,14 +728,25 @@ type GetEstimateFunction<
|
|
699
728
|
TChain extends Chain | undefined,
|
700
729
|
TAccount extends Account | undefined,
|
701
730
|
TAbi extends Abi | readonly unknown[],
|
702
|
-
TFunctionName extends
|
731
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
732
|
+
TArgs extends ContractFunctionArgs<
|
733
|
+
TAbi,
|
734
|
+
'nonpayable' | 'payable',
|
735
|
+
TFunctionName
|
736
|
+
> = ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName>,
|
703
737
|
TAbiFunction extends AbiFunction = TAbi extends Abi
|
704
738
|
? ExtractAbiFunction<TAbi, TFunctionName>
|
705
739
|
: AbiFunction,
|
706
740
|
Args = AbiParametersToPrimitiveTypes<TAbiFunction['inputs']>,
|
707
741
|
Options = Prettify<
|
708
742
|
UnionOmit<
|
709
|
-
EstimateContractGasParameters<
|
743
|
+
EstimateContractGasParameters<
|
744
|
+
TAbi,
|
745
|
+
TFunctionName,
|
746
|
+
TArgs,
|
747
|
+
TChain,
|
748
|
+
TAccount
|
749
|
+
>,
|
710
750
|
'abi' | 'address' | 'args' | 'functionName'
|
711
751
|
>
|
712
752
|
>,
|
@@ -742,7 +782,12 @@ type GetSimulateFunction<
|
|
742
782
|
Narrowable extends boolean,
|
743
783
|
TChain extends Chain | undefined,
|
744
784
|
TAbi extends Abi | readonly unknown[],
|
745
|
-
TFunctionName extends
|
785
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
786
|
+
TArgs extends ContractFunctionArgs<
|
787
|
+
TAbi,
|
788
|
+
'nonpayable' | 'payable',
|
789
|
+
TFunctionName
|
790
|
+
> = ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName>,
|
746
791
|
TAbiFunction extends AbiFunction = TAbi extends Abi
|
747
792
|
? ExtractAbiFunction<TAbi, TFunctionName>
|
748
793
|
: AbiFunction,
|
@@ -755,6 +800,7 @@ type GetSimulateFunction<
|
|
755
800
|
SimulateContractParameters<
|
756
801
|
TAbi,
|
757
802
|
TFunctionName,
|
803
|
+
TArgs,
|
758
804
|
TChain,
|
759
805
|
TChainOverride
|
760
806
|
>,
|
@@ -766,7 +812,13 @@ type GetSimulateFunction<
|
|
766
812
|
? [options?: Options]
|
767
813
|
: [args: Args, options?: Options]
|
768
814
|
) => Promise<
|
769
|
-
SimulateContractReturnType<
|
815
|
+
SimulateContractReturnType<
|
816
|
+
TAbi,
|
817
|
+
TFunctionName,
|
818
|
+
TArgs,
|
819
|
+
TChain,
|
820
|
+
TChainOverride
|
821
|
+
>
|
770
822
|
>
|
771
823
|
: <
|
772
824
|
TChainOverride extends Chain | undefined,
|
@@ -775,6 +827,7 @@ type GetSimulateFunction<
|
|
775
827
|
SimulateContractParameters<
|
776
828
|
TAbi,
|
777
829
|
TFunctionName,
|
830
|
+
TArgs,
|
778
831
|
TChain,
|
779
832
|
TChainOverride
|
780
833
|
>,
|
@@ -792,7 +845,12 @@ type GetWriteFunction<
|
|
792
845
|
TChain extends Chain | undefined,
|
793
846
|
TAccount extends Account | undefined,
|
794
847
|
TAbi extends Abi | readonly unknown[],
|
795
|
-
TFunctionName extends
|
848
|
+
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
|
849
|
+
TArgs extends ContractFunctionArgs<
|
850
|
+
TAbi,
|
851
|
+
'nonpayable' | 'payable',
|
852
|
+
TFunctionName
|
853
|
+
> = ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName>,
|
796
854
|
TAbiFunction extends AbiFunction = TAbi extends Abi
|
797
855
|
? ExtractAbiFunction<TAbi, TFunctionName>
|
798
856
|
: AbiFunction,
|
@@ -807,6 +865,7 @@ type GetWriteFunction<
|
|
807
865
|
WriteContractParameters<
|
808
866
|
TAbi,
|
809
867
|
TFunctionName,
|
868
|
+
TArgs,
|
810
869
|
TChain,
|
811
870
|
TAccount,
|
812
871
|
TChainOverride
|
@@ -833,6 +892,7 @@ type GetWriteFunction<
|
|
833
892
|
WriteContractParameters<
|
834
893
|
TAbi,
|
835
894
|
TFunctionName,
|
895
|
+
TArgs,
|
836
896
|
TChain,
|
837
897
|
TAccount,
|
838
898
|
TChainOverride
|