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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs