Instead of housing the logic in a modifier, moving it into an internal function, and making a call to that function inside the modifier, can save a lot of gas.
The modifier inState
is used thrice. As such, its code is copied over to each modified function and is duplicated three times. If, instead, we create an internal view function with the state check, and invoke that function inside the modifier, we can save a lot of gas. What this does is that instead of the actual check being copied over in each modified function, just an invocation to the internal function is made.
Gas
Forge, Foundry Toolkit (gas report, gas snapshots)
Use the inState
modifier in conjunction with an internal _inState
function to reduce contract size and save on gas. Only the following change needs to be made:
With this change in place, a gas saving of ~13000
gas was seen on each test through the gas snapshot. A total gas reduction of 303596
was seen across all tests. In addition, the deployment size for Escrow.sol reduced by 5 bytes and deployment cost reduced by 13012
gas. Saving ~13K gas on each deployment adds up to significant savings in the long-term.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.