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
@@ -0,0 +1,547 @@
1
+ import { AbiParameter } from 'abitype'
2
+ import { describe, expect, test } from 'vitest'
3
+ import { wagmiContractConfig } from '../../_test'
4
+ import { encodeBytes } from '../encoding'
5
+ import { getAbiItem, isArgOfType } from './getAbiItem'
6
+
7
+ test('default', () => {
8
+ expect(
9
+ getAbiItem({
10
+ abi: wagmiContractConfig.abi,
11
+ name: 'balanceOf',
12
+ args: ['0x0000000000000000000000000000000000000000'],
13
+ }),
14
+ ).toMatchInlineSnapshot(`
15
+ {
16
+ "inputs": [
17
+ {
18
+ "name": "owner",
19
+ "type": "address",
20
+ },
21
+ ],
22
+ "name": "balanceOf",
23
+ "outputs": [
24
+ {
25
+ "name": "",
26
+ "type": "uint256",
27
+ },
28
+ ],
29
+ "stateMutability": "view",
30
+ "type": "function",
31
+ }
32
+ `)
33
+ })
34
+
35
+ test('no matching name', () => {
36
+ expect(
37
+ getAbiItem({
38
+ abi: [],
39
+ // @ts-expect-error
40
+ name: 'balanceOf',
41
+ args: ['0x0000000000000000000000000000000000000000'],
42
+ }),
43
+ ).toMatchInlineSnapshot('undefined')
44
+ })
45
+
46
+ test('overloads: no inputs', () => {
47
+ expect(
48
+ getAbiItem({
49
+ abi: [
50
+ {
51
+ name: 'balanceOf',
52
+ outputs: [{ name: 'x', type: 'uint256' }],
53
+ stateMutability: 'view',
54
+ type: 'function',
55
+ },
56
+ {
57
+ inputs: [{ name: 'x', type: 'uint256' }],
58
+ name: 'balanceOf',
59
+ outputs: [{ name: '', type: 'uint256' }],
60
+ stateMutability: 'view',
61
+ type: 'function',
62
+ },
63
+ ],
64
+ name: 'balanceOf',
65
+ args: ['0x0000000000000000000000000000000000000000'],
66
+ }),
67
+ ).toMatchInlineSnapshot(`
68
+ {
69
+ "name": "balanceOf",
70
+ "outputs": [
71
+ {
72
+ "name": "x",
73
+ "type": "uint256",
74
+ },
75
+ ],
76
+ "stateMutability": "view",
77
+ "type": "function",
78
+ }
79
+ `)
80
+ })
81
+
82
+ test('overloads: undefined inputs', () => {
83
+ expect(
84
+ getAbiItem({
85
+ abi: [
86
+ {
87
+ inputs: undefined,
88
+ name: 'balanceOf',
89
+ outputs: [{ name: '', type: 'uint256' }],
90
+ stateMutability: 'view',
91
+ type: 'function',
92
+ },
93
+ {
94
+ inputs: [],
95
+ name: 'balanceOf',
96
+ outputs: [{ name: '', type: 'uint256' }],
97
+ stateMutability: 'view',
98
+ type: 'function',
99
+ },
100
+ ],
101
+ name: 'balanceOf',
102
+ args: ['0x0000000000000000000000000000000000000000'],
103
+ }),
104
+ ).toMatchInlineSnapshot(`
105
+ {
106
+ "inputs": undefined,
107
+ "name": "balanceOf",
108
+ "outputs": [
109
+ {
110
+ "name": "",
111
+ "type": "uint256",
112
+ },
113
+ ],
114
+ "stateMutability": "view",
115
+ "type": "function",
116
+ }
117
+ `)
118
+ })
119
+
120
+ test('overloads: no args', () => {
121
+ expect(
122
+ getAbiItem({
123
+ abi: [
124
+ {
125
+ inputs: [{ name: '', type: 'uint256' }],
126
+ name: 'balanceOf',
127
+ outputs: [{ name: '', type: 'uint256' }],
128
+ stateMutability: 'view',
129
+ type: 'function',
130
+ },
131
+ {
132
+ inputs: [],
133
+ name: 'balanceOf',
134
+ outputs: [{ name: '', type: 'uint256' }],
135
+ stateMutability: 'view',
136
+ type: 'function',
137
+ },
138
+ ],
139
+ name: 'balanceOf',
140
+ }),
141
+ ).toMatchInlineSnapshot(`
142
+ {
143
+ "inputs": [],
144
+ "name": "balanceOf",
145
+ "outputs": [
146
+ {
147
+ "name": "",
148
+ "type": "uint256",
149
+ },
150
+ ],
151
+ "stateMutability": "view",
152
+ "type": "function",
153
+ }
154
+ `)
155
+ })
156
+
157
+ test('overload: different lengths', () => {
158
+ expect(
159
+ getAbiItem({
160
+ abi: wagmiContractConfig.abi,
161
+ name: 'safeTransferFrom',
162
+ args: [
163
+ '0x0000000000000000000000000000000000000000',
164
+ '0x0000000000000000000000000000000000000000',
165
+ 420n,
166
+ ],
167
+ }),
168
+ ).toMatchInlineSnapshot(`
169
+ {
170
+ "inputs": [
171
+ {
172
+ "name": "from",
173
+ "type": "address",
174
+ },
175
+ {
176
+ "name": "to",
177
+ "type": "address",
178
+ },
179
+ {
180
+ "name": "tokenId",
181
+ "type": "uint256",
182
+ },
183
+ ],
184
+ "name": "safeTransferFrom",
185
+ "outputs": [],
186
+ "stateMutability": "nonpayable",
187
+ "type": "function",
188
+ }
189
+ `)
190
+
191
+ expect(
192
+ getAbiItem({
193
+ abi: wagmiContractConfig.abi,
194
+ name: 'safeTransferFrom',
195
+ args: [
196
+ '0x0000000000000000000000000000000000000000',
197
+ '0x0000000000000000000000000000000000000000',
198
+ 420n,
199
+ '0x0000000000000000000000000000000000000000',
200
+ ],
201
+ }),
202
+ ).toMatchInlineSnapshot(`
203
+ {
204
+ "inputs": [
205
+ {
206
+ "name": "from",
207
+ "type": "address",
208
+ },
209
+ {
210
+ "name": "to",
211
+ "type": "address",
212
+ },
213
+ {
214
+ "name": "tokenId",
215
+ "type": "uint256",
216
+ },
217
+ {
218
+ "name": "_data",
219
+ "type": "bytes",
220
+ },
221
+ ],
222
+ "name": "safeTransferFrom",
223
+ "outputs": [],
224
+ "stateMutability": "nonpayable",
225
+ "type": "function",
226
+ }
227
+ `)
228
+ })
229
+
230
+ test('overload: different types', () => {
231
+ const abi = [
232
+ {
233
+ inputs: [],
234
+ name: 'mint',
235
+ outputs: [],
236
+ stateMutability: 'nonpayable',
237
+ type: 'function',
238
+ },
239
+ {
240
+ inputs: [{ name: 'tokenId', type: 'uint256' }],
241
+ name: 'mint',
242
+ outputs: [],
243
+ stateMutability: 'nonpayable',
244
+ type: 'function',
245
+ },
246
+ {
247
+ inputs: [{ name: 'tokenId', type: 'string' }],
248
+ name: 'mint',
249
+ outputs: [],
250
+ stateMutability: 'nonpayable',
251
+ type: 'function',
252
+ },
253
+ ]
254
+
255
+ expect(
256
+ getAbiItem({
257
+ abi,
258
+ name: 'mint',
259
+ }),
260
+ ).toMatchInlineSnapshot(`
261
+ {
262
+ "inputs": [],
263
+ "name": "mint",
264
+ "outputs": [],
265
+ "stateMutability": "nonpayable",
266
+ "type": "function",
267
+ }
268
+ `)
269
+
270
+ expect(
271
+ getAbiItem({
272
+ abi,
273
+ name: 'mint',
274
+ args: [420n],
275
+ }),
276
+ ).toMatchInlineSnapshot(`
277
+ {
278
+ "inputs": [
279
+ {
280
+ "name": "tokenId",
281
+ "type": "uint256",
282
+ },
283
+ ],
284
+ "name": "mint",
285
+ "outputs": [],
286
+ "stateMutability": "nonpayable",
287
+ "type": "function",
288
+ }
289
+ `)
290
+
291
+ expect(
292
+ getAbiItem({
293
+ abi,
294
+ name: 'mint',
295
+ args: ['foo'],
296
+ }),
297
+ ).toMatchInlineSnapshot(`
298
+ {
299
+ "inputs": [
300
+ {
301
+ "name": "tokenId",
302
+ "type": "string",
303
+ },
304
+ ],
305
+ "name": "mint",
306
+ "outputs": [],
307
+ "stateMutability": "nonpayable",
308
+ "type": "function",
309
+ }
310
+ `)
311
+ })
312
+
313
+ test('overloads: tuple', () => {
314
+ expect(
315
+ getAbiItem({
316
+ abi: [
317
+ {
318
+ inputs: [
319
+ { name: 'foo', type: 'uint256' },
320
+ {
321
+ name: 'bar',
322
+ type: 'tuple',
323
+ components: [
324
+ { name: 'a', type: 'string' },
325
+ {
326
+ name: 'b',
327
+ type: 'tuple',
328
+ components: [
329
+ { name: 'merp', type: 'string' },
330
+ { name: 'meep', type: 'string' },
331
+ ],
332
+ },
333
+ { name: 'c', type: 'uint256' },
334
+ ],
335
+ },
336
+ ],
337
+ name: 'foo',
338
+ outputs: [],
339
+ stateMutability: 'nonpayable',
340
+ type: 'function',
341
+ },
342
+ {
343
+ inputs: [
344
+ { name: 'foo', type: 'uint256' },
345
+ {
346
+ name: 'bar',
347
+ type: 'tuple',
348
+ components: [
349
+ { name: 'a', type: 'string' },
350
+ {
351
+ name: 'b',
352
+ type: 'tuple',
353
+ components: [
354
+ { name: 'merp', type: 'string' },
355
+ { name: 'meep', type: 'string' },
356
+ ],
357
+ },
358
+ { name: 'c', type: 'address' },
359
+ ],
360
+ },
361
+ ],
362
+ name: 'foo',
363
+ outputs: [],
364
+ stateMutability: 'nonpayable',
365
+ type: 'function',
366
+ },
367
+ ],
368
+ name: 'foo',
369
+ args: [
370
+ 420n,
371
+ {
372
+ a: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
373
+ b: { merp: 'test', meep: 'test' },
374
+ c: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
375
+ },
376
+ ],
377
+ }),
378
+ ).toMatchInlineSnapshot(`
379
+ {
380
+ "inputs": [
381
+ {
382
+ "name": "foo",
383
+ "type": "uint256",
384
+ },
385
+ {
386
+ "components": [
387
+ {
388
+ "name": "a",
389
+ "type": "string",
390
+ },
391
+ {
392
+ "components": [
393
+ {
394
+ "name": "merp",
395
+ "type": "string",
396
+ },
397
+ {
398
+ "name": "meep",
399
+ "type": "string",
400
+ },
401
+ ],
402
+ "name": "b",
403
+ "type": "tuple",
404
+ },
405
+ {
406
+ "name": "c",
407
+ "type": "address",
408
+ },
409
+ ],
410
+ "name": "bar",
411
+ "type": "tuple",
412
+ },
413
+ ],
414
+ "name": "foo",
415
+ "outputs": [],
416
+ "stateMutability": "nonpayable",
417
+ "type": "function",
418
+ }
419
+ `)
420
+ })
421
+
422
+ describe.each([
423
+ // array
424
+ { arg: ['foo'], abiParameter: { type: 'string[]' }, expected: true },
425
+ { arg: ['foo'], abiParameter: { type: 'string[1]' }, expected: true },
426
+ { arg: [['foo']], abiParameter: { type: 'string[][]' }, expected: true },
427
+ { arg: [['foo']], abiParameter: { type: 'string[][1]' }, expected: true },
428
+ {
429
+ arg: [1n],
430
+ abiParameter: { type: 'uint256[]' },
431
+ expected: true,
432
+ },
433
+ {
434
+ arg: [{ foo: 1n, bar: [{ baz: 1n }] }],
435
+ abiParameter: {
436
+ type: 'tuple[]',
437
+ components: [
438
+ { name: 'foo', type: 'uint256' },
439
+ {
440
+ name: 'bar',
441
+ type: 'tuple[]',
442
+ components: [{ name: 'baz', type: 'uint256' }],
443
+ },
444
+ ],
445
+ },
446
+ expected: true,
447
+ },
448
+ { arg: ['foo'], abiParameter: { type: 'string[test]' }, expected: false },
449
+ { arg: [1], abiParameter: { type: 'uint69[]' }, expected: false },
450
+ // address
451
+ {
452
+ arg: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
453
+ abiParameter: { type: 'address' },
454
+ expected: true,
455
+ },
456
+ {
457
+ arg: 'A0Cf798816D4b9b9866b5330EEa46a18382f251e',
458
+ abiParameter: { type: 'address' },
459
+ expected: true,
460
+ },
461
+ { arg: 'test', abiParameter: { type: 'address' }, expected: false },
462
+ // bool
463
+ { arg: true, abiParameter: { type: 'bool' }, expected: true },
464
+ { arg: false, abiParameter: { type: 'bool' }, expected: true },
465
+ { arg: 'true', abiParameter: { type: 'bool' }, expected: false },
466
+ // bytes
467
+ { arg: 'foo', abiParameter: { type: 'bytes' }, expected: true },
468
+ { arg: 'foo', abiParameter: { type: 'bytes32' }, expected: true },
469
+ { arg: encodeBytes('foo'), abiParameter: { type: 'bytes' }, expected: true },
470
+ { arg: 1, abiParameter: { type: 'bytes32' }, expected: false },
471
+ // function
472
+ { arg: 'foo', abiParameter: { type: 'function' }, expected: true },
473
+ { arg: 1, abiParameter: { type: 'function' }, expected: false },
474
+ // int
475
+ { arg: 1, abiParameter: { type: 'int' }, expected: true },
476
+ { arg: 1n, abiParameter: { type: 'int' }, expected: true },
477
+ { arg: 1n, abiParameter: { type: 'int' }, expected: true },
478
+ { arg: 1, abiParameter: { type: 'uint' }, expected: true },
479
+ { arg: 1n, abiParameter: { type: 'uint' }, expected: true },
480
+ { arg: 1n, abiParameter: { type: 'uint' }, expected: true },
481
+ { arg: 1, abiParameter: { type: 'int256' }, expected: true },
482
+ { arg: 1, abiParameter: { type: 'uint256' }, expected: true },
483
+ { arg: 1, abiParameter: { type: 'int69' }, expected: false },
484
+ { arg: 1, abiParameter: { type: 'uint69' }, expected: false },
485
+ // string
486
+ { arg: 'foo', abiParameter: { type: 'string' }, expected: true },
487
+ { arg: 1, abiParameter: { type: 'string' }, expected: false },
488
+ // tuple
489
+ {
490
+ arg: { bar: 1, baz: 'test' },
491
+ abiParameter: {
492
+ name: 'foo',
493
+ type: 'tuple',
494
+ components: [
495
+ { name: 'bar', type: 'uint256' },
496
+ { name: 'baz', type: 'string' },
497
+ ],
498
+ },
499
+ expected: true,
500
+ },
501
+ {
502
+ arg: [1, 'test'],
503
+ abiParameter: {
504
+ name: 'foo',
505
+ type: 'tuple',
506
+ components: [
507
+ { name: 'bar', type: 'uint256' },
508
+ { name: 'baz', type: 'string' },
509
+ ],
510
+ },
511
+ expected: true,
512
+ },
513
+ {
514
+ arg: { bar: ['test'] },
515
+ abiParameter: {
516
+ name: 'foo',
517
+ type: 'tuple',
518
+ components: [
519
+ {
520
+ name: 'bar',
521
+ type: 'tuple',
522
+ components: [{ name: 'baz', type: 'string' }],
523
+ },
524
+ ],
525
+ },
526
+ expected: true,
527
+ },
528
+ {
529
+ arg: {},
530
+ abiParameter: {
531
+ name: 'foo',
532
+ type: 'tuple',
533
+ components: [
534
+ { name: 'bar', type: 'uint256' },
535
+ { name: 'baz', type: 'uint256' },
536
+ ],
537
+ },
538
+ expected: false,
539
+ },
540
+ ] as { arg: unknown; abiParameter: AbiParameter; expected: boolean }[])(
541
+ 'isArgOfType($arg, $abiParameter)',
542
+ ({ arg, abiParameter, expected }) => {
543
+ test(`isArgOfType: returns ${expected}`, () => {
544
+ expect(isArgOfType(arg, abiParameter)).toEqual(expected)
545
+ })
546
+ },
547
+ )
@@ -1,5 +1,95 @@
1
- import { Abi } from 'abitype'
1
+ import type { Abi, AbiParameter, Address, Narrow } from 'abitype'
2
+ import type { ExtractArgsFromAbi, ExtractNameFromAbi } from '../../types'
3
+ import { isAddress } from '../address'
2
4
 
3
- export function getAbiItem({ abi, name }: { abi: Abi; name: string }) {
4
- return abi.find((x) => 'name' in x && x.name === name)
5
+ export type GetAbiItemArgs<
6
+ TAbi extends Abi | readonly unknown[] = Abi,
7
+ TFunctionName extends string = any,
8
+ > = {
9
+ abi: Narrow<TAbi>
10
+ name: ExtractNameFromAbi<TAbi, TFunctionName>
11
+ } & Partial<ExtractArgsFromAbi<TAbi, TFunctionName>>
12
+
13
+ export function getAbiItem<
14
+ TAbi extends Abi | readonly unknown[],
15
+ TFunctionName extends string,
16
+ >({ abi, args = [], name }: GetAbiItemArgs<TAbi, TFunctionName>) {
17
+ const abiItems = (abi as Abi).filter((x) => 'name' in x && x.name === name)
18
+
19
+ if (abiItems.length === 0) return undefined
20
+ if (abiItems.length === 1) return abiItems[0]
21
+
22
+ for (const abiItem of abiItems) {
23
+ if (!('inputs' in abiItem)) continue
24
+ if (!args || args.length === 0) {
25
+ if (!abiItem.inputs || abiItem.inputs.length === 0) return abiItem
26
+ continue
27
+ }
28
+ if (!abiItem.inputs) continue
29
+ if (abiItem.inputs.length === 0) continue
30
+ const matched = (args as readonly unknown[]).every((arg, index) => {
31
+ const abiParameter = 'inputs' in abiItem && abiItem.inputs![index]
32
+ if (!abiParameter) return false
33
+ return isArgOfType(arg, abiParameter as AbiParameter)
34
+ })
35
+ if (matched) return abiItem
36
+ }
37
+ return abiItems[0]
38
+ }
39
+
40
+ export function isArgOfType(arg: unknown, abiParameter: AbiParameter): boolean {
41
+ const argType = typeof arg
42
+ const abiParameterType = abiParameter.type
43
+ switch (abiParameterType) {
44
+ case 'address':
45
+ return isAddress(arg as Address)
46
+ case 'bool':
47
+ return argType === 'boolean'
48
+ case 'function':
49
+ return argType === 'string'
50
+ case 'string':
51
+ return argType === 'string'
52
+ default: {
53
+ if (abiParameterType === 'tuple' && 'components' in abiParameter)
54
+ return Object.values(abiParameter.components).every(
55
+ (component, index) => {
56
+ return isArgOfType(
57
+ Object.values(arg as unknown[] | Record<string, unknown>)[index],
58
+ component as AbiParameter,
59
+ )
60
+ },
61
+ )
62
+
63
+ // `(u)int<M>`: (un)signed integer type of `M` bits, `0 < M <= 256`, `M % 8 == 0`
64
+ // https://regexr.com/6v8hp
65
+ if (
66
+ /^u?int(8|16|24|32|40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)?$/.test(
67
+ abiParameterType,
68
+ )
69
+ )
70
+ return argType === 'number' || argType === 'bigint'
71
+
72
+ // `bytes<M>`: binary type of `M` bytes, `0 < M <= 32`
73
+ // https://regexr.com/6va55
74
+ if (/^bytes([1-9]|1[0-9]|2[0-9]|3[0-2])?$/.test(abiParameterType))
75
+ return argType === 'string' || arg instanceof Uint8Array
76
+
77
+ // fixed-length (`<type>[M]`) and dynamic (`<type>[]`) arrays
78
+ // https://regexr.com/6va6i
79
+ if (/[a-z]+[1-9]{0,3}(\[[0-9]{0,}\])+$/.test(abiParameterType)) {
80
+ return (
81
+ Array.isArray(arg) &&
82
+ arg.every((x: unknown) =>
83
+ isArgOfType(x, {
84
+ ...abiParameter,
85
+ // Pop off `[]` or `[M]` from end of type
86
+ type: abiParameterType.replace(/(\[[0-9]{0,}\])$/, ''),
87
+ } as AbiParameter),
88
+ )
89
+ )
90
+ }
91
+
92
+ return false
93
+ }
94
+ }
5
95
  }
@@ -1,7 +1,10 @@
1
1
  export type { DecodeAbiArgs } from './decodeAbi'
2
2
  export { decodeAbi } from './decodeAbi'
3
3
 
4
- export type { DecodeErrorResultArgs } from './decodeErrorResult'
4
+ export type {
5
+ DecodeErrorResultArgs,
6
+ DecodeErrorResultResponse,
7
+ } from './decodeErrorResult'
5
8
  export { decodeErrorResult } from './decodeErrorResult'
6
9
 
7
10
  export type { DecodeFunctionDataArgs } from './decodeFunctionData'
@@ -35,4 +38,5 @@ export { formatAbiItemWithArgs } from './formatAbiItemWithArgs'
35
38
 
36
39
  export { formatAbiItem } from './formatAbiItem'
37
40
 
41
+ export type { GetAbiItemArgs } from './getAbiItem'
38
42
  export { getAbiItem } from './getAbiItem'
@@ -31,16 +31,16 @@ describe('errors', () => {
31
31
  expect(() =>
32
32
  getAddress('0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678az'),
33
33
  ).toThrowErrorMatchingInlineSnapshot(`
34
- "Address \\"0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678az\\" is invalid.
34
+ "Address \\"0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678az\\" is invalid.
35
35
 
36
- Version: viem@1.0.2"
37
- `)
36
+ Version: viem@1.0.2"
37
+ `)
38
38
  expect(() =>
39
39
  getAddress('0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678aff'),
40
40
  ).toThrowErrorMatchingInlineSnapshot(`
41
- "Address \\"0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678aff\\" is invalid.
41
+ "Address \\"0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678aff\\" is invalid.
42
42
 
43
- Version: viem@1.0.2"
44
- `)
43
+ Version: viem@1.0.2"
44
+ `)
45
45
  })
46
46
  })