-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·111 lines (89 loc) · 2.64 KB
/
index.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
#!/usr/bin/env node
const path = require('path');
const { exec } = require('child_process');
const program = require('commander');
const pkg = require(path.join(__dirname, 'package.json'));
const colors = require('colors');
const config = require('./src/config');
const screenOutput = require('./src/screenOutput');
const ethTools = require('./src/ethTools');
const tryWeb3 = require('./src/tryWeb3');
const DEFAULT_RPC_PORT = 8545;
function nodeSpecifier(program) {
if (program.host) {
const nodeConfig = {};
nodeConfig[program.host] = [program.port || DEFAULT_RPC_PORT];
return nodeConfig;
}
return config.nodes;
}
function activeNodes(program) {
const activeNodes = nodeConfig => {
const activeNodes = [];
Object.keys(nodeConfig).forEach(host => {
const ports = nodeConfig[host];
ports.forEach(port => {
const web3 = tryWeb3(host, port);
if (web3) {
activeNodes.push({
host,
port,
web3
});
}
});
});
return activeNodes;
};
return activeNodes(nodeSpecifier(program)).map(node => ethTools.inspectNode(node));
}
function info() {
console.log(screenOutput.info(activeNodes(program)));
}
function balance(address) {
activeNodes(program).forEach(node => {
console.log();
console.log(screenOutput.info([node]));
console.log(`${colors.yellow('Address')} ${address}: ${colors.cyan(ethTools.balance(node, address))} ΞTH`);
});
}
function attach() {
const nodes = activeNodes(program);
if (nodes.length > 0) {
const node = nodes[0];
// doesn't work !
exec(`geth attach http://${node.host}:${node.port}`, (err, stdout, stderr) => {
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
}
}
program
.version(pkg.version)
.description('Eth command line tools')
.option('--host <host>', '[Config]: Host of Ethereum node')
.option('-p, --port <port>', '[Config]: Port on which the node is running');
program
.command('info')
.description('show the accessible nodes info (nodes are specified in config file)')
.action((cmd, env) => {
info();
});
program
.command('balance [address]')
.description('get the balance on a given address')
.action((address, env) => {
balance(address);
});
// program
// .command('attach')
// .description('enter node.js console with web3 object ready for interaction')
// .action((cmd, env) => {
// attach();
// });
program.parse(process.argv);
if (program.args.length === 0) {
info();
console.log(colors.gray('Use -h option for more eth-tools commands.'));
}