@certik/skynet 0.8.16 → 0.9.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.2
4
+
5
+ - Enhanced security by moving secret files into nomad secrets folder
6
+
7
+ ## 0.9.1
8
+
9
+ - Supported using `--schedule` argument to override default schedule when deploying
10
+
11
+ ## 0.9.0
12
+
13
+ - Added `web3` library with call support
14
+
3
15
  ## 0.8.16
4
16
 
5
17
  - 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/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
 
@@ -120,7 +125,7 @@ const genConfig = ({
120
125
 
121
126
  template {
122
127
  change_mode = "restart"
123
- destination = "context.env"
128
+ destination = "secrets/context.env"
124
129
  env = true
125
130
 
126
131
  data = <<EOH
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@certik/skynet",
3
- "version": "0.8.16",
3
+ "version": "0.9.2",
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
+ };