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

Constants should be used instead of magic numbers.

Summary

Magic number is used in _calculateHealthFactor function.

Vulnerability Details

It is better to use readable constants instead of hex/numeric literals. Instance:

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

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

Here, we can see 1e18 which is a magic number used for calculating health factor of the user.

Impact

Use of constants over magic numbers improves the readability of the code and helps the developers to understand the code better.

Tools Used

Manual review

Recommendations

There is already a constant state variable PRECISION that is equal to 1e18. Therefore, use PRECISION instead of 1e18.

    return (collateralAdjustedForThreshold * PRECISION) / totalDscMinted;

Support

FAQs

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