I am trying to send an erc20 transaction but I keep getting an error telling me that the account I'm sending the transaction from has a nonce value of 1 while the tx has a nonce value of 31613. I am running ganache-cli on the terminal and compiled + deployed my smart contract using the remix IDE by connecting to a web3 provider on my localhost. Account 1 and Account 2 are accounts from the 10 generated by ganache. I made sure to make the sending address the first account from the 10 because it is the one that contains the initial supply of tokens. I do not know where the problem with the nonce value occurs can someone point me to the right direction?
This is the code that sends the transaction:
var Web3 = require('web3')
var fs = require('fs')
const Tx = require('ethereumjs-tx').Transaction
var web3 = web3 ? new Web3(web3.currentProvider) : new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var txHash
var senderAddress
var receiverAddress
var contractAddress = "0x687c065c7a2b5711b9a651c13d9b51f5db71451d" //Ethereum contract address
var abiArray = JSON.parse(fs.readFileSync('tixtokenabi.json', 'utf-8')) //abi of contract
var contract = new web3.eth.Contract(abiArray, contractAddress) //Smart Contract instance
var rawTransaction = {
"from": account1,
"nonce": web3.utils.toHex(web3.eth.getTransactionCount(account1)),
"gasPrice": web3.utils.toHex(web3.eth.gasPrice),
"gasLimit": web3.utils.toHex(90000),
"to": contractAddress,
"value": "0x0",
"data": contract.methods.transfer(account2, amount).encodeABI(),
"chainId": "0x01"
};
var privKey = new Buffer(private_key, 'hex')
var tx = new Tx(rawTransaction)
tx.sign(privKey)
var serializedTx = tx.serialize()
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
//if transaction goes through
if (!err) {
console.log("Transaction successful! redirecting...")
//Get transaction hash
console.log(hash)
setTxHash(hash)
//Get balances of two accounts
var balance1 = contract.methods.balanceOf(account1).send()
var balance2 = contract.methods.balanceOf(account2).send()
return {
balance1:balance1,
balance2:balance2
}
//if transaction fails
} else {
console.log("Error! Transaction failed..")
console.log(err)
}
});
And here is the error log:
Error! Transaction failed..
Error: Returned error: the tx doesn't have the correct nonce. account has nonce of: 1 tx has nonce of: 31613
at Object.ErrorResponse (/home/zein/node_modules/web3-core-helpers/src/errors.js:29:16)
at /home/zein/node_modules/web3-core-requestmanager/src/index.js:140:36
at XMLHttpRequest.request.onreadystatechange (/home/zein/node_modules/web3-providers-http/src/index.js:110:13)
at XMLHttpRequestEventTarget.dispatchEvent (/home/zein/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
at XMLHttpRequest._setReadyState (/home/zein/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
at XMLHttpRequest._onHttpResponseEnd (/home/zein/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
at IncomingMessage.<anonymous> (/home/zein/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
at emitNone (events.js:111:20)
at XMLHttpRequest.request.onreadystatechange (/home/zein/node_modules/web3-providers-http/src/index.js:110:13)
at XMLHttpRequestEventTarget.dispatchEvent (/home/zein/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
at XMLHttpRequest._setReadyState (/home/zein/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
at XMLHttpRequest._onHttpResponseEnd (/home/zein/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
at IncomingMessage.<anonymous> (/home/zein/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9