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

Gas optimization possible in the DSCEngine.getAccountCollateralValue

Summary

The method getAccountCollateralValue in the DSCEngine contract contains the following loop:

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

As the upper bound of the iterator is the length of an array, it will not overflow, thus the incrementaion can be moved to unchecked block for gas savings.

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

Vulnerability Details

n/a

Impact

Unnecessary gas consumption

Tools Used

Manual review

Recommendations

Consider changing the loop code block as suggested in summary.

Support

FAQs

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