@pancakeswap/multicall 0.0.2
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/.turbo/turbo-build.log +16 -0
- package/CHANGELOG.md +7 -0
- package/Multicall.json +236 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +352 -0
- package/dist/index.mjs +325 -0
- package/index.ts +132 -0
- package/package.json +32 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +9 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
warning package.json: No license field
|
|
2
|
+
$ tsup
|
|
3
|
+
[34mCLI[39m Building entry: {"index":"index.ts"}
|
|
4
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
5
|
+
[34mCLI[39m tsup v5.12.9
|
|
6
|
+
[34mCLI[39m Using tsup config: /home/runner/work/pancake-frontend/pancake-frontend/packages/multicall/tsup.config.ts
|
|
7
|
+
[34mCLI[39m Target: node12
|
|
8
|
+
[34mESM[39m Build start
|
|
9
|
+
[34mCJS[39m Build start
|
|
10
|
+
[32mESM[39m ⚡️ Build success in 20ms
|
|
11
|
+
[32mCJS[39m ⚡️ Build success in 20ms
|
|
12
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m9.98 KB[39m
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m11.20 KB[39m
|
|
14
|
+
[34mDTS[39m Build start
|
|
15
|
+
[32mDTS[39m ⚡️ Build success in 2877ms
|
|
16
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m1.74 KB[39m
|
package/CHANGELOG.md
ADDED
package/Multicall.json
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"inputs": [
|
|
4
|
+
{
|
|
5
|
+
"components": [
|
|
6
|
+
{ "internalType": "address", "name": "target", "type": "address" },
|
|
7
|
+
{ "internalType": "bytes", "name": "callData", "type": "bytes" }
|
|
8
|
+
],
|
|
9
|
+
"internalType": "struct Multicall3.Call[]",
|
|
10
|
+
"name": "calls",
|
|
11
|
+
"type": "tuple[]"
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
"name": "aggregate",
|
|
15
|
+
"outputs": [
|
|
16
|
+
{ "internalType": "uint256", "name": "blockNumber", "type": "uint256" },
|
|
17
|
+
{ "internalType": "bytes[]", "name": "returnData", "type": "bytes[]" }
|
|
18
|
+
],
|
|
19
|
+
"stateMutability": "payable",
|
|
20
|
+
"type": "function"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"inputs": [
|
|
24
|
+
{
|
|
25
|
+
"components": [
|
|
26
|
+
{ "internalType": "address", "name": "target", "type": "address" },
|
|
27
|
+
{ "internalType": "bool", "name": "allowFailure", "type": "bool" },
|
|
28
|
+
{ "internalType": "bytes", "name": "callData", "type": "bytes" }
|
|
29
|
+
],
|
|
30
|
+
"internalType": "struct Multicall3.Call3[]",
|
|
31
|
+
"name": "calls",
|
|
32
|
+
"type": "tuple[]"
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
"name": "aggregate3",
|
|
36
|
+
"outputs": [
|
|
37
|
+
{
|
|
38
|
+
"components": [
|
|
39
|
+
{ "internalType": "bool", "name": "success", "type": "bool" },
|
|
40
|
+
{ "internalType": "bytes", "name": "returnData", "type": "bytes" }
|
|
41
|
+
],
|
|
42
|
+
"internalType": "struct Multicall3.Result[]",
|
|
43
|
+
"name": "returnData",
|
|
44
|
+
"type": "tuple[]"
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
"stateMutability": "payable",
|
|
48
|
+
"type": "function"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"inputs": [
|
|
52
|
+
{
|
|
53
|
+
"components": [
|
|
54
|
+
{ "internalType": "address", "name": "target", "type": "address" },
|
|
55
|
+
{ "internalType": "bool", "name": "allowFailure", "type": "bool" },
|
|
56
|
+
{ "internalType": "uint256", "name": "value", "type": "uint256" },
|
|
57
|
+
{ "internalType": "bytes", "name": "callData", "type": "bytes" }
|
|
58
|
+
],
|
|
59
|
+
"internalType": "struct Multicall3.Call3Value[]",
|
|
60
|
+
"name": "calls",
|
|
61
|
+
"type": "tuple[]"
|
|
62
|
+
}
|
|
63
|
+
],
|
|
64
|
+
"name": "aggregate3Value",
|
|
65
|
+
"outputs": [
|
|
66
|
+
{
|
|
67
|
+
"components": [
|
|
68
|
+
{ "internalType": "bool", "name": "success", "type": "bool" },
|
|
69
|
+
{ "internalType": "bytes", "name": "returnData", "type": "bytes" }
|
|
70
|
+
],
|
|
71
|
+
"internalType": "struct Multicall3.Result[]",
|
|
72
|
+
"name": "returnData",
|
|
73
|
+
"type": "tuple[]"
|
|
74
|
+
}
|
|
75
|
+
],
|
|
76
|
+
"stateMutability": "payable",
|
|
77
|
+
"type": "function"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"inputs": [
|
|
81
|
+
{
|
|
82
|
+
"components": [
|
|
83
|
+
{ "internalType": "address", "name": "target", "type": "address" },
|
|
84
|
+
{ "internalType": "bytes", "name": "callData", "type": "bytes" }
|
|
85
|
+
],
|
|
86
|
+
"internalType": "struct Multicall3.Call[]",
|
|
87
|
+
"name": "calls",
|
|
88
|
+
"type": "tuple[]"
|
|
89
|
+
}
|
|
90
|
+
],
|
|
91
|
+
"name": "blockAndAggregate",
|
|
92
|
+
"outputs": [
|
|
93
|
+
{ "internalType": "uint256", "name": "blockNumber", "type": "uint256" },
|
|
94
|
+
{ "internalType": "bytes32", "name": "blockHash", "type": "bytes32" },
|
|
95
|
+
{
|
|
96
|
+
"components": [
|
|
97
|
+
{ "internalType": "bool", "name": "success", "type": "bool" },
|
|
98
|
+
{ "internalType": "bytes", "name": "returnData", "type": "bytes" }
|
|
99
|
+
],
|
|
100
|
+
"internalType": "struct Multicall3.Result[]",
|
|
101
|
+
"name": "returnData",
|
|
102
|
+
"type": "tuple[]"
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
"stateMutability": "payable",
|
|
106
|
+
"type": "function"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"inputs": [],
|
|
110
|
+
"name": "getBasefee",
|
|
111
|
+
"outputs": [{ "internalType": "uint256", "name": "basefee", "type": "uint256" }],
|
|
112
|
+
"stateMutability": "view",
|
|
113
|
+
"type": "function"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"inputs": [{ "internalType": "uint256", "name": "blockNumber", "type": "uint256" }],
|
|
117
|
+
"name": "getBlockHash",
|
|
118
|
+
"outputs": [{ "internalType": "bytes32", "name": "blockHash", "type": "bytes32" }],
|
|
119
|
+
"stateMutability": "view",
|
|
120
|
+
"type": "function"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"inputs": [],
|
|
124
|
+
"name": "getBlockNumber",
|
|
125
|
+
"outputs": [{ "internalType": "uint256", "name": "blockNumber", "type": "uint256" }],
|
|
126
|
+
"stateMutability": "view",
|
|
127
|
+
"type": "function"
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"inputs": [],
|
|
131
|
+
"name": "getChainId",
|
|
132
|
+
"outputs": [{ "internalType": "uint256", "name": "chainid", "type": "uint256" }],
|
|
133
|
+
"stateMutability": "view",
|
|
134
|
+
"type": "function"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"inputs": [],
|
|
138
|
+
"name": "getCurrentBlockCoinbase",
|
|
139
|
+
"outputs": [{ "internalType": "address", "name": "coinbase", "type": "address" }],
|
|
140
|
+
"stateMutability": "view",
|
|
141
|
+
"type": "function"
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
"inputs": [],
|
|
145
|
+
"name": "getCurrentBlockDifficulty",
|
|
146
|
+
"outputs": [{ "internalType": "uint256", "name": "difficulty", "type": "uint256" }],
|
|
147
|
+
"stateMutability": "view",
|
|
148
|
+
"type": "function"
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
"inputs": [],
|
|
152
|
+
"name": "getCurrentBlockGasLimit",
|
|
153
|
+
"outputs": [{ "internalType": "uint256", "name": "gaslimit", "type": "uint256" }],
|
|
154
|
+
"stateMutability": "view",
|
|
155
|
+
"type": "function"
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
"inputs": [],
|
|
159
|
+
"name": "getCurrentBlockTimestamp",
|
|
160
|
+
"outputs": [{ "internalType": "uint256", "name": "timestamp", "type": "uint256" }],
|
|
161
|
+
"stateMutability": "view",
|
|
162
|
+
"type": "function"
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
"inputs": [{ "internalType": "address", "name": "addr", "type": "address" }],
|
|
166
|
+
"name": "getEthBalance",
|
|
167
|
+
"outputs": [{ "internalType": "uint256", "name": "balance", "type": "uint256" }],
|
|
168
|
+
"stateMutability": "view",
|
|
169
|
+
"type": "function"
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"inputs": [],
|
|
173
|
+
"name": "getLastBlockHash",
|
|
174
|
+
"outputs": [{ "internalType": "bytes32", "name": "blockHash", "type": "bytes32" }],
|
|
175
|
+
"stateMutability": "view",
|
|
176
|
+
"type": "function"
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
"inputs": [
|
|
180
|
+
{ "internalType": "bool", "name": "requireSuccess", "type": "bool" },
|
|
181
|
+
{
|
|
182
|
+
"components": [
|
|
183
|
+
{ "internalType": "address", "name": "target", "type": "address" },
|
|
184
|
+
{ "internalType": "bytes", "name": "callData", "type": "bytes" }
|
|
185
|
+
],
|
|
186
|
+
"internalType": "struct Multicall3.Call[]",
|
|
187
|
+
"name": "calls",
|
|
188
|
+
"type": "tuple[]"
|
|
189
|
+
}
|
|
190
|
+
],
|
|
191
|
+
"name": "tryAggregate",
|
|
192
|
+
"outputs": [
|
|
193
|
+
{
|
|
194
|
+
"components": [
|
|
195
|
+
{ "internalType": "bool", "name": "success", "type": "bool" },
|
|
196
|
+
{ "internalType": "bytes", "name": "returnData", "type": "bytes" }
|
|
197
|
+
],
|
|
198
|
+
"internalType": "struct Multicall3.Result[]",
|
|
199
|
+
"name": "returnData",
|
|
200
|
+
"type": "tuple[]"
|
|
201
|
+
}
|
|
202
|
+
],
|
|
203
|
+
"stateMutability": "payable",
|
|
204
|
+
"type": "function"
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
"inputs": [
|
|
208
|
+
{ "internalType": "bool", "name": "requireSuccess", "type": "bool" },
|
|
209
|
+
{
|
|
210
|
+
"components": [
|
|
211
|
+
{ "internalType": "address", "name": "target", "type": "address" },
|
|
212
|
+
{ "internalType": "bytes", "name": "callData", "type": "bytes" }
|
|
213
|
+
],
|
|
214
|
+
"internalType": "struct Multicall3.Call[]",
|
|
215
|
+
"name": "calls",
|
|
216
|
+
"type": "tuple[]"
|
|
217
|
+
}
|
|
218
|
+
],
|
|
219
|
+
"name": "tryBlockAndAggregate",
|
|
220
|
+
"outputs": [
|
|
221
|
+
{ "internalType": "uint256", "name": "blockNumber", "type": "uint256" },
|
|
222
|
+
{ "internalType": "bytes32", "name": "blockHash", "type": "bytes32" },
|
|
223
|
+
{
|
|
224
|
+
"components": [
|
|
225
|
+
{ "internalType": "bool", "name": "success", "type": "bool" },
|
|
226
|
+
{ "internalType": "bytes", "name": "returnData", "type": "bytes" }
|
|
227
|
+
],
|
|
228
|
+
"internalType": "struct Multicall3.Result[]",
|
|
229
|
+
"name": "returnData",
|
|
230
|
+
"type": "tuple[]"
|
|
231
|
+
}
|
|
232
|
+
],
|
|
233
|
+
"stateMutability": "payable",
|
|
234
|
+
"type": "function"
|
|
235
|
+
}
|
|
236
|
+
]
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Contract, CallOverrides } from '@ethersproject/contracts';
|
|
2
|
+
import { Provider } from '@ethersproject/providers';
|
|
3
|
+
import { ChainId } from '@pancakeswap/sdk';
|
|
4
|
+
|
|
5
|
+
declare const multicallAddresses: {
|
|
6
|
+
1: string;
|
|
7
|
+
4: string;
|
|
8
|
+
5: string;
|
|
9
|
+
56: string;
|
|
10
|
+
97: string;
|
|
11
|
+
};
|
|
12
|
+
declare const getMulticallContract: (chainId: ChainId, provider: Provider) => Contract | null;
|
|
13
|
+
interface Call {
|
|
14
|
+
address: string;
|
|
15
|
+
name: string;
|
|
16
|
+
params?: any[];
|
|
17
|
+
}
|
|
18
|
+
interface MulticallOptions extends CallOverrides {
|
|
19
|
+
requireSuccess?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Multicall V2 uses the new "tryAggregate" function. It is different in 2 ways
|
|
23
|
+
*
|
|
24
|
+
* 1. If "requireSuccess" is false multicall will not bail out if one of the calls fails
|
|
25
|
+
* 2. The return includes a boolean whether the call was successful e.g. [wasSuccessful, callResult]
|
|
26
|
+
*/
|
|
27
|
+
interface MulticallV2Params {
|
|
28
|
+
abi: any[];
|
|
29
|
+
calls: Call[];
|
|
30
|
+
chainId?: ChainId;
|
|
31
|
+
options?: MulticallOptions;
|
|
32
|
+
provider?: Provider;
|
|
33
|
+
}
|
|
34
|
+
interface CallV3 extends Call {
|
|
35
|
+
abi: any[];
|
|
36
|
+
allowFailure?: boolean;
|
|
37
|
+
}
|
|
38
|
+
interface MulticallV3Params {
|
|
39
|
+
calls: CallV3[];
|
|
40
|
+
chainId?: ChainId;
|
|
41
|
+
allowFailure?: boolean;
|
|
42
|
+
overrides?: CallOverrides;
|
|
43
|
+
}
|
|
44
|
+
declare type MultiCallV2 = <T = any>(params: MulticallV2Params) => Promise<T>;
|
|
45
|
+
declare type MultiCall = <T = any>(abi: any[], calls: Call[], chainId?: ChainId) => Promise<T>;
|
|
46
|
+
declare function createMulticall<TProvider extends Provider>(provider: ({ chainId }: {
|
|
47
|
+
chainId?: number | undefined;
|
|
48
|
+
}) => TProvider): {
|
|
49
|
+
multicall: MultiCall;
|
|
50
|
+
multicallv2: MultiCallV2;
|
|
51
|
+
multicallv3: ({ calls, chainId, allowFailure, overrides }: MulticallV3Params) => Promise<any>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export { Call, CallV3, MultiCall, MultiCallV2, MulticallOptions, createMulticall, getMulticallContract, multicallAddresses };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// index.ts
|
|
21
|
+
var multicall_exports = {};
|
|
22
|
+
__export(multicall_exports, {
|
|
23
|
+
createMulticall: () => createMulticall,
|
|
24
|
+
getMulticallContract: () => getMulticallContract,
|
|
25
|
+
multicallAddresses: () => multicallAddresses
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(multicall_exports);
|
|
28
|
+
var import_abi = require("@ethersproject/abi");
|
|
29
|
+
var import_contracts = require("@ethersproject/contracts");
|
|
30
|
+
var import_sdk = require("@pancakeswap/sdk");
|
|
31
|
+
|
|
32
|
+
// Multicall.json
|
|
33
|
+
var Multicall_default = [
|
|
34
|
+
{
|
|
35
|
+
inputs: [
|
|
36
|
+
{
|
|
37
|
+
components: [
|
|
38
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
39
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
40
|
+
],
|
|
41
|
+
internalType: "struct Multicall3.Call[]",
|
|
42
|
+
name: "calls",
|
|
43
|
+
type: "tuple[]"
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
name: "aggregate",
|
|
47
|
+
outputs: [
|
|
48
|
+
{ internalType: "uint256", name: "blockNumber", type: "uint256" },
|
|
49
|
+
{ internalType: "bytes[]", name: "returnData", type: "bytes[]" }
|
|
50
|
+
],
|
|
51
|
+
stateMutability: "payable",
|
|
52
|
+
type: "function"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
inputs: [
|
|
56
|
+
{
|
|
57
|
+
components: [
|
|
58
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
59
|
+
{ internalType: "bool", name: "allowFailure", type: "bool" },
|
|
60
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
61
|
+
],
|
|
62
|
+
internalType: "struct Multicall3.Call3[]",
|
|
63
|
+
name: "calls",
|
|
64
|
+
type: "tuple[]"
|
|
65
|
+
}
|
|
66
|
+
],
|
|
67
|
+
name: "aggregate3",
|
|
68
|
+
outputs: [
|
|
69
|
+
{
|
|
70
|
+
components: [
|
|
71
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
72
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
73
|
+
],
|
|
74
|
+
internalType: "struct Multicall3.Result[]",
|
|
75
|
+
name: "returnData",
|
|
76
|
+
type: "tuple[]"
|
|
77
|
+
}
|
|
78
|
+
],
|
|
79
|
+
stateMutability: "payable",
|
|
80
|
+
type: "function"
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
inputs: [
|
|
84
|
+
{
|
|
85
|
+
components: [
|
|
86
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
87
|
+
{ internalType: "bool", name: "allowFailure", type: "bool" },
|
|
88
|
+
{ internalType: "uint256", name: "value", type: "uint256" },
|
|
89
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
90
|
+
],
|
|
91
|
+
internalType: "struct Multicall3.Call3Value[]",
|
|
92
|
+
name: "calls",
|
|
93
|
+
type: "tuple[]"
|
|
94
|
+
}
|
|
95
|
+
],
|
|
96
|
+
name: "aggregate3Value",
|
|
97
|
+
outputs: [
|
|
98
|
+
{
|
|
99
|
+
components: [
|
|
100
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
101
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
102
|
+
],
|
|
103
|
+
internalType: "struct Multicall3.Result[]",
|
|
104
|
+
name: "returnData",
|
|
105
|
+
type: "tuple[]"
|
|
106
|
+
}
|
|
107
|
+
],
|
|
108
|
+
stateMutability: "payable",
|
|
109
|
+
type: "function"
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
inputs: [
|
|
113
|
+
{
|
|
114
|
+
components: [
|
|
115
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
116
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
117
|
+
],
|
|
118
|
+
internalType: "struct Multicall3.Call[]",
|
|
119
|
+
name: "calls",
|
|
120
|
+
type: "tuple[]"
|
|
121
|
+
}
|
|
122
|
+
],
|
|
123
|
+
name: "blockAndAggregate",
|
|
124
|
+
outputs: [
|
|
125
|
+
{ internalType: "uint256", name: "blockNumber", type: "uint256" },
|
|
126
|
+
{ internalType: "bytes32", name: "blockHash", type: "bytes32" },
|
|
127
|
+
{
|
|
128
|
+
components: [
|
|
129
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
130
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
131
|
+
],
|
|
132
|
+
internalType: "struct Multicall3.Result[]",
|
|
133
|
+
name: "returnData",
|
|
134
|
+
type: "tuple[]"
|
|
135
|
+
}
|
|
136
|
+
],
|
|
137
|
+
stateMutability: "payable",
|
|
138
|
+
type: "function"
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
inputs: [],
|
|
142
|
+
name: "getBasefee",
|
|
143
|
+
outputs: [{ internalType: "uint256", name: "basefee", type: "uint256" }],
|
|
144
|
+
stateMutability: "view",
|
|
145
|
+
type: "function"
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
inputs: [{ internalType: "uint256", name: "blockNumber", type: "uint256" }],
|
|
149
|
+
name: "getBlockHash",
|
|
150
|
+
outputs: [{ internalType: "bytes32", name: "blockHash", type: "bytes32" }],
|
|
151
|
+
stateMutability: "view",
|
|
152
|
+
type: "function"
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
inputs: [],
|
|
156
|
+
name: "getBlockNumber",
|
|
157
|
+
outputs: [{ internalType: "uint256", name: "blockNumber", type: "uint256" }],
|
|
158
|
+
stateMutability: "view",
|
|
159
|
+
type: "function"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
inputs: [],
|
|
163
|
+
name: "getChainId",
|
|
164
|
+
outputs: [{ internalType: "uint256", name: "chainid", type: "uint256" }],
|
|
165
|
+
stateMutability: "view",
|
|
166
|
+
type: "function"
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
inputs: [],
|
|
170
|
+
name: "getCurrentBlockCoinbase",
|
|
171
|
+
outputs: [{ internalType: "address", name: "coinbase", type: "address" }],
|
|
172
|
+
stateMutability: "view",
|
|
173
|
+
type: "function"
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
inputs: [],
|
|
177
|
+
name: "getCurrentBlockDifficulty",
|
|
178
|
+
outputs: [{ internalType: "uint256", name: "difficulty", type: "uint256" }],
|
|
179
|
+
stateMutability: "view",
|
|
180
|
+
type: "function"
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
inputs: [],
|
|
184
|
+
name: "getCurrentBlockGasLimit",
|
|
185
|
+
outputs: [{ internalType: "uint256", name: "gaslimit", type: "uint256" }],
|
|
186
|
+
stateMutability: "view",
|
|
187
|
+
type: "function"
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
inputs: [],
|
|
191
|
+
name: "getCurrentBlockTimestamp",
|
|
192
|
+
outputs: [{ internalType: "uint256", name: "timestamp", type: "uint256" }],
|
|
193
|
+
stateMutability: "view",
|
|
194
|
+
type: "function"
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
inputs: [{ internalType: "address", name: "addr", type: "address" }],
|
|
198
|
+
name: "getEthBalance",
|
|
199
|
+
outputs: [{ internalType: "uint256", name: "balance", type: "uint256" }],
|
|
200
|
+
stateMutability: "view",
|
|
201
|
+
type: "function"
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
inputs: [],
|
|
205
|
+
name: "getLastBlockHash",
|
|
206
|
+
outputs: [{ internalType: "bytes32", name: "blockHash", type: "bytes32" }],
|
|
207
|
+
stateMutability: "view",
|
|
208
|
+
type: "function"
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
inputs: [
|
|
212
|
+
{ internalType: "bool", name: "requireSuccess", type: "bool" },
|
|
213
|
+
{
|
|
214
|
+
components: [
|
|
215
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
216
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
217
|
+
],
|
|
218
|
+
internalType: "struct Multicall3.Call[]",
|
|
219
|
+
name: "calls",
|
|
220
|
+
type: "tuple[]"
|
|
221
|
+
}
|
|
222
|
+
],
|
|
223
|
+
name: "tryAggregate",
|
|
224
|
+
outputs: [
|
|
225
|
+
{
|
|
226
|
+
components: [
|
|
227
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
228
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
229
|
+
],
|
|
230
|
+
internalType: "struct Multicall3.Result[]",
|
|
231
|
+
name: "returnData",
|
|
232
|
+
type: "tuple[]"
|
|
233
|
+
}
|
|
234
|
+
],
|
|
235
|
+
stateMutability: "payable",
|
|
236
|
+
type: "function"
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
inputs: [
|
|
240
|
+
{ internalType: "bool", name: "requireSuccess", type: "bool" },
|
|
241
|
+
{
|
|
242
|
+
components: [
|
|
243
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
244
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
245
|
+
],
|
|
246
|
+
internalType: "struct Multicall3.Call[]",
|
|
247
|
+
name: "calls",
|
|
248
|
+
type: "tuple[]"
|
|
249
|
+
}
|
|
250
|
+
],
|
|
251
|
+
name: "tryBlockAndAggregate",
|
|
252
|
+
outputs: [
|
|
253
|
+
{ internalType: "uint256", name: "blockNumber", type: "uint256" },
|
|
254
|
+
{ internalType: "bytes32", name: "blockHash", type: "bytes32" },
|
|
255
|
+
{
|
|
256
|
+
components: [
|
|
257
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
258
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
259
|
+
],
|
|
260
|
+
internalType: "struct Multicall3.Result[]",
|
|
261
|
+
name: "returnData",
|
|
262
|
+
type: "tuple[]"
|
|
263
|
+
}
|
|
264
|
+
],
|
|
265
|
+
stateMutability: "payable",
|
|
266
|
+
type: "function"
|
|
267
|
+
}
|
|
268
|
+
];
|
|
269
|
+
|
|
270
|
+
// index.ts
|
|
271
|
+
var multicallAddresses = {
|
|
272
|
+
1: "0xcA11bde05977b3631167028862bE2a173976CA11",
|
|
273
|
+
4: "0xcA11bde05977b3631167028862bE2a173976CA11",
|
|
274
|
+
5: "0xcA11bde05977b3631167028862bE2a173976CA11",
|
|
275
|
+
56: "0xcA11bde05977b3631167028862bE2a173976CA11",
|
|
276
|
+
97: "0xcA11bde05977b3631167028862bE2a173976CA11"
|
|
277
|
+
};
|
|
278
|
+
var getMulticallContract = (chainId, provider) => {
|
|
279
|
+
if (multicallAddresses[chainId]) {
|
|
280
|
+
return new import_contracts.Contract(multicallAddresses[chainId], Multicall_default, provider);
|
|
281
|
+
}
|
|
282
|
+
return null;
|
|
283
|
+
};
|
|
284
|
+
function createMulticall(provider) {
|
|
285
|
+
const multicall = async (abi, calls, chainId = import_sdk.ChainId.BSC) => {
|
|
286
|
+
const multi = getMulticallContract(chainId, provider({ chainId }));
|
|
287
|
+
if (!multi)
|
|
288
|
+
throw new Error(`Multicall Provider missing for ${chainId}`);
|
|
289
|
+
const itf = new import_abi.Interface(abi);
|
|
290
|
+
const calldata = calls.map((call) => ({
|
|
291
|
+
target: call.address.toLowerCase(),
|
|
292
|
+
callData: itf.encodeFunctionData(call.name, call.params)
|
|
293
|
+
}));
|
|
294
|
+
const { returnData } = await multi.callStatic.aggregate(calldata);
|
|
295
|
+
const res = returnData.map((call, i) => itf.decodeFunctionResult(calls[i].name, call));
|
|
296
|
+
return res;
|
|
297
|
+
};
|
|
298
|
+
const multicallv2 = async ({ abi, calls, chainId = import_sdk.ChainId.BSC, options, provider: _provider }) => {
|
|
299
|
+
const { requireSuccess = true, ...overrides } = options || {};
|
|
300
|
+
const multi = getMulticallContract(chainId, _provider || provider({ chainId }));
|
|
301
|
+
if (!multi)
|
|
302
|
+
throw new Error(`Multicall Provider missing for ${chainId}`);
|
|
303
|
+
const itf = new import_abi.Interface(abi);
|
|
304
|
+
const calldata = calls.map((call) => ({
|
|
305
|
+
target: call.address.toLowerCase(),
|
|
306
|
+
callData: itf.encodeFunctionData(call.name, call.params)
|
|
307
|
+
}));
|
|
308
|
+
const returnData = await multi.callStatic.tryAggregate(requireSuccess, calldata, overrides);
|
|
309
|
+
const res = returnData.map((call, i) => {
|
|
310
|
+
const [result, data] = call;
|
|
311
|
+
return result && data !== "0x" ? itf.decodeFunctionResult(calls[i].name, data) : null;
|
|
312
|
+
});
|
|
313
|
+
return res;
|
|
314
|
+
};
|
|
315
|
+
const multicallv3 = async ({ calls, chainId = import_sdk.ChainId.BSC, allowFailure, overrides }) => {
|
|
316
|
+
const multi = getMulticallContract(chainId, provider({ chainId }));
|
|
317
|
+
if (!multi)
|
|
318
|
+
throw new Error(`Multicall Provider missing for ${chainId}`);
|
|
319
|
+
const _calls = calls.map(({ abi, address, name, params, allowFailure: _allowFailure }) => {
|
|
320
|
+
const contract = new import_contracts.Contract(address, abi);
|
|
321
|
+
const callData = contract.interface.encodeFunctionData(name, params != null ? params : []);
|
|
322
|
+
if (!contract[name])
|
|
323
|
+
console.error(`${name} missing on ${address}`);
|
|
324
|
+
return {
|
|
325
|
+
target: address,
|
|
326
|
+
allowFailure: allowFailure || _allowFailure,
|
|
327
|
+
callData
|
|
328
|
+
};
|
|
329
|
+
});
|
|
330
|
+
const result = await multi.callStatic.aggregate3([...[_calls], ...overrides ? [overrides] : []]);
|
|
331
|
+
return result.map((call, i) => {
|
|
332
|
+
const { returnData, success } = call;
|
|
333
|
+
if (!success || returnData === "0x")
|
|
334
|
+
return null;
|
|
335
|
+
const { address, abi, name } = calls[i];
|
|
336
|
+
const contract = new import_contracts.Contract(address, abi);
|
|
337
|
+
const decoded = contract.interface.decodeFunctionResult(name, returnData);
|
|
338
|
+
return decoded;
|
|
339
|
+
});
|
|
340
|
+
};
|
|
341
|
+
return {
|
|
342
|
+
multicall,
|
|
343
|
+
multicallv2,
|
|
344
|
+
multicallv3
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
348
|
+
0 && (module.exports = {
|
|
349
|
+
createMulticall,
|
|
350
|
+
getMulticallContract,
|
|
351
|
+
multicallAddresses
|
|
352
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { Interface } from "@ethersproject/abi";
|
|
3
|
+
import { Contract } from "@ethersproject/contracts";
|
|
4
|
+
import { ChainId } from "@pancakeswap/sdk";
|
|
5
|
+
|
|
6
|
+
// Multicall.json
|
|
7
|
+
var Multicall_default = [
|
|
8
|
+
{
|
|
9
|
+
inputs: [
|
|
10
|
+
{
|
|
11
|
+
components: [
|
|
12
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
13
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
14
|
+
],
|
|
15
|
+
internalType: "struct Multicall3.Call[]",
|
|
16
|
+
name: "calls",
|
|
17
|
+
type: "tuple[]"
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
name: "aggregate",
|
|
21
|
+
outputs: [
|
|
22
|
+
{ internalType: "uint256", name: "blockNumber", type: "uint256" },
|
|
23
|
+
{ internalType: "bytes[]", name: "returnData", type: "bytes[]" }
|
|
24
|
+
],
|
|
25
|
+
stateMutability: "payable",
|
|
26
|
+
type: "function"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
inputs: [
|
|
30
|
+
{
|
|
31
|
+
components: [
|
|
32
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
33
|
+
{ internalType: "bool", name: "allowFailure", type: "bool" },
|
|
34
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
35
|
+
],
|
|
36
|
+
internalType: "struct Multicall3.Call3[]",
|
|
37
|
+
name: "calls",
|
|
38
|
+
type: "tuple[]"
|
|
39
|
+
}
|
|
40
|
+
],
|
|
41
|
+
name: "aggregate3",
|
|
42
|
+
outputs: [
|
|
43
|
+
{
|
|
44
|
+
components: [
|
|
45
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
46
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
47
|
+
],
|
|
48
|
+
internalType: "struct Multicall3.Result[]",
|
|
49
|
+
name: "returnData",
|
|
50
|
+
type: "tuple[]"
|
|
51
|
+
}
|
|
52
|
+
],
|
|
53
|
+
stateMutability: "payable",
|
|
54
|
+
type: "function"
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
inputs: [
|
|
58
|
+
{
|
|
59
|
+
components: [
|
|
60
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
61
|
+
{ internalType: "bool", name: "allowFailure", type: "bool" },
|
|
62
|
+
{ internalType: "uint256", name: "value", type: "uint256" },
|
|
63
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
64
|
+
],
|
|
65
|
+
internalType: "struct Multicall3.Call3Value[]",
|
|
66
|
+
name: "calls",
|
|
67
|
+
type: "tuple[]"
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
name: "aggregate3Value",
|
|
71
|
+
outputs: [
|
|
72
|
+
{
|
|
73
|
+
components: [
|
|
74
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
75
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
76
|
+
],
|
|
77
|
+
internalType: "struct Multicall3.Result[]",
|
|
78
|
+
name: "returnData",
|
|
79
|
+
type: "tuple[]"
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
stateMutability: "payable",
|
|
83
|
+
type: "function"
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
inputs: [
|
|
87
|
+
{
|
|
88
|
+
components: [
|
|
89
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
90
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
91
|
+
],
|
|
92
|
+
internalType: "struct Multicall3.Call[]",
|
|
93
|
+
name: "calls",
|
|
94
|
+
type: "tuple[]"
|
|
95
|
+
}
|
|
96
|
+
],
|
|
97
|
+
name: "blockAndAggregate",
|
|
98
|
+
outputs: [
|
|
99
|
+
{ internalType: "uint256", name: "blockNumber", type: "uint256" },
|
|
100
|
+
{ internalType: "bytes32", name: "blockHash", type: "bytes32" },
|
|
101
|
+
{
|
|
102
|
+
components: [
|
|
103
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
104
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
105
|
+
],
|
|
106
|
+
internalType: "struct Multicall3.Result[]",
|
|
107
|
+
name: "returnData",
|
|
108
|
+
type: "tuple[]"
|
|
109
|
+
}
|
|
110
|
+
],
|
|
111
|
+
stateMutability: "payable",
|
|
112
|
+
type: "function"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
inputs: [],
|
|
116
|
+
name: "getBasefee",
|
|
117
|
+
outputs: [{ internalType: "uint256", name: "basefee", type: "uint256" }],
|
|
118
|
+
stateMutability: "view",
|
|
119
|
+
type: "function"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
inputs: [{ internalType: "uint256", name: "blockNumber", type: "uint256" }],
|
|
123
|
+
name: "getBlockHash",
|
|
124
|
+
outputs: [{ internalType: "bytes32", name: "blockHash", type: "bytes32" }],
|
|
125
|
+
stateMutability: "view",
|
|
126
|
+
type: "function"
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
inputs: [],
|
|
130
|
+
name: "getBlockNumber",
|
|
131
|
+
outputs: [{ internalType: "uint256", name: "blockNumber", type: "uint256" }],
|
|
132
|
+
stateMutability: "view",
|
|
133
|
+
type: "function"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
inputs: [],
|
|
137
|
+
name: "getChainId",
|
|
138
|
+
outputs: [{ internalType: "uint256", name: "chainid", type: "uint256" }],
|
|
139
|
+
stateMutability: "view",
|
|
140
|
+
type: "function"
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
inputs: [],
|
|
144
|
+
name: "getCurrentBlockCoinbase",
|
|
145
|
+
outputs: [{ internalType: "address", name: "coinbase", type: "address" }],
|
|
146
|
+
stateMutability: "view",
|
|
147
|
+
type: "function"
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
inputs: [],
|
|
151
|
+
name: "getCurrentBlockDifficulty",
|
|
152
|
+
outputs: [{ internalType: "uint256", name: "difficulty", type: "uint256" }],
|
|
153
|
+
stateMutability: "view",
|
|
154
|
+
type: "function"
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
inputs: [],
|
|
158
|
+
name: "getCurrentBlockGasLimit",
|
|
159
|
+
outputs: [{ internalType: "uint256", name: "gaslimit", type: "uint256" }],
|
|
160
|
+
stateMutability: "view",
|
|
161
|
+
type: "function"
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
inputs: [],
|
|
165
|
+
name: "getCurrentBlockTimestamp",
|
|
166
|
+
outputs: [{ internalType: "uint256", name: "timestamp", type: "uint256" }],
|
|
167
|
+
stateMutability: "view",
|
|
168
|
+
type: "function"
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
inputs: [{ internalType: "address", name: "addr", type: "address" }],
|
|
172
|
+
name: "getEthBalance",
|
|
173
|
+
outputs: [{ internalType: "uint256", name: "balance", type: "uint256" }],
|
|
174
|
+
stateMutability: "view",
|
|
175
|
+
type: "function"
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
inputs: [],
|
|
179
|
+
name: "getLastBlockHash",
|
|
180
|
+
outputs: [{ internalType: "bytes32", name: "blockHash", type: "bytes32" }],
|
|
181
|
+
stateMutability: "view",
|
|
182
|
+
type: "function"
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
inputs: [
|
|
186
|
+
{ internalType: "bool", name: "requireSuccess", type: "bool" },
|
|
187
|
+
{
|
|
188
|
+
components: [
|
|
189
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
190
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
191
|
+
],
|
|
192
|
+
internalType: "struct Multicall3.Call[]",
|
|
193
|
+
name: "calls",
|
|
194
|
+
type: "tuple[]"
|
|
195
|
+
}
|
|
196
|
+
],
|
|
197
|
+
name: "tryAggregate",
|
|
198
|
+
outputs: [
|
|
199
|
+
{
|
|
200
|
+
components: [
|
|
201
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
202
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
203
|
+
],
|
|
204
|
+
internalType: "struct Multicall3.Result[]",
|
|
205
|
+
name: "returnData",
|
|
206
|
+
type: "tuple[]"
|
|
207
|
+
}
|
|
208
|
+
],
|
|
209
|
+
stateMutability: "payable",
|
|
210
|
+
type: "function"
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
inputs: [
|
|
214
|
+
{ internalType: "bool", name: "requireSuccess", type: "bool" },
|
|
215
|
+
{
|
|
216
|
+
components: [
|
|
217
|
+
{ internalType: "address", name: "target", type: "address" },
|
|
218
|
+
{ internalType: "bytes", name: "callData", type: "bytes" }
|
|
219
|
+
],
|
|
220
|
+
internalType: "struct Multicall3.Call[]",
|
|
221
|
+
name: "calls",
|
|
222
|
+
type: "tuple[]"
|
|
223
|
+
}
|
|
224
|
+
],
|
|
225
|
+
name: "tryBlockAndAggregate",
|
|
226
|
+
outputs: [
|
|
227
|
+
{ internalType: "uint256", name: "blockNumber", type: "uint256" },
|
|
228
|
+
{ internalType: "bytes32", name: "blockHash", type: "bytes32" },
|
|
229
|
+
{
|
|
230
|
+
components: [
|
|
231
|
+
{ internalType: "bool", name: "success", type: "bool" },
|
|
232
|
+
{ internalType: "bytes", name: "returnData", type: "bytes" }
|
|
233
|
+
],
|
|
234
|
+
internalType: "struct Multicall3.Result[]",
|
|
235
|
+
name: "returnData",
|
|
236
|
+
type: "tuple[]"
|
|
237
|
+
}
|
|
238
|
+
],
|
|
239
|
+
stateMutability: "payable",
|
|
240
|
+
type: "function"
|
|
241
|
+
}
|
|
242
|
+
];
|
|
243
|
+
|
|
244
|
+
// index.ts
|
|
245
|
+
var multicallAddresses = {
|
|
246
|
+
1: "0xcA11bde05977b3631167028862bE2a173976CA11",
|
|
247
|
+
4: "0xcA11bde05977b3631167028862bE2a173976CA11",
|
|
248
|
+
5: "0xcA11bde05977b3631167028862bE2a173976CA11",
|
|
249
|
+
56: "0xcA11bde05977b3631167028862bE2a173976CA11",
|
|
250
|
+
97: "0xcA11bde05977b3631167028862bE2a173976CA11"
|
|
251
|
+
};
|
|
252
|
+
var getMulticallContract = (chainId, provider) => {
|
|
253
|
+
if (multicallAddresses[chainId]) {
|
|
254
|
+
return new Contract(multicallAddresses[chainId], Multicall_default, provider);
|
|
255
|
+
}
|
|
256
|
+
return null;
|
|
257
|
+
};
|
|
258
|
+
function createMulticall(provider) {
|
|
259
|
+
const multicall = async (abi, calls, chainId = ChainId.BSC) => {
|
|
260
|
+
const multi = getMulticallContract(chainId, provider({ chainId }));
|
|
261
|
+
if (!multi)
|
|
262
|
+
throw new Error(`Multicall Provider missing for ${chainId}`);
|
|
263
|
+
const itf = new Interface(abi);
|
|
264
|
+
const calldata = calls.map((call) => ({
|
|
265
|
+
target: call.address.toLowerCase(),
|
|
266
|
+
callData: itf.encodeFunctionData(call.name, call.params)
|
|
267
|
+
}));
|
|
268
|
+
const { returnData } = await multi.callStatic.aggregate(calldata);
|
|
269
|
+
const res = returnData.map((call, i) => itf.decodeFunctionResult(calls[i].name, call));
|
|
270
|
+
return res;
|
|
271
|
+
};
|
|
272
|
+
const multicallv2 = async ({ abi, calls, chainId = ChainId.BSC, options, provider: _provider }) => {
|
|
273
|
+
const { requireSuccess = true, ...overrides } = options || {};
|
|
274
|
+
const multi = getMulticallContract(chainId, _provider || provider({ chainId }));
|
|
275
|
+
if (!multi)
|
|
276
|
+
throw new Error(`Multicall Provider missing for ${chainId}`);
|
|
277
|
+
const itf = new Interface(abi);
|
|
278
|
+
const calldata = calls.map((call) => ({
|
|
279
|
+
target: call.address.toLowerCase(),
|
|
280
|
+
callData: itf.encodeFunctionData(call.name, call.params)
|
|
281
|
+
}));
|
|
282
|
+
const returnData = await multi.callStatic.tryAggregate(requireSuccess, calldata, overrides);
|
|
283
|
+
const res = returnData.map((call, i) => {
|
|
284
|
+
const [result, data] = call;
|
|
285
|
+
return result && data !== "0x" ? itf.decodeFunctionResult(calls[i].name, data) : null;
|
|
286
|
+
});
|
|
287
|
+
return res;
|
|
288
|
+
};
|
|
289
|
+
const multicallv3 = async ({ calls, chainId = ChainId.BSC, allowFailure, overrides }) => {
|
|
290
|
+
const multi = getMulticallContract(chainId, provider({ chainId }));
|
|
291
|
+
if (!multi)
|
|
292
|
+
throw new Error(`Multicall Provider missing for ${chainId}`);
|
|
293
|
+
const _calls = calls.map(({ abi, address, name, params, allowFailure: _allowFailure }) => {
|
|
294
|
+
const contract = new Contract(address, abi);
|
|
295
|
+
const callData = contract.interface.encodeFunctionData(name, params != null ? params : []);
|
|
296
|
+
if (!contract[name])
|
|
297
|
+
console.error(`${name} missing on ${address}`);
|
|
298
|
+
return {
|
|
299
|
+
target: address,
|
|
300
|
+
allowFailure: allowFailure || _allowFailure,
|
|
301
|
+
callData
|
|
302
|
+
};
|
|
303
|
+
});
|
|
304
|
+
const result = await multi.callStatic.aggregate3([...[_calls], ...overrides ? [overrides] : []]);
|
|
305
|
+
return result.map((call, i) => {
|
|
306
|
+
const { returnData, success } = call;
|
|
307
|
+
if (!success || returnData === "0x")
|
|
308
|
+
return null;
|
|
309
|
+
const { address, abi, name } = calls[i];
|
|
310
|
+
const contract = new Contract(address, abi);
|
|
311
|
+
const decoded = contract.interface.decodeFunctionResult(name, returnData);
|
|
312
|
+
return decoded;
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
return {
|
|
316
|
+
multicall,
|
|
317
|
+
multicallv2,
|
|
318
|
+
multicallv3
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
export {
|
|
322
|
+
createMulticall,
|
|
323
|
+
getMulticallContract,
|
|
324
|
+
multicallAddresses
|
|
325
|
+
};
|
package/index.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { Interface } from '@ethersproject/abi'
|
|
2
|
+
import { CallOverrides, Contract } from '@ethersproject/contracts'
|
|
3
|
+
import { Provider } from '@ethersproject/providers'
|
|
4
|
+
import { ChainId } from '@pancakeswap/sdk'
|
|
5
|
+
|
|
6
|
+
import multicallAbi from './Multicall.json'
|
|
7
|
+
|
|
8
|
+
export const multicallAddresses = {
|
|
9
|
+
1: '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
10
|
+
4: '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
11
|
+
5: '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
12
|
+
56: '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
13
|
+
97: '0xcA11bde05977b3631167028862bE2a173976CA11',
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const getMulticallContract = (chainId: ChainId, provider: Provider) => {
|
|
17
|
+
if (multicallAddresses[chainId]) {
|
|
18
|
+
return new Contract(multicallAddresses[chainId], multicallAbi, provider)
|
|
19
|
+
}
|
|
20
|
+
return null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface Call {
|
|
24
|
+
address: string // Address of the contract
|
|
25
|
+
name: string // Function name on the contract (example: balanceOf)
|
|
26
|
+
params?: any[] // Function params
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface MulticallOptions extends CallOverrides {
|
|
30
|
+
requireSuccess?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Multicall V2 uses the new "tryAggregate" function. It is different in 2 ways
|
|
35
|
+
*
|
|
36
|
+
* 1. If "requireSuccess" is false multicall will not bail out if one of the calls fails
|
|
37
|
+
* 2. The return includes a boolean whether the call was successful e.g. [wasSuccessful, callResult]
|
|
38
|
+
*/
|
|
39
|
+
interface MulticallV2Params {
|
|
40
|
+
abi: any[]
|
|
41
|
+
calls: Call[]
|
|
42
|
+
chainId?: ChainId
|
|
43
|
+
options?: MulticallOptions
|
|
44
|
+
provider?: Provider
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface CallV3 extends Call {
|
|
48
|
+
abi: any[]
|
|
49
|
+
allowFailure?: boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface MulticallV3Params {
|
|
53
|
+
calls: CallV3[]
|
|
54
|
+
chainId?: ChainId
|
|
55
|
+
allowFailure?: boolean
|
|
56
|
+
overrides?: CallOverrides
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type MultiCallV2 = <T = any>(params: MulticallV2Params) => Promise<T>
|
|
60
|
+
export type MultiCall = <T = any>(abi: any[], calls: Call[], chainId?: ChainId) => Promise<T>
|
|
61
|
+
|
|
62
|
+
export function createMulticall<TProvider extends Provider>(
|
|
63
|
+
provider: ({ chainId }: { chainId?: number | undefined }) => TProvider,
|
|
64
|
+
) {
|
|
65
|
+
const multicall: MultiCall = async (abi: any[], calls: Call[], chainId = ChainId.BSC) => {
|
|
66
|
+
const multi = getMulticallContract(chainId, provider({ chainId }))
|
|
67
|
+
if (!multi) throw new Error(`Multicall Provider missing for ${chainId}`)
|
|
68
|
+
const itf = new Interface(abi)
|
|
69
|
+
|
|
70
|
+
const calldata = calls.map((call) => ({
|
|
71
|
+
target: call.address.toLowerCase(),
|
|
72
|
+
callData: itf.encodeFunctionData(call.name, call.params),
|
|
73
|
+
}))
|
|
74
|
+
const { returnData } = await multi.callStatic.aggregate(calldata)
|
|
75
|
+
|
|
76
|
+
const res = returnData.map((call: any, i: number) => itf.decodeFunctionResult(calls[i].name, call))
|
|
77
|
+
|
|
78
|
+
return res as any
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const multicallv2: MultiCallV2 = async ({ abi, calls, chainId = ChainId.BSC, options, provider: _provider }) => {
|
|
82
|
+
const { requireSuccess = true, ...overrides } = options || {}
|
|
83
|
+
const multi = getMulticallContract(chainId, _provider || provider({ chainId }))
|
|
84
|
+
if (!multi) throw new Error(`Multicall Provider missing for ${chainId}`)
|
|
85
|
+
const itf = new Interface(abi)
|
|
86
|
+
|
|
87
|
+
const calldata = calls.map((call) => ({
|
|
88
|
+
target: call.address.toLowerCase(),
|
|
89
|
+
callData: itf.encodeFunctionData(call.name, call.params),
|
|
90
|
+
}))
|
|
91
|
+
|
|
92
|
+
const returnData = await multi.callStatic.tryAggregate(requireSuccess, calldata, overrides)
|
|
93
|
+
const res = returnData.map((call: any, i: number) => {
|
|
94
|
+
const [result, data] = call
|
|
95
|
+
return result && data !== '0x' ? itf.decodeFunctionResult(calls[i].name, data) : null
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
return res as any
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const multicallv3 = async ({ calls, chainId = ChainId.BSC, allowFailure, overrides }: MulticallV3Params) => {
|
|
102
|
+
const multi = getMulticallContract(chainId, provider({ chainId }))
|
|
103
|
+
if (!multi) throw new Error(`Multicall Provider missing for ${chainId}`)
|
|
104
|
+
const _calls = calls.map(({ abi, address, name, params, allowFailure: _allowFailure }) => {
|
|
105
|
+
const contract = new Contract(address, abi)
|
|
106
|
+
const callData = contract.interface.encodeFunctionData(name, params ?? [])
|
|
107
|
+
if (!contract[name]) console.error(`${name} missing on ${address}`)
|
|
108
|
+
return {
|
|
109
|
+
target: address,
|
|
110
|
+
allowFailure: allowFailure || _allowFailure,
|
|
111
|
+
callData,
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
const result = await multi.callStatic.aggregate3([...[_calls], ...(overrides ? [overrides] : [])])
|
|
116
|
+
|
|
117
|
+
return result.map((call: any, i: number) => {
|
|
118
|
+
const { returnData, success } = call
|
|
119
|
+
if (!success || returnData === '0x') return null
|
|
120
|
+
const { address, abi, name } = calls[i]
|
|
121
|
+
const contract = new Contract(address, abi)
|
|
122
|
+
const decoded = contract.interface.decodeFunctionResult(name, returnData)
|
|
123
|
+
return decoded
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
multicall,
|
|
129
|
+
multicallv2,
|
|
130
|
+
multicallv3,
|
|
131
|
+
}
|
|
132
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pancakeswap/multicall",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsup",
|
|
7
|
+
"prepublishOnly": "yarn run build",
|
|
8
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
9
|
+
},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"@pancakeswap/sdk": "^3.1.0"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@ethersproject/abi": "^5.0.0",
|
|
15
|
+
"@ethersproject/contracts": "^5.0.0",
|
|
16
|
+
"@ethersproject/providers": "^5.0.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@pancakeswap/sdk": "^3.1.0",
|
|
20
|
+
"tsconfig": "*",
|
|
21
|
+
"tsup": "^5.10.1",
|
|
22
|
+
"typescript": "^4.5.3"
|
|
23
|
+
},
|
|
24
|
+
"exports": {
|
|
25
|
+
"./package.json": "./package.json",
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.mjs",
|
|
29
|
+
"require": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
package/tsconfig.json
ADDED