DEV Community

foxgem
foxgem

Posted on • Edited on

5

Troubleshooting: exceeds block gas limit

Symptom

Calling a contract method but getting the following error:

MetaMask - RPC Error: [ethjs-query] while formatting outputs from RPC '{"value":{"code":-32000, "message": "exceeds block gas limit"}}'
Enter fullscreen mode Exit fullscreen mode

The weirdest thing about this error is that the same calling succeeded before with the same contract, the same method, the same parameters, even the same wallet. But now, it fails.

Cause

Unlike other contracts calling failures, when you get this error, the tx is not actually sent on chain, the calling is rejected at the MetaMask level. So you don't see any information about tx or contract in the error message at all, only MetaMask.

The reason is because:

  • In Ethereumn, the block gas limit is different for each block. The value of the new block is determined by the algorithm and the miner vote. You can easily get this value with ethers.js: provider.getBlock('latest')).gasLimit.
  • If the gas limit of the sent tx is bigger than it, you get this error.

If the gas limit in your method is calculated on the fly, you may get this error.

Solution

Now that the cause has been found, the solution is simple: check the gas limit calculation and try to make it as accurate as possible, making sure it not exceeds the block gas limit.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay