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

Change for loop to save gas

Solidity for loop could be optimized by using unchecked to save gas.

code:

function getAccountCollateralValue(address user) public view returns (uint256 totalCollateralValueInUsd) {
// loop through each collateral token, get the amount they have deposited, and map it to
// the price, to get the USD value
for (uint256 i = 0; i < s_collateralTokens.length; i++) {
address token = s_collateralTokens[i];
uint256 amount = s_collateralDeposited[user][token];
totalCollateralValueInUsd += getUsdValue(token, amount);
}
return totalCollateralValueInUsd;
}

can change to:

for (uint256 i; i < s_collateralTokens.length;) {
address token = s_collateralTokens[i];
uint256 amount = s_collateralDeposited[user][token];
totalCollateralValueInUsd += getUsdValue(token, amount);
unchecked {
++i;
}
}
return totalCollateralValueInUsd;

Support

FAQs

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