const addressFrom = '0x2B547F3098408F0632a4063EB1C86595efBAF470'
const privKey = '512F597B114326678A6E3E85D8297728245F641965488EAA9D7400EB7C4C3ECA'
const addressTo = '0x23915B0E11EE8DEa952bc88457eFEbf5e6561B5d'
function sendSigned(txData, cb) {
const privateKey = new Buffer(privKey, 'hex')
const transaction = new Tx(txData)
transaction.sign(privateKey)
const serializedTx = transaction.serialize().toString('hex')
web3.eth.sendSignedTransaction('0x' + serializedTx, cb)
}
web3.eth.getTransactionCount(addressFrom).then(txCount => {
const txData = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(25000),
gasPrice: web3.utils.toHex(10e9), // 10 Gwei
to: addressTo,
from: addressFrom,
value: web3.utils.toHex(web3.utils.toWei("0.1", "ether"))
}
sendSigned(txData, function(err, result) {
if (err) return console.log('error', err)
console.log('sent', result)
})
})
网友评论