I need to create a smart contract for which it would be hard to estimate gas usage.

Specifically, if I estimate execution gas usage at 90 000 gas, I want to consume e.g. 110 000 gas, so that the transaction will fail if I execute it with a 100 000 gas limit.

Any ideas how I could proceed?

EDIT: The solution should expose only receive and fallback external functions. Such a smart contract is useful for testing execution paths of critical systems.

1 Answer 1


You could do something like have a for loop that does an arbitrary number of iterations based on something like a chainlink vrf (random number from oracle) https://docs.chain.link/vrf/v2/introduction/. Bonus points if you modify or create storage on every iteration of the loop, so it consumes a tooooon of gas.

If you want it to specifically always consume strictly MORE gas than it estimates, you could do something that depends on block number maybe? So that the gas at prediction time is on a lower block, and then your contract decides to do more opertations when it actually executes on a later block.

  • Thanks for the suggestions. For the smart contract usage to be practical, I would prefer a solution which would defeat the gas usage estimator based on code in the contract alone. If I add randomness or depend on some external variable like time, then the contract becomes a bit impractical to use. Apr 5, 2023 at 6:43
  • 1
    Well if all your contract code is deterministic, the gas estimator will just run the function to determine how much gas is used. I think you will have a hard time finding something to defeat the gas estimator, or rather gas estimators in general using completely deterministic logic, I think a solution is going to be pretty hard to find. The estimators can always just run the code to determine how much gas it uses, and I believe that's what most of them do, since on chain computations use trivial computing power when run off chain.
    – Bruce
    Apr 6, 2023 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.