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

Unwanted gas expenditure by calling storage variable

Summary

Storage variable is used instead of using the cached variable in memory.

Vulnerability Details

https://github.com/Cyfrin/2023-07-beedle/blob/658e046bda8b010a5b82d2d85e824f3823602d27/src/Lender.sol#L232C1-L287C6

function borrow(Borrow[] calldata borrows) public {
for (uint256 i = 0; i < borrows.length; i++) {
..... more code
// pool is cached to memory here
Pool memory pool = pools[poolId];
...... more code
// storage is used instead of the cached variable
_updatePoolBalance(poolId, pools[poolId].poolBalance - debt);
......more code
}

https://github.com/Cyfrin/2023-07-beedle/blob/658e046bda8b010a5b82d2d85e824f3823602d27/src/Lender.sol#L355-L432

function giveLoan(
uint256[] calldata loanIds,
bytes32[] calldata poolIds
) external {
for (uint256 i = 0; i < loanIds.length; i++) {
...... more code
// loan cached to memory here
Loan memory loan = loans[loanId];
....... more code
emit Borrowed(
loan.borrower,
pool.lender,
loanId,
loans[loanId].debt,
// storage is used instead of the cached variable
loans[loanId].collateral,
pool.interestRate,
block.timestamp
);
}
}

https://github.com/Cyfrin/2023-07-beedle/blob/658e046bda8b010a5b82d2d85e824f3823602d27/src/Lender.sol#L465-L534

function buyLoan(uint256 loanId, bytes32 poolId) public {
// loan cached to memory here
Loan memory loan = loans[loanId];
..... more code
emit Borrowed(
loan.borrower,
msg.sender,
loanId,
loans[loanId].debt,
// storage is used instead of the cached variable
loans[loanId].collateral,
pools[poolId].interestRate,
block.timestamp
);
emit LoanBought(loanId);
}

Impact

Unwanted expenditure of gas

Tools Used

Forge gas reporter

Recommendations

Use the cached variable instead of storage variable

Support

FAQs

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