DEV Community

foxgem
foxgem

Posted on • Edited on

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.

Top comments (0)