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

Fixing Precision Loss in `_calculateHealthFactor()` Function

Summary

The _calculateHealthFactor() function in DSCEngine.sol suffers from precision loss, leading to user funds getting stuck when collateralValueInUsd is equal to 1. Although the amount getting stuck is minimal, users will still be unable to withdraw their 1 dollar.

Vulnerability Details

The issue can be observed on line 334 of DSCEngine.sol. When collateralValueInUsd is set to 1, it results in precision loss during the calculations:

function _calculateHealthFactor(uint256 totalDscMinted, uint256 collateralValueInUsd)
internal
pure
returns (uint256)
{
if (totalDscMinted == 0) return type(uint256).max;
// 100 * 1e18 / 200 = 500e16 1 * 50 = 50 / 100 = 0
uint256 collateralAdjustedForThreshold = (collateralValueInUsd * LIQUIDATION_THRESHOLD) / LIQUIDATION_PRECISION;
return (collateralAdjustedForThreshold * 1e18) / totalDscMinted;
}

##Impact
A user with 1 dollar of collateralValueInUsd would not be able to withdraw their collateral unless they deposited more and then attempted to withdraw.

Tools Used
Manual review.

Recommendations
Consider checking if collateralValueInUsd = 1 and if it does, then return 1 instead.

Support

FAQs

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