Categories
Cryptocurrency Development Ethereum

accceptOwnership Tx “Returned error: nonce too low”, though getTransactionCount(account) gives a nonce that looks ok

Could anyone let me know what is wrong with the code below?

Before executing the below code, transferOwnership(newOwner) went through successfully. And then, I execute the below code, “Error: Returned error: nonce too low. getTransactionCount(newOwner) returns ‘0x7’. Metamask account has been also altered to the new owner.

myContract.methods.newOwner().call()
    .then((newOwner)=>{
        var txAcceptOwnership=myContract.methods.acceptOwnership();
        var encodedTx = txAcceptOwnership.encodeABI();
        console.log('new owner:'+newOwner);

        txAcceptOwnership
        .estimateGas({to:contractAddr,data:encodedTx,from:newOwner})
        .then((gasAmount) => {
            console.log('app.js: estimated gas: ' + gasAmount.toString(10));

            web3.eth.getTransactionCount(newOwner).then(_nonce => {
                nonce = web3.utils.toHex(_nonce);
                console.log('app.js: tx nonce: ' + nonce);

                var tx = new ethTx({
                    nonce: nonce,
                    gasPrice: gasPrice,
                    gasLimit: gasLimit,
                    from: newOwner,
                    to: contractAddr,
                    data: encodedTx
                },{chain: 'rinkeby' , hardfork: 'petersburg'});

                tx.sign(privKey);
                var signedRawTx = '0x' + tx.serialize().toString('hex');

                web3.eth.sendSignedTransaction(signedRawTx)
                .on('receipt', receipt => {
                    console.log('signed tx receipt:\n' + JSON.stringify(receipt));
                }).catch((err) => {
                    console.log('ERROR: signed tx failed.');
                    console.log(err);
                });
            });
        }).then((result) => {
            console.log('result:'+result);
            }).catch((err) => {
                console.log('ERROR: Gas Estimate failed');
                console.log(err);
            });
        });
});

Leave a Reply

Your email address will not be published. Required fields are marked *