[Ethereum] getting error nonce too low , but nonce is proper stuck since one day

dappsethereumjsproxy-contractsweb3js

I am working with web3.js since sometime now , but now i am stuck with nonce too low error , the nonce is proper also i have checked private key and other addresses , they also seem to proper , however when i call addNum function is gives nonce too low error

var Web3 = require('web3');
let tx = require('ethereumjs-tx');

var Web3 =  new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/kG4Z6EC5EsknbOqki4mz")); 

let abi = [
    {
        "constant": false,
        "inputs": [
            {
                "name": "x",
                "type": "uint256"
            }
        ],
        "name": "addNum",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "getSum",
        "outputs": [
            {
                "name": "sum",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
];


let contract = Web3.eth.contract(abi);
let contractAddress = "0xc8fe6f104a5076a8d3b1284cd1a48095d6f5b5cd";
let contractInstance = contract.at(contractAddress);

let defaultAccount = '0x262b76df1dcb6542aa4a19296a824f0fdd1b4b04'; 
let privatekey = 'ba456ad306a69c3830eed33f690e8f5b93e36cea3cd37ed9604f71b95a5ae1e3';



    var num = 2;

    var data = contractInstance.addNum.getData(num);
            sendRaw(data, function(err, hash){
                if(err)
                {
                   console.log("err "+err);
                }
                else
                {
                    console.log("hash "+hash);
                }
            });



/**
 * Function to signed and call function in solidity  
 * param data
 * return (err, transactionhash)
 */
var count = Web3.eth.getTransactionCount(defaultAccount);

function sendRaw(data, callback) 
{
    var rawTx  = {
        nonce: Web3.toHex(++count),
        from: Web3.toHex(defaultAccount),
        gasLimit: Web3.toHex(300000),
        gasPrice: Web3.toHex(Web3.toWei('20', 'gwei')),
        to: Web3.toHex(contractAddress),
        value: 0,
        data: data
    };
    var privateKey = new Buffer(privatekey, 'hex');
    var transaction = new tx(rawTx);
    transaction.sign(privateKey);
    var serializedTx = transaction.serialize().toString('hex');
    Web3.eth.sendRawTransaction(
    '0x' + serializedTx, function(err, result) {
        if(err) 
        {
            callback(err, '');
        } 
        else 
        {
            callback('', result);
        }
    });
}

Also i am testing upgradable smart contract so the address is of proxy contract and abi is of logic contract

Best Answer

This line:

var count = Web3.eth.getTransactionCount(defaultAccount,'pending');

You're starting your count at the number of outstanding/pending transactions the node is aware of. That's the wrong queue.

You could start here:

var count = Web3.eth.getTransactionCount(defaultAccount);

This will drop the next transaction on the nonce that follows the last known confirmed transaction.

It's not a good pattern, generally, to rely on the node's transaction count to set the nonce. Ideally, the software client will be aware of every transaction sent from the account and will know the correct number without asking.

Have a look over here for more info: Why does sendSignedTransaction return a tx hash BUT does not post to the Rinkeby network (React, Metamask, web3, infura)

And here: Concurrency patterns for account nonce

Hope it helps.

Related Topic