I work with Foundry (Forge, Anvil, Cast).

According to their documentation one can run a gas estimation for the tests one has written.

I have a very simple test for which I wanted to have a gas estimate, because I could then infer how much gas this function call (I'm talking about (withdraw)) approx. uses.

function testWithdrawIsolated() public {
        vm.startPrank(whitelistedAddresses[0]); // <- withdraw can only called by owner, this is the owner

It reported CryptoDevsTest:testWithdrawIsolated() (gas: 13113)

So I assumed withdraw uses approx. less than 13k gas.

However, when I ran the withdraw function in remix with my browser wallet, 5 times out of 5 it ran with 30621 Gas.

My Question

Why is the gas amount so different?

  • Remix and Forge are two rather different development environments, so it is expected for there to be a few slight differences, but in your case the most likely explanation is that you haven't enabled the optimizer in Remix, whereas in Foundry you did. Sep 29, 2022 at 7:52
  • Does this answer your question? How does the solidity optimizer work? Sep 29, 2022 at 7:52
  • okay I haven't thought about optimisation, fair point. But I deployed the contract with Foundry to Goerli and to Localhost, and in when I was working with Remix I just interacted with the contract deployed to Goerli (referenced the contract via the deployed address) and in both cases (localhost with anvil and goerli via remix) it used 30621. @PaulRazvanBerg Sep 29, 2022 at 13:10
  • but for the testing contract it used only 13k Sep 29, 2022 at 13:10


Your Answer

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

Browse other questions tagged or ask your own question.