14

I am using the Hardhat console to test my smart contract, which worked flawlessly until I removed the getter functions I wrote for my public state variables in favor of the automatically generated view functions. For example, I had a function like this:

function getName() public view returns(string memory) {
        return name;
}

Running Contract.getName() returned my name variable as expected. However, after I removed this function and tried using Contract.name(), I get an error saying cannot estimate gas; transaction may fail or may require manual gas limit. What is this inconsistency?

By the way, I'm using ethers.js and the Hardhat console, and my contract is deployed to the Ropsten testnet.

2
  • Were you able to fix this issue? I am facing the same as well and couldn't fix it yet.
    – rihekopo
    Commented Nov 18, 2021 at 21:10
  • 1
    Can you post the whole contract including the definition of the state variable?
    – Richard
    Commented Feb 23, 2022 at 10:23

5 Answers 5

5

In my case, I got that exact error message because I instantiated a contract using an incorrect address. Fixing the address made the message go away.

1
  • This is most likely the cause for most of us that are getting this error. I had the contract setup to use an address on Mainnet and was setting something in Contructor with it, but was deploying on testnet. Had to use an address on testnet for it to work.
    – Steven K
    Commented May 3, 2022 at 13:22
4

I got this error because a require statement got triggered. If you read the error carefully you may see a message which indicates what you have to change in your Solidity code.

1

The point is this message is ethers.js related and this Github comment helped me to understand the situation. In a nutshell, this error message is not because you have insufficient funds, it can be a sign of a different issue. In my case it was a sign of mistake I made in my solidity implementation. (I used a main net router address instead of test net address). I know you run a different task instead of contract.myTransaction(), but this information regarding the rpcalls of ether.js can be useful:

But as you say you get cannot estimate gas error instead of insufficient funds error. I think I have an explanation for this. When you run contract.myTransaction(), ethers js does 2 rpcalls to node internally:

  • Makes an estimate gas rpcall to the node. If call reverts you will get error cannot estimate gas that you are getting. (I don't think a gas estimate requires to have enough eth at an address)
  • Since estimation was successful, it signs tx and sends to the node. If funds are not present, the node returns an error result, which ethers js throws as insufficient funds

source: Github comment

0

I had the same issue, I solved it with this, in your hardhat.config.ts, replace the solidity: "0.8.x" with:

solidity: {
    version: "0.8.8",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
      },
    },
  },

https://github.com/PatrickAlphaC/dao-template/issues/13#issuecomment-1175613443

I hope this helps.

0

In my case, I got that exact error message if the fund of connected wallet has is zero, The capped gas limit is zero because there is NO associated fund.

Your Answer

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

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