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

Increments can be unchecked in for-loops

Summary

In Solidity 0.8+, there’s a default overflow check on unsigned integers, which we can skip when iterating over loops and therefore save gas. The risk of overflow is non-existent for uint256 here.
Here we loop through all the tokens, which are valid for collateral.

Vulnerability Details

As the project is a template for a stable coin, which other developers could use as a ready solution, we are not sure about the size of the collateral array, so the optimization could save a good amount of gas.

Impact

Gas usage

Tools Used

Manual Review

Recommendations

Consider wrapping with an unchecked block (around 25 gas saved per instance).

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
uint256 allTokens = s_collateralTokens.length;
for (uint256 i = 0; i < allTokens;) {
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.