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

For loop gas opt

Summary

Unchecked i variable inside for block to save gas

Vulnerability Details

for loop ---> set i variable as unchecked{++i;}
to save gas...
functions can be implementable on
Lender.sol
function refinance(Refinance[] calldata refinances) public
function seizeLoan(uint256[] calldata loanIds) public
function startAuction(uint256[] calldata loanIds) public
function repay(uint256[] calldata loanIds) public
function borrow(Borrow[] calldata borrows) public

Example Implemantation:

function seizeLoan(uint256[] calldata loanIds) public {
for (uint256 i; i < loanIds.length;) {
uint256 loanId = loanIds[i];
// get the loan info
Loan memory loan = loans[loanId];
// validate the loan
if (loan.auctionStartTimestamp == type(uint256).max)
revert AuctionNotStarted();
if (
block.timestamp <
loan.auctionStartTimestamp + loan.auctionLength
) revert AuctionNotEnded();
// calculate the fee
uint256 govFee = (borrowerFee * loan.collateral) / 10000;
// transfer the protocol fee to governance
IERC20(loan.collateralToken).transfer(feeReceiver, govFee);
// transfer the collateral tokens from the contract to the lender
IERC20(loan.collateralToken).transfer(
loan.lender,
loan.collateral - govFee
);
bytes32 poolId = keccak256(
abi.encode(loan.lender, loan.loanToken, loan.collateralToken)
);
// update the pool outstanding loans
pools[poolId].outstandingLoans -= loan.debt;
emit LoanSiezed(
loan.borrower,
loan.lender,
loanId,
loan.collateral
);
// delete the loan
delete loans[loanId];
unchecked{
++i;
}
}

Support

FAQs

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