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.

josephawallace
  • 141
  • 1
  • 1
  • 4

5 Answers5

4

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.

James Moore
  • 258
  • 2
  • 9
  • 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 May 03 '22 at 13:22
3

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.

Richard
  • 4,934
  • 2
  • 7
  • 25
Calypso
  • 167
  • 5
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

rihekopo
  • 316
  • 1
  • 12
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.

persec10000
  • 131
  • 3