web3 1.0 转账示例代码

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)
    })

})
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容