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
@@ -4,9 +4,9 @@ import { RpcError } from './rpc'
4
4
  export class RequestError extends BaseError {
5
5
  constructor(
6
6
  err: Error,
7
- { docsPath, humanMessage }: { docsPath?: string; humanMessage: string },
7
+ { docsPath, shortMessage }: { docsPath?: string; shortMessage: string },
8
8
  ) {
9
- super(humanMessage, {
9
+ super(shortMessage, {
10
10
  cause: err,
11
11
  docsPath,
12
12
  })
@@ -19,9 +19,9 @@ export class RpcRequestError extends RequestError {
19
19
 
20
20
  constructor(
21
21
  err: RpcError,
22
- { docsPath, humanMessage }: { docsPath?: string; humanMessage: string },
22
+ { docsPath, shortMessage }: { docsPath?: string; shortMessage: string },
23
23
  ) {
24
- super(err, { docsPath, humanMessage })
24
+ super(err, { docsPath, shortMessage })
25
25
  this.code = err.code
26
26
  this.name = err.name
27
27
  }
@@ -33,7 +33,7 @@ export class ParseRpcError extends RpcRequestError {
33
33
 
34
34
  constructor(err: RpcError) {
35
35
  super(err, {
36
- humanMessage:
36
+ shortMessage:
37
37
  'Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.',
38
38
  })
39
39
  }
@@ -44,7 +44,7 @@ export class InvalidRequestRpcError extends RpcRequestError {
44
44
  code = -32600
45
45
 
46
46
  constructor(err: RpcError) {
47
- super(err, { humanMessage: 'JSON is not a valid request object.' })
47
+ super(err, { shortMessage: 'JSON is not a valid request object.' })
48
48
  }
49
49
  }
50
50
 
@@ -54,7 +54,7 @@ export class MethodNotFoundRpcError extends RpcRequestError {
54
54
 
55
55
  constructor(err: RpcError) {
56
56
  super(err, {
57
- humanMessage: 'The method does not exist / is not available.',
57
+ shortMessage: 'The method does not exist / is not available.',
58
58
  })
59
59
  }
60
60
  }
@@ -65,7 +65,7 @@ export class InvalidParamsRpcError extends RpcRequestError {
65
65
 
66
66
  constructor(err: RpcError) {
67
67
  super(err, {
68
- humanMessage: [
68
+ shortMessage: [
69
69
  'Invalid parameters were provided to the RPC method.',
70
70
  'Double check you have provided the correct parameters.',
71
71
  ].join('\n'),
@@ -78,7 +78,7 @@ export class InternalRpcError extends RpcRequestError {
78
78
  code = -32603
79
79
 
80
80
  constructor(err: RpcError) {
81
- super(err, { humanMessage: 'An internal error was received.' })
81
+ super(err, { shortMessage: 'An internal error was received.' })
82
82
  }
83
83
  }
84
84
 
@@ -88,7 +88,7 @@ export class InvalidInputRpcError extends RpcRequestError {
88
88
 
89
89
  constructor(err: RpcError) {
90
90
  super(err, {
91
- humanMessage: [
91
+ shortMessage: [
92
92
  'Missing or invalid parameters.',
93
93
  'Double check you have provided the correct parameters.',
94
94
  ].join('\n'),
@@ -101,7 +101,7 @@ export class ResourceNotFoundRpcError extends RpcRequestError {
101
101
  code = -32001
102
102
 
103
103
  constructor(err: RpcError) {
104
- super(err, { humanMessage: 'Requested resource not found.' })
104
+ super(err, { shortMessage: 'Requested resource not found.' })
105
105
  }
106
106
  }
107
107
 
@@ -110,7 +110,7 @@ export class ResourceUnavailableRpcError extends RpcRequestError {
110
110
  code = -32002
111
111
 
112
112
  constructor(err: RpcError) {
113
- super(err, { humanMessage: 'Requested resource not available.' })
113
+ super(err, { shortMessage: 'Requested resource not available.' })
114
114
  }
115
115
  }
116
116
 
@@ -119,7 +119,7 @@ export class TransactionRejectedRpcError extends RpcRequestError {
119
119
  code = -32003
120
120
 
121
121
  constructor(err: RpcError) {
122
- super(err, { humanMessage: 'Transaction creation failed.' })
122
+ super(err, { shortMessage: 'Transaction creation failed.' })
123
123
  }
124
124
  }
125
125
 
@@ -128,7 +128,7 @@ export class MethodNotSupportedRpcError extends RpcRequestError {
128
128
  code = -32004
129
129
 
130
130
  constructor(err: RpcError) {
131
- super(err, { humanMessage: 'Method is not implemented.' })
131
+ super(err, { shortMessage: 'Method is not implemented.' })
132
132
  }
133
133
  }
134
134
 
@@ -137,7 +137,7 @@ export class LimitExceededRpcError extends RpcRequestError {
137
137
  code = -32005
138
138
 
139
139
  constructor(err: RpcError) {
140
- super(err, { humanMessage: 'Request exceeds defined limit.' })
140
+ super(err, { shortMessage: 'Request exceeds defined limit.' })
141
141
  }
142
142
  }
143
143
 
@@ -147,7 +147,7 @@ export class JsonRpcVersionUnsupportedError extends RpcRequestError {
147
147
 
148
148
  constructor(err: RpcError) {
149
149
  super(err, {
150
- humanMessage: 'Version of JSON-RPC protocol is not supported.',
150
+ shortMessage: 'Version of JSON-RPC protocol is not supported.',
151
151
  })
152
152
  }
153
153
  }
@@ -157,7 +157,7 @@ export class UnknownRpcError extends RequestError {
157
157
 
158
158
  constructor(err: Error) {
159
159
  super(err, {
160
- humanMessage: 'An unknown RPC error occurred.',
160
+ shortMessage: 'An unknown RPC error occurred.',
161
161
  })
162
162
  }
163
163
  }
@@ -21,8 +21,7 @@ test('RpcError', () => {
21
21
  Request body: {"foo":"bar"}
22
22
 
23
23
  Details: Error
24
- Version: viem@1.0.2
25
- Internal Error: {"code":420,"message":"Error"}]
24
+ Version: viem@1.0.2]
26
25
  `)
27
26
  })
28
27
 
@@ -19,40 +19,40 @@ describe('TransactionNotFoundError', () => {
19
19
  expect(
20
20
  new TransactionNotFoundError({ blockHash: '0x123', index: 420 }),
21
21
  ).toMatchInlineSnapshot(`
22
- [TransactionNotFoundError: Transaction at block hash "0x123" at index "420" could not be found.
22
+ [TransactionNotFoundError: Transaction at block hash "0x123" at index "420" could not be found.
23
23
 
24
- Version: viem@1.0.2]
25
- `)
24
+ Version: viem@1.0.2]
25
+ `)
26
26
  })
27
27
 
28
28
  test('blockTag', async () => {
29
29
  expect(
30
30
  new TransactionNotFoundError({ blockTag: 'latest', index: 420 }),
31
31
  ).toMatchInlineSnapshot(`
32
- [TransactionNotFoundError: Transaction at block time "latest" at index "420" could not be found.
32
+ [TransactionNotFoundError: Transaction at block time "latest" at index "420" could not be found.
33
33
 
34
- Version: viem@1.0.2]
35
- `)
34
+ Version: viem@1.0.2]
35
+ `)
36
36
  })
37
37
 
38
38
  test('blockNumber', async () => {
39
39
  expect(
40
40
  new TransactionNotFoundError({ blockNumber: 42069n, index: 420 }),
41
41
  ).toMatchInlineSnapshot(`
42
- [TransactionNotFoundError: Transaction at block number "42069" at index "420" could not be found.
42
+ [TransactionNotFoundError: Transaction at block number "42069" at index "420" could not be found.
43
43
 
44
- Version: viem@1.0.2]
45
- `)
44
+ Version: viem@1.0.2]
45
+ `)
46
46
  })
47
47
 
48
48
  test('hash', async () => {
49
49
  expect(
50
50
  new TransactionNotFoundError({ hash: '0x123' }),
51
51
  ).toMatchInlineSnapshot(`
52
- [TransactionNotFoundError: Transaction with hash "0x123" could not be found.
52
+ [TransactionNotFoundError: Transaction with hash "0x123" could not be found.
53
53
 
54
- Version: viem@1.0.2]
55
- `)
54
+ Version: viem@1.0.2]
55
+ `)
56
56
  })
57
57
  })
58
58
 
@@ -6,7 +6,6 @@ test('UrlRequiredError', () => {
6
6
  [ViemError: No URL was provided to the Transport. Please provide a valid RPC URL to the Transport.
7
7
 
8
8
  Docs: https://viem.sh/docs/clients/intro
9
-
10
9
  Version: viem@1.0.2]
11
10
  `)
12
11
  })
package/src/index.test.ts CHANGED
@@ -19,6 +19,9 @@ test('exports actions', () => {
19
19
  "AbiFunctionSignatureNotFoundError": [Function],
20
20
  "BaseError": [Function],
21
21
  "BlockNotFoundError": [Function],
22
+ "ContractFunctionExecutionError": [Function],
23
+ "ContractFunctionRevertedError": [Function],
24
+ "ContractFunctionZeroDataError": [Function],
22
25
  "DataLengthTooLongError": [Function],
23
26
  "DataLengthTooShortError": [Function],
24
27
  "FilterTypeNotSupportedError": [Function],
@@ -151,6 +154,50 @@ test('exports actions', () => {
151
154
  "isHex": [Function],
152
155
  "keccak256": [Function],
153
156
  "mine": [Function],
157
+ "multicall": [Function],
158
+ "multicall3Abi": [
159
+ {
160
+ "inputs": [
161
+ {
162
+ "components": [
163
+ {
164
+ "name": "target",
165
+ "type": "address",
166
+ },
167
+ {
168
+ "name": "allowFailure",
169
+ "type": "bool",
170
+ },
171
+ {
172
+ "name": "callData",
173
+ "type": "bytes",
174
+ },
175
+ ],
176
+ "name": "calls",
177
+ "type": "tuple[]",
178
+ },
179
+ ],
180
+ "name": "aggregate3",
181
+ "outputs": [
182
+ {
183
+ "components": [
184
+ {
185
+ "name": "success",
186
+ "type": "bool",
187
+ },
188
+ {
189
+ "name": "returnData",
190
+ "type": "bytes",
191
+ },
192
+ ],
193
+ "name": "returnData",
194
+ "type": "tuple[]",
195
+ },
196
+ ],
197
+ "stateMutability": "view",
198
+ "type": "function",
199
+ },
200
+ ],
154
201
  "numberToBytes": [Function],
155
202
  "numberToHex": [Function],
156
203
  "pad": [Function],
package/src/index.ts CHANGED
@@ -45,6 +45,7 @@ export type {
45
45
  ImpersonateAccountArgs,
46
46
  IncreaseTimeArgs,
47
47
  MineArgs,
48
+ MulticallArgs,
48
49
  OnBlock,
49
50
  OnBlockNumber,
50
51
  OnBlockNumberResponse,
@@ -124,6 +125,7 @@ export {
124
125
  increaseTime,
125
126
  inspectTxpool,
126
127
  mine,
128
+ multicall,
127
129
  readContract,
128
130
  removeBlockTimestampInterval,
129
131
  reset,
@@ -191,6 +193,8 @@ export {
191
193
  webSocket,
192
194
  } from './clients'
193
195
 
196
+ export { multicall3Abi } from './constants'
197
+
194
198
  export {
195
199
  AbiConstructorNotFoundError,
196
200
  AbiConstructorParamsNotFoundError,
@@ -206,6 +210,9 @@ export {
206
210
  AbiFunctionSignatureNotFoundError,
207
211
  BaseError,
208
212
  BlockNotFoundError,
213
+ ContractFunctionExecutionError,
214
+ ContractFunctionRevertedError,
215
+ ContractFunctionZeroDataError,
209
216
  DataLengthTooLongError,
210
217
  DataLengthTooShortError,
211
218
  HttpRequestError,
@@ -254,6 +261,8 @@ export type {
254
261
  BlockNumber,
255
262
  BlockTag,
256
263
  ByteArray,
264
+ Chain,
265
+ ContractConfig,
257
266
  Hex,
258
267
  FeeHistory,
259
268
  FeeValues,
@@ -300,6 +309,7 @@ export type {
300
309
  GetContractAddressOptions,
301
310
  GetCreateAddressOptions,
302
311
  GetCreate2AddressOptions,
312
+ GetAbiItemArgs,
303
313
  EncodeRlpResponse,
304
314
  FormattedBlock,
305
315
  FormattedTransaction,
@@ -8,15 +8,17 @@ import type {
8
8
  AbiParameter,
9
9
  AbiParameterToPrimitiveType,
10
10
  AbiParametersToPrimitiveTypes,
11
+ AbiStateMutability,
11
12
  ExtractAbiFunction,
12
13
  ExtractAbiEvent,
13
14
  ExtractAbiEventNames,
14
15
  ExtractAbiError,
15
- AbiStateMutability,
16
+ ExtractAbiErrorNames,
16
17
  ExtractAbiFunctionNames,
18
+ Narrow,
17
19
  } from 'abitype'
18
- import { TransactionRequest } from './transaction'
19
-
20
+ import type { Address } from './misc'
21
+ import type { TransactionRequest } from './transaction'
20
22
  import type { Trim } from './utils'
21
23
 
22
24
  //////////////////////////////////////////////////////////////////////
@@ -163,6 +165,18 @@ export type ExtractEventArgsFromAbi<
163
165
  args?: TArgs
164
166
  }
165
167
 
168
+ export type ExtractErrorNameFromAbi<
169
+ TAbi extends Abi | readonly unknown[] = Abi,
170
+ TErrorName extends string = string,
171
+ > = TAbi extends Abi
172
+ ? ExtractAbiErrorNames<TAbi> extends infer AbiErrorNames
173
+ ?
174
+ | AbiErrorNames
175
+ | (TErrorName extends AbiErrorNames ? TErrorName : never)
176
+ | (Abi extends TAbi ? string : never)
177
+ : never
178
+ : TErrorName
179
+
166
180
  export type ExtractEventNameFromAbi<
167
181
  TAbi extends Abi | readonly unknown[] = Abi,
168
182
  TEventName extends string = string,
@@ -191,6 +205,23 @@ export type ExtractFunctionNameFromAbi<
191
205
  : never
192
206
  : TFunctionName
193
207
 
208
+ type ExtractNames<TAbi extends Abi> =
209
+ | ExtractAbiFunctionNames<TAbi>
210
+ | ExtractAbiEventNames<TAbi>
211
+ | ExtractAbiErrorNames<TAbi>
212
+
213
+ export type ExtractNameFromAbi<
214
+ TAbi extends Abi | readonly unknown[] = Abi,
215
+ TName extends string = string,
216
+ > = TAbi extends Abi
217
+ ? ExtractNames<TAbi> extends infer AbiNames
218
+ ?
219
+ | AbiNames
220
+ | (TName extends AbiNames ? TName : never)
221
+ | (Abi extends TAbi ? string : never)
222
+ : never
223
+ : TName
224
+
194
225
  export type ExtractResultFromAbi<
195
226
  TAbi extends Abi | readonly unknown[] = Abi,
196
227
  TFunctionName extends string = string,
@@ -214,6 +245,9 @@ export type ExtractResultFromAbi<
214
245
  //////////////////////////////////////////////////////////////////////
215
246
  // Event/Function Definitions
216
247
 
248
+ // REFACTOR: Remove below once we implement `ParseAbi`.
249
+ // https://github.com/wagmi-dev/viem/issues/29
250
+
217
251
  export type EventDefinition = `${string}(${string})`
218
252
 
219
253
  type ExtractArgsFromDefinitionConfig = {
@@ -326,7 +360,24 @@ export type ExtractArgsFromFunctionDefinition<TDef> = ExtractArgsFromDefinition<
326
360
  >
327
361
 
328
362
  //////////////////////////////////////////////////////////////////////
329
- // Call Args
363
+ // Args
364
+
365
+ export type ContractConfig<
366
+ TAbi extends Abi | readonly unknown[] = Abi,
367
+ TFunctionName extends string = string,
368
+ TAbiStateMutability extends AbiStateMutability = AbiStateMutability,
369
+ > = {
370
+ /** Contract ABI */
371
+ abi: Narrow<TAbi>
372
+ /** Contract address */
373
+ address: Address
374
+ /** Function to invoke on the contract */
375
+ functionName: ExtractFunctionNameFromAbi<
376
+ TAbi,
377
+ TFunctionName,
378
+ TAbiStateMutability
379
+ >
380
+ } & ExtractArgsFromAbi<TAbi, TFunctionName>
330
381
 
331
382
  export type GetValue<
332
383
  TAbi extends Abi | readonly unknown[],
@@ -11,15 +11,18 @@ export type { Chain } from './chain'
11
11
  export type {
12
12
  AbiItem,
13
13
  AbiEventParametersToPrimitiveTypes,
14
+ ContractConfig,
14
15
  EventDefinition,
15
16
  ExtractArgsFromAbi,
16
17
  ExtractArgsFromEventDefinition,
17
18
  ExtractArgsFromFunctionDefinition,
18
19
  ExtractConstructorArgsFromAbi,
19
20
  ExtractErrorArgsFromAbi,
21
+ ExtractErrorNameFromAbi,
20
22
  ExtractEventArgsFromAbi,
21
23
  ExtractEventNameFromAbi,
22
24
  ExtractFunctionNameFromAbi,
25
+ ExtractNameFromAbi,
23
26
  ExtractResultFromAbi,
24
27
  GetValue,
25
28
  } from './contract'
@@ -40,6 +43,8 @@ export type { Log } from './log'
40
43
 
41
44
  export type { Address, ByteArray, Hex, Hash, LogTopic } from './misc'
42
45
 
46
+ export type { MulticallContracts } from './multicall'
47
+
43
48
  export type {
44
49
  Index,
45
50
  Quantity,
@@ -0,0 +1,82 @@
1
+ import { Abi } from 'abitype';
2
+ import { ContractConfig, ExtractResultFromAbi } from './contract';
3
+
4
+ type MAXIMUM_DEPTH = 20
5
+
6
+ export type Contract<
7
+ TAbi extends Abi | readonly unknown[] = Abi | readonly unknown[],
8
+ TFunctionName extends string = string,
9
+ > = { abi: TAbi; functionName: TFunctionName }
10
+
11
+ export type MulticallContracts<
12
+ TContracts extends Contract[],
13
+ TProperties extends Record<string, any> = object,
14
+ Result extends any[] = [],
15
+ Depth extends ReadonlyArray<number> = [],
16
+ > = Depth['length'] extends MAXIMUM_DEPTH
17
+ ? (ContractConfig & TProperties)[]
18
+ : TContracts extends []
19
+ ? []
20
+ : TContracts extends [infer Head extends Contract]
21
+ ? [
22
+ ...Result,
23
+ ContractConfig<Head['abi'], Head['functionName']> &
24
+ TProperties,
25
+ ]
26
+ : TContracts extends [
27
+ infer Head extends Contract,
28
+ ...infer Tail extends Contract[],
29
+ ]
30
+ ? MulticallContracts<
31
+ [...Tail],
32
+ TProperties,
33
+ [
34
+ ...Result,
35
+ ContractConfig<Head['abi'], Head['functionName']> &
36
+ TProperties,
37
+ ],
38
+ [...Depth, 1]
39
+ >
40
+ : unknown[] extends TContracts
41
+ ? TContracts
42
+ : // If `TContracts` is *some* array but we couldn't assign `unknown[]` to it, then it must hold some known/homogenous type!
43
+ // use this to infer the param types in the case of Array.map() argument
44
+ TContracts extends ContractConfig<infer TAbi, infer TFunctionName>[]
45
+ ? (ContractConfig<TAbi, TFunctionName> & TProperties)[]
46
+ : (ContractConfig & TProperties)[]
47
+
48
+
49
+ export type MulticallResult<Result, TAllowFailure extends boolean = true> = TAllowFailure extends true ? ({
50
+ error?: undefined
51
+ result: Result
52
+ status: 'success'
53
+ } | {
54
+ error: Error
55
+ result?: undefined
56
+ status: 'error'
57
+ }) : Result
58
+
59
+ export type MulticallResults<
60
+ TContracts extends Contract[],
61
+ TAllowFailure extends boolean = true,
62
+ Result extends any[] = [],
63
+ Depth extends ReadonlyArray<number> = [],
64
+ > = Depth['length'] extends MAXIMUM_DEPTH
65
+ ? MulticallResult<ExtractResultFromAbi, TAllowFailure>[]
66
+ : TContracts extends []
67
+ ? []
68
+ : TContracts extends [infer Head extends Contract]
69
+ ? [...Result, MulticallResult<ExtractResultFromAbi<Head['abi'], Head['functionName']>, TAllowFailure>]
70
+ : TContracts extends [
71
+ infer Head extends Contract,
72
+ ...infer Tail extends Contract[],
73
+ ]
74
+ ? MulticallResults<
75
+ [...Tail],
76
+ TAllowFailure,
77
+ [...Result, MulticallResult<ExtractResultFromAbi<Head['abi'], Head['functionName']>, TAllowFailure>],
78
+ [...Depth, 1]
79
+ >
80
+ : TContracts extends ContractConfig<infer TAbi, infer TFunctionName>[]
81
+ ? MulticallResult<ExtractResultFromAbi<TAbi, TFunctionName>, TAllowFailure>[]
82
+ : MulticallResult<ExtractResultFromAbi, TAllowFailure>[]
@@ -1582,7 +1582,6 @@ test('invalid type', () => {
1582
1582
  Please provide a valid ABI type.
1583
1583
 
1584
1584
  Docs: https://viem.sh/docs/contract/decodeAbi
1585
-
1586
1585
  Version: viem@1.0.2"
1587
1586
  `)
1588
1587
  })
@@ -1604,7 +1603,7 @@ test('error: zero data', () => {
1604
1603
  stateMutability: 'pure',
1605
1604
  type: 'function',
1606
1605
  },
1607
- ] as const,
1606
+ ],
1608
1607
  data: '0x',
1609
1608
  }),
1610
1609
  ).toThrowErrorMatchingInlineSnapshot(`
@@ -2,6 +2,7 @@ import {
2
2
  AbiParameter,
3
3
  AbiParametersToPrimitiveTypes,
4
4
  AbiParameterToPrimitiveType,
5
+ Narrow,
5
6
  } from 'abitype'
6
7
 
7
8
  import {
@@ -15,21 +16,24 @@ import { size, slice, trim } from '../data'
15
16
  import { hexToBigInt, hexToBool, hexToNumber, hexToString } from '../encoding'
16
17
  import { getArrayComponents } from './encodeAbi'
17
18
 
18
- export type DecodeAbiArgs<TParams extends readonly AbiParameter[]> = {
19
+ export type DecodeAbiArgs<
20
+ TParams extends
21
+ | readonly AbiParameter[]
22
+ | readonly unknown[] = readonly AbiParameter[],
23
+ > = {
19
24
  data: Hex
20
- params: TParams
25
+ params: Narrow<TParams>
21
26
  }
22
27
 
23
- export function decodeAbi<TParams extends readonly AbiParameter[]>({
24
- data,
25
- params,
26
- }: DecodeAbiArgs<TParams>) {
28
+ export function decodeAbi<
29
+ TParams extends readonly AbiParameter[] | readonly unknown[],
30
+ >({ data, params }: DecodeAbiArgs<TParams>) {
27
31
  if (data === '0x' && params.length > 0) throw new AbiDecodingZeroDataError()
28
32
  if (size(data) % 32 !== 0)
29
33
  throw new AbiDecodingDataSizeInvalidError(size(data))
30
34
  const values = decodeParams({
31
35
  data,
32
- params,
36
+ params: params as readonly AbiParameter[],
33
37
  })
34
38
  if (values.length === 0) return undefined
35
39
  return values