trufflesuite / ganache

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.
https://consensys.io/blog/consensys-announces-the-sunset-of-truffle-and-ganache-and-new-hardhat?utm_source=github&utm_medium=referral&utm_campaign=2023_Sep_truffle-sunset-2023_announcement_
MIT License
2.62k stars 683 forks source link

Truffle v5.11.4 (core: 5.11.4) + Ganache v7.9.1: the tx doesn't have the correct nonce #4530

Open zhangxf55 opened 1 year ago

zhangxf55 commented 1 year ago
  1. Environment:
    [root@zhangxf55 unirouter1]# truffle --version
    Truffle v5.11.4 (core: 5.11.4)
    Ganache v7.9.1
    Solidity - 0.8.21 (solc-js)
    Node v18.17.1
    Web3.js v1.10.0
  2. Fork the mainnet with command:
    ganache -h 127.0.0.1 -p 9000 --fork https://eth-mainnet.g.alchemy.com/v2/KW8************************Zttj0 -D
  3. The contract I deployed:
    
    // SPDX-License-Identifier: MIT
    pragma solidity >=0.4.22 <0.9.0;

contract Authorizor {

address internal admin;
mapping(address => bool) internal users;

constructor() {
    admin = msg.sender;
}

modifier onlyadmin() {
    require(msg.sender == admin, "UniverseRouter02: ONLY_ADMIN");
    _;
}

modifier onlyuser() {
    require(users[msg.sender], "ONLY_USER");
    _;
}

function getAdmin() external view returns (address result) {
    result = admin;
}

function setAdmin(address _admin) onlyadmin external {
    require(_admin != admin, "ADMIN_NOT_CHANGE");
    admin = _admin;
}

function addUser(address _user) onlyadmin external {
    require(!users[_user], "USER_EXIST");
    users[_user] = true;
}

function deleteUser(address _user) onlyadmin external {
    require(users[_user], "USER_NOT_EXIST");
    users[_user] = false;
}

function checkUser(address _user) external view returns (bool result) {
    result = users[_user];
}

}

4. Use ethers.js(6.7.1) to call addUser and deleteUser in RPC (Not truffle test mode):
```javascript
const { ethers } = require('ethers');

const private = require('../privatekey.json');

const routerabi = require('../build/contracts/UniverseRouter02.json').abi;
const erc20abi = require('../build/contracts/IERC20.json').abi;

const routerAddress = '0x548De337842AF790d92dDf93F52B3E9A87E1Da0B';
const distributorAddress = '0xC9412b0791F10320e9Deb867F2a046D84E5Ae8C5';

const usdtAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';
const wethAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
const pepeAddress = '0x6982508145454ce325ddbe47a25d4ec3d2311933';

const v3pool_wethusdt = '0x4e68ccd3e89f51c3074ca5072bbac773960dfa36';
const v3pool_pepeweth = '0x11950d141ecb863f01007add7d1a342041227b58';

const v2pool_wethusdt = '0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852';
const v2pool_pepeweth = '0xa43fe16908251ee70ef74718545e4fe6c5ccec9f';

const provider = new ethers.JsonRpcProvider("http://127.0.0.1:9000/");
const wallet = new ethers.Wallet(private.zhangxf55, provider);

const contract = new ethers.Contract(routerAddress, routerabi, wallet);

async function testUser() {
    const tx = await contract.addUser(wallet.address);
    let result = await contract.checkUser(wallet.address);
    console.log(result);
    await contract.deleteUser(wallet.address);
    result = await contract.checkUser(wallet.address);
    console.log(result);
}

testUser();
  1. The result:
    
    [root@zhangxf55 unirouter1]# node local/testweb3.js 
    true
    /data/unirouter1/node_modules/ethers/lib.commonjs/utils/errors.js:125
            error = new Error(message);
                    ^

Error: could not coalesce error (error={ "code": -32000, "message": "the tx doesn't have the correct nonce. account has nonce of: 44 tx has nonce of: 43", "stack": "Error: the tx doesn't have the correct nonce. account has nonce of: 44 tx has nonce of: 43\n at TransactionPool.prepareTransaction (/root/.nvm/versions/node/v18.17.1/lib/node_modules/ganache/dist/node/1.js:2:188489)" }, payload={ "id": 18, "jsonrpc": "2.0", "method": "eth_sendRawTransaction", "params": [ "0x02f8908205392b843b9aca0084f8cde7b282730094548de337842af790d92ddf93f52b3e9a87e1da0b80a45c60f226000000000000000000000000d3dfa878b32204cb7de5297a3d785b4bd38f995ec080a0b567e71468c74b71693239a722d3c5215eac1d19643a80e8982fe6b71c654fcda072d6169dc2146fbafc961d9f1756078df9766434a1b456cf857034c6c43f11b0" ] }, code=UNKNOWN_ERROR, version=6.7.1) at makeError (/data/unirouter1/node_modules/ethers/lib.commonjs/utils/errors.js:125:21) at JsonRpcProvider.getRpcError (/data/unirouter1/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:653:41) at /data/unirouter1/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:268:45 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'UNKNOWN_ERROR', error: { message: "the tx doesn't have the correct nonce. account has nonce of: 44 tx has nonce of: 43", stack: "Error: the tx doesn't have the correct nonce. account has nonce of: 44 tx has nonce of: 43\n" + ' at TransactionPool.prepareTransaction (/root/.nvm/versions/node/v18.17.1/lib/node_modules/ganache/dist/node/1.js:2:188489)', code: -32000 }, payload: { method: 'eth_sendRawTransaction', params: [ '0x02f8908205392b843b9aca0084f8cde7b282730094548de337842af790d92ddf93f52b3e9a87e1da0b80a45c60f226000000000000000000000000d3dfa878b32204cb7de5297a3d785b4bd38f995ec080a0b567e71468c74b71693239a722d3c5215eac1d19643a80e8982fe6b71c654fcda072d6169dc2146fbafc961d9f1756078df9766434a1b456cf857034c6c43f11b0' ], id: 18, jsonrpc: '2.0' } }

Node.js v18.17.1

davidmurdoch commented 1 year ago

Can you share the ganache logs (you'll need to omit the -D flag)?

zhangxf55 commented 1 year ago

ganache logs below:

eth_chainId
eth_getTransactionCount
eth_chainId
eth_estimateGas
eth_chainId
eth_chainId
eth_getBlockByNumber
eth_gasPrice
eth_blockNumber
eth_sendRawTransaction
eth_chainId

  Transaction: 0x0b0f788ac61ddb6fec97079bf5967ed5577856d4ba513260effb31a217e26cdf
  Gas usage: 46602
  Block number: 138252503
  Block time: Sat Oct 07 2023 09:10:06 GMT+0800 (China Standard Time)

eth_call
eth_chainId
eth_chainId
eth_estimateGas
eth_chainId
eth_chainId
eth_sendRawTransaction
eth_chainId
zhangxf55 commented 1 year ago

Exception:

[root@zhangxf55 unirouter1]# node local/testrpc.js 
true
/data/unirouter1/node_modules/ethers/lib.commonjs/utils/errors.js:125
            error = new Error(message);
                    ^

Error: could not coalesce error (error={ "code": -32000, "message": "the tx doesn't have the correct nonce. account has nonce of: 24 tx has nonce of: 23", "stack": "Error: the tx doesn't have the correct nonce. account has nonce of: 24 tx has nonce of: 23\n    at TransactionPool.prepareTransaction (/root/.nvm/versions/node/v18.17.1/lib/node_modules/ganache/dist/node/1.js:2:188489)" }, payload={ "id": 18, "jsonrpc": "2.0", "method": "eth_sendRawTransaction", "params": [ "0x02f89082053917843b9aca0084413182b482730e949935113fdbce1650efe9879266fac13556df1de080a45c60f226000000000000000000000000d3dfa878b32204cb7de5297a3d785b4bd38f995ec080a09d5f5d2f738c62a502bede8c810d5327ad80e4604418e7ad7eaa50789be52eb4a06f3482c709411fa9e011fb21043452261f4196afcc7aec98fb27e4268cff0997" ] }, code=UNKNOWN_ERROR, version=6.7.1)
    at makeError (/data/unirouter1/node_modules/ethers/lib.commonjs/utils/errors.js:125:21)
    at JsonRpcProvider.getRpcError (/data/unirouter1/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:653:41)
    at /data/unirouter1/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:268:45
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'UNKNOWN_ERROR',
  error: {
    message: "the tx doesn't have the correct nonce. account has nonce of: 24 tx has nonce of: 23",
    stack: "Error: the tx doesn't have the correct nonce. account has nonce of: 24 tx has nonce of: 23\n" +
      '    at TransactionPool.prepareTransaction (/root/.nvm/versions/node/v18.17.1/lib/node_modules/ganache/dist/node/1.js:2:188489)',
    code: -32000
  },
  payload: {
    method: 'eth_sendRawTransaction',
    params: [
      '0x02f89082053917843b9aca0084413182b482730e949935113fdbce1650efe9879266fac13556df1de080a45c60f226000000000000000000000000d3dfa878b32204cb7de5297a3d785b4bd38f995ec080a09d5f5d2f738c62a502bede8c810d5327ad80e4604418e7ad7eaa50789be52eb4a06f3482c709411fa9e011fb21043452261f4196afcc7aec98fb27e4268cff0997'
    ],
    id: 18,
    jsonrpc: '2.0'
  }
}

Node.js v18.17.1