From ac725e29da6f930c10dfedda80836ac42c8c51ea Mon Sep 17 00:00:00 2001 From: Anil Kushwaha <96755510+theirrationalone@users.noreply.github.com> Date: Mon, 11 Sep 2023 07:39:18 +0530 Subject: [PATCH] Magic Numbers Fixed (#41) --- src/DSCEngine.sol | 11 ++++++++--- test/unit/DSCEngineTest.t.sol | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/DSCEngine.sol b/src/DSCEngine.sol index 65b3be7..d82c16f 100644 --- a/src/DSCEngine.sol +++ b/src/DSCEngine.sol @@ -70,6 +70,7 @@ contract DSCEngine is ReentrancyGuard { uint256 private constant LIQUIDATION_THRESHOLD = 50; // This means you need to be 200% over-collateralized uint256 private constant LIQUIDATION_BONUS = 10; // This means you get assets at a 10% discount when liquidating + uint256 private constant LIQUIDATION_PRECISION = 100; uint256 private constant MIN_HEALTH_FACTOR = 1e18; uint256 private constant PRECISION = 1e18; uint256 private constant ADDITIONAL_FEED_PRECISION = 1e10; @@ -209,7 +210,7 @@ contract DSCEngine is ReentrancyGuard { // So we are giving the liquidator $110 of WETH for 100 DSC // We should implement a feature to liquidate in the event the protocol is insolvent // And sweep extra amounts into a treasury - uint256 bonusCollateral = (tokenAmountFromDebtCovered * LIQUIDATION_BONUS) / 100; + uint256 bonusCollateral = (tokenAmountFromDebtCovered * LIQUIDATION_BONUS) / LIQUIDATION_PRECISION; // Burn DSC equal to debtToCover // Figure out how much collateral to recover based on how much burnt _redeemCollateral(collateral, tokenAmountFromDebtCovered + bonusCollateral, user, msg.sender); @@ -317,8 +318,8 @@ contract DSCEngine is ReentrancyGuard { returns (uint256) { if (totalDscMinted == 0) return type(uint256).max; - uint256 collateralAdjustedForThreshold = (collateralValueInUsd * LIQUIDATION_THRESHOLD) / 100; - return (collateralAdjustedForThreshold * 1e18) / totalDscMinted; + uint256 collateralAdjustedForThreshold = (collateralValueInUsd * LIQUIDATION_THRESHOLD) / LIQUIDATION_PRECISION; + return (collateralAdjustedForThreshold * PRECISION) / totalDscMinted; } function revertIfHealthFactorIsBroken(address user) internal view { @@ -395,6 +396,10 @@ contract DSCEngine is ReentrancyGuard { return LIQUIDATION_BONUS; } + function getLiquidationPrecision() external pure returns (uint256) { + return LIQUIDATION_PRECISION; + } + function getMinHealthFactor() external pure returns (uint256) { return MIN_HEALTH_FACTOR; } diff --git a/test/unit/DSCEngineTest.t.sol b/test/unit/DSCEngineTest.t.sol index aebaf01..5dd4fbe 100644 --- a/test/unit/DSCEngineTest.t.sol +++ b/test/unit/DSCEngineTest.t.sol @@ -552,6 +552,12 @@ contract DSCEngineTest is StdCheats, Test { assertEq(dscAddress, address(dsc)); } + function testLiquidationPrecision() public { + uint256 expectedLiquidationPrecision = 100; + uint256 actualLiquidationPrecision = dsce.getLiquidationPrecision(); + assertEq(actualLiquidationPrecision, expectedLiquidationPrecision); + } + // How do we adjust our invariant tests for this? // function testInvariantBreaks() public depositedCollateralAndMintedDsc { // MockV3Aggregator(ethUsdPriceFeed).updateAnswer(0);