web3 eth sendSignedTransaction always return Returned error nonce too low

0 votes

I used web3@1.00 in node.js to interact with private blockchain. And I write the code according to the official documents of web3@1.00.

    var Web3 = require('web3');
    var Tx = require('ethereumjs-tx');
    var web3 = new Web3('http://localhost:8101');

    //get the privatekey
    var decryptPK = web3.eth.accounts.decrypt({"address":"68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10",
    "crypto":{"cipher":"aes-128-ctr",
    "ciphertext":"96b6a86bd5ff16a5669975974eabba844bc414bc52d9cc36843b4f41e89d46b9",
    "cipherparams":{"iv":"6858473ac21c43e94add7e0b70306da5"},
    "kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,
    "salt":"4dee1ee7cf929cd38938aaf0a6c83ceeb743348980052b6fb94ab139ae7e09db"}
    ,"mac":"f7d9bbd81f9c91ffa21e564e613bae57bd77c1914a6690f6018623ff2ce80845"},
    "id":"0eb12580-d11a-49f5-91bb-3592a530e4d8","version":3}
    , '123');
    var privateKey = new Buffer(decryptPK.privateKey.substring(2),'hex');


  //check the nonce
web3.eth.getTransactionCount('0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10').then(console.log);

    var rawTx = web3.eth.accounts.signTransaction({
        from: '0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10',
        to: '0x7fdec66a5c1b69824dfe3bc29138fac9ddf13ed4',
        value: '1000000000',
        gas: 2000000
    }, decryptPK.privateKey)
    .then(console.log).rawTransaction;


    var tx = new Tx(rawTx);
    tx.sign(privateKey);

    var serializedTx = tx.serialize();

    web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));

But, I am not able to send the transaction. The web3.eth.sendSignedTransaction() gives me the following error:

    (node:78916) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Returned error: nonce too low
(node:78916) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Please help me know how to fix this problem.

Oct 1, 2018 in Blockchain by digger
• 26,740 points
6,026 views

1 answer to this question.

0 votes

This bit of code looks wrong:

var rawTx = web3.eth.accounts.signTransaction({
    from: '0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10',
    to: '0x7fdec66a5c1b69824dfe3bc29138fac9ddf13ed4',
    value: '1000000000',
    gas: 2000000
}, decryptPK.privateKey)
.then(console.log).rawTransaction;

I assume rawTx is undefined after this? You're trying to access a field called "rawTransaction" on a Promise object.

The code should probably look something like this (completely untested):

web3.eth.accounts.signTransaction({
    nonce: 1,  // Use the right nonce here, just hardcoding at 1 for the example.
    from: '0x68c5cb5aa9f568ae2a6ec530e982f4f1144f2d10',
    to: '0x7fdec66a5c1b69824dfe3bc29138fac9ddf13ed4',
    value: '1000000000',
    gas: 2000000
}, decryptPK.privateKey)
.then(tx => {
    var rawTx = tx.rawTransaction;
    web3.eth.sendSignedTransaction(rawTx).on('receipt', console.log);
});
answered Oct 1, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

0 votes
1 answer

Web3 1.0: What does `web3.eth.call(tx)` return for a contract creation?

The returned bytestring is the deployedBytecode of the contract, ...READ MORE

answered Sep 17, 2018 in Blockchain by digger
• 26,740 points
1,220 views
0 votes
1 answer

Ethereum truffle MetaCoin : 'intrinsic gas too low' error

Change the genesis block gasLimit to "gasLimit": "0x2FEFD800", config ...READ MORE

answered Oct 22, 2018 in Blockchain by Omkar
• 69,220 points
1,314 views
0 votes
1 answer

Çannot implement web3.eth.subscribe on Web3 version 1.0.0-beta.27

As the error suggests, pub/sub is not ...READ MORE

answered Jun 19, 2018 in Blockchain by Perry
• 17,100 points

edited Jun 19, 2018 by Perry 2,024 views
0 votes
1 answer

eth.getBalance() always returns 0 in testnet

You are not connecting to the right ...READ MORE

answered Jul 24, 2018 in Blockchain by Christine
• 15,790 points
2,608 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,427 views
0 votes
1 answer

"set RNG(RandomUtils.Random) before generating random numbers" error

Use the following code, you just have ...READ MORE

answered Jul 5, 2018 in Blockchain by slayer
• 29,370 points
769 views
0 votes
1 answer

Error connecting due to rpc error

In the previous version, this problem was ...READ MORE

answered Jul 6, 2018 in Blockchain by slayer
• 29,370 points
804 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP