You can first check if the user has deposited the token and then do the calculations.
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.
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;
}
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.