@certik/skynet 0.8.16 → 0.9.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.0
4
+
5
+ - Added `web3` library with call support
6
+
3
7
  ## 0.8.16
4
8
 
5
9
  - Changed monitor exit behavior
package/abi.js CHANGED
@@ -6,35 +6,35 @@ const ERC20 = [
6
6
  outputs: [
7
7
  {
8
8
  name: "",
9
- type: "string"
10
- }
9
+ type: "string",
10
+ },
11
11
  ],
12
12
  payable: false,
13
13
  stateMutability: "view",
14
- type: "function"
14
+ type: "function",
15
15
  },
16
16
  {
17
17
  constant: false,
18
18
  inputs: [
19
19
  {
20
20
  name: "_spender",
21
- type: "address"
21
+ type: "address",
22
22
  },
23
23
  {
24
24
  name: "_value",
25
- type: "uint256"
26
- }
25
+ type: "uint256",
26
+ },
27
27
  ],
28
28
  name: "approve",
29
29
  outputs: [
30
30
  {
31
31
  name: "",
32
- type: "bool"
33
- }
32
+ type: "bool",
33
+ },
34
34
  ],
35
35
  payable: false,
36
36
  stateMutability: "nonpayable",
37
- type: "function"
37
+ type: "function",
38
38
  },
39
39
  {
40
40
  constant: true,
@@ -43,39 +43,39 @@ const ERC20 = [
43
43
  outputs: [
44
44
  {
45
45
  name: "",
46
- type: "uint256"
47
- }
46
+ type: "uint256",
47
+ },
48
48
  ],
49
49
  payable: false,
50
50
  stateMutability: "view",
51
- type: "function"
51
+ type: "function",
52
52
  },
53
53
  {
54
54
  constant: false,
55
55
  inputs: [
56
56
  {
57
57
  name: "_from",
58
- type: "address"
58
+ type: "address",
59
59
  },
60
60
  {
61
61
  name: "_to",
62
- type: "address"
62
+ type: "address",
63
63
  },
64
64
  {
65
65
  name: "_value",
66
- type: "uint256"
67
- }
66
+ type: "uint256",
67
+ },
68
68
  ],
69
69
  name: "transferFrom",
70
70
  outputs: [
71
71
  {
72
72
  name: "",
73
- type: "bool"
74
- }
73
+ type: "bool",
74
+ },
75
75
  ],
76
76
  payable: false,
77
77
  stateMutability: "nonpayable",
78
- type: "function"
78
+ type: "function",
79
79
  },
80
80
  {
81
81
  constant: true,
@@ -84,31 +84,31 @@ const ERC20 = [
84
84
  outputs: [
85
85
  {
86
86
  name: "",
87
- type: "uint8"
88
- }
87
+ type: "uint8",
88
+ },
89
89
  ],
90
90
  payable: false,
91
91
  stateMutability: "view",
92
- type: "function"
92
+ type: "function",
93
93
  },
94
94
  {
95
95
  constant: true,
96
96
  inputs: [
97
97
  {
98
98
  name: "_owner",
99
- type: "address"
100
- }
99
+ type: "address",
100
+ },
101
101
  ],
102
102
  name: "balanceOf",
103
103
  outputs: [
104
104
  {
105
105
  name: "balance",
106
- type: "uint256"
107
- }
106
+ type: "uint256",
107
+ },
108
108
  ],
109
109
  payable: false,
110
110
  stateMutability: "view",
111
- type: "function"
111
+ type: "function",
112
112
  },
113
113
  {
114
114
  constant: true,
@@ -117,63 +117,63 @@ const ERC20 = [
117
117
  outputs: [
118
118
  {
119
119
  name: "",
120
- type: "string"
121
- }
120
+ type: "string",
121
+ },
122
122
  ],
123
123
  payable: false,
124
124
  stateMutability: "view",
125
- type: "function"
125
+ type: "function",
126
126
  },
127
127
  {
128
128
  constant: false,
129
129
  inputs: [
130
130
  {
131
131
  name: "_to",
132
- type: "address"
132
+ type: "address",
133
133
  },
134
134
  {
135
135
  name: "_value",
136
- type: "uint256"
137
- }
136
+ type: "uint256",
137
+ },
138
138
  ],
139
139
  name: "transfer",
140
140
  outputs: [
141
141
  {
142
142
  name: "",
143
- type: "bool"
144
- }
143
+ type: "bool",
144
+ },
145
145
  ],
146
146
  payable: false,
147
147
  stateMutability: "nonpayable",
148
- type: "function"
148
+ type: "function",
149
149
  },
150
150
  {
151
151
  constant: true,
152
152
  inputs: [
153
153
  {
154
154
  name: "_owner",
155
- type: "address"
155
+ type: "address",
156
156
  },
157
157
  {
158
158
  name: "_spender",
159
- type: "address"
160
- }
159
+ type: "address",
160
+ },
161
161
  ],
162
162
  name: "allowance",
163
163
  outputs: [
164
164
  {
165
165
  name: "",
166
- type: "uint256"
167
- }
166
+ type: "uint256",
167
+ },
168
168
  ],
169
169
  payable: false,
170
170
  stateMutability: "view",
171
- type: "function"
171
+ type: "function",
172
172
  },
173
173
  {
174
174
  payable: true,
175
175
  stateMutability: "payable",
176
- type: "fallback"
176
+ type: "fallback",
177
177
  },
178
178
  {
179
179
  anonymous: false,
@@ -181,21 +181,21 @@ const ERC20 = [
181
181
  {
182
182
  indexed: true,
183
183
  name: "owner",
184
- type: "address"
184
+ type: "address",
185
185
  },
186
186
  {
187
187
  indexed: true,
188
188
  name: "spender",
189
- type: "address"
189
+ type: "address",
190
190
  },
191
191
  {
192
192
  indexed: false,
193
193
  name: "value",
194
- type: "uint256"
195
- }
194
+ type: "uint256",
195
+ },
196
196
  ],
197
197
  name: "Approval",
198
- type: "event"
198
+ type: "event",
199
199
  },
200
200
  {
201
201
  anonymous: false,
@@ -203,25 +203,151 @@ const ERC20 = [
203
203
  {
204
204
  indexed: true,
205
205
  name: "from",
206
- type: "address"
206
+ type: "address",
207
207
  },
208
208
  {
209
209
  indexed: true,
210
210
  name: "to",
211
- type: "address"
211
+ type: "address",
212
212
  },
213
213
  {
214
214
  indexed: false,
215
215
  name: "value",
216
- type: "uint256"
217
- }
216
+ type: "uint256",
217
+ },
218
218
  ],
219
219
  name: "Transfer",
220
- type: "event"
221
- }
220
+ type: "event",
221
+ },
222
+ ];
223
+
224
+ const MULTICALL = [
225
+ {
226
+ inputs: [{ internalType: "address", name: "_addr", type: "address" }],
227
+ name: "callBalanceOf",
228
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
229
+ stateMutability: "view",
230
+ type: "function",
231
+ },
232
+ {
233
+ inputs: [],
234
+ name: "callBlockNumber",
235
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
236
+ stateMutability: "view",
237
+ type: "function",
238
+ },
239
+ {
240
+ inputs: [{ internalType: "uint256", name: "_i", type: "uint256" }],
241
+ name: "callBlockhash",
242
+ outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
243
+ stateMutability: "view",
244
+ type: "function",
245
+ },
246
+ {
247
+ inputs: [],
248
+ name: "callChainId",
249
+ outputs: [{ internalType: "uint256", name: "id", type: "uint256" }],
250
+ stateMutability: "pure",
251
+ type: "function",
252
+ },
253
+ {
254
+ inputs: [{ internalType: "address", name: "_addr", type: "address" }],
255
+ name: "callCode",
256
+ outputs: [{ internalType: "bytes", name: "code", type: "bytes" }],
257
+ stateMutability: "view",
258
+ type: "function",
259
+ },
260
+ {
261
+ inputs: [{ internalType: "address", name: "_addr", type: "address" }],
262
+ name: "callCodeHash",
263
+ outputs: [{ internalType: "bytes32", name: "codeHash", type: "bytes32" }],
264
+ stateMutability: "view",
265
+ type: "function",
266
+ },
267
+ {
268
+ inputs: [{ internalType: "address", name: "_addr", type: "address" }],
269
+ name: "callCodeSize",
270
+ outputs: [{ internalType: "uint256", name: "size", type: "uint256" }],
271
+ stateMutability: "view",
272
+ type: "function",
273
+ },
274
+ {
275
+ inputs: [],
276
+ name: "callCoinbase",
277
+ outputs: [{ internalType: "address", name: "", type: "address" }],
278
+ stateMutability: "view",
279
+ type: "function",
280
+ },
281
+ {
282
+ inputs: [],
283
+ name: "callDifficulty",
284
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
285
+ stateMutability: "view",
286
+ type: "function",
287
+ },
288
+ {
289
+ inputs: [],
290
+ name: "callGasLeft",
291
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
292
+ stateMutability: "view",
293
+ type: "function",
294
+ },
295
+ {
296
+ inputs: [],
297
+ name: "callGasLimit",
298
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
299
+ stateMutability: "view",
300
+ type: "function",
301
+ },
302
+ {
303
+ inputs: [],
304
+ name: "callGasPrice",
305
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
306
+ stateMutability: "view",
307
+ type: "function",
308
+ },
309
+ {
310
+ inputs: [],
311
+ name: "callOrigin",
312
+ outputs: [{ internalType: "address", name: "", type: "address" }],
313
+ stateMutability: "view",
314
+ type: "function",
315
+ },
316
+ {
317
+ inputs: [],
318
+ name: "callTimestamp",
319
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
320
+ stateMutability: "view",
321
+ type: "function",
322
+ },
323
+ {
324
+ inputs: [
325
+ {
326
+ components: [
327
+ { internalType: "bool", name: "delegateCall", type: "bool" },
328
+ { internalType: "bool", name: "revertOnError", type: "bool" },
329
+ { internalType: "uint256", name: "gasLimit", type: "uint256" },
330
+ { internalType: "address", name: "target", type: "address" },
331
+ { internalType: "uint256", name: "value", type: "uint256" },
332
+ { internalType: "bytes", name: "data", type: "bytes" },
333
+ ],
334
+ internalType: "struct IModuleCalls.Transaction[]",
335
+ name: "_txs",
336
+ type: "tuple[]",
337
+ },
338
+ ],
339
+ name: "multiCall",
340
+ outputs: [
341
+ { internalType: "bool[]", name: "_successes", type: "bool[]" },
342
+ { internalType: "bytes[]", name: "_results", type: "bytes[]" },
343
+ ],
344
+ stateMutability: "payable",
345
+ type: "function",
346
+ },
222
347
  ];
223
348
 
224
349
  module.exports = {
350
+ MULTICALL,
225
351
  ERC20,
226
- BEP20: ERC20
352
+ BEP20: ERC20,
227
353
  };
package/const.js CHANGED
@@ -23,6 +23,7 @@ const PROTOCOLS = {
23
23
  endpoint: "https://api.etherscan.io/api",
24
24
  key: getEtherScanApiKey(),
25
25
  },
26
+ multiCallProvider: "0xCa731e0f33Afbcfa9363d6F7449d1f5447d10C80",
26
27
  scanUrl: "https://etherscan.io/",
27
28
  },
28
29
  bsc: {
@@ -39,6 +40,7 @@ const PROTOCOLS = {
39
40
  endpoint: "https://api.bscscan.com/api",
40
41
  key: getBscScanApiKey(),
41
42
  },
43
+ multiCallProvider: "0xe7144e57d832c9005D252f415d205b4b8D78228e",
42
44
  scanUrl: "https://bscscan.com/",
43
45
  },
44
46
  polygon: {
@@ -56,8 +58,9 @@ const PROTOCOLS = {
56
58
  endpoint: "https://api.polygonscan.com/api",
57
59
  key: getPolygonScanApiKey(),
58
60
  },
61
+ multiCallProvider: "", // TODO
59
62
  scanUrl: "https://polygonscan.com/",
60
- },
63
+ }
61
64
  };
62
65
 
63
66
  const TIME = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@certik/skynet",
3
- "version": "0.8.16",
3
+ "version": "0.9.0",
4
4
  "description": "Skynet Shared JS library",
5
5
  "main": "index.js",
6
6
  "author": "CertiK Engineering",
package/util.js CHANGED
@@ -10,10 +10,7 @@ function partition(startAt, endAt, numGroups) {
10
10
  const step = Math.max(Math.floor(span / numGroups), 1);
11
11
  const adjustedStep = span % step === 0 ? step : step + 1;
12
12
 
13
- return [
14
- [startAt, startAt + adjustedStep - 1],
15
- ...partition(startAt + adjustedStep, endAt, numGroups - 1)
16
- ];
13
+ return [[startAt, startAt + adjustedStep - 1], ...partition(startAt + adjustedStep, endAt, numGroups - 1)];
17
14
  }
18
15
 
19
16
  // Inclusive range
@@ -50,9 +47,21 @@ function fillRange(start, end) {
50
47
  return result;
51
48
  }
52
49
 
50
+ function chunk(array, count) {
51
+ if (count == null || count < 1) return [];
52
+ var result = [];
53
+ var i = 0,
54
+ length = array.length;
55
+ while (i < length) {
56
+ result.push(array.slice(i, (i += count)));
57
+ }
58
+ return result;
59
+ }
60
+
53
61
  module.exports = {
54
62
  arrayGroup,
55
63
  partition,
56
64
  range,
57
- fillRange
65
+ fillRange,
66
+ chunk
58
67
  };
package/web3.js ADDED
@@ -0,0 +1,117 @@
1
+ const Web3 = require("web3");
2
+ const { PROTOCOLS } = require("./const");
3
+ const { MULTICALL } = require("./abi");
4
+ const { chunk } = require("./util");
5
+ const MULTICALL_CHUNK_SIZE = 400;
6
+
7
+ function newWeb3ByProtocol(protocol) {
8
+ if (!Object.keys(PROTOCOLS).includes(protocol)) {
9
+ return null;
10
+ }
11
+
12
+ return new Web3(PROTOCOLS[protocol].endpoint);
13
+ }
14
+
15
+ function normalizeCallParams(params) {
16
+ if (params === undefined) {
17
+ return [];
18
+ }
19
+
20
+ if (Array.isArray(params)) {
21
+ return params;
22
+ }
23
+
24
+ return [params];
25
+ }
26
+
27
+ async function singleCall(protocol, abi, target, params) {
28
+ const web3 = newWeb3ByProtocol(protocol);
29
+
30
+ if (!web3) {
31
+ throw new Error(`unsupported protocol`);
32
+ }
33
+
34
+ const contract = new web3.eth.Contract([abi], target);
35
+ const functionSignature = web3.eth.abi.encodeFunctionSignature(abi);
36
+ const method = contract.methods[functionSignature];
37
+
38
+ const result = await method(...normalizeCallParams(params)).call();
39
+
40
+ return result;
41
+ }
42
+
43
+ // limiter if provided, should be an instance of Bottleneck, which is part of bottleneck package
44
+ async function multiCall({ protocol, limiter = null, target, abi, calls }) {
45
+ const web3 = newWeb3ByProtocol(protocol);
46
+
47
+ if (!web3) {
48
+ throw new Error(`unsupported protocol`);
49
+ }
50
+
51
+ const multiCallProviderAddress = PROTOCOLS[protocol].multiCallProvider;
52
+
53
+ if (!multiCallProviderAddress) {
54
+ throw new Error("protocol doesn't support multicall yet");
55
+ }
56
+
57
+ if (calls.length === 0) return { callCount: 0, output: [] };
58
+
59
+ const multiCallContract = new web3.eth.Contract(MULTICALL, multiCallProviderAddress);
60
+
61
+ let maybeRateLimitedCallChunk = async (callChunk) => {
62
+ const txs = callChunk.map((call) => ({
63
+ delegateCall: false,
64
+ revertOnError: false,
65
+ gasLimit: 0,
66
+ target: call.target || target,
67
+ value: 0,
68
+ data: web3.eth.abi.encodeFunctionCall(abi, normalizeCallParams(call.params)),
69
+ }));
70
+
71
+ const response = await multiCallContract.methods.multiCall(txs).call();
72
+
73
+ return response._results.map((res, idx) => {
74
+ const input = {
75
+ target: callChunk[idx].target || target,
76
+ params: normalizeCallParams(callChunk[idx].params),
77
+ };
78
+
79
+ try {
80
+ const callResult = web3.eth.abi.decodeParameters(abi.outputs, res);
81
+
82
+ return {
83
+ input,
84
+ success: response._successes[idx],
85
+ // according to SDK doc, if there's only one, return result directly
86
+ output: abi.outputs.length === 1 ? callResult[0] : callResult,
87
+ };
88
+ } catch (decodeErr) {
89
+ console.log("decode err", abi.name, callChunk[idx].target || target, callChunk[idx].params, decodeErr.message);
90
+
91
+ return {
92
+ input,
93
+ success: false,
94
+ output: null,
95
+ };
96
+ }
97
+ });
98
+ };
99
+
100
+ if (limiter) {
101
+ maybeRateLimitedCallChunk = limiter.wrap(maybeRateLimitedCallChunk);
102
+ }
103
+
104
+ const callChunks = chunk(calls, MULTICALL_CHUNK_SIZE);
105
+
106
+ const responses = (
107
+ await Promise.all(callChunks.map(async (callChunk) => await maybeRateLimitedCallChunk(callChunk)))
108
+ ).flat();
109
+
110
+ return { actualCallCount: callChunks.length, output: responses };
111
+ }
112
+
113
+ module.exports = {
114
+ newWeb3ByProtocol,
115
+ singleCall,
116
+ multiCall,
117
+ };