viem 0.0.1-alpha.20 → 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-ZVGTYLKU.mjs → chunk-JMFNAGCD.mjs} +4 -3
  7. package/dist/chunk-JMFNAGCD.mjs.map +1 -0
  8. package/dist/{chunk-NHAKUPTF.js → chunk-MVD4Y3AE.js} +67 -63
  9. package/dist/chunk-MVD4Y3AE.js.map +1 -0
  10. package/dist/{chunk-YODUQCHJ.mjs → chunk-OZIBQJYR.mjs} +2 -2
  11. package/dist/{chunk-YODUQCHJ.mjs.map → chunk-OZIBQJYR.mjs.map} +0 -0
  12. package/dist/{chunk-FHXXG7I6.js → chunk-PMPJGAHY.js} +14 -13
  13. package/dist/chunk-PMPJGAHY.js.map +1 -0
  14. package/dist/{chunk-YFKR74XG.js → chunk-QA3IE5DU.js} +14 -14
  15. package/dist/{chunk-YFKR74XG.js.map → chunk-QA3IE5DU.js.map} +0 -0
  16. package/dist/{chunk-T7CBAKLI.mjs → chunk-QN2NCTEN.mjs} +2 -2
  17. package/dist/{chunk-T7CBAKLI.mjs.map → chunk-QN2NCTEN.mjs.map} +1 -1
  18. package/dist/{chunk-IAQPMSGJ.js → chunk-S77NLWHO.js} +13 -13
  19. package/dist/{chunk-IAQPMSGJ.js.map → chunk-S77NLWHO.js.map} +1 -1
  20. package/dist/{chunk-SF4I2NKC.js → chunk-V5U5S7PQ.js} +411 -167
  21. package/dist/chunk-V5U5S7PQ.js.map +1 -0
  22. package/dist/{chunk-5Q6FSUU2.mjs → chunk-WWJ5YPTJ.mjs} +412 -168
  23. package/dist/chunk-WWJ5YPTJ.mjs.map +1 -0
  24. package/dist/{chunk-MYQNZSWD.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 +40 -7
  129. package/src/utils/abi/encodeAbi.ts +28 -13
  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-5Q6FSUU2.mjs.map +0 -1
  166. package/dist/chunk-FHXXG7I6.js.map +0 -1
  167. package/dist/chunk-MYQNZSWD.mjs.map +0 -1
  168. package/dist/chunk-NHAKUPTF.js.map +0 -1
  169. package/dist/chunk-SF4I2NKC.js.map +0 -1
  170. package/dist/chunk-ZVGTYLKU.mjs.map +0 -1
@@ -0,0 +1,452 @@
1
+ /**
2
+ * TODO: Heaps more test cases :D
3
+ * - Complex calldata types
4
+ * - Complex return types (tuple/structs)
5
+ */
6
+
7
+ import { describe, expect, test } from 'vitest'
8
+ import {
9
+ accounts,
10
+ initialBlockNumber,
11
+ publicClient,
12
+ usdcContractConfig,
13
+ vitalikAddress,
14
+ } from '../../_test'
15
+ import { baycContractConfig, wagmiContractConfig } from '../../_test/abis'
16
+
17
+ import { multicall } from './multicall'
18
+
19
+ test('default', async () => {
20
+ expect(
21
+ await multicall(publicClient, {
22
+ blockNumber: initialBlockNumber,
23
+ contracts: [
24
+ {
25
+ ...usdcContractConfig,
26
+ functionName: 'totalSupply',
27
+ },
28
+ {
29
+ ...usdcContractConfig,
30
+ functionName: 'balanceOf',
31
+ args: [vitalikAddress],
32
+ },
33
+ {
34
+ ...baycContractConfig,
35
+ functionName: 'totalSupply',
36
+ },
37
+ ],
38
+ multicallAddress: '0xca11bde05977b3631167028862be2a173976ca11',
39
+ }),
40
+ ).toMatchInlineSnapshot(`
41
+ [
42
+ {
43
+ "result": 41119586940119550n,
44
+ "status": "success",
45
+ },
46
+ {
47
+ "result": 231481998602n,
48
+ "status": "success",
49
+ },
50
+ {
51
+ "result": 10000n,
52
+ "status": "success",
53
+ },
54
+ ]
55
+ `)
56
+ })
57
+
58
+ describe('errors', async () => {
59
+ describe('allowFailure is truthy', async () => {
60
+ test('function not found', async () => {
61
+ expect(
62
+ await multicall(publicClient, {
63
+ blockNumber: initialBlockNumber,
64
+ contracts: [
65
+ {
66
+ ...usdcContractConfig,
67
+ // @ts-expect-error
68
+ functionName: 'lol',
69
+ },
70
+ {
71
+ ...usdcContractConfig,
72
+ functionName: 'balanceOf',
73
+ args: [vitalikAddress],
74
+ },
75
+ {
76
+ ...baycContractConfig,
77
+ functionName: 'totalSupply',
78
+ },
79
+ ],
80
+ multicallAddress: '0xca11bde05977b3631167028862be2a173976ca11',
81
+ }),
82
+ ).toMatchInlineSnapshot(`
83
+ [
84
+ {
85
+ "error": [ContractFunctionExecutionError: The contract function "lol" returned no data ("0x").
86
+
87
+ This could be due to any of the following:
88
+ - The contract does not have the function "lol",
89
+ - The parameters passed to the contract function may be invalid, or
90
+ - The address is not a contract.
91
+
92
+ Contract: 0x0000000000000000000000000000000000000000
93
+
94
+ Docs: https://viem.sh/docs/contract/multicall
95
+ Version: viem@1.0.2],
96
+ "result": undefined,
97
+ "status": "failure",
98
+ },
99
+ {
100
+ "result": 231481998602n,
101
+ "status": "success",
102
+ },
103
+ {
104
+ "result": 10000n,
105
+ "status": "success",
106
+ },
107
+ ]
108
+ `)
109
+ })
110
+
111
+ test('invalid params', async () => {
112
+ expect(
113
+ await multicall(publicClient, {
114
+ blockNumber: initialBlockNumber,
115
+ // @ts-expect-error
116
+ contracts: [
117
+ {
118
+ ...usdcContractConfig,
119
+ functionName: 'balanceOf',
120
+ args: [vitalikAddress, 1n],
121
+ },
122
+ {
123
+ ...usdcContractConfig,
124
+ functionName: 'balanceOf',
125
+ args: [vitalikAddress],
126
+ },
127
+ {
128
+ ...baycContractConfig,
129
+ functionName: 'totalSupply',
130
+ },
131
+ ] as const,
132
+ multicallAddress: '0xca11bde05977b3631167028862be2a173976ca11',
133
+ }),
134
+ ).toMatchInlineSnapshot(`
135
+ [
136
+ {
137
+ "error": [ContractFunctionExecutionError: The contract function "balanceOf" returned no data ("0x").
138
+
139
+ This could be due to any of the following:
140
+ - The contract does not have the function "balanceOf",
141
+ - The parameters passed to the contract function may be invalid, or
142
+ - The address is not a contract.
143
+
144
+ Contract: 0x0000000000000000000000000000000000000000
145
+ Function: balanceOf(address account)
146
+ Arguments: (0xd8da6bf26964af9d7eed9e03e53415d37aa96045)
147
+
148
+ Docs: https://viem.sh/docs/contract/multicall
149
+ Version: viem@1.0.2],
150
+ "result": undefined,
151
+ "status": "failure",
152
+ },
153
+ {
154
+ "result": 231481998602n,
155
+ "status": "success",
156
+ },
157
+ {
158
+ "result": 10000n,
159
+ "status": "success",
160
+ },
161
+ ]
162
+ `)
163
+ })
164
+
165
+ test('invalid contract address', async () => {
166
+ expect(
167
+ await multicall(publicClient, {
168
+ blockNumber: initialBlockNumber,
169
+ contracts: [
170
+ {
171
+ ...usdcContractConfig,
172
+ address: '0x0000000000000000000000000000000000000000',
173
+ functionName: 'balanceOf',
174
+ args: [vitalikAddress],
175
+ },
176
+ {
177
+ ...usdcContractConfig,
178
+ functionName: 'balanceOf',
179
+ args: [vitalikAddress],
180
+ },
181
+ {
182
+ ...baycContractConfig,
183
+ functionName: 'totalSupply',
184
+ },
185
+ ] as const,
186
+ multicallAddress: '0xca11bde05977b3631167028862be2a173976ca11',
187
+ }),
188
+ ).toMatchInlineSnapshot(`
189
+ [
190
+ {
191
+ "error": [ContractFunctionExecutionError: The contract function "balanceOf" returned no data ("0x").
192
+
193
+ This could be due to any of the following:
194
+ - The contract does not have the function "balanceOf",
195
+ - The parameters passed to the contract function may be invalid, or
196
+ - The address is not a contract.
197
+
198
+ Contract: 0x0000000000000000000000000000000000000000
199
+ Function: balanceOf(address account)
200
+ Arguments: (0xd8da6bf26964af9d7eed9e03e53415d37aa96045)
201
+
202
+ Docs: https://viem.sh/docs/contract/multicall
203
+ Version: viem@1.0.2],
204
+ "result": undefined,
205
+ "status": "failure",
206
+ },
207
+ {
208
+ "result": 231481998602n,
209
+ "status": "success",
210
+ },
211
+ {
212
+ "result": 10000n,
213
+ "status": "success",
214
+ },
215
+ ]
216
+ `)
217
+ })
218
+
219
+ test('contract revert', async () => {
220
+ expect(
221
+ await multicall(publicClient, {
222
+ blockNumber: initialBlockNumber,
223
+ contracts: [
224
+ {
225
+ ...usdcContractConfig,
226
+ functionName: 'balanceOf',
227
+ args: [vitalikAddress],
228
+ },
229
+ {
230
+ ...usdcContractConfig,
231
+ functionName: 'balanceOf',
232
+ args: [vitalikAddress],
233
+ },
234
+ {
235
+ ...wagmiContractConfig,
236
+ functionName: 'transferFrom',
237
+ args: [vitalikAddress, accounts[0].address, 1n],
238
+ },
239
+ {
240
+ ...baycContractConfig,
241
+ functionName: 'totalSupply',
242
+ },
243
+ {
244
+ ...baycContractConfig,
245
+ functionName: 'tokenOfOwnerByIndex',
246
+ args: [vitalikAddress, 1n],
247
+ },
248
+ ] as const,
249
+ multicallAddress: '0xca11bde05977b3631167028862be2a173976ca11',
250
+ }),
251
+ ).toMatchInlineSnapshot(`
252
+ [
253
+ {
254
+ "result": 231481998602n,
255
+ "status": "success",
256
+ },
257
+ {
258
+ "result": 231481998602n,
259
+ "status": "success",
260
+ },
261
+ {
262
+ "error": [ContractFunctionExecutionError: The contract function "transferFrom" reverted with the following reason:
263
+ ERC721: transfer caller is not owner nor approved
264
+
265
+ Contract: 0x0000000000000000000000000000000000000000
266
+ Function: transferFrom(address from, address to, uint256 tokenId)
267
+ Arguments: (0xd8da6bf26964af9d7eed9e03e53415d37aa96045, 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266, 1)
268
+
269
+ Docs: https://viem.sh/docs/contract/multicall
270
+ Version: viem@1.0.2],
271
+ "result": undefined,
272
+ "status": "failure",
273
+ },
274
+ {
275
+ "result": 10000n,
276
+ "status": "success",
277
+ },
278
+ {
279
+ "error": [ContractFunctionExecutionError: The contract function "tokenOfOwnerByIndex" reverted with the following reason:
280
+ EnumerableSet: index out of bounds
281
+
282
+ Contract: 0x0000000000000000000000000000000000000000
283
+ Function: tokenOfOwnerByIndex(address owner, uint256 index)
284
+ Arguments: (0xd8da6bf26964af9d7eed9e03e53415d37aa96045, 1)
285
+
286
+ Docs: https://viem.sh/docs/contract/multicall
287
+ Version: viem@1.0.2],
288
+ "result": undefined,
289
+ "status": "failure",
290
+ },
291
+ ]
292
+ `)
293
+ })
294
+ })
295
+
296
+ describe('allowFailure is falsy', async () => {
297
+ test('function not found', async () => {
298
+ await expect(() =>
299
+ multicall(publicClient, {
300
+ allowFailure: false,
301
+ contracts: [
302
+ {
303
+ ...usdcContractConfig,
304
+ // @ts-expect-error
305
+ functionName: 'lol',
306
+ },
307
+ {
308
+ ...usdcContractConfig,
309
+ functionName: 'balanceOf',
310
+ args: [vitalikAddress],
311
+ },
312
+ {
313
+ ...baycContractConfig,
314
+ functionName: 'totalSupply',
315
+ },
316
+ ],
317
+ multicallAddress: '0xca11bde05977b3631167028862be2a173976ca11',
318
+ }),
319
+ ).rejects.toMatchInlineSnapshot(`
320
+ [ContractFunctionExecutionError: Function "lol" not found on ABI.
321
+ Make sure you are using the correct ABI and that the function exists on it.
322
+
323
+ Contract: 0x0000000000000000000000000000000000000000
324
+
325
+ Docs: https://viem.sh/docs/contract/encodeFunctionData
326
+ Version: viem@1.0.2]
327
+ `)
328
+ })
329
+
330
+ test('invalid params', async () => {
331
+ await expect(() =>
332
+ multicall(publicClient, {
333
+ allowFailure: false,
334
+ // @ts-expect-error
335
+ contracts: [
336
+ {
337
+ ...usdcContractConfig,
338
+ functionName: 'balanceOf',
339
+ args: [vitalikAddress, 1n],
340
+ },
341
+ {
342
+ ...usdcContractConfig,
343
+ functionName: 'balanceOf',
344
+ args: [vitalikAddress],
345
+ },
346
+ {
347
+ ...baycContractConfig,
348
+ functionName: 'totalSupply',
349
+ },
350
+ ] as const,
351
+ multicallAddress: '0xca11bde05977b3631167028862be2a173976ca11',
352
+ }),
353
+ ).rejects.toMatchInlineSnapshot(`
354
+ [ContractFunctionExecutionError: ABI encoding params/values length mismatch.
355
+ Expected length (params): 1
356
+ Given length (values): 2
357
+
358
+ Contract: 0x0000000000000000000000000000000000000000
359
+ Function: balanceOf(address account)
360
+ Arguments: (0xd8da6bf26964af9d7eed9e03e53415d37aa96045)
361
+
362
+ Docs: https://viem.sh/docs/contract/multicall
363
+ Version: viem@1.0.2]
364
+ `)
365
+ })
366
+
367
+ test('invalid contract address', async () => {
368
+ await expect(() =>
369
+ multicall(publicClient, {
370
+ allowFailure: false,
371
+ contracts: [
372
+ {
373
+ ...usdcContractConfig,
374
+ address: '0x0000000000000000000000000000000000000000',
375
+ functionName: 'balanceOf',
376
+ args: [vitalikAddress],
377
+ },
378
+ {
379
+ ...usdcContractConfig,
380
+ functionName: 'balanceOf',
381
+ args: [vitalikAddress],
382
+ },
383
+ {
384
+ ...baycContractConfig,
385
+ functionName: 'totalSupply',
386
+ },
387
+ ] as const,
388
+ multicallAddress: '0xca11bde05977b3631167028862be2a173976ca11',
389
+ }),
390
+ ).rejects.toMatchInlineSnapshot(`
391
+ [ContractFunctionExecutionError: The contract function "balanceOf" returned no data ("0x").
392
+
393
+ This could be due to any of the following:
394
+ - The contract does not have the function "balanceOf",
395
+ - The parameters passed to the contract function may be invalid, or
396
+ - The address is not a contract.
397
+
398
+ Contract: 0x0000000000000000000000000000000000000000
399
+ Function: balanceOf(address account)
400
+ Arguments: (0xd8da6bf26964af9d7eed9e03e53415d37aa96045)
401
+
402
+ Docs: https://viem.sh/docs/contract/multicall
403
+ Version: viem@1.0.2]
404
+ `)
405
+ })
406
+
407
+ test('contract revert', async () => {
408
+ await expect(() =>
409
+ multicall(publicClient, {
410
+ allowFailure: false,
411
+ contracts: [
412
+ {
413
+ ...usdcContractConfig,
414
+ functionName: 'balanceOf',
415
+ args: [vitalikAddress],
416
+ },
417
+ {
418
+ ...usdcContractConfig,
419
+ functionName: 'balanceOf',
420
+ args: [vitalikAddress],
421
+ },
422
+ {
423
+ ...wagmiContractConfig,
424
+ functionName: 'transferFrom',
425
+ args: [vitalikAddress, accounts[0].address, 1n],
426
+ },
427
+ {
428
+ ...baycContractConfig,
429
+ functionName: 'totalSupply',
430
+ },
431
+ {
432
+ ...baycContractConfig,
433
+ functionName: 'tokenOfOwnerByIndex',
434
+ args: [vitalikAddress, 1n],
435
+ },
436
+ ] as const,
437
+ multicallAddress: '0xca11bde05977b3631167028862be2a173976ca11',
438
+ }),
439
+ ).rejects.toMatchInlineSnapshot(`
440
+ [ContractFunctionExecutionError: The contract function "transferFrom" reverted with the following reason:
441
+ ERC721: transfer caller is not owner nor approved
442
+
443
+ Contract: 0x0000000000000000000000000000000000000000
444
+ Function: transferFrom(address from, address to, uint256 tokenId)
445
+ Arguments: (0xd8da6bf26964af9d7eed9e03e53415d37aa96045, 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266, 1)
446
+
447
+ Docs: https://viem.sh/docs/contract/multicall
448
+ Version: viem@1.0.2]
449
+ `)
450
+ })
451
+ })
452
+ })
@@ -0,0 +1,103 @@
1
+ import { PublicClient } from '../../clients'
2
+ import { multicall3Abi } from '../../constants'
3
+ import {
4
+ AbiDecodingZeroDataError,
5
+ BaseError,
6
+ RawContractError,
7
+ } from '../../errors'
8
+ import { Address, ContractConfig, Hex, MulticallContracts } from '../../types'
9
+ import { MulticallResults } from '../../types/multicall'
10
+ import {
11
+ decodeFunctionResult,
12
+ encodeFunctionData,
13
+ EncodeFunctionDataArgs,
14
+ getContractError,
15
+ } from '../../utils'
16
+ import { CallArgs } from './call'
17
+ import { readContract } from './readContract'
18
+
19
+ export type MulticallArgs<
20
+ TContracts extends ContractConfig[],
21
+ TAllowFailure extends boolean = true,
22
+ > = Pick<CallArgs, 'blockNumber' | 'blockTag'> & {
23
+ allowFailure?: TAllowFailure
24
+ contracts: readonly [...MulticallContracts<TContracts>]
25
+ multicallAddress: Address
26
+ }
27
+
28
+ export async function multicall<
29
+ TContracts extends ContractConfig[],
30
+ TAllowFailure extends boolean = true,
31
+ >(
32
+ client: PublicClient,
33
+ args: MulticallArgs<TContracts, TAllowFailure>,
34
+ ): Promise<MulticallResults<TContracts, TAllowFailure>> {
35
+ const {
36
+ allowFailure = true,
37
+ blockNumber,
38
+ blockTag,
39
+ contracts,
40
+ multicallAddress,
41
+ } = args
42
+
43
+ const calls = contracts.map(({ abi, address, args, functionName }) => {
44
+ try {
45
+ const callData = encodeFunctionData({
46
+ abi,
47
+ args,
48
+ functionName,
49
+ } as unknown as EncodeFunctionDataArgs)
50
+ return {
51
+ allowFailure: true,
52
+ callData,
53
+ target: address,
54
+ }
55
+ } catch (err) {
56
+ const error = getContractError(err as BaseError, {
57
+ abi,
58
+ address,
59
+ args,
60
+ docsPath: '/docs/contract/multicall',
61
+ functionName,
62
+ })
63
+ if (!allowFailure) throw error
64
+ return {
65
+ allowFailure: true,
66
+ callData: '0x' as Hex,
67
+ target: address,
68
+ }
69
+ }
70
+ })
71
+ const results = await readContract(client, {
72
+ abi: multicall3Abi,
73
+ address: multicallAddress,
74
+ args: [calls],
75
+ blockNumber,
76
+ blockTag,
77
+ functionName: 'aggregate3',
78
+ })
79
+ return results.map(({ returnData, success }, i) => {
80
+ const { callData } = calls[i]
81
+ const { abi, address, functionName, args } = contracts[i]
82
+ try {
83
+ if (callData === '0x') throw new AbiDecodingZeroDataError()
84
+ if (!success) throw new RawContractError({ data: returnData })
85
+ const result = decodeFunctionResult({
86
+ abi,
87
+ data: returnData,
88
+ functionName: functionName,
89
+ })
90
+ return { result, status: 'success' }
91
+ } catch (err) {
92
+ const error = getContractError(err as BaseError, {
93
+ abi,
94
+ address,
95
+ args,
96
+ docsPath: '/docs/contract/multicall',
97
+ functionName,
98
+ })
99
+ if (!allowFailure) throw error
100
+ return { error, result: undefined, status: 'failure' }
101
+ }
102
+ }) as MulticallResults<TContracts, TAllowFailure>
103
+ }