I tested the gas consumption of a contract in the jsVM, which showed consistent outputs. I then deployed it to mainnet, and the function calls consume much different amounts of gas. Is the jsVM running an old version of Ethereum? What is the best way to test gas consumption of a contract that reflects mainnet?


The gas estimation of a function depends on any state variable that the function (or any other function that it calls) depends on.

So even checking the gas estimation of the exact same function several times may yield a different result each time.

You may have received consistent results because no related state variable has changed when you checked the gas estimation.

On networks with many participants (typically public networks), this is mostly not the case.

In addition to all of that:

  • Different EVM versions may yield different gas costs for the same bytecode executed on the same state
  • Different compiler versions may yield different bytecode for the same source code, which in turn, may yield different gas costs
  • My function for testing only operates on calldata so I don't think people chaing state variables should affect my test. I would assume that Remix's jsVM is made to emulate mainnet though? It seems strange to have a significant difference when emulation is the goal of the jsVM. – the_cranky_panda Jun 30 '20 at 18:15
  • @the_cranky_panda: Your function is pure or view? – goodvibration Jun 30 '20 at 18:24
  • neither, external I'm just testing how best to optimize gas cost with input data, but the testing function doesn't actually do anything other than accept inputs – the_cranky_panda Jun 30 '20 at 18:27
  • @the_cranky_panda: Then I guess the reason for the difference is either you're using different compiler versions, or simply an older version of EVM when you test with jsVM (I'm not even sure what that is, but a while back, mainnet has changed the cost of every read operation from 200 gas to 800 gas). – goodvibration Jun 30 '20 at 18:37
  • jsVM is the local EVM in javascript that you can test with in Remix. Yes I think it must just be a different version to mainnet. I suppose I'll just test on a testnet instead – the_cranky_panda Jun 30 '20 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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