15,000 USDC
View results
Submission Details
Severity: gas

Storage Variable as Loop Length

Summary

Avoid using storage variables to set the length of a for loop.

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++) { //@audit creo que existen mejores formas para optimizar este loop
address token = s_collateralTokens[i];
uint256 amount = s_collateralDeposited[user][token];
totalCollateralValueInUsd += getUsdValue(token, amount);
}
return totalCollateralValueInUsd;
}

Instead assign it to a variable in memory to avoid unnecessary SLOADs.

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
uint poriro = s_collateralTokens.length;
for (uint256 i = 0; i < poriro; i++) {
address token = s_collateralTokens[i];
uint256 amount = s_collateralDeposited[user][token];
totalCollateralValueInUsd += getUsdValue(token, amount);
}
return totalCollateralValueInUsd;
}

Recommendation

assign it to a variable in memory to avoid unnecessary SLOADs

Support

FAQs

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