Number | Issue | Instances |
---|---|---|
[G-01] | Using == for uints to check for 0 value saves gas |
2 |
[G-02] | Use assembly to check for address(0) |
2 |
[G-03] | Cache array length outside of for loop |
2 |
[G-04] | Reduce gas usage by moving to Solidity 0.8.19 or later | 3 |
[G-05] | Use unchecked{} for increments/decrements that cannot overflow |
2 |
[G-06] | ++i costs less gas than i++ , especially when used in for -loops |
2 |
[G-07] | <x> += <y> costs more gas than <x> = <x> + <y> for state variables |
2 |
[G-08] | Re-initialising variables to their default value wastes gas | 2 |
[G-09] | Use constants instead of type(uintx).max |
1 |
[G-10] | Use hardcode address instead of address(this) |
2 |
==
for uints
to check for 0
value saves gasUsing == 0
when comparing uints
instead of, for example, <= 0
is more gas efficient.
There are 2 instances of this issue:
address(0)
Assembly can be used to more efficiently check for a zero address.
References:
There are 2 instances of this issue:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L103
for
loopStoring the array length before looping is more efficient as it prevents its length from being checked with each iteration.
There are 2 instances of this issue:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L118
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L353
See "References" below.
References
There are 3 instances of this issue:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L24
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/libraries\OracleLib.sol#L3
unchecked{}
for increments/decrements that cannot overflowUse unchecked{i++}
to avoid checking for underflows/overflows when appropriate, such as in a for
loop, to save gas.
There are 2 instances of this issue:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L118
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L353
++i
costs less gas than i++
, especially when it's used in for
-loops (--i
/i--
too)This saves 5 gas per loop.
There are 2 instances of this issue:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L118
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L353
<x> += <y>
costs more gas than <x> = <x> + <y>
for state variablesUsing the addition operator instead of plus-equals saves 113 gas per instance.
There are 2 instances of this issue:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L155
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L198
Setting for example a uint
to 0
incurs overhead as 0
is already the default value when the variable is defined - there is no need to reset it to 0
again.
There are 2 instances of this issue:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L118
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L353
type(uintx).max
Using type(uintx).max
such as type(uint128).max
incurs more gas in distribution and for each transaction. Constants (such as 340282366920938463463374607431768211455
for type(uint128).max
) should be used instead.
There is 1 instance of this issue:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L329
address(this)
Instead of address(this), it is more gas-efficient to pre-calculate and use the address with a hardcode. Foundry's script.sol and solmate LibRlp.sol
contracts can do this.
References:
https://book.getfoundry.sh/reference/forge-std/compute-create-address
https://twitter.com/transmissions11/status/1518507047943245824
There are 2 instances of this issue:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L157
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L274
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.