-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsendEth-stages.js
More file actions
84 lines (72 loc) · 2.49 KB
/
sendEth-stages.js
File metadata and controls
84 lines (72 loc) · 2.49 KB
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
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;
const ethlib = require('eth-lib');
keyStore = require('./from_address.json');
prompt.run()
.then(password => {
var account = accounts.decrypt(keyStore, password);
var keyStore_to = require('./to_address.json');
var tryIndex = 0;
function _internalSend(){
generateSignedTransaction(web3, account, keyStore_to.address, '0.005')
.then(signedTx =>{
console.log('tx', signedTx, ethlib.Hash.keccak256s(signedTx));
web3.eth.sendSignedTransaction(signedTx)
.on('transactionHash', (hash)=>{
console.log('hash', hash);
})
.on('receipt', receipt=>{
console.log('receipt', JSON.stringify(receipt, null ,2))
})
.on('confirmation', (n, receipt) =>{
console.log('confirmation', n, receipt);
})
.on('error', (err)=>{
_internalSend();
})
})
.catch( e=>{
console.log(tryIndex++, e);
setTimeout(_internalSend, 3000);
})
}
_internalSend();
})
.catch((x) => {
console.log(x)
process.exit(1)
})
// console.log(account3);
function generateSignedTransaction(web3, fromAccount, toAddress, eth_amount) {
var rawTx = {
to: toAddress,
from: fromAccount.address,
nonce: '0x00',
value: web3.utils.toWei(eth_amount),
gasPrice: '0x09184e72a000',
gasLimit: '21000', //
}
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 / 20;
rawTx.nonce = count;
console.log('gasPrice', price);
return fromAccount.signTransaction(rawTx)
}).then(tx => {
return tx.rawTransaction
});
}