Mediator
Mediator

Reputation: 15378

How send eth from new created account by private key?

Try to create a new account and send some eth to another account by web3 library.

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

Now I want to send eth from this account (I sent some eth to this account) I have a few weak places:

error:

Error: Returned error: the tx doesn't have the correct nonce. account has nonce of: 1 tx has nonce of: 0 at Object.ErrorResponse (\node_modules\web3-core-helpers\src\errors.js:29:16) at \node_modules\web3-core-requestmanager\src\index.js:140:36 at XMLHttpRequest.request.onreadystatechange (\node_modules\web3-providers-http\src\index.js:110:13) at XMLHttpRequestEventTarget.dispatchEvent (\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22) at XMLHttpRequest._setReadyState (\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14) at XMLHttpRequest._onHttpResponseEnd (\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14) at IncomingMessage. (\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61) at IncomingMessage.emit (events.js:215:7) at IncomingMessage.EventEmitter.emit (domain.js:498:23) at endReadableNT (_stream_readable.js:1184:12) at processTicksAndRejections (internal/process/task_queues.js:80:21)

code:

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

Upvotes: 0

Views: 1108

Answers (1)

Monarth Sarvaiya
Monarth Sarvaiya

Reputation: 1126

You have to use getTrnsactionCount for nonce:

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

And also in rawTransaction object convert nonce variable to hex:

nonce: web3.utils.toHex(nonce);

You can also use pending to include pending transaction count:

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

Upvotes: 1

Related Questions