15,000 USDC
View results
Submission Details
Severity: gas

Add a check to see if the user has any tokens before calculating the value of it

Summary

You can first check if the user has deposited the token and then do the calculations.

Details

In the DSCEngine contract in the getAccountCollateralValue function you should check if the amount of tokens that the user
deposited is greater than 0 and then proceed with the calculations if the user has deposited any tokens. This way we can
save gas and not call the getUsdValue function every time because that is gas expensive.

Recommendations

You can do something like this:

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++) {
      uint256 amount = s_collateralDeposited[user][token];

      if(amount > 0) {
          address token = s_collateralTokens[i];
          totalCollateralValueInUsd += getUsdValue(token, amount);
      }
    }
    return totalCollateralValueInUsd;
}

Support

FAQs

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