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
count++
to++count
. Otherwise, you'll just keep trying it with the value returned byWeb3.eth.getTransactionCount
, which is the current nonce. – goodvibration Sep 15 '18 at 18:33Web3.toHex
in many place? I think it's not necessary! – Ha ĐANG Sep 15 '18 at 18:35getTransactionCount()
. The blockchain will increment the transaction count for you, when your transaction is mined. Unless you are sending multiple transactions, you don't need to increment anything. – Nulik Sep 15 '18 at 19:09