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.
- 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-EC3NUIJE.js → chunk-7Y5QVLX7.js} +13 -13
- package/dist/{chunk-EC3NUIJE.js.map → chunk-7Y5QVLX7.js.map} +1 -1
- package/dist/{chunk-KEHGSYDO.js → chunk-BV5NTHUX.js} +14 -13
- package/dist/chunk-BV5NTHUX.js.map +1 -0
- package/dist/{chunk-46BO7YAQ.mjs → chunk-EGVXCZNJ.mjs} +4 -3
- package/dist/chunk-EGVXCZNJ.mjs.map +1 -0
- package/dist/{chunk-57ZOFERP.mjs → chunk-GX2KDAM3.mjs} +2 -2
- package/dist/{chunk-57ZOFERP.mjs.map → chunk-GX2KDAM3.mjs.map} +0 -0
- package/dist/{chunk-W7BWWAC4.js → chunk-NW6724MI.js} +14 -14
- package/dist/{chunk-W7BWWAC4.js.map → chunk-NW6724MI.js.map} +0 -0
- package/dist/{chunk-DGO77E2H.mjs → chunk-RJLUZTJS.mjs} +86 -3
- package/dist/chunk-RJLUZTJS.mjs.map +1 -0
- package/dist/{chunk-O2GYLJVD.js → chunk-SSPRUPGN.js} +405 -165
- package/dist/chunk-SSPRUPGN.js.map +1 -0
- package/dist/{chunk-DSPMAIDO.mjs → chunk-TXHOG6KU.mjs} +2 -2
- package/dist/{chunk-DSPMAIDO.mjs.map → chunk-TXHOG6KU.mjs.map} +1 -1
- package/dist/{chunk-62VTYU2V.mjs → chunk-XBUH66KN.mjs} +406 -166
- package/dist/chunk-XBUH66KN.mjs.map +1 -0
- package/dist/{chunk-KZMJR27B.js → chunk-ZKYGWITF.js} +147 -64
- package/dist/chunk-ZKYGWITF.js.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-4c3a37b3.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 +174 -113
- package/dist/index.js +16 -6
- package/dist/index.mjs +15 -5
- package/dist/normalize-ef9240c0.d.ts +33 -0
- package/dist/{parseGwei-4308ad80.d.ts → parseGwei-3411cf2d.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-57df1a13.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/constants.ts +4 -3
- package/src/_test/index.ts +4 -2
- package/src/_test/utils.ts +39 -1
- package/src/actions/index.test.ts +1 -0
- package/src/actions/index.ts +2 -0
- 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/getFilterChanges.test.ts +9 -9
- package/src/actions/public/getFilterLogs.test.ts +7 -7
- package/src/actions/public/getGasPrice.bench.ts +1 -10
- package/src/actions/public/getLogs.test.ts +7 -7
- package/src/actions/public/getTransaction.bench.ts +5 -5
- package/src/actions/public/getTransactionReceipt.bench.ts +5 -5
- package/src/actions/public/index.test.ts +1 -0
- package/src/actions/public/index.ts +3 -0
- 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 +61 -57
- package/src/actions/public/watchEvent.test.ts +42 -37
- package/src/actions/test/impersonateAccount.test.ts +4 -6
- package/src/actions/test/sendUnsignedTransaction.test.ts +2 -2
- package/src/actions/test/stopImpersonatingAccount.test.ts +5 -7
- 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 +47 -0
- package/src/index.ts +10 -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 +3 -4
- package/src/utils/abi/encodeAbi.ts +19 -10
- 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-46BO7YAQ.mjs.map +0 -1
- package/dist/chunk-62VTYU2V.mjs.map +0 -1
- package/dist/chunk-DGO77E2H.mjs.map +0 -1
- package/dist/chunk-KEHGSYDO.js.map +0 -1
- package/dist/chunk-KZMJR27B.js.map +0 -1
- package/dist/chunk-O2GYLJVD.js.map +0 -1
@@ -1,8 +1,8 @@
|
|
1
1
|
import { bench, describe } from 'vitest'
|
2
2
|
|
3
3
|
import {
|
4
|
-
essentialProvider,
|
5
4
|
ethersProvider,
|
5
|
+
ethersV6Provider,
|
6
6
|
publicClient,
|
7
7
|
web3Provider,
|
8
8
|
} from '../../_test'
|
@@ -23,11 +23,11 @@ describe('Get Transaction', () => {
|
|
23
23
|
await ethersProvider.getTransaction(hash)
|
24
24
|
})
|
25
25
|
|
26
|
-
bench('
|
27
|
-
await
|
26
|
+
bench('ethers@6: `getTransaction`', async () => {
|
27
|
+
await ethersV6Provider.getTransaction(hash)
|
28
28
|
})
|
29
29
|
|
30
|
-
bench('
|
31
|
-
await
|
30
|
+
bench('web3.js: `getTransaction`', async () => {
|
31
|
+
await web3Provider.eth.getTransaction(hash)
|
32
32
|
})
|
33
33
|
})
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { bench, describe } from 'vitest'
|
2
2
|
|
3
3
|
import {
|
4
|
-
essentialProvider,
|
5
4
|
ethersProvider,
|
5
|
+
ethersV6Provider,
|
6
6
|
publicClient,
|
7
7
|
web3Provider,
|
8
8
|
} from '../../_test'
|
@@ -23,11 +23,11 @@ describe('Get Transaction Receipt', () => {
|
|
23
23
|
await ethersProvider.getTransactionReceipt(hash)
|
24
24
|
})
|
25
25
|
|
26
|
-
bench('
|
27
|
-
await
|
26
|
+
bench('ethers@6: `getTransactionReceipt`', async () => {
|
27
|
+
await ethersV6Provider.getTransactionReceipt(hash)
|
28
28
|
})
|
29
29
|
|
30
|
-
bench('
|
31
|
-
await
|
30
|
+
bench('web3.js: `getTransactionReceipt`', async () => {
|
31
|
+
await web3Provider.eth.getTransactionReceipt(hash)
|
32
32
|
})
|
33
33
|
})
|
@@ -29,6 +29,7 @@ test('exports actions', () => {
|
|
29
29
|
"getTransactionConfirmations": [Function],
|
30
30
|
"getTransactionCount": [Function],
|
31
31
|
"getTransactionReceipt": [Function],
|
32
|
+
"multicall": [Function],
|
32
33
|
"readContract": [Function],
|
33
34
|
"simulateContract": [Function],
|
34
35
|
"uninstallFilter": [Function],
|
@@ -105,6 +105,9 @@ export type {
|
|
105
105
|
GetTransactionReceiptResponse,
|
106
106
|
} from './getTransactionReceipt'
|
107
107
|
|
108
|
+
export { multicall } from './multicall'
|
109
|
+
export type { MulticallArgs } from './multicall'
|
110
|
+
|
108
111
|
export { readContract } from './readContract'
|
109
112
|
export type {
|
110
113
|
ReadContractArgs,
|
@@ -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
|
+
address,
|
11
|
+
initialBlockNumber,
|
12
|
+
publicClient,
|
13
|
+
usdcContractConfig,
|
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: [address.vitalik],
|
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: [address.vitalik],
|
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: [address.vitalik, 1n],
|
121
|
+
},
|
122
|
+
{
|
123
|
+
...usdcContractConfig,
|
124
|
+
functionName: 'balanceOf',
|
125
|
+
args: [address.vitalik],
|
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: [address.vitalik],
|
175
|
+
},
|
176
|
+
{
|
177
|
+
...usdcContractConfig,
|
178
|
+
functionName: 'balanceOf',
|
179
|
+
args: [address.vitalik],
|
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: [address.vitalik],
|
228
|
+
},
|
229
|
+
{
|
230
|
+
...usdcContractConfig,
|
231
|
+
functionName: 'balanceOf',
|
232
|
+
args: [address.vitalik],
|
233
|
+
},
|
234
|
+
{
|
235
|
+
...wagmiContractConfig,
|
236
|
+
functionName: 'transferFrom',
|
237
|
+
args: [address.vitalik, accounts[0].address, 1n],
|
238
|
+
},
|
239
|
+
{
|
240
|
+
...baycContractConfig,
|
241
|
+
functionName: 'totalSupply',
|
242
|
+
},
|
243
|
+
{
|
244
|
+
...baycContractConfig,
|
245
|
+
functionName: 'tokenOfOwnerByIndex',
|
246
|
+
args: [address.vitalik, 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: [address.vitalik],
|
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: [address.vitalik, 1n],
|
340
|
+
},
|
341
|
+
{
|
342
|
+
...usdcContractConfig,
|
343
|
+
functionName: 'balanceOf',
|
344
|
+
args: [address.vitalik],
|
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: [address.vitalik],
|
377
|
+
},
|
378
|
+
{
|
379
|
+
...usdcContractConfig,
|
380
|
+
functionName: 'balanceOf',
|
381
|
+
args: [address.vitalik],
|
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: [address.vitalik],
|
416
|
+
},
|
417
|
+
{
|
418
|
+
...usdcContractConfig,
|
419
|
+
functionName: 'balanceOf',
|
420
|
+
args: [address.vitalik],
|
421
|
+
},
|
422
|
+
{
|
423
|
+
...wagmiContractConfig,
|
424
|
+
functionName: 'transferFrom',
|
425
|
+
args: [address.vitalik, accounts[0].address, 1n],
|
426
|
+
},
|
427
|
+
{
|
428
|
+
...baycContractConfig,
|
429
|
+
functionName: 'totalSupply',
|
430
|
+
},
|
431
|
+
{
|
432
|
+
...baycContractConfig,
|
433
|
+
functionName: 'tokenOfOwnerByIndex',
|
434
|
+
args: [address.vitalik, 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
|
+
}
|