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.
- package/dist/{chain-32f56cfa.d.ts → chain-4b39613a.d.ts} +2 -2
- package/dist/{chain-f12cdc7f.d.ts → chain-aa4898d0.d.ts} +1 -1
- package/dist/chains.d.ts +3 -3
- package/dist/chains.js +46 -46
- package/dist/chains.mjs +1 -1
- package/dist/{chunk-ZVGTYLKU.mjs → chunk-JMFNAGCD.mjs} +4 -3
- package/dist/chunk-JMFNAGCD.mjs.map +1 -0
- package/dist/{chunk-NHAKUPTF.js → chunk-MVD4Y3AE.js} +67 -63
- package/dist/chunk-MVD4Y3AE.js.map +1 -0
- package/dist/{chunk-YODUQCHJ.mjs → chunk-OZIBQJYR.mjs} +2 -2
- package/dist/{chunk-YODUQCHJ.mjs.map → chunk-OZIBQJYR.mjs.map} +0 -0
- package/dist/{chunk-FHXXG7I6.js → chunk-PMPJGAHY.js} +14 -13
- package/dist/chunk-PMPJGAHY.js.map +1 -0
- package/dist/{chunk-YFKR74XG.js → chunk-QA3IE5DU.js} +14 -14
- package/dist/{chunk-YFKR74XG.js.map → chunk-QA3IE5DU.js.map} +0 -0
- package/dist/{chunk-T7CBAKLI.mjs → chunk-QN2NCTEN.mjs} +2 -2
- package/dist/{chunk-T7CBAKLI.mjs.map → chunk-QN2NCTEN.mjs.map} +1 -1
- package/dist/{chunk-IAQPMSGJ.js → chunk-S77NLWHO.js} +13 -13
- package/dist/{chunk-IAQPMSGJ.js.map → chunk-S77NLWHO.js.map} +1 -1
- package/dist/{chunk-SF4I2NKC.js → chunk-V5U5S7PQ.js} +411 -167
- package/dist/chunk-V5U5S7PQ.js.map +1 -0
- package/dist/{chunk-5Q6FSUU2.mjs → chunk-WWJ5YPTJ.mjs} +412 -168
- package/dist/chunk-WWJ5YPTJ.mjs.map +1 -0
- package/dist/{chunk-MYQNZSWD.mjs → chunk-ZXXEENRD.mjs} +7 -3
- package/dist/chunk-ZXXEENRD.mjs.map +1 -0
- package/dist/clients/index.d.ts +7 -7
- package/dist/clients/index.js +3 -3
- package/dist/clients/index.mjs +2 -2
- package/dist/{contract-9af4608b.d.ts → contract-74f34ac9.d.ts} +14 -3
- package/dist/{createClient-5d316c7e.d.ts → createClient-55a04188.d.ts} +2 -2
- package/dist/{createPublicClient-9d2b42e1.d.ts → createPublicClient-3b27b282.d.ts} +3 -3
- package/dist/{createTestClient-79498dab.d.ts → createTestClient-93f9eac6.d.ts} +3 -3
- package/dist/{createWalletClient-f69a5230.d.ts → createWalletClient-c10df94d.d.ts} +3 -3
- package/dist/{eip1193-6c485d63.d.ts → eip1193-4c24765a.d.ts} +1 -1
- package/dist/ens.d.ts +1 -0
- package/dist/ens.js +11 -0
- package/dist/ens.js.map +1 -0
- package/dist/ens.mjs +11 -0
- package/dist/ens.mjs.map +1 -0
- package/dist/index.d.ts +125 -113
- package/dist/index.js +14 -6
- package/dist/index.mjs +13 -5
- package/dist/normalize-ef9240c0.d.ts +33 -0
- package/dist/{parseGwei-4308ad80.d.ts → parseGwei-b323fb0a.d.ts} +101 -37
- package/dist/public.d.ts +9 -9
- package/dist/public.js +4 -4
- package/dist/public.mjs +3 -3
- package/dist/{sendTransaction-1c8290a9.d.ts → sendTransaction-f17a2389.d.ts} +3 -3
- package/dist/{stopImpersonatingAccount-7781842a.d.ts → stopImpersonatingAccount-afb26486.d.ts} +2 -2
- package/dist/test.d.ts +5 -5
- package/dist/test.js +3 -3
- package/dist/test.mjs +2 -2
- package/dist/{transactionRequest-341b6ed2.d.ts → transactionRequest-93e9f001.d.ts} +2 -2
- package/dist/utils/index.d.ts +14 -16
- package/dist/utils/index.js +8 -2
- package/dist/utils/index.mjs +7 -1
- package/dist/wallet.d.ts +7 -7
- package/dist/wallet.js +3 -3
- package/dist/wallet.mjs +2 -2
- package/dist/{watchAsset-afaad3c7.d.ts → watchAsset-efd3dd05.d.ts} +3 -3
- package/dist/{watchPendingTransactions-3b722547.d.ts → watchPendingTransactions-a8688b26.d.ts} +17 -27
- package/dist/{webSocket-b180e679.d.ts → webSocket-d2e7bd0e.d.ts} +2 -2
- package/dist/window.d.ts +2 -2
- package/package.json +8 -2
- package/src/_test/abis.ts +7 -0
- package/src/_test/bench.ts +4 -4
- package/src/_test/index.ts +3 -1
- package/src/_test/utils.ts +39 -1
- package/src/actions/public/call.bench.ts +5 -5
- package/src/actions/public/deployContract.ts +4 -4
- package/src/actions/public/estimateGas.bench.ts +6 -6
- package/src/actions/public/getBlock.bench.ts +5 -5
- package/src/actions/public/getBlockNumber.bench.ts +5 -5
- package/src/actions/public/getGasPrice.bench.ts +1 -10
- package/src/actions/public/getTransaction.bench.ts +5 -5
- package/src/actions/public/getTransactionReceipt.bench.ts +5 -5
- package/src/actions/public/multicall.test.ts +452 -0
- package/src/actions/public/multicall.ts +103 -0
- package/src/actions/public/readContract.test.ts +226 -26
- package/src/actions/public/readContract.ts +13 -27
- package/src/actions/public/simulateContract.bench.ts +10 -0
- package/src/actions/public/simulateContract.test.ts +209 -37
- package/src/actions/public/simulateContract.ts +17 -21
- package/src/actions/public/watchContractEvent.test.ts +36 -32
- package/src/actions/public/watchEvent.test.ts +34 -29
- package/src/actions/wallet/signMessage.test.ts +0 -1
- package/src/actions/wallet/switchChain.test.ts +1 -2
- package/src/actions/wallet/watchAsset.test.ts +1 -2
- package/src/actions/wallet/writeContract.test.ts +37 -7
- package/src/actions/wallet/writeContract.ts +5 -14
- package/src/clients/transports/fallback.test.ts +34 -0
- package/src/clients/transports/fallback.ts +3 -1
- package/src/clients/transports/http.test.ts +0 -1
- package/src/clients/transports/webSocket.test.ts +0 -1
- package/src/constants/abis.test.ts +53 -0
- package/src/constants/abis.ts +44 -0
- package/src/constants/index.test.ts +14 -0
- package/src/constants/index.ts +3 -0
- package/src/constants/solidity.test.ts +41 -0
- package/src/constants/solidity.ts +35 -0
- package/src/ens.ts +5 -0
- package/src/errors/abi.test.ts +0 -2
- package/src/errors/base.test.ts +44 -2
- package/src/errors/base.ts +12 -12
- package/src/errors/block.test.ts +6 -6
- package/src/errors/contract.test.ts +233 -0
- package/src/errors/contract.ts +133 -80
- package/src/errors/index.ts +4 -2
- package/src/errors/request.test.ts +3 -4
- package/src/errors/request.ts +17 -17
- package/src/errors/rpc.test.ts +1 -2
- package/src/errors/transaction.test.ts +12 -12
- package/src/errors/transport.test.ts +0 -1
- package/src/index.test.ts +46 -0
- package/src/index.ts +8 -0
- package/src/types/contract.ts +55 -4
- package/src/types/index.ts +5 -0
- package/src/types/multicall.ts +82 -0
- package/src/utils/abi/decodeAbi.test.ts +1 -2
- package/src/utils/abi/decodeAbi.ts +11 -7
- package/src/utils/abi/decodeDeployData.test.ts +7 -15
- package/src/utils/abi/decodeDeployData.ts +10 -7
- package/src/utils/abi/decodeErrorResult.test.ts +103 -3
- package/src/utils/abi/decodeErrorResult.ts +30 -13
- package/src/utils/abi/decodeFunctionData.test.ts +0 -1
- package/src/utils/abi/decodeFunctionResult.test.ts +80 -9
- package/src/utils/abi/decodeFunctionResult.ts +15 -11
- package/src/utils/abi/encodeAbi.test.ts +40 -7
- package/src/utils/abi/encodeAbi.ts +28 -13
- package/src/utils/abi/encodeDeployData.test.ts +6 -13
- package/src/utils/abi/encodeDeployData.ts +10 -7
- package/src/utils/abi/encodeErrorResult.test.ts +2 -7
- package/src/utils/abi/encodeErrorResult.ts +18 -11
- package/src/utils/abi/encodeEventTopics.test.ts +11 -14
- package/src/utils/abi/encodeEventTopics.ts +15 -9
- package/src/utils/abi/encodeFunctionData.test.ts +5 -7
- package/src/utils/abi/encodeFunctionData.ts +16 -9
- package/src/utils/abi/encodeFunctionResult.test.ts +10 -15
- package/src/utils/abi/encodeFunctionResult.ts +9 -7
- package/src/utils/abi/getAbiItem.test.ts +547 -0
- package/src/utils/abi/getAbiItem.ts +93 -3
- package/src/utils/abi/index.ts +5 -1
- package/src/utils/address/getAddress.test.ts +6 -6
- package/src/utils/contract/getContractError.test.ts +247 -62
- package/src/utils/contract/getContractError.ts +30 -38
- package/src/utils/data/pad.bench.ts +0 -9
- package/src/utils/data/trim.bench.ts +0 -16
- package/src/utils/encoding/encodeBytes.bench.ts +0 -12
- package/src/utils/encoding/encodeHex.bench.ts +0 -11
- package/src/utils/ens/index.test.ts +13 -0
- package/src/utils/ens/index.ts +5 -0
- package/src/utils/ens/labelhash.test.ts +55 -0
- package/src/utils/ens/labelhash.ts +16 -0
- package/src/utils/ens/namehash.test.ts +65 -0
- package/src/utils/ens/namehash.ts +28 -0
- package/src/utils/ens/normalize.bench.ts +14 -0
- package/src/utils/ens/normalize.test.ts +35 -0
- package/src/utils/ens/normalize.ts +14 -0
- package/src/utils/hash/keccak256.ts +3 -5
- package/src/utils/index.test.ts +3 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/rpc.test.ts +3 -6
- package/src/utils/unit/formatUnit.bench.ts +0 -5
- package/src/utils/unit/parseUnit.bench.ts +0 -5
- package/dist/chunk-5Q6FSUU2.mjs.map +0 -1
- package/dist/chunk-FHXXG7I6.js.map +0 -1
- package/dist/chunk-MYQNZSWD.mjs.map +0 -1
- package/dist/chunk-NHAKUPTF.js.map +0 -1
- package/dist/chunk-SF4I2NKC.js.map +0 -1
- package/dist/chunk-ZVGTYLKU.mjs.map +0 -1
@@ -19,40 +19,40 @@ describe('TransactionNotFoundError', () => {
|
|
19
19
|
expect(
|
20
20
|
new TransactionNotFoundError({ blockHash: '0x123', index: 420 }),
|
21
21
|
).toMatchInlineSnapshot(`
|
22
|
-
|
22
|
+
[TransactionNotFoundError: Transaction at block hash "0x123" at index "420" could not be found.
|
23
23
|
|
24
|
-
|
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
|
-
|
32
|
+
[TransactionNotFoundError: Transaction at block time "latest" at index "420" could not be found.
|
33
33
|
|
34
|
-
|
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
|
-
|
42
|
+
[TransactionNotFoundError: Transaction at block number "42069" at index "420" could not be found.
|
43
43
|
|
44
|
-
|
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
|
-
|
52
|
+
[TransactionNotFoundError: Transaction with hash "0x123" could not be found.
|
53
53
|
|
54
|
-
|
55
|
-
|
54
|
+
Version: viem@1.0.2]
|
55
|
+
`)
|
56
56
|
})
|
57
57
|
})
|
58
58
|
|
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,49 @@ test('exports actions', () => {
|
|
151
154
|
"isHex": [Function],
|
152
155
|
"keccak256": [Function],
|
153
156
|
"mine": [Function],
|
157
|
+
"multicall3Abi": [
|
158
|
+
{
|
159
|
+
"inputs": [
|
160
|
+
{
|
161
|
+
"components": [
|
162
|
+
{
|
163
|
+
"name": "target",
|
164
|
+
"type": "address",
|
165
|
+
},
|
166
|
+
{
|
167
|
+
"name": "allowFailure",
|
168
|
+
"type": "bool",
|
169
|
+
},
|
170
|
+
{
|
171
|
+
"name": "callData",
|
172
|
+
"type": "bytes",
|
173
|
+
},
|
174
|
+
],
|
175
|
+
"name": "calls",
|
176
|
+
"type": "tuple[]",
|
177
|
+
},
|
178
|
+
],
|
179
|
+
"name": "aggregate3",
|
180
|
+
"outputs": [
|
181
|
+
{
|
182
|
+
"components": [
|
183
|
+
{
|
184
|
+
"name": "success",
|
185
|
+
"type": "bool",
|
186
|
+
},
|
187
|
+
{
|
188
|
+
"name": "returnData",
|
189
|
+
"type": "bytes",
|
190
|
+
},
|
191
|
+
],
|
192
|
+
"name": "returnData",
|
193
|
+
"type": "tuple[]",
|
194
|
+
},
|
195
|
+
],
|
196
|
+
"stateMutability": "view",
|
197
|
+
"type": "function",
|
198
|
+
},
|
199
|
+
],
|
154
200
|
"numberToBytes": [Function],
|
155
201
|
"numberToHex": [Function],
|
156
202
|
"pad": [Function],
|
package/src/index.ts
CHANGED
@@ -191,6 +191,8 @@ export {
|
|
191
191
|
webSocket,
|
192
192
|
} from './clients'
|
193
193
|
|
194
|
+
export { multicall3Abi } from './constants'
|
195
|
+
|
194
196
|
export {
|
195
197
|
AbiConstructorNotFoundError,
|
196
198
|
AbiConstructorParamsNotFoundError,
|
@@ -206,6 +208,9 @@ export {
|
|
206
208
|
AbiFunctionSignatureNotFoundError,
|
207
209
|
BaseError,
|
208
210
|
BlockNotFoundError,
|
211
|
+
ContractFunctionExecutionError,
|
212
|
+
ContractFunctionRevertedError,
|
213
|
+
ContractFunctionZeroDataError,
|
209
214
|
DataLengthTooLongError,
|
210
215
|
DataLengthTooShortError,
|
211
216
|
HttpRequestError,
|
@@ -254,6 +259,8 @@ export type {
|
|
254
259
|
BlockNumber,
|
255
260
|
BlockTag,
|
256
261
|
ByteArray,
|
262
|
+
Chain,
|
263
|
+
ContractConfig,
|
257
264
|
Hex,
|
258
265
|
FeeHistory,
|
259
266
|
FeeValues,
|
@@ -300,6 +307,7 @@ export type {
|
|
300
307
|
GetContractAddressOptions,
|
301
308
|
GetCreateAddressOptions,
|
302
309
|
GetCreate2AddressOptions,
|
310
|
+
GetAbiItemArgs,
|
303
311
|
EncodeRlpResponse,
|
304
312
|
FormattedBlock,
|
305
313
|
FormattedTransaction,
|
package/src/types/contract.ts
CHANGED
@@ -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
|
-
|
16
|
+
ExtractAbiErrorNames,
|
16
17
|
ExtractAbiFunctionNames,
|
18
|
+
Narrow,
|
17
19
|
} from 'abitype'
|
18
|
-
import {
|
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
|
-
//
|
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[],
|
package/src/types/index.ts
CHANGED
@@ -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
|
-
]
|
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<
|
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<
|
24
|
-
|
25
|
-
|
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
|
@@ -11,7 +11,7 @@ test('constructor()', () => {
|
|
11
11
|
stateMutability: 'nonpayable',
|
12
12
|
type: 'constructor',
|
13
13
|
},
|
14
|
-
]
|
14
|
+
],
|
15
15
|
bytecode:
|
16
16
|
'0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
|
17
17
|
data: '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
|
@@ -22,13 +22,12 @@ test('constructor()', () => {
|
|
22
22
|
})
|
23
23
|
expect(
|
24
24
|
decodeDeployData({
|
25
|
-
// @ts-expect-error
|
26
25
|
abi: [
|
27
26
|
{
|
28
27
|
stateMutability: 'nonpayable',
|
29
28
|
type: 'constructor',
|
30
29
|
},
|
31
|
-
]
|
30
|
+
],
|
32
31
|
bytecode:
|
33
32
|
'0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
|
34
33
|
data: '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
|
@@ -54,7 +53,7 @@ test('constructor(uint256)', () => {
|
|
54
53
|
stateMutability: 'nonpayable',
|
55
54
|
type: 'constructor',
|
56
55
|
},
|
57
|
-
]
|
56
|
+
],
|
58
57
|
bytecode:
|
59
58
|
'0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
|
60
59
|
data: '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c634300080700330000000000000000000000000000000000000000000000000000000000010f2c',
|
@@ -69,8 +68,7 @@ test('constructor(uint256)', () => {
|
|
69
68
|
test('error: constructor not found', () => {
|
70
69
|
expect(() =>
|
71
70
|
decodeDeployData({
|
72
|
-
|
73
|
-
abi: [{}] as const,
|
71
|
+
abi: [{}],
|
74
72
|
bytecode:
|
75
73
|
'0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
|
76
74
|
data: '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c634300080700330000000000000000000000000000000000000000000000000000000000010f2c',
|
@@ -80,7 +78,6 @@ test('error: constructor not found', () => {
|
|
80
78
|
Make sure you are using the correct ABI and that the constructor exists on it.
|
81
79
|
|
82
80
|
Docs: https://viem.sh/docs/contract/decodeDeployData
|
83
|
-
|
84
81
|
Version: viem@1.0.2"
|
85
82
|
`)
|
86
83
|
})
|
@@ -88,13 +85,12 @@ test('error: constructor not found', () => {
|
|
88
85
|
test('error: no inputs', () => {
|
89
86
|
expect(() =>
|
90
87
|
decodeDeployData({
|
91
|
-
// @ts-expect-error
|
92
88
|
abi: [
|
93
89
|
{
|
94
90
|
stateMutability: 'nonpayable',
|
95
91
|
type: 'constructor',
|
96
92
|
},
|
97
|
-
]
|
93
|
+
],
|
98
94
|
bytecode:
|
99
95
|
'0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
|
100
96
|
data: '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c634300080700330000000000000000000000000000000000000000000000000000000000010f2c',
|
@@ -105,7 +101,6 @@ test('error: no inputs', () => {
|
|
105
101
|
Make sure you are using the correct ABI, and that the \`inputs\` attribute on the constructor exists.
|
106
102
|
|
107
103
|
Docs: https://viem.sh/docs/contract/decodeDeployData
|
108
|
-
|
109
104
|
Version: viem@1.0.2"
|
110
105
|
`,
|
111
106
|
)
|
@@ -117,7 +112,7 @@ test('error: no inputs', () => {
|
|
117
112
|
stateMutability: 'nonpayable',
|
118
113
|
type: 'constructor',
|
119
114
|
},
|
120
|
-
]
|
115
|
+
],
|
121
116
|
bytecode:
|
122
117
|
'0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
|
123
118
|
data: '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c634300080700330000000000000000000000000000000000000000000000000000000000010f2c',
|
@@ -128,20 +123,18 @@ test('error: no inputs', () => {
|
|
128
123
|
Make sure you are using the correct ABI, and that the \`inputs\` attribute on the constructor exists.
|
129
124
|
|
130
125
|
Docs: https://viem.sh/docs/contract/decodeDeployData
|
131
|
-
|
132
126
|
Version: viem@1.0.2"
|
133
127
|
`,
|
134
128
|
)
|
135
129
|
expect(() =>
|
136
130
|
decodeDeployData({
|
137
|
-
// @ts-expect-error
|
138
131
|
abi: [
|
139
132
|
{
|
140
133
|
inputs: undefined,
|
141
134
|
stateMutability: 'nonpayable',
|
142
135
|
type: 'constructor',
|
143
136
|
},
|
144
|
-
]
|
137
|
+
],
|
145
138
|
bytecode:
|
146
139
|
'0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c63430008070033',
|
147
140
|
data: '0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea2646970667358221220116554d4ba29ee08da9e97dc54ff9a2a65d67a648140d616fc225a25ff08c86364736f6c634300080700330000000000000000000000000000000000000000000000000000000000010f2c',
|
@@ -152,7 +145,6 @@ test('error: no inputs', () => {
|
|
152
145
|
Make sure you are using the correct ABI, and that the \`inputs\` attribute on the constructor exists.
|
153
146
|
|
154
147
|
Docs: https://viem.sh/docs/contract/decodeDeployData
|
155
|
-
|
156
148
|
Version: viem@1.0.2"
|
157
149
|
`,
|
158
150
|
)
|
@@ -9,24 +9,27 @@ import { decodeAbi } from './decodeAbi'
|
|
9
9
|
|
10
10
|
const docsPath = '/docs/contract/decodeDeployData'
|
11
11
|
|
12
|
-
export type DecodeDeployDataArgs<TAbi extends Abi = Abi> =
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
export type DecodeDeployDataArgs<TAbi extends Abi | readonly unknown[] = Abi> =
|
13
|
+
{
|
14
|
+
abi: TAbi
|
15
|
+
bytecode: Hex
|
16
|
+
data: Hex
|
17
|
+
}
|
17
18
|
export type DecodeDeployDataResponse = {
|
18
19
|
args?: readonly unknown[] | undefined
|
19
20
|
bytecode: Hex
|
20
21
|
}
|
21
22
|
|
22
|
-
export function decodeDeployData<TAbi extends Abi
|
23
|
+
export function decodeDeployData<TAbi extends Abi | readonly unknown[]>({
|
23
24
|
abi,
|
24
25
|
bytecode,
|
25
26
|
data,
|
26
27
|
}: DecodeDeployDataArgs<TAbi>): DecodeDeployDataResponse {
|
27
28
|
if (data === bytecode) return { bytecode }
|
28
29
|
|
29
|
-
const description = abi.find(
|
30
|
+
const description = (abi as Abi).find(
|
31
|
+
(x) => 'type' in x && x.type === 'constructor',
|
32
|
+
)
|
30
33
|
if (!description) throw new AbiConstructorNotFoundError({ docsPath })
|
31
34
|
if (!('inputs' in description))
|
32
35
|
throw new AbiConstructorParamsNotFoundError({ docsPath })
|