Skip to content

Commit

Permalink
Magic Numbers Fixed (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
theirrationalone authored Sep 11, 2023
1 parent 55e15db commit ac725e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/DSCEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions test/unit/DSCEngineTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ac725e2

Please sign in to comment.