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

Increase loop inside a unchecked block

Summary

Loop in Lender::startAuction can be increased inside a unchecked block because is unrealistic a possible overflow. The following setup can save up to 71 units of gas in execution cost per loop run.

function startAuction(uint256[] calldata loanIds) public {
for (uint256 i = 0; i < loanIds.length;) {
uint256 loanId = loanIds[i];
// get the loan info
Loan memory loan = loans[loanId];
// validate the loan
if (msg.sender != loan.lender) revert Unauthorized();
if (loan.auctionStartTimestamp != type(uint256).max)
revert AuctionStarted();
// set the auction start timestamp
loans[loanId].auctionStartTimestamp = block.timestamp;
emit AuctionStart(
loan.borrower,
loan.lender,
loanId,
loan.debt,
loan.collateral,
block.timestamp,
loan.auctionLength
);
unchecked {
i++;
}
}
}

Support

FAQs

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