Summary
Unwanted gas is spent on recomputing a value that has already been computed.
Vulnerability Details
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
uint256 totalDebt = loan.debt + lenderInterest + protocolInterest;
...... more code
emit Repaid(
loan.borrower,
loan.lender,
loanId,
loan.debt + lenderInterest + protocolInterest,
loan.collateral,
loan.interestRate,
loan.startTimestamp
);
..... more code
}
https://github.com/Cyfrin/2023-07-beedle/blob/658e046bda8b010a5b82d2d85e824f3823602d27/src/Lender.sol#L465-L515
function buyLoan(uint256 loanId, bytes32 poolId) public {
..... more code
uint256 totalDebt = loan.debt + lenderInterest + protocolInterest;
...... more code
emit Repaid(
loan.borrower,
loan.lender,
loanId,
loan.debt + lenderInterest + protocolInterest,
loan.collateral,
loan.interestRate,
loan.startTimestamp
);
..... more code
}
Impact
Unwanted gas expenditure
Tools Used
Forge gas reporter
Recommendations
Avoid recomputation and use the already computed value