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 memory pool = pools[poolId];
...... more code
_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 memory loan = loans[loanId];
....... more code
emit Borrowed(
loan.borrower,
pool.lender,
loanId,
loans[loanId].debt,
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 memory loan = loans[loanId];
..... more code
emit Borrowed(
loan.borrower,
msg.sender,
loanId,
loans[loanId].debt,
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