美文网首页
使用web3进行以太坊转账测试

使用web3进行以太坊转账测试

作者: 张亚伦 | 来源:发表于2022-02-18 11:24 被阅读0次

    目标:使用web3以及ethereumjs-tx实现转账功能。(本文仅作为给同事的一个demo)
    章节流程:1. 实验操作。2. 结论汇总。
    引用资料:https://web3js.readthedocs.io/en/v1.3.4/web3-eth.html#sendsignedtransaction

    实验操作

    我们在测试网ropsten进行实验,准备两个账户(作为目标账户的余额为0)。

    1. 执行转账交易

    执行转账(10000000)命令后,输出结果如下:

    D:\exercise\nodestudy\html>node ropsten_test.js
    number: 1
    signedTransactionData0xf8660184646534a68252089401a54572f83124bf78ba630faef643e6812662f8839896808029a08db048a4771a817644539f78a0c94ddb1c8bbbc8fecd2b10b7b3d3d35e54dffba07528a1d2eec21ee68f7532096aaf794d02dabb97a9f9233dfd26b5d7bbcfe869
    0xf8af0a766d7ec19e581551bef49d28682ff37a6821f3e50bb4c2f3ebc434bdfb
    {
      blockHash: '0xf570951524f989f5aa4efd3db7df6a468419271054f3d0320584348181ecfabd',
      blockNumber: 11982020,
      contractAddress: null,
      cumulativeGasUsed: 6761800,
      effectiveGasPrice: 1684354214,
      from: '0xaa1a88aa89f50ee9b7e3f6124f18a31d5e6db1f9',
      gasUsed: 21000,
      logs: [],
      logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
      status: true,
      to: '0x01a54572f83124bf78ba630faef643e6812662f8',
      transactionHash: '0xf8af0a766d7ec19e581551bef49d28682ff37a6821f3e50bb4c2f3ebc434bdfb',
      transactionIndex: 6,
      type: '0x0'
    }
    
    

    2. 查询余额

    查询目标账户的余额,结果输出如下:

    D:\exercise\nodestudy\html>node ropsten_test.js
    _to balance:
    10000000
    

    结论汇总

    首先,转账测试成功了,整个类库的使用还是很方便的。
    其次,有几个点注意下:
    * 1)使用ethereumjs-tx时 'chain'默认为'mainnet'
    * 2)gasPrice的值,可以去参考https://ropsten.etherscan.io/最近的交易


    完整代码如下:

    const Web3 = require('web3');
    var _from = '0xAa1a88aa89F50ee9B7e3F6124f18a31d5E6dB1F9';
    var _from_pk = '';
    var _to = '0x01a54572F83124bF78BA630FAEF643e6812662f8';
    
    const web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'))
    async function transfer() {
        web3.eth.getTransactionCount(_from).then(function (number) {
            console.log(`number: ${number}`);
            var Tx = require('ethereumjs-tx').Transaction;
            var privateKey = Buffer.from(_from_pk, 'hex');
            var rawTx = {
                nonce: '0x' + number,
                gasPrice: '0x646534a6',//"1684354214",
                gasLimit: '0x5208',//"21000",
                to: _to,
                value: '0x989680',//"10000000",//'0x00',
                // data: ''
            }
    
            var tx = new Tx(rawTx, { 'chain': 'ropsten' });
            // var tx = new Tx(rawTx);
            tx.sign(privateKey);
            var serializedTx = tx.serialize();
            var signedTransactionData = '0x' + serializedTx.toString('hex');
            console.log("signedTransactionData" + signedTransactionData);
    
            web3.eth.sendSignedTransaction(signedTransactionData, function (err, hash) {
                if (!err) {
                    console.log(hash);
                } else {
                    console.log(err);
                }
            }).on('receipt', console.log);
        });
    }
    
    async function balance() {
        // console.log("_from balance:");
        // web3.eth.getBalance(_from).then(console.log);
    
        console.log("_to balance:");
        web3.eth.getBalance(_to).then(console.log);
    
    }
    async function main() {
        // transfer();
        balance();
    }
    if (require.main === module) {
        main();
    }
    

    相关文章

      网友评论

          本文标题:使用web3进行以太坊转账测试

          本文链接:https://www.haomeiwen.com/subject/gnaelrtx.html