0

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
        cryptoDevs.withdraw();
    }

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?

Yves Boutellier
  • 402
  • 4
  • 12
  • 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. – Paul Razvan Berg Sep 29 '22 at 07: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 – Yves Boutellier Sep 29 '22 at 13:10
  • but for the testing contract it used only 13k – Yves Boutellier Sep 29 '22 at 13:10

0 Answers0