【问题标题】:How send eth from new created account by private key?如何通过私钥从新创建的帐户发送 eth?
【发布时间】:2020-06-26 19:08:09
【问题描述】:

尝试创建一个新帐户并通过 web3 库将一些 eth 发送到另一个帐户。

const account = web3.eth.accounts.create()

现在我想从这个账户发送 eth(我发送了一些 eth 到这个账户) 我有几个弱点:

  • 不太明白“nonce”是什么我尝试了设置 0 或 1。
  • chainId 字段。它是什么?仅取自一些示例。

错误:

错误:返回错误:tx 没有正确的随机数。帐户的随机数为:1 tx 的随机数为:0 在 Object.ErrorResponse (\node_modules\web3-core-helpers\src\errors.js:29:16) 在 \node_modules\web3-core-requestmanager\src\index.js:140:36 在 XMLHttpRequest.request.onreadystatechange (\node_modules\web3-providers-http\src\index.js:110:13) 在 XMLHttpRequestEventTarget.dispatchEvent (\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22) 在 XMLHttpRequest._setReadyState (\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14) 在 XMLHttpRequest._onHttpResponseEnd (\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14) 在传入消息。 (\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61) 在 IncomingMessage.emit (events.js:215:7) 在 IncomingMessage.EventEmitter.emit (domain.js:498:23) 在 endReadableNT (_stream_readable.js:1184:12) 在 processTicksAndRejections (internal/process/task_queues.js:80:21)

代码:

const Web3 = require('web3')
        const Tx = require('ethereumjs-tx').Transaction
        const rpcURL = 'HTTP://127.0.0.1:7545' // Your RCkP URL goes here
        const web3 = new Web3(rpcURL)
        var privateKey = "aa424f25bcb01b0dd9622cac2b6f1a51432a7b1c45a4a5b74040999f903d7b8e"//get from created account by web3.eth.accounts.create()
        var addr = "0x9658BdD2a5CE0fFd202eF473E033DEE49e28d282"

        const toAddress = '0xc74fB4Ac0011D4a78F35b7AE88407c83d95372E0'
        const amountToSend = 0.5
        var gasPrice = 2;//or get with web3.eth.gasPrice
        var gasLimit = 3000000;

        var rawTransaction = {
          "from": addr,
          nonce: '0x00',//maybe this is problem
          gasPrice: '0x09184e72a000',
          gasLimit: '0x2710',
          "to": toAddress,
          "value":  web3.utils.toWei(amountToSend, "ether") ,
          "chainId": 'mainnet'//4 //not really understand chain
        };

            var privKey = new Buffer(privateKey, 'hex');
            var tx = new Tx(rawTransaction);

            tx.sign(privKey);
            var serializedTx = tx.serialize();

            web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
              if (!err)
                  {
                    console.log('Txn Sent and hash is '+hash);
                  }
              else
                  {
                    console.error(err);
                  }
            });

【问题讨论】:

    标签: javascript blockchain ethereum web3 cryptocurrency


    【解决方案1】:

    nonce 必须使用 getTrnsactionCount

    nonce = await web3.eth.getTransactionCount(addr);
    

    并且还在rawTransaction 对象中将 nonce 变量转换为十六进制:

    nonce: web3.utils.toHex(nonce);
    

    您也可以使用pending 来包含待处理的事务计数:

    nonce = await web3.eth.getTransactionCount(addr, "pending");
    

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多