Gas estimate in etherjs is incorrect by 2 orders of magnitude


  1. Write any contract function, let’s say it costs 100k gas.
  2. Wrap it in OpenGSN RelayProvider and back in etherjs.
  3. Run a gasEstimate on it, it will come about to about 200k (with opengsn overhead), which is correct.
  4. If you inspect the “gas” value passed to relayClient, it will be significantly higher (40x more).

Previously, this wasn’t a problem. Even though the estimate was very wrong, the transaction would still settle because it consumed a reasonable amount of gas. But in OpenGSN 2.2.1, we add 10% extra to this inflated gas value and also check that it’s under the block.gasLimit in the contract.

Unfortunately, this broke many of my transactions, which were around 300k gas, but ended up being estimated at 12.8M gas in the contract check, well over the block.gasLimit.


The workaround is to manually pass in a gasLimit which overrides the ‘gas’ value passed to the relayClient.

      await contract.connect(signer).call function(param1, { gasLimit: GAS_LIMIT });

I’m not sure where the root cause lies, since these mis-estimates have been reported in ethersjs before. However, it would very helpful to document the breaking changes (10% increased gas, check for gaslimit) in the release notes, and perhaps add a note about working around this somewhere in the guide, it may save someone else hours of debugging.