-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multicall.js
186 lines (157 loc) · 4.73 KB
/
Multicall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
module.exports = class Multicall {
constructor(selectedNetwork=null,web3=null) {
this.contractAddress = '0xcA11bde05977b3631167028862bE2a173976CA11';
if (selectedNetwork){
this.setNetwork(selectedNetwork);
}
if (web3){
this.setWeb3(web3);
}
}
setNetwork(selectedNetwork){
this.selectedNetwork = selectedNetwork;
}
setWeb3(web3) {
this.web3 = web3;
}
getCallData(contract, methodName, params=[], extraData={}) {
const methodAbi = contract._jsonInterface.find(f => f.name === methodName && f.inputs.length === params.length);
if (!methodAbi){
return null;
}
const inputTypes = methodAbi.inputs.map( i => i.type );
const returnTypes = methodAbi.outputs.map( i => i.type );
const returnFields = methodAbi.outputs.map( i => i.name );
if (contract._address === '0x0000000000000000000000000000000000000000'){
return null;
}
const checkAddress = (address) => {
return address ? address.match(/^0x[a-fA-F0-9]{40}$/) !== null : false;
}
const args = params.map( (p,i) => {
const inputType = inputTypes[i];
if (inputType === 'address' && !checkAddress(p)){
p = '0x0000000000000000000000000000000000000000';
}
return [p].concat(inputType);
});
return {
args,
extraData,
returnFields,
returnTypes,
target:contract._address,
method:methodName+'('+inputTypes.join(',')+')',
rawCall:contract.methods[methodName](...params)
};
}
prepareMulticallData(calls,web3=null) {
web3 = this.web3 || web3;
if (!web3){
return false;
}
const strip0x = (str) => {
return str.replace(/^0x/, '');
}
const values = [
calls.map(({ target, method, args, returnTypes }) => {
return [
target,
web3.utils.keccak256(method).substr(0, 10) +
(args && args.length > 0
? strip0x(web3.eth.abi.encodeParameters(args.map(a => a[1]), args.map(a => a[0])))
: '')
];
})
];
const calldata = web3.eth.abi.encodeParameters(
[
{
components: [{ type: 'address' }, { type: 'bytes' }],
name: 'data',
type: 'tuple[]'
}
],
values
);
return '0x252dba42'+strip0x(calldata);
}
async executeMultipleBatches(callBatches,web3=null) {
const calls = [];
callBatches.forEach( (batchedCalls,batchId) => {
batchedCalls.forEach( call => {
call.batchId = batchId;
calls.push(call);
});
});
const results = await this.executeMulticalls(calls);
return results ? results.reduce( (output,r) => {
const batchId = r.callData.batchId;
if (!output[batchId]){
output[batchId] = [];
}
output[batchId].push(r);
return output;
},[]) : [];
}
catchEm(promise) {
return promise.then(data => [null,data])
.catch(err => [err,null]);
}
async executeMulticalls(calls,web3=null) {
web3 = this.web3 || web3;
const calldata = this.prepareMulticallData(calls,web3);
if (!calldata){
return null;
}
let decodedResults = [];
try {
const results = await web3.eth.call({
data: calldata,
to:this.contractAddress
});
const decodedParams = web3.eth.abi.decodeParameters(['uint256', 'bytes[]'], results);
if (decodedParams && typeof decodedParams[1] !== 'undefined'){
decodedResults = decodedParams[1];
if (decodedResults && decodedResults.length){
return decodedResults.map( (d,i) => {
const output = {
data:null,
callData:calls[i],
...calls[i].extraData
};
const returnTypes = calls[i].returnTypes;
const returnFields = calls[i].returnFields;
const decodedValues = Object.values(web3.eth.abi.decodeParameters(returnTypes,d));
if (returnTypes.length === 1){
output.data = decodedValues[0];
} else {
const values = decodedValues.splice(0,returnTypes.length);
output.data = values ? values.reduce( (acc,v,j) => {
acc[j] = v;
acc[returnFields[j]] = v;
return acc;
},{}) : {};
}
return output;
});
}
}
} catch (err) {
for (let i=0; i<calls.length; i++){
const output = {
data:null,
callData:calls[i],
...calls[i].extraData
};
const [err,result] = await this.catchEm(calls[i].rawCall.call());
if (result){
output.data = result;
}
decodedResults.push(output);
}
return decodedResults;
}
return null;
}
}