This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
79 lines (66 loc) · 1.61 KB
/
cli.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
const meow = require('meow')
const getBalance = require('.')
const cli = meow(`
Usage
$ eth_balance [address] --network <network>
Options
--address, -a Address to check balance
--network, -n Network name or network provider URI (default "mainnet")
--convert, -c Unit to convert to (default "ether")
Examples
$ eth_balance 0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1 --network rinkeby
0.297098768
$ eth_balance 0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1 --network rinkeby --convert wei
297098768000000000
`, {
flags: {
address: {
type: 'string',
alias: 'a'
},
convert: {
type: 'string',
alias: 'c'
},
network: {
type: 'string',
alias: 'n'
},
}
})
let address = cli.flags.a || cli.flags.address || cli.input[0]
const network = cli.flags.n || cli.flags.network
const convert = cli.flags.c || cli.flags.convert
if (process.stdin) {
process.stdin.setEncoding('utf8')
process.stdin.resume()
let content = ''
process.stdin.on('data', (buf) => {
content += buf.toString()
})
setTimeout(() => {
content = content.trim()
address = (content || address)
run(address, network, convert)
}, 10)
} else {
run(address, network, convert)
}
async function run(address, network, convert) {
if (address === undefined) {
console.log('address argument is required')
process.exit(1)
}
try {
const balance = await getBalance({
address,
network,
convert,
})
console.log(balance)
process.exit(0)
} catch(err) {
console.error(err.message)
process.exit(1)
}
}