forked from Tezsure/Tezster-CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tezster.js
executable file
·367 lines (327 loc) · 13.2 KB
/
tezster.js
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
#!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.1.9', '-v, --version')
.command('setup')
.action(function() {
console.log('setting up tezos node, this could take a while....');
const { exec } = require('child_process');
const fs = require("fs");
let workingDir = __dirname + '/script';
let setup_successfile_dir = workingDir + '/setup.successful';
const _cliProgress = require('cli-progress');
let progress = 1;
let progressInterval;
const progressbar = new _cliProgress.Bar({
format: 'progress [{bar}] {percentage}% | ETA: {eta}s'
}, _cliProgress.Presets.shades_classic);
progressbar.start(100, progress);
exec('./setup.sh > setup.log',{cwd : workingDir}, (err, stdout, stderr) => {
clearInterval(progressInterval);
progressbar.update(100);
progressbar.stop();
if (err) {
console.error(`tezster setup error: ${err}`);
return;
}
console.log(`${stdout}`);
if (fs.existsSync(setup_successfile_dir)) {
console.log('Setup has been completed successfully');
} else {
console.log('setup is not successful, please try running "tezster setup" again....');
}
});
progressInterval = setInterval(() => {
progress = progress + 0.055;
if (progressInterval >= 100) {
clearInterval(progressInterval);
progressbar.update(100);
progressbar.stop();
return;
}
progressbar.update(progress);
}, 1000);
});
program
.command('start-nodes')
.action(function() {
console.log('starting the nodes.....');
const { exec } = require('child_process');
let workingDir = __dirname + '/script';
const _cliProgress = require('cli-progress');
let progress = 0;
let progressInterval;
const progressbar = new _cliProgress.Bar({
format: 'progress [{bar}] {percentage}% | ETA: {eta}s'
}, _cliProgress.Presets.shades_classic);
progressbar.start(100, progress);
exec('./start_nodes.sh',{cwd : workingDir}, (err, stdout, stderr) => {
clearInterval(progressInterval);
progressbar.update(100);
progressbar.stop();
if (err) {
return;
}
console.log(`${stdout}`);
});
progressInterval = setInterval(() => {
progress = progress + 1.8;
if (progressInterval >= 100) {
clearInterval(progressInterval);
progressbar.update(100);
return;
}
progressbar.update(progress);
}, 1000);
});
program
.command('stop-nodes')
.action(function() {
console.log('stopping the nodes....');
const { exec } = require('child_process');
let workingDir = __dirname + '/script';
exec('./stop_nodes.sh ',{cwd : workingDir}, (err, stdout, stderr) => {
if (err) {
console.error(`tezster stopping nodes error: ${err}`);
return;
}
console.log(`${stdout}`);
});
});
//*******for check the balance check */
program
.command('get-balance')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 1) {
console.log(tezsterManager.outputInfo("Incorrect usage of get-balance command \n Correct usage: - tezster get-balance account/contract"));
return;
}
await tezsterManager.loadTezsterConfig();
tezsterManager.getBalance(args[0]).then((result) => {
console.log(result);
});
});
//******* To get the list accounts */
program
.command('list-accounts')
.action(async function(){
const tezsterManager = require('./tezster-manager');
await tezsterManager.loadTezsterConfig();
const config = tezsterManager.config;
if(Object.keys(config.accounts).length > 0){
for(var i in config.accounts){
console.log(tezsterManager.output(config.accounts[i].label + " - " + config.accounts[i].pkh + " (" + config.accounts[i].identity + ")"));
}
}
else{
console.log(tezsterManager.outputError("No Account is available !!"));
}
});
//******* TO get the list Contracts */
program
.command('list-contracts')
.action(async function(){
const tezsterManager = require('./tezster-manager');
await tezsterManager.loadTezsterConfig();
const config = tezsterManager.config;
if(Object.keys(config.contracts).length > 0){
for(var i in config.contracts){
console.log(tezsterManager.output(config.contracts[i].label + " - " + config.contracts[i].pkh + " (" + config.contracts[i].identity + ")"));
}
}
else{
console.log(tezsterManager.outputError("No Contracts are Available !!"));
}
});
//******* To get the Provider */
program
.command('get-provider')
.action(async function(){
const tezsterManager = require('./tezster-manager');
await tezsterManager.loadTezsterConfig();
console.log(tezsterManager.getProvider());
});
//******* To set the Provider */
program
.command('set-provider')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 1){
console.log(tezsterManager.outputError("Incorrect usage - tezster set-provider http://{ip}:{port}"));
return;
}
await tezsterManager.loadTezsterConfig();
console.log(tezsterManager.setProvider(args));
});
//******* To transfer the amount */
program
.command('transfer')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 2) {
console.log(tezsterManager.outputError("Incorrect usage - tezster transfer <amount> <from> <to> <fees>"));
return;
}
await tezsterManager.loadTezsterConfig();
console.log(`Please run "tezster bake-for <account-name> to bake this operation if operation is successful`);
tezsterManager.transferAmount(args).then((result) => {
console.log(result);
});
});
//******* To bake any operation */
program
.command('bake-for')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 1) {
console.log(tezsterManager.outputError("Incorrect usage - tezster bake-for <identity-label>"));
return;
}
console.log('baking the previous operation.....');
const { exec } = require('child_process');
let workingDir = __dirname + '/script';
exec('./bake_tx.sh ' + args[0],{cwd : workingDir}, (err, stdout, stderr) => {
if (err) {
console.error(`tezster baking opertaion error: ${err}`);
return;
}
console.log(`Baking successful ${stdout}`);
});
});
//*******deploy contract written in Michelson*/
program
.command('deploy')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 4) {
console.log(tezsterManager.outputInfo("Incorrect usage of deploy command \n Correct usage: - tezster deploy contract-label contract-absolute-path init-storage-value account"));
return;
}
await tezsterManager.loadTezsterConfig();
let result = await tezsterManager.deployContract(args[0], args[1], args[2], args[3]);
console.log(result);
console.log(tezsterManager.outputInfo(`If you're running a local node, Please run "tezster bake-for <account-name> to bake this operation`));
console.log(tezsterManager.outputInfo(`If you're using babylonnet node, use https://babylonnet.tzstats.com to check contract/transactions`));
});
//*******calls contract written in Michelson*/
program
.command('call')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 3) {
console.log(tezsterManager.outputInfo("Incorrect usage of call command \n Correct usage: - tezster call contract-name argument-value account"));
return;
}
await tezsterManager.loadTezsterConfig();
let result = await tezsterManager.invokeContract(args[0], args[1], args[2]);
console.log(result);
console.log(tezsterManager.outputInfo(`If you're running a local node, Please run "tezster bake-for <account-name> to bake this operation`));
console.log(tezsterManager.outputInfo(`If you're using babylonnet node, use https://babylonnet.tzstats.com to check contract/transactions`));
});
//*******gets storage for a contract*/
program
.command('get-storage')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 1) {
console.log(tezsterManager.outputInfo("Incorrect usage of get-storage command \n Correct usage: - tezster get-storage contract-name"));
return;
}
await tezsterManager.loadTezsterConfig();
let result = await tezsterManager.getStorage(args[0]);
console.log(result);
});
/* Restores an testnet faucet account */
program
.command('add-testnet-account')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 2) {
console.log(tezsterManager.outputInfo("Incorrect usage of add-testnet-account command \n Correct usage: - tezster add-testnet-account <account-label> <absolut-path-to-json-file>"));
return;
}
await tezsterManager.loadTezsterConfig();
let result = tezsterManager.restoreAlphanetAccount(args[0], args[1]);
console.log(result);
});
/* Restores an testnet faucet account */
program
.command('activate-testnet-account')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 1) {
console.log(tezsterManager.outputInfo("Incorrect usage of activate-testnet-account command \n Correct usage: - tezster activate-testnet-account <account-label>"));
return;
}
await tezsterManager.loadTezsterConfig();
let result = await tezsterManager.activateAlphanetAccount(args[0]);
console.log(result);
console.log(tezsterManager.outputInfo(`If this account has already been activated, it may throw 'invalid_activation' error. You can visit https://babylonnet.tzstats.com for more information on this account`));
});
/* list transactions done with tezster */
program
.command('list-transactions')
.action(async function(){
const tezsterManager = require('./tezster-manager');
await tezsterManager.loadTezsterConfig();
const config = tezsterManager.config;
console.log(tezsterManager.outputInfo('For transactions done on babylonnet node ,you can visit https://babylonnet.tzstats.com for more information'))
if(Object.keys(config.transactions).length > 0){
for(var i in config.transactions){
console.log(tezsterManager.output(JSON.stringify(config.transactions[i])));
}
} else{
console.log(tezsterManager.outputError("No transactions are Available !!"));
}
});
//******* To Create an account */
program
.command('create-account')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 3) return console.log(tezsterManager.outputError("Incorrect usage - tezster create-account <Identity> <Account Label> <amount> <spendable=true[Optional]> <delegatable=true[Optional]> <delegate[Optional]> <fee=0[Optional]>"));
await tezsterManager.loadTezsterConfig();
tezsterManager.createAccount(args).then((result) => {
console.log(result);
});
});
//******* To Create an account */
program
.command('add-contract')
.action(async function(){
var args = process.argv.slice(3);
const tezsterManager = require('./tezster-manager');
if (args.length < 2) return console.log(tezsterManager.outputError("Incorrect usage - tezster add-contract <label> <Address>"));
await tezsterManager.loadTezsterConfig();
console.log(tezsterManager.addContract(args[0], args[1], ''));
});
program
.command('help')
.action(async function(){
const tezsterManager = require('./tezster-manager');
console.log(tezsterManager.helpData); //'\x1b[33m%s\x1b[0m',
});
if (process.argv.length <= 2){
console.log('\x1b[31m%s\x1b[0m', "Error: " +"Please enter a command!");
}
var commands=process.argv[2];
const validCommands = ['list-Identities','list-accounts','list-contracts','get-balance','transfer',
'bake-for','set-provider','get-provider',
'stop-nodes','start-nodes','setup','call','deploy','help','create-account','list-transactions',
'get-storage', 'add-testnet-account', 'activate-testnet-account', 'add-contract'];
if (validCommands.indexOf(commands) < 0 && process.argv.length >2 ) {
console.log('\x1b[31m%s\x1b[0m', "Error: " + "Invalid command\nPlease run tezster help to get info about commands ");
}
program.parse(process.argv);