[Ethereum] Why is an ‘UNPREDICTABLE_GAS_LIMIT’ error thrown when calling a view function created from a public variable

ethers.jsgas-limithardhatsoliditystate-variable

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.

Best Answer

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.

Related Topic