Incorrect nonce using JsonRpcProvider with ethers.js

67 views Asked by At

I am trying to send parallel transactions to my own private EVM network using ethers.js v6 with a very simple and standard code below.

Since I am managing the nonce myself, and sometimes I call this code many times before the block is mined, I would like to be able to also send transactions with a higher nonce (below I try with +1). But I always get an error. Any ideas how to do this without getting an error? I use to be able to do this on ethers 5 but I am not able to revert to those packages.

const hre = require("hardhat");
const { JsonRpcProvider } = require('ethers');

const ContractJson = require("../artifacts/contracts/ERC20.sol/ERC20.sol");

const abi = ContractJson.abi;

async function main() {
    const jsonRpcProvider = new JsonRpcProvider(process.env.API_URL);

    const nonce =  await jsonRpcProvider.getTransactionCount(process.env.MY_ADDRESS)

    const userWallet = new hre.ethers.Wallet(process.env.PRIVATE_KEY, jsonRpcProvider);

    const token = new hre.ethers.Contract(
        process.env.CONTRACT_ADDRESS,
        abi,
        userWallet
    )


    const minting = await token.mint(process.env.MY_ADDRESS, BigInt( 1 * 10 ** 18), {nonce: nonce +1 });
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
});

Error:

  shortMessage: 'missing revert data',
  info: {
    error: { code: -32600, message: 'incorrect nonce' },
    payload: {
      method: 'eth_estimateGas',
      params: [Array],
      id: 7,
      jsonrpc: '2.0'


I am at a loss...I haven't been able to find anything regarding higher nonces. Usually people have problems with trying with too low nonces.

0

There are 0 answers