parsecer
parsecer

Reputation: 5136

Calling a method in a smart contract: nonce has already been used, Nonce too low

I have a contract that has a simple method:

function getOwner() external returns (address) {
  return owner;
}

I'm trying to call it in NodeJs using ethers library:

// create the provider and the signer
const provider = new ethers.getDefaultProvider("http://127.0.0.1:8545/");
let signer =  new ethers.Wallet('[privatekey]',
    provider );
let manager = new NonceManager(signer);

// create contract
let contractAddress = "[contractaddress]";
let abiString = fs.readFileSync(contractAbiPath).toString();
let abi = JSON.parse(abiString);
let contract = new ethers.Contract(contractAddress, abi, signer);
await contract.waitForDeployment();

// get the current nonce
let walletNonce = await signer.getNonce(); 

// send the transaction

// error1: if I pass walletNonce + 1
let transaction = await contract.getOwner({
    nonce: walletNonce
});
// error2 : if in the line above I passed walletNonce
let response = await manager.sendTransaction(transaction);
await response.wait(1);

If walletNonce is 6, and I pass it like this: nonce: walletNonce, I get the error at line let response = await manager.sendTransaction(transaction);:

Nonce too low. Expected nonce to be 7 but got 6.'

However if I pass nonce: walletNonce + 1, I get the error at line

let transaction = await contract.getOwnerAddress({
    nonce: walletNonce
});
Nonce too high. Expected nonce to be 6 but got 7. Note that transactions can't be queued when automining.

could not coalesce error

I'm lost as to what to do.

Upvotes: 0

Views: 546

Answers (1)

Yilmaz
Yilmaz

Reputation: 49661

either re migrate the project or from nonce-too-high-error-with-metamask-and-hardhat

Open up your MetaMask window and click on the icon in the top right to display accounts. Go to Settings, then Advanced and hit Reset Account.

Now the error will not appear when you make transactions.

Upvotes: 0

Related Questions