/
cli.ts
468 lines (440 loc) · 13.8 KB
/
cli.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
global.Promise = require('bluebird');
import Ajv = require('ajv');
import fs = require('fs');
import net = require('net');
import path = require('path');
import {Strings} from './lib/utils';
import * as Web3 from 'web3';
import {address, IContract} from './globals';
import {ISNPCToken} from './contracts';
import {ICliConfig} from './cli.schema';
import {tokenGroupToId} from './lib/w3contracts/utils';
import * as BigNumber from 'bignumber.js';
import readline = require('readline');
type ContractName = 'SNPCToken';
const ctx = {
contractNames: ['SNPCToken'],
cmdOpts: new Array<string>(),
verbose: false,
cfile: 'cli.yml',
SNPCToken: {},
} as {
contractNames: string[];
cmd: string;
cmdOpts: string[];
cfile: string;
cfg: ICliConfig;
verbose: boolean;
web3: Web3;
provider: Web3.providers.Provider;
SNPCToken: {
meta: IContract<ISNPCToken>;
instance: ISNPCToken;
};
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const handlers = {} as {
[k: string]: () => Promise<void>;
};
async function setup() {
const TruffleContract = require('truffle-contract');
loadConfig(ctx.cfile);
await setupWeb3();
await loadDeployedContracts();
async function loadDeployedContracts() {
const ecfg = ctx.cfg.ethereum;
const w3defaults = {
from: ecfg.from,
gas: ecfg.gas,
gasPrice: ecfg.gasPrice
};
return Promise.mapSeries(ctx.contractNames, async cn => {
if (!ecfg[cn]) {
return;
}
const c = ctx as any;
c[cn].meta = TruffleContract(JSON.parse(fs.readFileSync(ecfg[cn].schema).toString()));
c[cn].meta.setProvider(ctx.web3.currentProvider);
c[cn].meta.defaults(w3defaults);
c[cn].meta.synchronization_timeout = 0;
const addr = readDeployedContractAddress(cn);
if (addr) {
c[cn].instance = await c[cn].meta.at(addr);
console.log(`Loaded ${cn} instance at: ${addr}`);
}
});
}
async function setupWeb3() {
const ecfg = ctx.cfg.ethereum;
const endpoint = ecfg.endpoint.trim();
if (endpoint.startsWith('ipc://')) {
console.log(`Using Web3.providers.IpcProvider for ${endpoint}`);
ctx.provider = new Web3.providers.IpcProvider(endpoint.substring('ipc://'.length), net);
} else if (endpoint.startsWith('http')) {
console.log(`Using Web3.providers.HttpProvider provider for: ${endpoint}`);
ctx.provider = new Web3.providers.HttpProvider(endpoint);
} else {
throw new Error(`Unknown web3 endpoint: '${endpoint}'`);
}
ctx.web3 = new Web3(ctx.provider);
await Promise.fromNode(cb => {
ctx.web3.version.getNode((err, node) => {
if (err) {
cb(err);
return;
}
console.log(`web3 node: ${node}`);
cb(err, node);
});
});
await Promise.fromNode(cb => {
ctx.web3.version.getNetwork((err, netId) => {
if (err) {
cb(err);
return;
}
switch (netId) {
case '1':
console.log('w3 connected to >>>> MAINNET <<<<');
break;
case '2':
console.log('w3 connected to >>>> MORDEN <<<<');
break;
case '3':
console.log('w3 connected to >>>> ROPSTEN <<<<');
break;
default:
console.log('w3 connected to >>>> UNKNOWN <<<<');
}
cb(err, netId);
});
});
}
function loadConfig(cpath: string) {
const ajv = new Ajv();
const configSchema = require('./cli.schema.json');
const yaml = require('js-yaml');
const subst = {
home: process.env['HOME'],
cwd: process.cwd(),
moduledir: __dirname
};
ctx.cfg = yaml.safeLoad(Strings.replaceTemplate(fs.readFileSync(cpath, 'utf8'), subst));
if (!ajv.validate(configSchema, ctx.cfg)) {
const msg = `env: Invalid configuration: ${cpath}: `;
console.error(msg, ajv.errors);
throw new Error(`Invalid configuration: ${cpath}`);
}
if (ctx.verbose) {
console.log('Configuration ', JSON.stringify(ctx.cfg, null, 2));
}
}
}
function readDeployedContractAddress(contract: string): string | null {
const p = path.join(ctx.cfg.ethereum.lockfilesDir, `${contract}.lock`);
if (fs.existsSync(p)) {
return fs.readFileSync(p).toString('utf8');
} else {
return null;
}
}
function writeDeployedContractAddress(contract: string, addr: address) {
const p = path.join(ctx.cfg.ethereum.lockfilesDir, `${contract}.lock`);
fs.writeFileSync(p, addr);
}
function deleteDeployedContractAddress(contract: string) {
const p = path.join(ctx.cfg.ethereum.lockfilesDir, `${contract}.lock`);
fs.unlinkSync(p);
}
function failIfDeployed(cname?: ContractName) {
const c = ctx as any;
if (cname) {
if (c[cname].instance) {
throw new Error(`Contract '${cname}' is already deployed`);
}
} else {
ctx.contractNames.forEach(cn => failIfDeployed(cn as any));
}
}
function failIfNotDeployed(cname?: ContractName) {
const c = ctx as any;
if (cname) {
if (!c[cname].instance) {
throw new Error(`Contract '${cname}' is not deployed`);
}
} else {
ctx.contractNames.forEach(cn => failIfNotDeployed(cn as any));
}
}
function checkEthNetwork(): Promise<void> {
return new Promise((resolve, reject) => {
// try synchronous call
let syncing: boolean | Web3.SyncingResult;
try {
syncing = ctx.web3.eth.syncing;
} catch (err) {
// async request
ctx.web3.eth.getSyncing((err: any, sync: boolean | Web3.SyncingResult) => {
if (err) {
reject(err);
return;
}
if (sync) {
reject('Ethereum network client in pending synchronization, try again later');
} else {
resolve();
}
});
return;
}
if (syncing) {
reject('Ethereum network client in pending synchronization, try again later');
return;
}
resolve();
});
}
function confirm(question: string, validAnswer: string): Promise<void> {
return new Promise((resolve, reject) => {
rl.question(question, (answer) => {
if (answer === validAnswer) {
resolve();
} else {
reject();
}
rl.close();
});
});
}
function signSelfdestruct(contractAddress: string): Promise<any> {
return new Promise(function (res, rej) {
const message = Buffer.from('Signed for Selfdestruct').toString('hex') // convert to hex
+ contractAddress.replace(/^0x/, '')
+ ctx.cfg.ethereum.from.replace(/^0x/, '');
const hash = ctx.web3.sha3(message, {encoding: 'hex'});
const signature = ctx.web3.eth.sign(ctx.cfg.ethereum.from, hash).slice(2);
if (!!signature) {
res({
v: Number(signature.substring(128, 130)) + 27,
r: '0x' + signature.substring(0, 64),
s: '0x' + signature.substring(64, 128),
});
} else {
console.error('\x1b[41m%s\x1b[37m', 'Could not sign message for address:', '\x1b[0m', contractAddress);
}
rej(null);
});
}
// -------------------- Operations
/**
* Deploy
*/
handlers['deploy'] = async () => {
await checkEthNetwork();
let icfg = null;
if (!ctx.SNPCToken.instance) {
icfg = ctx.cfg.ethereum.SNPCToken;
console.log(`Deployment: 'SNPCToken' `, icfg);
ctx.SNPCToken.instance = await ctx.SNPCToken.meta.new(
icfg.totalSupplyTokens,
icfg.reservedTeamTokens,
icfg.reservedBountyTokens,
icfg.reservedAdvisorsTokens,
icfg.reservedReserveTokens,
icfg.reservedStackingBonusTokens,
{
from: ctx.cfg.ethereum.from
}
);
console.log(`SNPCToken successfully deployed at: ${ctx.SNPCToken.instance.address}\n\n`);
writeDeployedContractAddress('SNPCToken', ctx.SNPCToken.instance.address);
}
};
/**
* Show status info
*/
handlers['status'] = async () => {
await checkEthNetwork();
failIfNotDeployed('SNPCToken');
const token = ctx.SNPCToken.instance;
const data = {};
(<any>data)['token'] = {
address: token.address,
owner: await token.owner.call(),
symbol: await token.symbol.call(),
totalSupply: await token.totalSupply.call(),
locked: await token.locked.call()
};
console.log(JSON.stringify(data, null, 2));
};
/**
* on Token group operations
*/
handlers['group'] = async () => {
await checkEthNetwork();
failIfNotDeployed('SNPCToken');
const token = ctx.SNPCToken.instance;
const wcmd = ctx.cmdOpts.shift();
switch (wcmd) {
case 'reserve': {
await token.assignReserved(
pullCmdArg('address'),
tokenGroupToId(pullCmdArg('group') as any),
new BigNumber(pullCmdArg('tokens')).mul('1e18')
);
break;
}
case 'reserved': {
const group = pullCmdArg('group');
const remaining = await token.getReservedTokens.call(tokenGroupToId(group as any));
console.log(
JSON.stringify({
group,
remaining
}, null, 2)
);
break;
}
default:
throw new Error(`Unknown group sub-command: ${wcmd || ''}`);
}
};
handlers['token'] = async () => {
await checkEthNetwork();
failIfNotDeployed('SNPCToken');
const token = ctx.SNPCToken.instance;
const wcmd = ctx.cmdOpts.shift();
switch (wcmd) {
case 'balance': {
const tokensWithDecimals = await token.balanceOf.call(pullCmdArg('address'));
const data = {
tokens: new BigNumber(tokensWithDecimals).divToInt('1e18'),
tokensWithDecimals
};
console.log(JSON.stringify(data, null, 2));
break;
}
case 'lock':
await token.lock();
console.log({locked: await token.locked.call()});
break;
case 'unlock':
await token.unlock();
console.log({locked: await token.locked.call()});
break;
case 'locked':
console.log({locked: await token.locked.call()});
break;
case 'burn':
await confirm("Burning tokens. Are you sure? (yes/no) ", "yes")
.then(async () => {
const balanceBefore = (await token.balanceOf.call(ctx.cfg.ethereum.from)).toString();
await token.burnTokens(new BigNumber(pullCmdArg('tokens')).mul('1e18'));
console.log({
totalSupply: (await token.totalSupply.call()).toString(),
balanceBefore,
balance: (await token.balanceOf.call(ctx.cfg.ethereum.from)).toString()
});
});
break;
case 'selfdestruct':
await confirm("This action start selfdestruct on token contract. " +
"Are you sure? (YES, I want to destroy token contract/no) ",
"YES, I want to destroy token contract")
.then(async () => {
const vrs = await signSelfdestruct(token.address);
if (vrs != null) {
await token.selfDestruct(vrs.v, vrs.r, vrs.s);
deleteDeployedContractAddress('SNPCToken');
}
});
break;
default:
throw new Error(`Unknown token sub-command: ${wcmd || ''}`);
}
};
// --------------------- Helpers
function pullCmdArg(name: string): address {
const arg = ctx.cmdOpts.shift();
if (!arg) {
throw new Error(`Missing required ${name} argument for command`);
}
return arg;
}
// -------------------- Run
// Parse options
(function () {
const args = process.argv.slice(2);
for (let i = 0; i < args.length; ++i) {
const av = (args[i] = args[i].trim());
if (av.charAt(0) !== '-') {
if (ctx.cmd) {
usage(`Command '${ctx.cmd}' already specified`);
}
ctx.cmd = av;
ctx.cmdOpts = args.slice(i + 1);
break;
}
if (av === '-h' || av === '--help') {
usage();
}
if (av === '-v' || av === '--verbose') {
ctx.verbose = true;
}
if (av === '-c' || av === '--config') {
ctx.cfile = args[++i] || usage(`Missing '-c|--config' option value`);
}
}
if (!ctx.cmd) {
usage('No command specified');
}
if (!handlers[ctx.cmd]) {
usage(`Invalid command specified: '${ctx.cmd}'`);
}
console.log(`Command: ${ctx.cmd} opts: `, ctx.cmdOpts);
})();
function usage(error?: string): never {
console.error(
'Usage: \n\tnode cli.js' +
'\n\t[-c|--config <config yaml file>]' +
'\n\t[-v|--verbose]' +
'\n\t[-h|--help]' +
'\n\t<command> [command options]' +
'\nCommands:' +
'\n\tdeploy - Deploy SNPC token smart contract' +
'\n\tstatus - Get contracts status' +
'\n\ttoken balance <addr> - Get token balance for address' +
'\n\ttoken lock - Lock token contract (no token transfers are allowed)' +
'\n\ttoken unlock - Unlock token contract' +
'\n\ttoken locked - Get token lock status' +
'\n\ttoken burn <tokens> - Burning of tokens (without decimals) on current wallet' +
'\n\ttoken selfdestruct - Destroy token contract in ethereum network (can be undone)' +
'\n\tgroup reserve <addr> <group> <tokens> - Reserve tokens (without decimals) to <addr> for <group>' +
'\n\tgroup reserved <group> - Get number of remaining tokens for <group>' +
'\n' +
'\n\t\t <group> - Token reservation group: team|bounty|advisors|reserve|stackingBonus' +
'\n\t\t <addr> - Ethereum address' +
'\n'
);
if (error) {
console.error(error);
process.exit(1);
}
process.exit();
throw Error();
}
// Start
setup()
.then(handlers[ctx.cmd])
.then(() => {
process.exit(0);
})
.catch(err => {
if (err) {
console.error(err);
}
process.exit(1);
});