viem 0.0.1-alpha.21 → 0.0.1-alpha.23

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 (181) hide show
  1. package/dist/{chain-32f56cfa.d.ts → chain-4b39613a.d.ts} +2 -2
  2. package/dist/{chain-f12cdc7f.d.ts → chain-aa4898d0.d.ts} +1 -1
  3. package/dist/chains.d.ts +3 -3
  4. package/dist/chains.js +46 -46
  5. package/dist/chains.mjs +1 -1
  6. package/dist/{chunk-EC3NUIJE.js → chunk-7Y5QVLX7.js} +13 -13
  7. package/dist/{chunk-EC3NUIJE.js.map → chunk-7Y5QVLX7.js.map} +1 -1
  8. package/dist/{chunk-KEHGSYDO.js → chunk-BV5NTHUX.js} +14 -13
  9. package/dist/chunk-BV5NTHUX.js.map +1 -0
  10. package/dist/{chunk-46BO7YAQ.mjs → chunk-EGVXCZNJ.mjs} +4 -3
  11. package/dist/chunk-EGVXCZNJ.mjs.map +1 -0
  12. package/dist/{chunk-57ZOFERP.mjs → chunk-GX2KDAM3.mjs} +2 -2
  13. package/dist/{chunk-57ZOFERP.mjs.map → chunk-GX2KDAM3.mjs.map} +0 -0
  14. package/dist/{chunk-W7BWWAC4.js → chunk-NW6724MI.js} +14 -14
  15. package/dist/{chunk-W7BWWAC4.js.map → chunk-NW6724MI.js.map} +0 -0
  16. package/dist/{chunk-DGO77E2H.mjs → chunk-RJLUZTJS.mjs} +86 -3
  17. package/dist/chunk-RJLUZTJS.mjs.map +1 -0
  18. package/dist/{chunk-O2GYLJVD.js → chunk-SSPRUPGN.js} +405 -165
  19. package/dist/chunk-SSPRUPGN.js.map +1 -0
  20. package/dist/{chunk-DSPMAIDO.mjs → chunk-TXHOG6KU.mjs} +2 -2
  21. package/dist/{chunk-DSPMAIDO.mjs.map → chunk-TXHOG6KU.mjs.map} +1 -1
  22. package/dist/{chunk-62VTYU2V.mjs → chunk-XBUH66KN.mjs} +406 -166
  23. package/dist/chunk-XBUH66KN.mjs.map +1 -0
  24. package/dist/{chunk-KZMJR27B.js → chunk-ZKYGWITF.js} +147 -64
  25. package/dist/chunk-ZKYGWITF.js.map +1 -0
  26. package/dist/clients/index.d.ts +7 -7
  27. package/dist/clients/index.js +3 -3
  28. package/dist/clients/index.mjs +2 -2
  29. package/dist/{contract-9af4608b.d.ts → contract-4c3a37b3.d.ts} +14 -3
  30. package/dist/{createClient-5d316c7e.d.ts → createClient-55a04188.d.ts} +2 -2
  31. package/dist/{createPublicClient-9d2b42e1.d.ts → createPublicClient-3b27b282.d.ts} +3 -3
  32. package/dist/{createTestClient-79498dab.d.ts → createTestClient-93f9eac6.d.ts} +3 -3
  33. package/dist/{createWalletClient-f69a5230.d.ts → createWalletClient-c10df94d.d.ts} +3 -3
  34. package/dist/{eip1193-6c485d63.d.ts → eip1193-4c24765a.d.ts} +1 -1
  35. package/dist/ens.d.ts +1 -0
  36. package/dist/ens.js +11 -0
  37. package/dist/ens.js.map +1 -0
  38. package/dist/ens.mjs +11 -0
  39. package/dist/ens.mjs.map +1 -0
  40. package/dist/index.d.ts +174 -113
  41. package/dist/index.js +16 -6
  42. package/dist/index.mjs +15 -5
  43. package/dist/normalize-ef9240c0.d.ts +33 -0
  44. package/dist/{parseGwei-4308ad80.d.ts → parseGwei-3411cf2d.d.ts} +101 -37
  45. package/dist/public.d.ts +9 -9
  46. package/dist/public.js +4 -4
  47. package/dist/public.mjs +3 -3
  48. package/dist/{sendTransaction-1c8290a9.d.ts → sendTransaction-f17a2389.d.ts} +3 -3
  49. package/dist/{stopImpersonatingAccount-7781842a.d.ts → stopImpersonatingAccount-afb26486.d.ts} +2 -2
  50. package/dist/test.d.ts +5 -5
  51. package/dist/test.js +3 -3
  52. package/dist/test.mjs +2 -2
  53. package/dist/{transactionRequest-341b6ed2.d.ts → transactionRequest-93e9f001.d.ts} +2 -2
  54. package/dist/utils/index.d.ts +14 -16
  55. package/dist/utils/index.js +8 -2
  56. package/dist/utils/index.mjs +7 -1
  57. package/dist/wallet.d.ts +7 -7
  58. package/dist/wallet.js +3 -3
  59. package/dist/wallet.mjs +2 -2
  60. package/dist/{watchAsset-afaad3c7.d.ts → watchAsset-efd3dd05.d.ts} +3 -3
  61. package/dist/{watchPendingTransactions-3b722547.d.ts → watchPendingTransactions-57df1a13.d.ts} +17 -27
  62. package/dist/{webSocket-b180e679.d.ts → webSocket-d2e7bd0e.d.ts} +2 -2
  63. package/dist/window.d.ts +2 -2
  64. package/package.json +8 -2
  65. package/src/_test/abis.ts +7 -0
  66. package/src/_test/bench.ts +4 -4
  67. package/src/_test/constants.ts +4 -3
  68. package/src/_test/index.ts +4 -2
  69. package/src/_test/utils.ts +39 -1
  70. package/src/actions/index.test.ts +1 -0
  71. package/src/actions/index.ts +2 -0
  72. package/src/actions/public/call.bench.ts +5 -5
  73. package/src/actions/public/deployContract.ts +4 -4
  74. package/src/actions/public/estimateGas.bench.ts +6 -6
  75. package/src/actions/public/getBlock.bench.ts +5 -5
  76. package/src/actions/public/getBlockNumber.bench.ts +5 -5
  77. package/src/actions/public/getFilterChanges.test.ts +9 -9
  78. package/src/actions/public/getFilterLogs.test.ts +7 -7
  79. package/src/actions/public/getGasPrice.bench.ts +1 -10
  80. package/src/actions/public/getLogs.test.ts +7 -7
  81. package/src/actions/public/getTransaction.bench.ts +5 -5
  82. package/src/actions/public/getTransactionReceipt.bench.ts +5 -5
  83. package/src/actions/public/index.test.ts +1 -0
  84. package/src/actions/public/index.ts +3 -0
  85. package/src/actions/public/multicall.test.ts +452 -0
  86. package/src/actions/public/multicall.ts +103 -0
  87. package/src/actions/public/readContract.test.ts +226 -26
  88. package/src/actions/public/readContract.ts +13 -27
  89. package/src/actions/public/simulateContract.bench.ts +10 -0
  90. package/src/actions/public/simulateContract.test.ts +209 -37
  91. package/src/actions/public/simulateContract.ts +17 -21
  92. package/src/actions/public/watchContractEvent.test.ts +61 -57
  93. package/src/actions/public/watchEvent.test.ts +42 -37
  94. package/src/actions/test/impersonateAccount.test.ts +4 -6
  95. package/src/actions/test/sendUnsignedTransaction.test.ts +2 -2
  96. package/src/actions/test/stopImpersonatingAccount.test.ts +5 -7
  97. package/src/actions/wallet/signMessage.test.ts +0 -1
  98. package/src/actions/wallet/switchChain.test.ts +1 -2
  99. package/src/actions/wallet/watchAsset.test.ts +1 -2
  100. package/src/actions/wallet/writeContract.test.ts +37 -7
  101. package/src/actions/wallet/writeContract.ts +5 -14
  102. package/src/clients/transports/fallback.test.ts +34 -0
  103. package/src/clients/transports/fallback.ts +3 -1
  104. package/src/clients/transports/http.test.ts +0 -1
  105. package/src/clients/transports/webSocket.test.ts +0 -1
  106. package/src/constants/abis.test.ts +53 -0
  107. package/src/constants/abis.ts +44 -0
  108. package/src/constants/index.test.ts +14 -0
  109. package/src/constants/index.ts +3 -0
  110. package/src/constants/solidity.test.ts +41 -0
  111. package/src/constants/solidity.ts +35 -0
  112. package/src/ens.ts +5 -0
  113. package/src/errors/abi.test.ts +0 -2
  114. package/src/errors/base.test.ts +44 -2
  115. package/src/errors/base.ts +12 -12
  116. package/src/errors/block.test.ts +6 -6
  117. package/src/errors/contract.test.ts +233 -0
  118. package/src/errors/contract.ts +133 -80
  119. package/src/errors/index.ts +4 -2
  120. package/src/errors/request.test.ts +3 -4
  121. package/src/errors/request.ts +17 -17
  122. package/src/errors/rpc.test.ts +1 -2
  123. package/src/errors/transaction.test.ts +12 -12
  124. package/src/errors/transport.test.ts +0 -1
  125. package/src/index.test.ts +47 -0
  126. package/src/index.ts +10 -0
  127. package/src/types/contract.ts +55 -4
  128. package/src/types/index.ts +5 -0
  129. package/src/types/multicall.ts +82 -0
  130. package/src/utils/abi/decodeAbi.test.ts +1 -2
  131. package/src/utils/abi/decodeAbi.ts +11 -7
  132. package/src/utils/abi/decodeDeployData.test.ts +7 -15
  133. package/src/utils/abi/decodeDeployData.ts +10 -7
  134. package/src/utils/abi/decodeErrorResult.test.ts +103 -3
  135. package/src/utils/abi/decodeErrorResult.ts +30 -13
  136. package/src/utils/abi/decodeFunctionData.test.ts +0 -1
  137. package/src/utils/abi/decodeFunctionResult.test.ts +80 -9
  138. package/src/utils/abi/decodeFunctionResult.ts +15 -11
  139. package/src/utils/abi/encodeAbi.test.ts +3 -4
  140. package/src/utils/abi/encodeAbi.ts +19 -10
  141. package/src/utils/abi/encodeDeployData.test.ts +6 -13
  142. package/src/utils/abi/encodeDeployData.ts +10 -7
  143. package/src/utils/abi/encodeErrorResult.test.ts +2 -7
  144. package/src/utils/abi/encodeErrorResult.ts +18 -11
  145. package/src/utils/abi/encodeEventTopics.test.ts +11 -14
  146. package/src/utils/abi/encodeEventTopics.ts +15 -9
  147. package/src/utils/abi/encodeFunctionData.test.ts +5 -7
  148. package/src/utils/abi/encodeFunctionData.ts +16 -9
  149. package/src/utils/abi/encodeFunctionResult.test.ts +10 -15
  150. package/src/utils/abi/encodeFunctionResult.ts +9 -7
  151. package/src/utils/abi/getAbiItem.test.ts +547 -0
  152. package/src/utils/abi/getAbiItem.ts +93 -3
  153. package/src/utils/abi/index.ts +5 -1
  154. package/src/utils/address/getAddress.test.ts +6 -6
  155. package/src/utils/contract/getContractError.test.ts +247 -62
  156. package/src/utils/contract/getContractError.ts +30 -38
  157. package/src/utils/data/pad.bench.ts +0 -9
  158. package/src/utils/data/trim.bench.ts +0 -16
  159. package/src/utils/encoding/encodeBytes.bench.ts +0 -12
  160. package/src/utils/encoding/encodeHex.bench.ts +0 -11
  161. package/src/utils/ens/index.test.ts +13 -0
  162. package/src/utils/ens/index.ts +5 -0
  163. package/src/utils/ens/labelhash.test.ts +55 -0
  164. package/src/utils/ens/labelhash.ts +16 -0
  165. package/src/utils/ens/namehash.test.ts +65 -0
  166. package/src/utils/ens/namehash.ts +28 -0
  167. package/src/utils/ens/normalize.bench.ts +14 -0
  168. package/src/utils/ens/normalize.test.ts +35 -0
  169. package/src/utils/ens/normalize.ts +14 -0
  170. package/src/utils/hash/keccak256.ts +3 -5
  171. package/src/utils/index.test.ts +3 -0
  172. package/src/utils/index.ts +4 -0
  173. package/src/utils/rpc.test.ts +3 -6
  174. package/src/utils/unit/formatUnit.bench.ts +0 -5
  175. package/src/utils/unit/parseUnit.bench.ts +0 -5
  176. package/dist/chunk-46BO7YAQ.mjs.map +0 -1
  177. package/dist/chunk-62VTYU2V.mjs.map +0 -1
  178. package/dist/chunk-DGO77E2H.mjs.map +0 -1
  179. package/dist/chunk-KEHGSYDO.js.map +0 -1
  180. package/dist/chunk-KZMJR27B.js.map +0 -1
  181. package/dist/chunk-O2GYLJVD.js.map +0 -1
@@ -11,6 +11,7 @@
11
11
  import { describe, expect, test } from 'vitest'
12
12
  import {
13
13
  accounts,
14
+ deployBAYC,
14
15
  publicClient,
15
16
  testClient,
16
17
  wagmiContractConfig,
@@ -21,9 +22,9 @@ import { encodeFunctionData } from '../../utils'
21
22
  import { mine } from '../test'
22
23
  import { sendTransaction } from '../wallet'
23
24
 
24
- import { deployContract } from './deployContract'
25
- import { getTransactionReceipt } from './getTransactionReceipt'
26
25
  import { simulateContract } from './simulateContract'
26
+ import { deployErrorExample } from '../../_test/utils'
27
+ import { errorsExampleABI } from '../../_test/generated'
27
28
 
28
29
  describe('wagmi', () => {
29
30
  test('default', async () => {
@@ -53,6 +54,18 @@ describe('wagmi', () => {
53
54
  ).toEqual(undefined)
54
55
  })
55
56
 
57
+ test('overloaded function', async () => {
58
+ expect(
59
+ (
60
+ await simulateContract(publicClient, {
61
+ ...wagmiContractConfig,
62
+ from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
63
+ functionName: 'mint',
64
+ })
65
+ ).result,
66
+ ).toEqual(undefined)
67
+ })
68
+
56
69
  test('revert', async () => {
57
70
  await expect(() =>
58
71
  simulateContract(publicClient, {
@@ -62,14 +75,15 @@ describe('wagmi', () => {
62
75
  from: accounts[0].address,
63
76
  }),
64
77
  ).rejects.toThrowErrorMatchingInlineSnapshot(`
65
- "ERC721: approval to current owner
66
-
67
- Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
78
+ "The contract function \\"approve\\" reverted with the following reason:
79
+ ERC721: approval to current owner
80
+
68
81
  Contract: 0x0000000000000000000000000000000000000000
69
82
  Function: approve(address to, uint256 tokenId)
70
83
  Arguments: (0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC, 420)
84
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
71
85
 
72
- Details: execution reverted: ERC721: approval to current owner
86
+ Docs: https://viem.sh/docs/contract/simulateContract
73
87
  Version: viem@1.0.2"
74
88
  `)
75
89
  await expect(() =>
@@ -80,14 +94,15 @@ describe('wagmi', () => {
80
94
  from: accounts[0].address,
81
95
  }),
82
96
  ).rejects.toThrowErrorMatchingInlineSnapshot(`
83
- "Token ID is taken
84
-
85
- Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
97
+ "The contract function \\"mint\\" reverted with the following reason:
98
+ Token ID is taken
99
+
86
100
  Contract: 0x0000000000000000000000000000000000000000
87
101
  Function: mint(uint256 tokenId)
88
102
  Arguments: (1)
103
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
89
104
 
90
- Details: execution reverted: Token ID is taken
105
+ Docs: https://viem.sh/docs/contract/simulateContract
91
106
  Version: viem@1.0.2"
92
107
  `)
93
108
  await expect(() =>
@@ -102,14 +117,15 @@ describe('wagmi', () => {
102
117
  ],
103
118
  }),
104
119
  ).rejects.toThrowErrorMatchingInlineSnapshot(`
105
- "ERC721: transfer caller is not owner nor approved
106
-
107
- Sender: 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC
120
+ "The contract function \\"safeTransferFrom\\" reverted with the following reason:
121
+ ERC721: transfer caller is not owner nor approved
122
+
108
123
  Contract: 0x0000000000000000000000000000000000000000
109
124
  Function: safeTransferFrom(address from, address to, uint256 tokenId)
110
125
  Arguments: (0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6, 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC, 1)
126
+ Sender: 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC
111
127
 
112
- Details: execution reverted: ERC721: transfer caller is not owner nor approved
128
+ Docs: https://viem.sh/docs/contract/simulateContract
113
129
  Version: viem@1.0.2"
114
130
  `)
115
131
  })
@@ -178,20 +194,188 @@ describe('BAYC', () => {
178
194
  args: [1n],
179
195
  }),
180
196
  ).rejects.toThrowErrorMatchingInlineSnapshot(`
181
- "Sale must be active to mint Ape
182
-
183
- Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
197
+ "The contract function \\"mintApe\\" reverted with the following reason:
198
+ Sale must be active to mint Ape
199
+
184
200
  Contract: 0x0000000000000000000000000000000000000000
185
201
  Function: mintApe(uint256 numberOfTokens)
186
202
  Arguments: (1)
203
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
187
204
 
188
- Details: execution reverted: Sale must be active to mint Ape
205
+ Docs: https://viem.sh/docs/contract/simulateContract
189
206
  Version: viem@1.0.2"
190
207
  `)
191
208
  })
192
209
  })
193
210
  })
194
211
 
212
+ describe('contract errors', () => {
213
+ test('revert', async () => {
214
+ const { contractAddress } = await deployErrorExample()
215
+
216
+ await expect(() =>
217
+ simulateContract(publicClient, {
218
+ abi: errorsExampleABI,
219
+ address: contractAddress!,
220
+ functionName: 'revertWrite',
221
+ from: accounts[0].address,
222
+ }),
223
+ ).rejects.toMatchInlineSnapshot(`
224
+ [ContractFunctionExecutionError: The contract function "revertWrite" reverted with the following reason:
225
+ This is a revert message
226
+
227
+ Contract: 0x0000000000000000000000000000000000000000
228
+ Function: revertWrite()
229
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
230
+
231
+ Docs: https://viem.sh/docs/contract/simulateContract
232
+ Version: viem@1.0.2]
233
+ `)
234
+ })
235
+
236
+ test('assert', async () => {
237
+ const { contractAddress } = await deployErrorExample()
238
+
239
+ await expect(() =>
240
+ simulateContract(publicClient, {
241
+ abi: errorsExampleABI,
242
+ address: contractAddress!,
243
+ functionName: 'assertWrite',
244
+ from: accounts[0].address,
245
+ }),
246
+ ).rejects.toMatchInlineSnapshot(`
247
+ [ContractFunctionExecutionError: The contract function "assertWrite" reverted with the following reason:
248
+ An \`assert\` condition failed.
249
+
250
+ Contract: 0x0000000000000000000000000000000000000000
251
+ Function: assertWrite()
252
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
253
+
254
+ Docs: https://viem.sh/docs/contract/simulateContract
255
+ Version: viem@1.0.2]
256
+ `)
257
+ })
258
+
259
+ test('overflow', async () => {
260
+ const { contractAddress } = await deployErrorExample()
261
+
262
+ await expect(() =>
263
+ simulateContract(publicClient, {
264
+ abi: errorsExampleABI,
265
+ address: contractAddress!,
266
+ functionName: 'overflowWrite',
267
+ from: accounts[0].address,
268
+ }),
269
+ ).rejects.toMatchInlineSnapshot(`
270
+ [ContractFunctionExecutionError: The contract function "overflowWrite" reverted with the following reason:
271
+ Arithmic operation resulted in underflow or overflow.
272
+
273
+ Contract: 0x0000000000000000000000000000000000000000
274
+ Function: overflowWrite()
275
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
276
+
277
+ Docs: https://viem.sh/docs/contract/simulateContract
278
+ Version: viem@1.0.2]
279
+ `)
280
+ })
281
+
282
+ test('divide by zero', async () => {
283
+ const { contractAddress } = await deployErrorExample()
284
+
285
+ await expect(() =>
286
+ simulateContract(publicClient, {
287
+ abi: errorsExampleABI,
288
+ address: contractAddress!,
289
+ functionName: 'divideByZeroWrite',
290
+ from: accounts[0].address,
291
+ }),
292
+ ).rejects.toMatchInlineSnapshot(`
293
+ [ContractFunctionExecutionError: The contract function "divideByZeroWrite" reverted with the following reason:
294
+ Division or modulo by zero (e.g. \`5 / 0\` or \`23 % 0\`).
295
+
296
+ Contract: 0x0000000000000000000000000000000000000000
297
+ Function: divideByZeroWrite()
298
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
299
+
300
+ Docs: https://viem.sh/docs/contract/simulateContract
301
+ Version: viem@1.0.2]
302
+ `)
303
+ })
304
+
305
+ test('require', async () => {
306
+ const { contractAddress } = await deployErrorExample()
307
+
308
+ await expect(() =>
309
+ simulateContract(publicClient, {
310
+ abi: errorsExampleABI,
311
+ address: contractAddress!,
312
+ functionName: 'requireWrite',
313
+ from: accounts[0].address,
314
+ }),
315
+ ).rejects.toMatchInlineSnapshot(`
316
+ [ContractFunctionExecutionError: The contract function "requireWrite" reverted with the following reason:
317
+ execution reverted
318
+
319
+ Contract: 0x0000000000000000000000000000000000000000
320
+ Function: requireWrite()
321
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
322
+
323
+ Docs: https://viem.sh/docs/contract/simulateContract
324
+ Version: viem@1.0.2]
325
+ `)
326
+ })
327
+
328
+ test('custom error: simple', async () => {
329
+ const { contractAddress } = await deployErrorExample()
330
+
331
+ await expect(() =>
332
+ simulateContract(publicClient, {
333
+ abi: errorsExampleABI,
334
+ address: contractAddress!,
335
+ functionName: 'simpleCustomWrite',
336
+ from: accounts[0].address,
337
+ }),
338
+ ).rejects.toMatchInlineSnapshot(`
339
+ [ContractFunctionExecutionError: The contract function "simpleCustomWrite" reverted.
340
+
341
+ Error: SimpleError(string message)
342
+ Arguments: (bugger)
343
+
344
+ Contract: 0x0000000000000000000000000000000000000000
345
+ Function: simpleCustomWrite()
346
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
347
+
348
+ Docs: https://viem.sh/docs/contract/simulateContract
349
+ Version: viem@1.0.2]
350
+ `)
351
+ })
352
+
353
+ test('custom error: complex', async () => {
354
+ const { contractAddress } = await deployErrorExample()
355
+
356
+ await expect(() =>
357
+ simulateContract(publicClient, {
358
+ abi: errorsExampleABI,
359
+ address: contractAddress!,
360
+ functionName: 'complexCustomWrite',
361
+ from: accounts[0].address,
362
+ }),
363
+ ).rejects.toMatchInlineSnapshot(`
364
+ [ContractFunctionExecutionError: The contract function "complexCustomWrite" reverted.
365
+
366
+ Error: ComplexError((address sender, uint256 bar), string message, uint256 number)
367
+ Arguments: ({"sender":"0x0000000000000000000000000000000000000000","bar":"69"}, bugger, 69)
368
+
369
+ Contract: 0x0000000000000000000000000000000000000000
370
+ Function: complexCustomWrite()
371
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
372
+
373
+ Docs: https://viem.sh/docs/contract/simulateContract
374
+ Version: viem@1.0.2]
375
+ `)
376
+ })
377
+ })
378
+
195
379
  test('fake contract address', async () => {
196
380
  await expect(() =>
197
381
  simulateContract(publicClient, {
@@ -207,32 +391,20 @@ test('fake contract address', async () => {
207
391
  address: '0x0000000000000000000000000000000000000069',
208
392
  functionName: 'mint',
209
393
  from: accounts[0].address,
210
- args: [],
211
394
  }),
212
395
  ).rejects.toThrowErrorMatchingInlineSnapshot(`
213
- "The contract method \\"mint\\" returned no data (\\"0x\\"). This could be due to any of the following:
396
+ "The contract function \\"mint\\" returned no data (\\"0x\\").
397
+
398
+ This could be due to any of the following:
214
399
  - The contract does not have the function \\"mint\\",
215
400
  - The parameters passed to the contract function may be invalid, or
216
401
  - The address is not a contract.
217
402
 
218
- Contract: 0x0000000000000000000000000000000000000000
219
- Function: mint()
220
- > \\"0x\\"
403
+ Contract: 0x0000000000000000000000000000000000000000
404
+ Function: mint()
405
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
221
406
 
407
+ Docs: https://viem.sh/docs/contract/simulateContract
222
408
  Version: viem@1.0.2"
223
409
  `)
224
410
  })
225
-
226
- // Deploy BAYC Contract
227
- async function deployBAYC() {
228
- const hash = await deployContract(walletClient, {
229
- ...baycContractConfig,
230
- args: ['Bored Ape Wagmi Club', 'BAYC', 69420n, 0n],
231
- from: accounts[0].address,
232
- })
233
- await mine(testClient, { blocks: 1 })
234
- const { contractAddress } = await getTransactionReceipt(publicClient, {
235
- hash,
236
- })
237
- return { contractAddress }
238
- }
@@ -1,9 +1,11 @@
1
- import { Abi } from 'abitype'
1
+ import { Abi, Narrow } from 'abitype'
2
2
 
3
3
  import type { PublicClient } from '../../clients'
4
+ import { BaseError } from '../../errors'
4
5
  import type {
5
6
  Address,
6
7
  Chain,
8
+ ContractConfig,
7
9
  ExtractArgsFromAbi,
8
10
  ExtractResultFromAbi,
9
11
  ExtractFunctionNameFromAbi,
@@ -14,6 +16,7 @@ import {
14
16
  decodeFunctionResult,
15
17
  encodeFunctionData,
16
18
  getContractError,
19
+ DecodeFunctionResultArgs,
17
20
  } from '../../utils'
18
21
  import { WriteContractArgs } from '../wallet'
19
22
  import { call, CallArgs } from './call'
@@ -22,16 +25,10 @@ export type SimulateContractArgs<
22
25
  TChain extends Chain = Chain,
23
26
  TAbi extends Abi | readonly unknown[] = Abi,
24
27
  TFunctionName extends string = any,
25
- > = Omit<CallArgs<TChain>, 'to' | 'data' | 'value'> & {
26
- address: Address
27
- abi: TAbi
28
- functionName: ExtractFunctionNameFromAbi<
29
- TAbi,
30
- TFunctionName,
31
- 'payable' | 'nonpayable'
32
- >
33
- value?: GetValue<TAbi, TFunctionName, CallArgs<TChain>['value']>
34
- } & ExtractArgsFromAbi<TAbi, TFunctionName>
28
+ > = Omit<CallArgs<TChain>, 'to' | 'data' | 'value'> &
29
+ ContractConfig<TAbi, TFunctionName, 'payable' | 'nonpayable'> & {
30
+ value?: GetValue<TAbi, TFunctionName, CallArgs<TChain>['value']>
31
+ }
35
32
 
36
33
  export type SimulateContractResponse<
37
34
  TChain extends Chain = Chain,
@@ -39,17 +36,14 @@ export type SimulateContractResponse<
39
36
  TFunctionName extends string = string,
40
37
  > = {
41
38
  result: ExtractResultFromAbi<TAbi, TFunctionName>
42
- request: WriteContractArgs<TChain, TAbi, TFunctionName> & {
43
- address: Address
44
- abi: TAbi
45
- functionName: ExtractFunctionNameFromAbi<TAbi, TFunctionName>
46
- } & ExtractArgsFromAbi<TAbi, TFunctionName>
39
+ request: WriteContractArgs<TChain, TAbi, TFunctionName> &
40
+ ContractConfig<TAbi, TFunctionName, 'payable' | 'nonpayable'>
47
41
  }
48
42
 
49
43
  export async function simulateContract<
50
44
  TChain extends Chain,
51
- TAbi extends Abi = Abi,
52
- TFunctionName extends string = any,
45
+ TAbi extends Abi | readonly unknown[],
46
+ TFunctionName extends string,
53
47
  >(
54
48
  client: PublicClient,
55
49
  {
@@ -73,9 +67,10 @@ export async function simulateContract<
73
67
  } as unknown as CallArgs<TChain>)
74
68
  const result = decodeFunctionResult({
75
69
  abi,
70
+ args,
76
71
  functionName,
77
72
  data: data || '0x',
78
- })
73
+ } as DecodeFunctionResultArgs)
79
74
  return {
80
75
  result,
81
76
  request: {
@@ -87,10 +82,11 @@ export async function simulateContract<
87
82
  },
88
83
  } as unknown as SimulateContractResponse<TChain, TAbi, TFunctionName>
89
84
  } catch (err) {
90
- throw getContractError(err, {
91
- abi,
85
+ throw getContractError(err as BaseError, {
86
+ abi: abi as Abi,
92
87
  address,
93
88
  args,
89
+ docsPath: '/docs/contract/simulateContract',
94
90
  functionName,
95
91
  sender: callRequest.from,
96
92
  })
@@ -2,10 +2,10 @@ import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
2
2
  import { wait } from '../../utils/wait'
3
3
  import {
4
4
  accounts,
5
+ address,
5
6
  publicClient,
6
7
  testClient,
7
8
  usdcContractConfig,
8
- vitalikAddress,
9
9
  walletClient,
10
10
  } from '../../_test'
11
11
  import { impersonateAccount, stopImpersonatingAccount } from '../test'
@@ -16,7 +16,7 @@ import { OnLogsResponse, watchContractEvent } from './watchContractEvent'
16
16
 
17
17
  beforeAll(async () => {
18
18
  await impersonateAccount(testClient, {
19
- address: vitalikAddress,
19
+ address: address.vitalik,
20
20
  })
21
21
  await impersonateAccount(testClient, {
22
22
  address: '0x5414d89a8bf7e99d732bc52f3e6a3ef461c0c078',
@@ -25,48 +25,52 @@ beforeAll(async () => {
25
25
 
26
26
  afterAll(async () => {
27
27
  await stopImpersonatingAccount(testClient, {
28
- address: vitalikAddress,
28
+ address: address.vitalik,
29
29
  })
30
30
  await stopImpersonatingAccount(testClient, {
31
31
  address: '0x5414d89a8bf7e99d732bc52f3e6a3ef461c0c078',
32
32
  })
33
33
  })
34
34
 
35
- test('default', async () => {
36
- let logs: OnLogsResponse[] = []
35
+ test(
36
+ 'default',
37
+ async () => {
38
+ let logs: OnLogsResponse[] = []
37
39
 
38
- const unwatch = watchContractEvent(publicClient, {
39
- ...usdcContractConfig,
40
- onLogs: (logs_) => logs.push(logs_),
41
- })
40
+ const unwatch = watchContractEvent(publicClient, {
41
+ ...usdcContractConfig,
42
+ onLogs: (logs_) => logs.push(logs_),
43
+ })
42
44
 
43
- await writeContract(walletClient, {
44
- ...usdcContractConfig,
45
- from: vitalikAddress,
46
- functionName: 'transfer',
47
- args: [vitalikAddress, 1n],
48
- })
49
- await writeContract(walletClient, {
50
- ...usdcContractConfig,
51
- from: vitalikAddress,
52
- functionName: 'transfer',
53
- args: [vitalikAddress, 1n],
54
- })
55
- await wait(1000)
56
- await writeContract(walletClient, {
57
- ...usdcContractConfig,
58
- from: vitalikAddress,
59
- functionName: 'approve',
60
- args: [vitalikAddress, 1n],
61
- })
45
+ await writeContract(walletClient, {
46
+ ...usdcContractConfig,
47
+ from: address.vitalik,
48
+ functionName: 'transfer',
49
+ args: [address.vitalik, 1n],
50
+ })
51
+ await writeContract(walletClient, {
52
+ ...usdcContractConfig,
53
+ from: address.vitalik,
54
+ functionName: 'transfer',
55
+ args: [address.vitalik, 1n],
56
+ })
57
+ await wait(1000)
58
+ await writeContract(walletClient, {
59
+ ...usdcContractConfig,
60
+ from: address.vitalik,
61
+ functionName: 'approve',
62
+ args: [address.vitalik, 1n],
63
+ })
62
64
 
63
- await wait(2000)
64
- unwatch()
65
+ await wait(2000)
66
+ unwatch()
65
67
 
66
- expect(logs.length).toBe(2)
67
- expect(logs[0].length).toBe(2)
68
- expect(logs[1].length).toBe(1)
69
- })
68
+ expect(logs.length).toBe(2)
69
+ expect(logs[0].length).toBe(2)
70
+ expect(logs[1].length).toBe(1)
71
+ },
72
+ { retry: 3 },
73
+ )
70
74
 
71
75
  test('args: batch', async () => {
72
76
  let logs: OnLogsResponse[] = []
@@ -79,22 +83,22 @@ test('args: batch', async () => {
79
83
 
80
84
  await writeContract(walletClient, {
81
85
  ...usdcContractConfig,
82
- from: vitalikAddress,
86
+ from: address.vitalik,
83
87
  functionName: 'transfer',
84
- args: [vitalikAddress, 1n],
88
+ args: [address.vitalik, 1n],
85
89
  })
86
90
  await writeContract(walletClient, {
87
91
  ...usdcContractConfig,
88
- from: vitalikAddress,
92
+ from: address.vitalik,
89
93
  functionName: 'transfer',
90
- args: [vitalikAddress, 1n],
94
+ args: [address.vitalik, 1n],
91
95
  })
92
96
  await wait(1000)
93
97
  await writeContract(walletClient, {
94
98
  ...usdcContractConfig,
95
- from: vitalikAddress,
99
+ from: address.vitalik,
96
100
  functionName: 'approve',
97
- args: [vitalikAddress, 1n],
101
+ args: [address.vitalik, 1n],
98
102
  })
99
103
 
100
104
  await wait(2000)
@@ -117,22 +121,22 @@ test('args: eventName', async () => {
117
121
 
118
122
  await writeContract(walletClient, {
119
123
  ...usdcContractConfig,
120
- from: vitalikAddress,
124
+ from: address.vitalik,
121
125
  functionName: 'transfer',
122
- args: [vitalikAddress, 1n],
126
+ args: [address.vitalik, 1n],
123
127
  })
124
128
  await writeContract(walletClient, {
125
129
  ...usdcContractConfig,
126
- from: vitalikAddress,
130
+ from: address.vitalik,
127
131
  functionName: 'transfer',
128
- args: [vitalikAddress, 1n],
132
+ args: [address.vitalik, 1n],
129
133
  })
130
134
  await wait(1000)
131
135
  await writeContract(walletClient, {
132
136
  ...usdcContractConfig,
133
- from: vitalikAddress,
137
+ from: address.vitalik,
134
138
  functionName: 'approve',
135
- args: [vitalikAddress, 1n],
139
+ args: [address.vitalik, 1n],
136
140
  })
137
141
 
138
142
  await wait(2000)
@@ -156,22 +160,22 @@ test('args: args', async () => {
156
160
 
157
161
  await writeContract(walletClient, {
158
162
  ...usdcContractConfig,
159
- from: vitalikAddress,
163
+ from: address.vitalik,
160
164
  functionName: 'transfer',
161
165
  args: [accounts[0].address, 1n],
162
166
  })
163
167
  await writeContract(walletClient, {
164
168
  ...usdcContractConfig,
165
- from: vitalikAddress,
169
+ from: address.vitalik,
166
170
  functionName: 'transfer',
167
171
  args: [accounts[1].address, 1n],
168
172
  })
169
173
  await wait(1000)
170
174
  await writeContract(walletClient, {
171
175
  ...usdcContractConfig,
172
- from: vitalikAddress,
176
+ from: address.vitalik,
173
177
  functionName: 'approve',
174
- args: [vitalikAddress, 1n],
178
+ args: [address.vitalik, 1n],
175
179
  })
176
180
 
177
181
  await wait(2000)
@@ -195,22 +199,22 @@ test('args: args', async () => {
195
199
 
196
200
  await writeContract(walletClient, {
197
201
  ...usdcContractConfig,
198
- from: vitalikAddress,
202
+ from: address.vitalik,
199
203
  functionName: 'transfer',
200
204
  args: [accounts[0].address, 1n],
201
205
  })
202
206
  await writeContract(walletClient, {
203
207
  ...usdcContractConfig,
204
- from: vitalikAddress,
208
+ from: address.vitalik,
205
209
  functionName: 'transfer',
206
210
  args: [accounts[1].address, 1n],
207
211
  })
208
212
  await wait(1000)
209
213
  await writeContract(walletClient, {
210
214
  ...usdcContractConfig,
211
- from: vitalikAddress,
215
+ from: address.vitalik,
212
216
  functionName: 'approve',
213
- args: [vitalikAddress, 1n],
217
+ args: [address.vitalik, 1n],
214
218
  })
215
219
 
216
220
  await wait(2000)
@@ -240,16 +244,16 @@ test('args: args', async () => {
240
244
  })
241
245
  await writeContract(walletClient, {
242
246
  ...usdcContractConfig,
243
- from: vitalikAddress,
247
+ from: address.vitalik,
244
248
  functionName: 'transfer',
245
249
  args: [accounts[1].address, 1n],
246
250
  })
247
251
  await wait(1000)
248
252
  await writeContract(walletClient, {
249
253
  ...usdcContractConfig,
250
- from: vitalikAddress,
254
+ from: address.vitalik,
251
255
  functionName: 'approve',
252
- args: [vitalikAddress, 1n],
256
+ args: [address.vitalik, 1n],
253
257
  })
254
258
 
255
259
  await wait(2000)