viem 0.0.1-alpha.21 → 0.0.1-alpha.22

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 (170) 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-46BO7YAQ.mjs → chunk-JMFNAGCD.mjs} +4 -3
  7. package/dist/chunk-JMFNAGCD.mjs.map +1 -0
  8. package/dist/{chunk-KZMJR27B.js → chunk-MVD4Y3AE.js} +67 -63
  9. package/dist/chunk-MVD4Y3AE.js.map +1 -0
  10. package/dist/{chunk-57ZOFERP.mjs → chunk-OZIBQJYR.mjs} +2 -2
  11. package/dist/{chunk-57ZOFERP.mjs.map → chunk-OZIBQJYR.mjs.map} +0 -0
  12. package/dist/{chunk-KEHGSYDO.js → chunk-PMPJGAHY.js} +14 -13
  13. package/dist/chunk-PMPJGAHY.js.map +1 -0
  14. package/dist/{chunk-W7BWWAC4.js → chunk-QA3IE5DU.js} +14 -14
  15. package/dist/{chunk-W7BWWAC4.js.map → chunk-QA3IE5DU.js.map} +0 -0
  16. package/dist/{chunk-DSPMAIDO.mjs → chunk-QN2NCTEN.mjs} +2 -2
  17. package/dist/{chunk-DSPMAIDO.mjs.map → chunk-QN2NCTEN.mjs.map} +1 -1
  18. package/dist/{chunk-EC3NUIJE.js → chunk-S77NLWHO.js} +13 -13
  19. package/dist/{chunk-EC3NUIJE.js.map → chunk-S77NLWHO.js.map} +1 -1
  20. package/dist/{chunk-O2GYLJVD.js → chunk-V5U5S7PQ.js} +403 -165
  21. package/dist/chunk-V5U5S7PQ.js.map +1 -0
  22. package/dist/{chunk-62VTYU2V.mjs → chunk-WWJ5YPTJ.mjs} +404 -166
  23. package/dist/chunk-WWJ5YPTJ.mjs.map +1 -0
  24. package/dist/{chunk-DGO77E2H.mjs → chunk-ZXXEENRD.mjs} +7 -3
  25. package/dist/chunk-ZXXEENRD.mjs.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-74f34ac9.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 +125 -113
  41. package/dist/index.js +14 -6
  42. package/dist/index.mjs +13 -5
  43. package/dist/normalize-ef9240c0.d.ts +33 -0
  44. package/dist/{parseGwei-4308ad80.d.ts → parseGwei-b323fb0a.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-a8688b26.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/index.ts +3 -1
  68. package/src/_test/utils.ts +39 -1
  69. package/src/actions/public/call.bench.ts +5 -5
  70. package/src/actions/public/deployContract.ts +4 -4
  71. package/src/actions/public/estimateGas.bench.ts +6 -6
  72. package/src/actions/public/getBlock.bench.ts +5 -5
  73. package/src/actions/public/getBlockNumber.bench.ts +5 -5
  74. package/src/actions/public/getGasPrice.bench.ts +1 -10
  75. package/src/actions/public/getTransaction.bench.ts +5 -5
  76. package/src/actions/public/getTransactionReceipt.bench.ts +5 -5
  77. package/src/actions/public/multicall.test.ts +452 -0
  78. package/src/actions/public/multicall.ts +103 -0
  79. package/src/actions/public/readContract.test.ts +226 -26
  80. package/src/actions/public/readContract.ts +13 -27
  81. package/src/actions/public/simulateContract.bench.ts +10 -0
  82. package/src/actions/public/simulateContract.test.ts +209 -37
  83. package/src/actions/public/simulateContract.ts +17 -21
  84. package/src/actions/public/watchContractEvent.test.ts +36 -32
  85. package/src/actions/public/watchEvent.test.ts +34 -29
  86. package/src/actions/wallet/signMessage.test.ts +0 -1
  87. package/src/actions/wallet/switchChain.test.ts +1 -2
  88. package/src/actions/wallet/watchAsset.test.ts +1 -2
  89. package/src/actions/wallet/writeContract.test.ts +37 -7
  90. package/src/actions/wallet/writeContract.ts +5 -14
  91. package/src/clients/transports/fallback.test.ts +34 -0
  92. package/src/clients/transports/fallback.ts +3 -1
  93. package/src/clients/transports/http.test.ts +0 -1
  94. package/src/clients/transports/webSocket.test.ts +0 -1
  95. package/src/constants/abis.test.ts +53 -0
  96. package/src/constants/abis.ts +44 -0
  97. package/src/constants/index.test.ts +14 -0
  98. package/src/constants/index.ts +3 -0
  99. package/src/constants/solidity.test.ts +41 -0
  100. package/src/constants/solidity.ts +35 -0
  101. package/src/ens.ts +5 -0
  102. package/src/errors/abi.test.ts +0 -2
  103. package/src/errors/base.test.ts +44 -2
  104. package/src/errors/base.ts +12 -12
  105. package/src/errors/block.test.ts +6 -6
  106. package/src/errors/contract.test.ts +233 -0
  107. package/src/errors/contract.ts +133 -80
  108. package/src/errors/index.ts +4 -2
  109. package/src/errors/request.test.ts +3 -4
  110. package/src/errors/request.ts +17 -17
  111. package/src/errors/rpc.test.ts +1 -2
  112. package/src/errors/transaction.test.ts +12 -12
  113. package/src/errors/transport.test.ts +0 -1
  114. package/src/index.test.ts +46 -0
  115. package/src/index.ts +8 -0
  116. package/src/types/contract.ts +55 -4
  117. package/src/types/index.ts +5 -0
  118. package/src/types/multicall.ts +82 -0
  119. package/src/utils/abi/decodeAbi.test.ts +1 -2
  120. package/src/utils/abi/decodeAbi.ts +11 -7
  121. package/src/utils/abi/decodeDeployData.test.ts +7 -15
  122. package/src/utils/abi/decodeDeployData.ts +10 -7
  123. package/src/utils/abi/decodeErrorResult.test.ts +103 -3
  124. package/src/utils/abi/decodeErrorResult.ts +30 -13
  125. package/src/utils/abi/decodeFunctionData.test.ts +0 -1
  126. package/src/utils/abi/decodeFunctionResult.test.ts +80 -9
  127. package/src/utils/abi/decodeFunctionResult.ts +15 -11
  128. package/src/utils/abi/encodeAbi.test.ts +3 -4
  129. package/src/utils/abi/encodeAbi.ts +19 -10
  130. package/src/utils/abi/encodeDeployData.test.ts +6 -13
  131. package/src/utils/abi/encodeDeployData.ts +10 -7
  132. package/src/utils/abi/encodeErrorResult.test.ts +2 -7
  133. package/src/utils/abi/encodeErrorResult.ts +18 -11
  134. package/src/utils/abi/encodeEventTopics.test.ts +11 -14
  135. package/src/utils/abi/encodeEventTopics.ts +15 -9
  136. package/src/utils/abi/encodeFunctionData.test.ts +5 -7
  137. package/src/utils/abi/encodeFunctionData.ts +16 -9
  138. package/src/utils/abi/encodeFunctionResult.test.ts +10 -15
  139. package/src/utils/abi/encodeFunctionResult.ts +9 -7
  140. package/src/utils/abi/getAbiItem.test.ts +547 -0
  141. package/src/utils/abi/getAbiItem.ts +93 -3
  142. package/src/utils/abi/index.ts +5 -1
  143. package/src/utils/address/getAddress.test.ts +6 -6
  144. package/src/utils/contract/getContractError.test.ts +247 -62
  145. package/src/utils/contract/getContractError.ts +30 -38
  146. package/src/utils/data/pad.bench.ts +0 -9
  147. package/src/utils/data/trim.bench.ts +0 -16
  148. package/src/utils/encoding/encodeBytes.bench.ts +0 -12
  149. package/src/utils/encoding/encodeHex.bench.ts +0 -11
  150. package/src/utils/ens/index.test.ts +13 -0
  151. package/src/utils/ens/index.ts +5 -0
  152. package/src/utils/ens/labelhash.test.ts +55 -0
  153. package/src/utils/ens/labelhash.ts +16 -0
  154. package/src/utils/ens/namehash.test.ts +65 -0
  155. package/src/utils/ens/namehash.ts +28 -0
  156. package/src/utils/ens/normalize.bench.ts +14 -0
  157. package/src/utils/ens/normalize.test.ts +35 -0
  158. package/src/utils/ens/normalize.ts +14 -0
  159. package/src/utils/hash/keccak256.ts +3 -5
  160. package/src/utils/index.test.ts +3 -0
  161. package/src/utils/index.ts +4 -0
  162. package/src/utils/rpc.test.ts +3 -6
  163. package/src/utils/unit/formatUnit.bench.ts +0 -5
  164. package/src/utils/unit/parseUnit.bench.ts +0 -5
  165. package/dist/chunk-46BO7YAQ.mjs.map +0 -1
  166. package/dist/chunk-62VTYU2V.mjs.map +0 -1
  167. package/dist/chunk-DGO77E2H.mjs.map +0 -1
  168. package/dist/chunk-KEHGSYDO.js.map +0 -1
  169. package/dist/chunk-KZMJR27B.js.map +0 -1
  170. 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
  })
@@ -32,41 +32,45 @@ afterAll(async () => {
32
32
  })
33
33
  })
34
34
 
35
- test('default', async () => {
36
- let logs: OnLogsResponse[] = []
37
-
38
- const unwatch = watchContractEvent(publicClient, {
39
- ...usdcContractConfig,
40
- onLogs: (logs_) => logs.push(logs_),
41
- })
35
+ test(
36
+ 'default',
37
+ async () => {
38
+ let logs: OnLogsResponse[] = []
39
+
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: vitalikAddress,
48
+ functionName: 'transfer',
49
+ args: [vitalikAddress, 1n],
50
+ })
51
+ await writeContract(walletClient, {
52
+ ...usdcContractConfig,
53
+ from: vitalikAddress,
54
+ functionName: 'transfer',
55
+ args: [vitalikAddress, 1n],
56
+ })
57
+ await wait(1000)
58
+ await writeContract(walletClient, {
59
+ ...usdcContractConfig,
60
+ from: vitalikAddress,
61
+ functionName: 'approve',
62
+ args: [vitalikAddress, 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[] = []
@@ -9,7 +9,7 @@ import {
9
9
  vitalikAddress,
10
10
  walletClient,
11
11
  } from '../../_test'
12
- import { impersonateAccount, stopImpersonatingAccount } from '../test'
12
+ import { impersonateAccount, mine, stopImpersonatingAccount } from '../test'
13
13
  import { sendTransaction } from '../wallet'
14
14
  import * as createEventFilter from './createEventFilter'
15
15
  import * as getFilterChanges from './getFilterChanges'
@@ -19,6 +19,7 @@ beforeAll(async () => {
19
19
  await impersonateAccount(testClient, {
20
20
  address: vitalikAddress,
21
21
  })
22
+ await mine(testClient, { blocks: 1 })
22
23
  })
23
24
 
24
25
  afterAll(async () => {
@@ -27,37 +28,41 @@ afterAll(async () => {
27
28
  })
28
29
  })
29
30
 
30
- test('default', async () => {
31
- let logs: OnLogsResponse[] = []
31
+ test(
32
+ 'default',
33
+ async () => {
34
+ let logs: OnLogsResponse[] = []
32
35
 
33
- const unwatch = watchEvent(publicClient, {
34
- onLogs: (logs_) => logs.push(logs_),
35
- })
36
+ const unwatch = watchEvent(publicClient, {
37
+ onLogs: (logs_) => logs.push(logs_),
38
+ })
36
39
 
37
- await wait(1000)
38
- await sendTransaction(walletClient, {
39
- from: vitalikAddress,
40
- to: usdcContractConfig.address,
41
- data: transfer1Data(accounts[0].address),
42
- })
43
- await sendTransaction(walletClient, {
44
- from: vitalikAddress,
45
- to: usdcContractConfig.address,
46
- data: transfer1Data(accounts[0].address),
47
- })
48
- await wait(1000)
49
- await sendTransaction(walletClient, {
50
- from: vitalikAddress,
51
- to: usdcContractConfig.address,
52
- data: transfer1Data(accounts[1].address),
53
- })
54
- await wait(2000)
55
- unwatch()
40
+ await wait(1000)
41
+ await sendTransaction(walletClient, {
42
+ from: vitalikAddress,
43
+ to: usdcContractConfig.address,
44
+ data: transfer1Data(accounts[0].address),
45
+ })
46
+ await sendTransaction(walletClient, {
47
+ from: vitalikAddress,
48
+ to: usdcContractConfig.address,
49
+ data: transfer1Data(accounts[0].address),
50
+ })
51
+ await wait(1000)
52
+ await sendTransaction(walletClient, {
53
+ from: vitalikAddress,
54
+ to: usdcContractConfig.address,
55
+ data: transfer1Data(accounts[1].address),
56
+ })
57
+ await wait(2000)
58
+ unwatch()
56
59
 
57
- expect(logs.length).toBe(2)
58
- expect(logs[0].length).toBe(2)
59
- expect(logs[1].length).toBe(1)
60
- })
60
+ expect(logs.length).toBe(2)
61
+ expect(logs[0].length).toBe(2)
62
+ expect(logs[1].length).toBe(1)
63
+ },
64
+ { retry: 3 },
65
+ )
61
66
 
62
67
  test('args: batch', async () => {
63
68
  let logs: OnLogsResponse[] = []
@@ -35,7 +35,6 @@ test('hex', async () => {
35
35
  "data (\\"deadbeaf\\") must be a hex value. Encode it first to a hex with the \`encodeHex\` util.
36
36
 
37
37
  Docs: https://viem.sh/TODO
38
-
39
38
  Version: viem@1.0.2"
40
39
  `,
41
40
  )
@@ -16,7 +16,6 @@ test('unsupported chain', async () => {
16
16
  [UnknownRpcError: An unknown RPC error occurred.
17
17
 
18
18
  Details: Unrecognized chain.
19
- Version: viem@1.0.2
20
- Internal Error: {"code":-4902,"details":"Unrecognized chain."}]
19
+ Version: viem@1.0.2]
21
20
  `)
22
21
  })
@@ -35,7 +35,6 @@ test('errors: unsupported type', async () => {
35
35
  Double check you have provided the correct parameters.
36
36
 
37
37
  Details: Token type ERC721 not supported.
38
- Version: viem@1.0.2
39
- Internal Error: {\\"code\\":-32602,\\"details\\":\\"Token type ERC721 not supported.\\"}"
38
+ Version: viem@1.0.2"
40
39
  `)
41
40
  })
@@ -12,6 +12,16 @@ import { mine } from '../test'
12
12
  import { writeContract } from './writeContract'
13
13
 
14
14
  test('default', async () => {
15
+ expect(
16
+ await writeContract(walletClient, {
17
+ ...wagmiContractConfig,
18
+ from: accounts[0].address,
19
+ functionName: 'mint',
20
+ }),
21
+ ).toBeDefined()
22
+ })
23
+
24
+ test('overloaded function', async () => {
15
25
  expect(
16
26
  await writeContract(walletClient, {
17
27
  ...wagmiContractConfig,
@@ -27,7 +37,26 @@ test('w/ simulateContract', async () => {
27
37
  ...wagmiContractConfig,
28
38
  from: accounts[0].address,
29
39
  functionName: 'mint',
30
- args: [69420n],
40
+ })
41
+ expect(await writeContract(walletClient, request)).toBeDefined()
42
+
43
+ await mine(testClient, { blocks: 1 })
44
+
45
+ expect(
46
+ await simulateContract(publicClient, {
47
+ ...wagmiContractConfig,
48
+ from: accounts[0].address,
49
+ functionName: 'mint',
50
+ }),
51
+ ).toBeDefined()
52
+ })
53
+
54
+ test('w/ simulateContract (overloaded)', async () => {
55
+ const { request } = await simulateContract(publicClient, {
56
+ ...wagmiContractConfig,
57
+ from: accounts[0].address,
58
+ functionName: 'mint',
59
+ args: [69421n],
31
60
  })
32
61
  expect(await writeContract(walletClient, request)).toBeDefined()
33
62
 
@@ -38,17 +67,18 @@ test('w/ simulateContract', async () => {
38
67
  ...wagmiContractConfig,
39
68
  from: accounts[0].address,
40
69
  functionName: 'mint',
41
- args: [69420n],
70
+ args: [69421n],
42
71
  }),
43
72
  ).rejects.toThrowErrorMatchingInlineSnapshot(`
44
- "Token ID is taken
45
-
46
- Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
73
+ "The contract function \\"mint\\" reverted with the following reason:
74
+ Token ID is taken
75
+
47
76
  Contract: 0x0000000000000000000000000000000000000000
48
77
  Function: mint(uint256 tokenId)
49
- Arguments: (69420)
78
+ Arguments: (69421)
79
+ Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
50
80
 
51
- Details: execution reverted: Token ID is taken
81
+ Docs: https://viem.sh/docs/contract/simulateContract
52
82
  Version: viem@1.0.2"
53
83
  `)
54
84
  })