@certik/skynet 0.8.15 → 0.9.1

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,8 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.1
4
+
5
+ - Support using `--schedule` argument to override default schedule when deploying
6
+
7
+ ## 0.9.0
8
+
9
+ - Added `web3` library with call support
10
+
11
+ ## 0.8.16
12
+
13
+ - Changed monitor exit behavior
14
+ - Improved OpsGenie message for service checks
15
+
3
16
  ## 0.8.15
4
17
 
5
- - OpsGenie bug fix
18
+ - Fixed OpsGenie message bug
6
19
 
7
20
  ## 0.8.14
8
21
 
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/deploy.js CHANGED
@@ -9,10 +9,15 @@ const { getEnvOrThrow } = require("./env");
9
9
  const { getBinaryName, detectSkynetDirectory } = require("./cli");
10
10
 
11
11
  const INTERVAL_ALIASES = {
12
+ secondly: "*/1 * * * * * *",
12
13
  "@secondly": "*/1 * * * * * *",
14
+ minutely: "0 * * * * * *",
13
15
  "@minutely": "0 * * * * * *",
16
+ hourly: "0 0 * * * * *",
14
17
  "@hourly": "0 0 * * * * *",
18
+ daily: "0 0 0 * * * *",
15
19
  "@daily": "0 0 0 * * * *",
20
+ weekly: "0 0 0 * * 0 *",
16
21
  "@weekly": "0 0 0 * * 0 *",
17
22
  };
18
23
 
@@ -295,7 +300,17 @@ function createModeDeploy({
295
300
  validateCpu,
296
301
  validateMem,
297
302
  }) {
298
- async function deployMode({ mode, from, to, stop, production, dryRun, verbose, ...selectorFlags }) {
303
+ async function deployMode({
304
+ mode,
305
+ from,
306
+ to,
307
+ stop,
308
+ production,
309
+ dryRun,
310
+ verbose,
311
+ schedule: cmdSchedule,
312
+ ...selectorFlags
313
+ }) {
299
314
  if (mode === "delta") {
300
315
  // delta mode will ignore from/to flags
301
316
  from = 0;
@@ -336,9 +351,17 @@ function createModeDeploy({
336
351
  // by default use delta cpu/mem settings
337
352
  const { cpu, mem } = modeResouces[mode] || modeResouces.delta;
338
353
 
339
- const deltaCron = typeof deltaSchedule === "function" ? deltaSchedule(jobName) : deltaSchedule;
354
+ let deltaCron = typeof deltaSchedule === "function" ? deltaSchedule(jobName) : deltaSchedule;
340
355
 
341
- const validateCron = typeof validateSchedule === "function" ? validateSchedule(jobName) : validateSchedule;
356
+ if (deltaSchedule && cmdSchedule) {
357
+ deltaCron = cmdSchedule;
358
+ }
359
+
360
+ let validateCron = typeof validateSchedule === "function" ? validateSchedule(jobName) : validateSchedule;
361
+
362
+ if (validateSchedule && cmdSchedule) {
363
+ validateCron = cmdSchedule;
364
+ }
342
365
 
343
366
  const modeIntervals = {
344
367
  delta: INTERVAL_ALIASES[deltaCron] || deltaCron,
@@ -404,7 +427,8 @@ ${getSelectorDesc(selector)}
404
427
  --from min id to build
405
428
  --to max id to build
406
429
  --stop stop job instead of running the job
407
- --production deploy to production environment
430
+ --production deploy to production, default is development
431
+ --schedule override default schedule, support aliases: secondly, minutely, hourly, daily, weekly
408
432
  --verbose Output debug messages
409
433
  --dry-run print nomad job file but do not really execute it
410
434
 
@@ -432,6 +456,9 @@ ${getSelectorDesc(selector)}
432
456
  type: "number",
433
457
  default: 0,
434
458
  },
459
+ schedule: {
460
+ type: "string",
461
+ },
435
462
  verbose: {
436
463
  type: "boolean",
437
464
  default: false,
@@ -476,7 +503,7 @@ function createDeploy({
476
503
  cpu,
477
504
  mem,
478
505
  }) {
479
- async function deployModeless({ production, stop, dryRun, verbose, ...selectorFlags }) {
506
+ async function deployModeless({ production, stop, dryRun, verbose, schedule: cmdSchedule, ...selectorFlags }) {
480
507
  const jobName = getJobName(name, selectorFlags, null);
481
508
 
482
509
  const selectorCmdPart = Object.keys(selectorFlags)
@@ -489,7 +516,12 @@ function createDeploy({
489
516
  args += ` --verbose`;
490
517
  }
491
518
 
492
- const cron = typeof schedule === "function" ? schedule(jobName) : schedule;
519
+ let cron = typeof schedule === "function" ? schedule(jobName) : schedule;
520
+
521
+ if (schedule && cmdSchedule) {
522
+ // cmd schedule has higher priority
523
+ cron = cmdSchedule;
524
+ }
493
525
 
494
526
  const nomadJobDefinition = genConfig({
495
527
  jobName,
@@ -549,6 +581,7 @@ function createDeploy({
549
581
  ${getSelectorDesc(selector)}
550
582
  --stop stop job instead of running the job
551
583
  --production deploy to production, default is development
584
+ --schedule override default schedule, support aliases: secondly, minutely, hourly, daily, weekly
552
585
  --verbose Output debug messages
553
586
  --dry-run print nomad job file but do not really execute it
554
587
  `,
@@ -557,6 +590,9 @@ ${getSelectorDesc(selector)}
557
590
  version: false,
558
591
  flags: {
559
592
  ...getSelectorFlags(selector),
593
+ schedule: {
594
+ type: "string",
595
+ },
560
596
  verbose: {
561
597
  type: "boolean",
562
598
  default: false,
package/monitor.js CHANGED
@@ -30,69 +30,44 @@ function sortErrors(errors) {
30
30
  }
31
31
 
32
32
  async function getMostRecentJobLaunch(name) {
33
- 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}`);
34
35
 
35
- if (!jobsRes.ok) {
36
- console.log(`[MONITOR] request local nomad API failed`);
36
+ if (!jobsRes.ok) {
37
+ console.log(`[MONITOR] request local nomad API failed`);
37
38
 
38
- return null;
39
- }
40
-
41
- const jobs = await jobsRes.json();
42
-
43
- if (jobs.length === 0) {
44
- console.log(`[MONITOR] did not see any jobs prefixed with ${name}`);
45
-
46
- return null;
47
- }
39
+ return null;
40
+ }
48
41
 
49
- const recentFinishedJob = jobs.reverse().find((job) => {
50
- // filter out monitor job
51
- return job.ID.indexOf("-monitor") === -1 && job.Status === "dead";
52
- });
42
+ const jobs = await jobsRes.json();
53
43
 
54
- if (!recentFinishedJob) {
55
- 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}`);
56
46
 
57
- return null;
58
- }
47
+ return null;
48
+ }
59
49
 
60
- 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
+ });
61
54
 
62
- return recentFinishedJob;
63
- }
55
+ if (!recentFinishedJob) {
56
+ console.log(`[MONITOR] did not see any dead jobs`);
64
57
 
65
- function checkMostRecentJobFailures(recentFinishedJob) {
66
- if (!recentFinishedJob) {
67
- return [];
68
- }
58
+ return null;
59
+ }
69
60
 
70
- const summary = recentFinishedJob.JobSummary.Summary;
71
- const groups = Object.keys(summary);
61
+ console.log("[MONITOR]", "most recent job info", recentFinishedJob.ID, recentFinishedJob.JobSummary.Summary);
72
62
 
73
- for (let group of groups) {
74
- if (summary[group].Failed > 0) {
75
- return [
76
- {
77
- type: ERROR_LEVEL.CRITICAL,
78
- message: `The most recent launch returned non-zero exit value, please investigate`,
79
- },
80
- ];
81
- }
63
+ return recentFinishedJob;
64
+ } catch (getJobError) {
65
+ console.log("[MONITOR]", "cannot get most recent job", getJobError.message);
66
+ return null;
82
67
  }
83
-
84
- return [];
85
68
  }
86
69
 
87
- function createMonitor({
88
- binaryName,
89
- name,
90
- type = "stateless",
91
- mode = false,
92
- selector = {},
93
- check,
94
- maxRetry = 2,
95
- }) {
70
+ function createMonitor({ binaryName, name, type = "stateless", mode = false, selector = {}, check, maxRetry = 2 }) {
96
71
  function monitor() {
97
72
  if (!binaryName) {
98
73
  binaryName = getBinaryName();
@@ -169,25 +144,26 @@ ${
169
144
 
170
145
  const jobName = getJobName(name, selectorFlags, mode);
171
146
  const mostRecentJob = await getMostRecentJobLaunch(jobName);
172
- const recentJobFailures = checkMostRecentJobFailures(mostRecentJob);
173
-
174
- result = result.concat(recentJobFailures);
175
147
 
176
148
  if (result.length > 0) {
177
149
  console.log("Found Errors", result);
178
150
 
179
151
  if (production) {
180
- // Also alert on opsgenie (func prevents duplicate alerts)
152
+ const nomadAddr = process.env.SKYNET_NOMAD_PRODUCTION_ADDR;
153
+
154
+ // alert on opsgenie
181
155
  await postGenieMessage(
182
- `${jobName} Monitor Errors`,
183
- `${sortErrors(result)
184
- .map((m) => m.message || m)
185
- .join("\n")}`,
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>`,
186
162
  verbose
187
163
  );
164
+ } else {
165
+ console.log("skip sending messages to opsgenie in dev env");
188
166
  }
189
-
190
- throw new Error(`failed due to critical errors`);
191
167
  }
192
168
 
193
169
  console.log(`[MONITOR] check successfully in ${Date.now() - startTime}ms`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@certik/skynet",
3
- "version": "0.8.15",
3
+ "version": "0.9.1",
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
+ };