This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
cli.js
142 lines (127 loc) · 3.15 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
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
const meow = require('meow')
const chalk = require('chalk')
const utils = require('web3-utils')
const send = require('.')
const cli = meow(`
Usage
$ eth-send --from <private-key> --to <address> --amount <ether> --network <network> [--silent]
Options
--from, -f Private key of sender (required)
--to, -t Address to send to (required)
--amount, -a Ether amount to send (required)
--value , -v Wei amount to send (alternative to --amount)
--network, -n Network name or network provider URI (default "mainnet")
--gasPrice, -p Gas price in gwei
--gas, -g Gas limit
--data, -d Transaction data
--silent, -s Silent output
Examples
$ eth-send --from 4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d --to 0xffcf8fdee72ac11b5c542428b35eef5769c409f0 --amount 0.01 --network rinkeby --silent
0x8ee7ed489c7cb206cd9b4ff65a5d2977324b4f727b12cd2e0c0bbcaa59219e00
`, {
flags: {
from: {
type: 'string',
alias: 'f'
},
to: {
type: 'string',
alias: 't'
},
amount: {
type: 'string',
alias: 'a'
},
value: {
type: 'string',
alias: 'v'
},
network: {
type: 'string',
alias: 'n'
},
gasPrice: {
type: 'string',
alias: 'p'
},
gas: {
type: 'string',
alias: 'g'
},
data: {
type: 'string',
alias: 'd'
},
silent: {
type: 'boolean',
alias: 's'
},
}
})
const from = cli.flags.f || cli.flags.from || process.env.FROM
const to = cli.flags.t || cli.flags.to
let value = cli.flags.v || cli.flags.value
let amount = cli.flags.a || cli.flags.amount
const data = cli.flags.d || cli.flags.data
const gas = cli.flags.g || cli.flags.gas
let gasPrice = cli.flags.p || cli.flags.gasPrice
const network = (cli.flags.n || cli.flags.network || '').toLowerCase()
const silent = cli.flags.s || cli.flags.silent
if (process.stdin) {
process.stdin.setEncoding('utf8')
process.stdin.resume()
let content = ''
process.stdin.on('data', (buf) => {
content += buf.toString()
})
setTimeout(() => {
content = content.trim()
if (content) {
amount = content
}
run()
}, 10)
} else {
run()
}
async function run() {
if (from === undefined) {
console.log('--from argument is required')
process.exit(1)
}
if (to === undefined) {
console.log('--to argument is required')
process.exit(1)
}
if (amount === undefined && value === undefined) {
console.log('--amount argument is required')
process.exit(1)
}
if (gasPrice) {
gasPrice = utils.toWei(gasPrice, 'gwei')
}
try {
const txHash = await send({
from,
to,
amount,
value,
network,
data,
gas,
gasPrice,
log: !silent
})
if (silent) {
console.log(txHash)
} else {
if (['mainnet', 'ropsten', 'rinkeby', 'kovan', 'goerli'].includes(network)) {
console.log('\n'+chalk.green(`https://${network !== 'mainnet' ? `${network}.` : '' }etherscan.io/tx/${txHash}`))
}
}
process.exit(0)
} catch(err) {
console.error(chalk.red(err.message))
process.exit(1)
}
}