-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsendEth.js
70 lines (55 loc) · 1.69 KB
/
sendEth.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
const Prompt = require('prompt-password');
var prompt = new Prompt({
type: 'password',
message: '输入密码',
name: 'password',
mask: require('prompt-password-strength')
});
// var Accounts = require('web3-eth-accounts');
// var Tx = require('ethereumjs-tx');
const web3 = require('./web3-instance');
const accounts = web3.eth.accounts;
keyStore = require('./from_address.json');
prompt.run()
.then(password => {
var account = accounts.decrypt(keyStore, password);
var keyStore_to = require('./to_address.json');
sendEth(web3, account, '0x25f7c8e6624edb5e288a6ba893cd18e1bc98bc7c', '0.008')
.then((r)=>{
console.log(r);
process.exit(0);
})
.catch (e=>{
console.error(e);
process.exit(1);
})
})
.catch((x) => {
console.log(x)
process.exit(1)
})
// console.log(account3);
function sendEth(web3, fromAccount, toAddress, eth_amount) {
var rawTx = {
to: toAddress,
from: fromAccount.address,
nonce: '0x00',
value: web3.utils.toWei(eth_amount),
gasPrice: '0x09184e72a000',
gasLimit: '42000', //
}
return Promise.all([
web3.eth.getGasPrice(),
web3.eth.getTransactionCount(rawTx.from)
]).then((results) => {
var price = results[0];
var count = results[1];
// var tx = new Tx(rawTx);
rawTx.gasPrice = price;
rawTx.nonce = count;
console.log('gasPrice', price);
return fromAccount.signTransaction(rawTx)
}).then((tx) => {
return web3.eth.sendSignedTransaction(tx.rawTransaction)
})
}