20,000 USDC
View results
Submission Details
Severity: gas
Valid

Don't declare the variable inside the loops

#[G-01]: Don't declare the variable inside the loops
In every iterations the new variables instance created this will consumes more gas . So just declare variables outside the loop and only use inside to save gas.

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L233
for (uint256 i = 0; i < borrows.length; i++) {
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L293
for (uint256 i = 0; i < loanIds.length; i++) {
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L359
for (uint256 i = 0; i < loanIds.length; i++) {
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L438
for (uint256 i = 0; i < loanIds.length; i++) {
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L549
for (uint256 i = 0; i < loanIds.length; i++) {
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L592
for (uint256 i = 0; i < refinances.length; i++) {

#[G-02]: Use assembly to check for address(0)
Saves 6 gas per instance

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L167
if (pools[poolId].lender == address(0)) {
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L240
if (pool.lender == address(0)) revert PoolConfig();

#[G-03] : State variables should be cached in stack variables rather than re-reading them from storage..
Approximate gas saved: 500 gas

Caching will replace each Gwarmaccess (100 gas) with a much cheaper stack read. Less obvious fixes/optimizations include having local storage variables of mappings within state variable mappings or mappings within state variable structs, having local storage variables of structs within mappings, having local memory caches of state variable structs, or having local caches of state variable contracts/addresses.

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L725
fees = (lenderFee * interest) / 10000; //@audit lenderFee cached
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L561
uint256 govFee = (borrowerFee * loan.collateral) / 10000; //@audit borrowerFee cached
https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol#L650
uint256 fee = (borrowerFee * (debt - debtToPay)) / 10000; //@audit borrowerFee cached

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!