Web3js
由於無效的 nonce 值,ERC20 交易失敗
我正在嘗試發送一個 erc20 交易,但我不斷收到錯誤消息,告訴我我從中發送交易的帳戶的 nonce 值為 1,而 tx 的 nonce 值為 31613。我正在執行 ganache-cli終端並通過連接到我的本地主機上的 web3 提供程序,使用 remix IDE 編譯 + 部署了我的智能合約。賬戶 1 和賬戶 2 是 ganache 生成的 10 中的賬戶。我確保將發送地址設為 10 中的第一個帳戶,因為它是包含初始代幣供應的那個。我不知道 nonce 值的問題出在哪裡,有人能指出我正確的方向嗎?
這是發送交易的程式碼:
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) } });
這是錯誤日誌:
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
函式
web3.eth.getTransactionCount
返回一個你需要解決的承諾。因此,您在這裡錯誤地使用了此功能:
"nonce": web3.utils.toHex(web3.eth.getTransactionCount(account1))