DEV Community

foxgem
foxgem

Posted on • Edited on

Troubleshooting: "UNPREDICTABLE_GAS_LIMIT" thrown when calling a readonly contract method.

Symptom

When a calling to a smart contract readonly method happened, a "UNPREDICTABLE_GAS_LIMIT" exception was thrown:

ERROR Error: Uncaught (in promise): Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"code":-32000,"message":"execution reverted"}, method="call", transaction={"to":"0xE6183d3094b9r360B121Ec1330afAE76A74d1cbF","data":"0xdb699765000000000000000000000000b2b7bedd7d7fc19804c7dd4a4e8174c4c73c220d0000000000000000000000000000000000000000000000000de0b6b3b7640000000000000000000000000000000000000000000000000000000000006c915aef"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.0.24)
    at d.makeError (main.a577643548eb15e6832e.js:1)
    at d.throwError (main.a577643548eb15e6832e.js:1)
    at Z (main.a577643548eb15e6832e.js:1)
    at $e.<anonymous> (main.a577643548eb15e6832e.js:1)
    at Generator.throw (<anonymous>)
    at a (main.a577643548eb15e6832e.js:1)
    at l.invoke (polyfills.797d13e303959ed8dd77.js:1)
    at Object.onInvoke (main.a577643548eb15e6832e.js:1)
    at l.invoke (polyfills.797d13e303959ed8dd77.js:1)
    at a.run (polyfills.797d13e303959ed8dd77.js:1)
    at Z (polyfills.797d13e303959ed8dd77.js:1)
    at polyfills.797d13e303959ed8dd77.js:1
    at a (main.a577643548eb15e6832e.js:1)
    at l.invoke (polyfills.797d13e303959ed8dd77.js:1)
    at Object.onInvoke (main.a577643548eb15e6832e.js:1)
    at l.invoke (polyfills.797d13e303959ed8dd77.js:1)
    at a.run (polyfills.797d13e303959ed8dd77.js:1)
    at polyfills.797d13e303959ed8dd77.js:1
    at l.invokeTask (polyfills.797d13e303959ed8dd77.js:1)
    at Object.onInvokeTask (main.a577643548eb15e6832e.js:1)
Enter fullscreen mode Exit fullscreen mode

NOTE

It is READONLY here.

Cause

If we are calling a write method and get this error, that's understandable. However, for a readonly method invocation, no transaction will be sent, so no gas limit estimation should happen at all!

In this case, it is highly probable that you are calling a non existent method in the contract. Especially, it is not uncommon when you are using "human readable abi" with ethers.js, such as a typo.

Solution

So, to solve this, you can check if the target contract includes the method you are calling:

  • wrong method names?
  • wrong method arguments?
  • wrong abis? Note, when you are using "human readable abi", please make sure the punctuation is not included. Fox example:
    • √: function testMethod(uint256) public view returns (uint256)
    • X: function testMethod(uint256) public view returns (uint256);

The only difference between these two abis is the ";" at the end.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay