如何通过私钥从新创建的帐户发送eth?

问题描述 投票:0回答:1

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

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

现在我想从该帐户发送eth(我向该帐户发送了一些eth)我有几个弱点:

  • 不太清楚,我尝试设置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)在IncomingMessage。 (\ 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_visible.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);
                  }
            });

[尝试创建一个新帐户,并通过web3库将eth发送到另一个帐户。 const account = web3.eth.accounts.create()现在,我想从该帐户发送eth(我向该帐户发送了一些eth)...

javascript blockchain ethereum web3 cryptocurrency
1个回答
0
投票

您必须将getTrnsactionCount用于nonce

© www.soinside.com 2019 - 2024. All rights reserved.