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

Precision loss

Summary

Precision loss

Vulnerability Details

To avoid loss in precision, perform multiplication before division.

Impact

https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/d1c5501aa79320ca0aeaa73f47f0dbc88c7b77e2/src/DSCEngine.sol#L330-L331

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

Division before multiplication can lead to truncation and give an incorrect output.

Tools Used

Manual review

Recommendations

function _calculateHealthFactor(uint256 totalDscMinted, uint256 collateralValueInUsd)
internal
pure
returns (uint256)
{
if (totalDscMinted == 0) return type(uint256).max;
// Multiply first to prevent truncation, then divide
return (collateralValueInUsd * LIQUIDATION_THRESHOLD * 1e18) / (totalDscMinted * 100);
}

Support

FAQs

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