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

Perform a division before a multiply

Summary

DSCEngine._calculateHealthFactor(uint256,uint256) (src/DSCEngine.sol#324-334) performs a multiplication on the result of a division:

  • collateralAdjustedForThreshold = (collateralValueInUsd * LIQUIDATION_THRESHOLD) / LIQUIDATION_PRECISION (src/DSCEngine.sol#330)

  • (collateralAdjustedForThreshold * 1e18) / totalDscMinted (src/DSCEngine.sol#331)

Vulnerability Details

Dividing before multiplying can lead to potential loss of precision and performing potential rounding errors.

Impact

The potential impact of this bug includes:

  • Loss of Precision

  • Incorrect Health Factor

  • Vulnerability to Exploitation

  • Inconsistent Behavior

Tools Used

Slither - static analysis framework.

Recommendations

Rearrange the order of operations to perform the multiplication before the division.

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

Support

FAQs

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