I have a couple of different smart contracts (Beacon Proxy etc) that can receive ERC20 tokens and performs various calls (for implementation and admin) and then moves all the ERC20 balance to a provided destination.
I've been trying to find a way to save on the gas required to perform the functions (and struggling) but one thing I've noticed is that the same function call is cheaper after it's been called.
I've been trying to investigate (and google search) for answers but I'm unable to piece it together.
Just wondering if someone would be able provide some reasons as to why the gas cost is reduced?
Is it to do with sload
?
Example of the gas cost on a Local Hardhat forked from Mainnet using USDC
.
First call
┌──────────┬─────────┐
│ (index) │ Values │
├──────────┼─────────┤
│ moveGas1 │ '83979' │
└──────────┴─────────┘
Second call
┌──────────┬─────────┐
│ (index) │ Values │
├──────────┼─────────┤
│ moveGas2 │ '66879' │
└──────────┴─────────┘