以太币转账 #web3 #区块链
npm install web3 -S
npm install ethereumjs-tx -S
const Web3 = require('web3')
const Tx = require('ethereumjs-tx')
const currentAccount = (loc) => {
return loc;
}
export const EthTransfer = () => {
web3.eth.getTransactionCount(currentAccount("xxxxxxxxxxx"), web3.eth.defaultBlock.pending).then(function(nonce){
const txData = {
nonce: web3.utils.toHex(nonce++),
gasLimit: web3.utils.toHex(99000),
gasPrice: web3.utils.toHex(10e9),
to: 'xxxxxxxxxxx',
from: currentAccount("xxxxxxxxxx"),
value: web3.utils.toHex(web3.utils.toWei('0.001', 'ether')),
data: ''
}
const tx = new Tx(txData)
const privateKey = new Buffer('xxxxxxx', 'hex')
tx.sign(privateKey)
const serializedTx = tx.serialize().toString('hex')
web3.eth.sendSignedTransaction(serializedTx.toString('hex'), function(err, hash) {
if (!err) {
alert(hash)
} else {
alert(err)
}
})
})
}