@certik/skynet 0.8.14 → 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,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.0
4
+
5
+ - Added `web3` library with call support
6
+
7
+ ## 0.8.16
8
+
9
+ - Changed monitor exit behavior
10
+ - Improved OpsGenie message for service checks
11
+
12
+ ## 0.8.15
13
+
14
+ - Fixed OpsGenie message bug
15
+
3
16
  ## 0.8.14
4
17
 
5
18
  - Switched monitor to send to opsgenie only
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/app.js CHANGED
@@ -202,8 +202,7 @@ function indexer({ name, selector, build, check, env = {}, region = "us-east-1"
202
202
  bin: `${bin} check`,
203
203
  schedule: check.schedule,
204
204
  cpu: check.cpu || 100,
205
- mem: check.mem || 100,
206
- hasSlack: check.slackChannel,
205
+ mem: check.mem || 100
207
206
  },
208
207
  });
209
208
 
@@ -216,8 +215,7 @@ function indexer({ name, selector, build, check, env = {}, region = "us-east-1"
216
215
  type: "stateless",
217
216
  selector,
218
217
  check: check.func,
219
- maxRetry: check.maxRetry,
220
- slackChannel: check.slackChannel,
218
+ maxRetry: check.maxRetry
221
219
  });
222
220
 
223
221
  monitor();
@@ -317,8 +315,7 @@ function modeIndexer({ name, selector, state, build, validate, check, env = {},
317
315
  bin: `${bin} check`,
318
316
  schedule: check.schedule,
319
317
  cpu: check.cpu || 100,
320
- mem: check.mem || 100,
321
- hasSlack: check.slackChannel,
318
+ mem: check.mem || 100
322
319
  },
323
320
  });
324
321
 
@@ -332,8 +329,7 @@ function modeIndexer({ name, selector, state, build, validate, check, env = {},
332
329
  selector,
333
330
  mode: true,
334
331
  check: check.func,
335
- maxRetry: check.maxRetry,
336
- slackChannel: check.slackChannel,
332
+ maxRetry: check.maxRetry
337
333
  });
338
334
 
339
335
  monitor();
@@ -421,8 +417,7 @@ function producer({ name, selector, produce, check, state, env = {}, region = "u
421
417
  bin: `${bin} check`,
422
418
  schedule: check.schedule,
423
419
  cpu: check.cpu || 100,
424
- mem: check.mem || 100,
425
- hasSlack: check.slackChannel,
420
+ mem: check.mem || 100
426
421
  },
427
422
  });
428
423
 
@@ -435,8 +430,7 @@ function producer({ name, selector, produce, check, state, env = {}, region = "u
435
430
  type: "stateful",
436
431
  selector,
437
432
  check: check.func,
438
- maxRetry: check.maxRetry,
439
- slackChannel: check.slackChannel,
433
+ maxRetry: check.maxRetry
440
434
  });
441
435
 
442
436
  monitor();
@@ -515,8 +509,7 @@ function consumer({ name, selector, consume, check, env = {}, region = "us-east-
515
509
  bin: `${bin} check`,
516
510
  schedule: check.schedule,
517
511
  cpu: check.cpu || 100,
518
- mem: check.mem || 100,
519
- hasSlack: check.slackChannel,
512
+ mem: check.mem || 100
520
513
  },
521
514
  });
522
515
 
@@ -529,8 +522,7 @@ function consumer({ name, selector, consume, check, env = {}, region = "us-east-
529
522
  type: "stateless",
530
523
  selector,
531
524
  check: check.func,
532
- maxRetry: check.maxRetry,
533
- slackChannel: check.slackChannel,
525
+ maxRetry: check.maxRetry
534
526
  });
535
527
 
536
528
  monitor();
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/monitor.js CHANGED
@@ -4,9 +4,8 @@ const { getSelectorFlags, getSelectorDesc, toSelectorString } = require("./selec
4
4
  const { getBinaryName } = require("./cli");
5
5
  const { exponentialRetry } = require("./availability");
6
6
  const { getIndexerLatestId, getIndexerValidatedId, getIndexerState } = require("./indexer");
7
- const { postMessage } = require("./slack");
8
7
  const { postGenieMessage } = require("./opsgenie");
9
- const { getJobName, getNomadAddr } = require("./deploy");
8
+ const { getJobName } = require("./deploy");
10
9
 
11
10
  const ERROR_LEVEL = {
12
11
  INFO: "Info",
@@ -31,70 +30,44 @@ function sortErrors(errors) {
31
30
  }
32
31
 
33
32
  async function getMostRecentJobLaunch(name) {
34
- const jobsRes = await fetch(`http://localhost:4646/v1/jobs?prefix=${name}`);
33
+ try {
34
+ const jobsRes = await fetch(`http://localhost:4646/v1/jobs?prefix=${name}`);
35
35
 
36
- if (!jobsRes.ok) {
37
- console.log(`[MONITOR] request local nomad API failed`);
36
+ if (!jobsRes.ok) {
37
+ console.log(`[MONITOR] request local nomad API failed`);
38
38
 
39
- return null;
40
- }
41
-
42
- const jobs = await jobsRes.json();
43
-
44
- if (jobs.length === 0) {
45
- console.log(`[MONITOR] did not see any jobs prefixed with ${name}`);
46
-
47
- return null;
48
- }
39
+ return null;
40
+ }
49
41
 
50
- const recentFinishedJob = jobs.reverse().find((job) => {
51
- // filter out monitor job
52
- return job.ID.indexOf("-monitor") === -1 && job.Status === "dead";
53
- });
42
+ const jobs = await jobsRes.json();
54
43
 
55
- if (!recentFinishedJob) {
56
- console.log(`[MONITOR] did not see any dead jobs`);
44
+ if (jobs.length === 0) {
45
+ console.log(`[MONITOR] did not see any jobs prefixed with ${name}`);
57
46
 
58
- return null;
59
- }
47
+ return null;
48
+ }
60
49
 
61
- console.log("[MONITOR]", "most recent job info", recentFinishedJob.ID, recentFinishedJob.JobSummary.Summary);
50
+ const recentFinishedJob = jobs.reverse().find((job) => {
51
+ // filter out monitor job
52
+ return job.ID.indexOf("-monitor") === -1 && job.Status === "dead";
53
+ });
62
54
 
63
- return recentFinishedJob;
64
- }
55
+ if (!recentFinishedJob) {
56
+ console.log(`[MONITOR] did not see any dead jobs`);
65
57
 
66
- function checkMostRecentJobFailures(recentFinishedJob) {
67
- if (!recentFinishedJob) {
68
- return [];
69
- }
58
+ return null;
59
+ }
70
60
 
71
- const summary = recentFinishedJob.JobSummary.Summary;
72
- const groups = Object.keys(summary);
61
+ console.log("[MONITOR]", "most recent job info", recentFinishedJob.ID, recentFinishedJob.JobSummary.Summary);
73
62
 
74
- for (let group of groups) {
75
- if (summary[group].Failed > 0) {
76
- return [
77
- {
78
- type: ERROR_LEVEL.CRITICAL,
79
- message: `The most recent launch returned non-zero exit value, please investigate`,
80
- },
81
- ];
82
- }
63
+ return recentFinishedJob;
64
+ } catch (getJobError) {
65
+ console.log("[MONITOR]", "cannot get most recent job", getJobError.message);
66
+ return null;
83
67
  }
84
-
85
- return [];
86
68
  }
87
69
 
88
- function createMonitor({
89
- binaryName,
90
- name,
91
- type = "stateless",
92
- mode = false,
93
- slackChannel,
94
- selector = {},
95
- check,
96
- maxRetry = 2,
97
- }) {
70
+ function createMonitor({ binaryName, name, type = "stateless", mode = false, selector = {}, check, maxRetry = 2 }) {
98
71
  function monitor() {
99
72
  if (!binaryName) {
100
73
  binaryName = getBinaryName();
@@ -171,22 +144,26 @@ ${
171
144
 
172
145
  const jobName = getJobName(name, selectorFlags, mode);
173
146
  const mostRecentJob = await getMostRecentJobLaunch(jobName);
174
- const recentJobFailures = checkMostRecentJobFailures(mostRecentJob);
175
-
176
- result = result.concat(recentJobFailures);
177
147
 
178
148
  if (result.length > 0) {
179
149
  console.log("Found Errors", result);
180
150
 
181
151
  if (production) {
182
- // Also alert on opsgenie (func prevents duplicate alerts)
152
+ const nomadAddr = process.env.SKYNET_NOMAD_PRODUCTION_ADDR;
153
+
154
+ // alert on opsgenie
183
155
  await postGenieMessage(
184
- `${jobName} Monitor Errors: ${result.map((m) => m.message || m).join("\n")}`,
185
- m.message
156
+ `Failed Service Check: ${jobName}`,
157
+ `<p><b>Service:</b><a href="${nomadAddr}/ui/jobs/${jobName}" target="_blank">${jobName}</a></p><p><b>Issues</b></p><ul>${sortErrors(
158
+ result
159
+ )
160
+ .map((m) => `<li><b>${m.type}:</b> ${m.message}</li>`)
161
+ .join("")}</ul>`,
162
+ verbose
186
163
  );
164
+ } else {
165
+ console.log("skip sending messages to opsgenie in dev env");
187
166
  }
188
-
189
- throw new Error(`failed due to critical errors`);
190
167
  }
191
168
 
192
169
  console.log(`[MONITOR] check successfully in ${Date.now() - startTime}ms`);
@@ -212,4 +189,4 @@ ${
212
189
  return { monitor };
213
190
  }
214
191
 
215
- module.exports = { createMonitor, ERROR_LEVEL };
192
+ module.exports = { createMonitor, ERROR_LEVEL, LEVEL_EMOJI };
package/opsgenie.js CHANGED
@@ -26,7 +26,7 @@ async function postGenieMessage(msg, desc, verbose) {
26
26
  };
27
27
 
28
28
  // Prevents duplicate alerts (See Opsgenie doc about alias)
29
- bodyHash = hash(body);
29
+ const bodyHash = hash(body);
30
30
  body.alias = bodyHash;
31
31
 
32
32
  if (verbose) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@certik/skynet",
3
- "version": "0.8.14",
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
+ };