0

Here is the error message :

Deploying, please wait...
BaseContract {
  target: '0x80f4D1df2Da11Ca9437A01519d3972d07E54A43E',
  interface: Interface {
    fragments: [
      [ConstructorFragment],
      [FunctionFragment],
      [FunctionFragment],
      [FunctionFragment]
    ],
    deploy: ConstructorFragment {
      type: 'constructor',
      inputs: [],
      payable: false,
      gas: null
    },
    fallback: null,
    receive: false
  },
  runner: Wallet {
    provider: JsonRpcProvider {},
    address: '0x54749f5B877355917E1B862c49F3E9Ddb1cBD161'
  },
  filters: {},
  fallback: null,
  [Symbol(_ethersInternal_contract)]: {}
}
Current money in my wallet: 0
Adding money in my wallet ...
Current money in my wallet: 10
Taking money in my wallet ...
/Users/axelm/Documents/wallet/node_modules/ethers/lib.commonjs/utils/errors.js:129
            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: 4 tx has nonce of: 3", "stack": "Error: the tx doesn't have the correct nonce. account has nonce of: 4 tx has nonce of: 3\n    at TransactionPool.prepareTransaction (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:122021)" }, payload={ "id": 30, "jsonrpc": "2.0", "method": "eth_sendRawTransaction", "params": [ "0x02f89082053903843b9aca008483b5cc008271b49480f4d1df2da11ca9437a01519d3972d07e54a43e80a44fd9efc40000000000000000000000000000000000000000000000000000000000000008c080a039fc3526c5dcda687379bc899275ebeca441585c265176647b8d543fa26ddbf0a06acfca03fe54ad066d724514d5604ccd79245db4a6f932493c6b1c60f623610c" ] }, code=UNKNOWN_ERROR, version=6.8.0)
    at makeError (/Users/axelm/Documents/wallet/node_modules/ethers/lib.commonjs/utils/errors.js:129:21)
    at JsonRpcProvider.getRpcError (/Users/axelm/Documents/wallet/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:653:41)
    at /Users/axelm/Documents/wallet/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: 4 tx has nonce of: 3",
    stack: "Error: the tx doesn't have the correct nonce. account has nonce of: 4 tx has nonce of: 3\n" +
      '    at TransactionPool.prepareTransaction (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:122021)',
    code: -32000
  },
  payload: {
    method: 'eth_sendRawTransaction',
    params: [
      '0x02f89082053903843b9aca008483b5cc008271b49480f4d1df2da11ca9437a01519d3972d07e54a43e80a44fd9efc40000000000000000000000000000000000000000000000000000000000000008c080a039fc3526c5dcda687379bc899275ebeca441585c265176647b8d543fa26ddbf0a06acfca03fe54ad066d724514d5604ccd79245db4a6f932493c6b1c60f623610c'
    ],
    id: 30,
    jsonrpc: '2.0'
  },
  shortMessage: 'could not coalesce error'
}

Node.js v18.18.1


Here is my JS file :

const ethers = require("ethers");
const fs = require("fs-extra");

async function main() {
    const provider =  new ethers.JsonRpcProvider("HTTP://127.0.0.1:7545"); //connect to the blockchain
    const wallet = new ethers.Wallet("0x74ceb5628edb7b7041079783416727af07236f2708bbdf08bb56fd9991775914", provider);

    //get the abi and the binary
    const abi = fs.readFileSync("./wallet_sol_wallet.abi", "utf8");
    const binary = fs.readFileSync("./wallet_sol_wallet.bin", "utf8");
    
    //object use to deploy contract
    const contractFactory = new ethers.ContractFactory(abi,binary,wallet);
    console.log("Deploying, please wait...");
    //deploy the contract
    const contract = await contractFactory.deploy(); // await for wait until the contract is totally deploy
    console.log(contract);

    let currentWallet = await contract.watch();
    console.log(`Current money in my wallet: ${currentWallet}`);
    console.log("Adding money in my wallet ...");
    let transactionResponse = await contract.store(10);
    currentWallet = await contract.watch();
    console.log(`Current money in my wallet: ${currentWallet}`);
    console.log("Taking money in my wallet ...");
    await contract.take(8);
    console.log(`Current money in my wallet: ${currentWallet}`);
   
    

}
main()

Here is my smart contract :

// SPDX-License-Identifier: MIT
pragma solidity "0.8.7";

contract wallet {
    uint256 private money;
    address private owner;

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Error it is not your wallet");
        _;
    }

    function store(uint256 _money) public onlyOwner {
        money = _money;
    }

    function take(uint256 takenMoney) public onlyOwner {
        require(
            takenMoney <= money,
            "Error you don't have enough money in your wallet"
        );
        money = money - takenMoney;
    }

    function watch() public view onlyOwner returns (uint256 _money) {
        _money = money;
    }
}

1 Answer 1

0

Maybe you need to add a small time delay for the nonce transaction counter to update. Or you can use the next script

const ethers = require("ethers");
const fs = require("fs-extra");

async function main() {
    const provider =  new ethers.JsonRpcProvider("HTTP://127.0.0.1:7545"); //connect to the blockchain
    const wallet = new ethers.Wallet("0x74ceb5628edb7b7041079783416727af07236f2708bbdf08bb56fd9991775914", provider);

    //get the abi and the binary
    const abi = fs.readFileSync("./wallet_sol_wallet.abi", "utf8");
    const binary = fs.readFileSync("./wallet_sol_wallet.bin", "utf8");
    
    //object use to deploy contract
    const contractFactory = new ethers.ContractFactory(abi,binary,wallet);
    console.log("Deploying, please wait...");
    //deploy the contract
    const contract = await contractFactory.deploy(); // await for wait until the contract is totally deploy
    console.log(contract);

    let pending_nonce = await wallet.getNonce(); // for ethers version 6.2.+
    // let pending_nonce = await provider.getTransactionCount(wallet, 'pending'); //or this for ethers version 5.7
    let currentWallet = await contract.watch({nonce: pending_nonce});
    console.log(`Current money in my wallet: ${currentWallet}`);
    console.log("Adding money in my wallet ...");
    pending_nonce = await wallet.getNonce();
    let transactionResponse = await contract.store(10, {nonce: pending_nonce});
    pending_nonce = await wallet.getNonce();
    currentWallet = await contract.watch({nonce: pending_nonce});
    console.log(`Current money in my wallet: ${currentWallet}`);
    console.log("Taking money in my wallet ...");
    pending_nonce = await wallet.getNonce();
    await contract.take(8, {nonce: pending_nonce});
    console.log(`Current money in my wallet: ${currentWallet}`);
   
    

}
main()
1
  • Thank you its works now :))
    – Marsattak
    Oct 17 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.