Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
daopunk committed May 22, 2024
1 parent b5e337d commit 671294d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 37 deletions.
17 changes: 8 additions & 9 deletions src/contracts/ODSaviour.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,19 @@ contract ODSaviour is Authorizable, Modifiable, ModifiablePerCollateral, IODSavi

uint256 _requiredCollateral;
{
ISAFEEngine.SAFEEngineCollateralData memory _safeEngCData = safeEngine.cData(_cType);

(uint256 _currentCollateral, uint256 _currentDebt) = getCurrentCollateralAndDebt(_cType, _safe);
uint256 _accumulatedRate = _safeEngCData.accumulatedRate;
uint256 _liquidationPrice = _safeEngCData.liquidationPrice;

ISAFEEngine.SAFEEngineCollateralData memory _safeEngCData = safeEngine.cData(_cType);
uint256 _safetyPrice = _safeEngCData.safetyPrice;

uint256 _collateralXliquidationPrice = _currentCollateral.wmul(_liquidationPrice);
uint256 _debtXaccumulatedRate = _currentDebt.wmul(_accumulatedRate);
uint256 _collateralXliquidationPrice = _currentCollateral.wmul(_safeEngCData.liquidationPrice);
uint256 _debtXaccumulatedRate = _currentDebt.wmul(_safeEngCData.accumulatedRate);

_requiredCollateral = (_debtXaccumulatedRate - _collateralXliquidationPrice).wdiv(_safetyPrice);
uint256 _newCollateralXliquidationPrice = (_requiredCollateral + _currentCollateral).wmul(_liquidationPrice);
/// @notice (lockedCollateral * liquidationPrice / safetyPrice) + (generatedDebt * accumulatedRate / safetyPrice)
uint256 _requiredTotalCollateral = _collateralXliquidationPrice.wdiv(_safetyPrice)
+ (_debtXaccumulatedRate - _collateralXliquidationPrice).wdiv(_safetyPrice);

if (_newCollateralXliquidationPrice <= _debtXaccumulatedRate) revert SafetyRatioNotMet();
_requiredCollateral = _requiredTotalCollateral - _currentCollateral;
}
IERC20 _token = _saviourTokenAddresses[_cType];
_token.transferFrom(saviourTreasury, address(this), _requiredCollateral);
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/IODSaviour.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ interface IODSaviour is ISAFESaviour {
error VaultNotAllowed(uint256 _vaultId);
error CollateralTransferFailed();
error OnlyLiquidationEngine();
error SafetyRatioNotMet();
error AlreadyInitialized(bytes32);
error UninitializedCollateral(bytes32);
error CollateralMustBeInitialized(bytes32);
Expand Down
41 changes: 16 additions & 25 deletions test/e2e/E2ESaviour.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -485,19 +485,24 @@ contract E2ESaviourTestFuzz is E2ESaviourTestRiskSetup {
_devaluation = bound(_devaluation, 0.1 ether, 1 ether - 1);
_devalueCollateral(_devaluation);

ISAFEEngine.SAFEEngineCollateralData memory _safeEngCData = safeEngine.cData(TKN);

(uint256 _currentCollateral, uint256 _currentDebt) = saviour.getCurrentCollateralAndDebt(TKN, aliceNFV.safeHandler);
uint256 _accumulatedRate = _safeEngCData.accumulatedRate;

ISAFEEngine.SAFEEngineCollateralData memory _safeEngCData = safeEngine.cData(TKN);
uint256 _liquidationPrice = _safeEngCData.liquidationPrice;
uint256 _safetyPrice = _safeEngCData.safetyPrice;
emit log_named_uint('Safety Price', 10);

uint256 _collateralXliquidationPrice = _currentCollateral.wmul(_liquidationPrice);
uint256 _debtXaccumulatedRate = _currentDebt.wmul(_accumulatedRate);
uint256 _debtXaccumulatedRate = _currentDebt.wmul(_safeEngCData.accumulatedRate);

if (_collateralXliquidationPrice < _debtXaccumulatedRate) {
uint256 _requiredAmount = (_debtXaccumulatedRate - _collateralXliquidationPrice).wdiv(_safetyPrice);
uint256 _requiredAmount;

/// @notice scoped to reduce stack
{
uint256 _collateralDeficit = (_debtXaccumulatedRate - _collateralXliquidationPrice).wdiv(_safetyPrice);
uint256 _safetyCollateral = _collateralXliquidationPrice.wdiv(_safetyPrice);
_requiredAmount = _collateralDeficit + _safetyCollateral - _currentCollateral;
}
uint256 _newCollateralXliquidationPrice = (_currentCollateral + _requiredAmount).wmul(_liquidationPrice);
assertTrue(_newCollateralXliquidationPrice > _debtXaccumulatedRate);
}
Expand Down Expand Up @@ -588,9 +593,12 @@ contract E2ESaviourTestFuzz is E2ESaviourTestRiskSetup {

assertTrue(_newCollateralXliquidationPrice > _debtXaccumulatedRate);

/// @notice compare to ratio using NFTRenderer formatted ratios
emit log_named_uint('_safetyCRatio -----------------', oracleRelayer.cParams(TKN).safetyCRatio / 1e18);
/**
* @notice compare ratio using NFTRenderer math
* formatted to 9 fixed-point decimals instead of 2 fixed-point decimals
*/
emit log_named_uint('_liquidationCRatio ------------', oracleRelayer.cParams(TKN).liquidationCRatio / 1e18);
emit log_named_uint('_safetyCRatio -----------------', oracleRelayer.cParams(TKN).safetyCRatio / 1e18);

/// @notice `_ratio` should approximately equal `_safetyCRatio`
emit log_named_uint(
Expand All @@ -603,21 +611,4 @@ contract E2ESaviourTestFuzz is E2ESaviourTestRiskSetup {
);
}
}

function test_liquidateProtectedSafe_static() public {
uint256 _devaluation = 0.15 ether;
(uint256 _collateralA, uint256 _debtA) = saviour.getCurrentCollateralAndDebt(TKN, aliceNFV.safeHandler);
assertTrue(_collateralA > 0 && _debtA > 0);
assertTrue((_collateralA * liquidationPrice) >= (_debtA * accumulatedRate));

_devalueCollateral(_devaluation);
(uint256 _collateralB, uint256 _debtB) = saviour.getCurrentCollateralAndDebt(TKN, aliceNFV.safeHandler);
if (liquidationPrice != 0 && (_collateralB * liquidationPrice) < (_debtB * accumulatedRate)) {
liquidationEngine.liquidateSAFE(TKN, aliceNFV.safeHandler);
}
(uint256 _collateralC, uint256 _debtC) = saviour.getCurrentCollateralAndDebt(TKN, aliceNFV.safeHandler);
assertTrue(_collateralC >= _collateralA);
assertEq(_debtC, _debtA);
emit log_named_uint('x', _collateralA);
}
}
4 changes: 2 additions & 2 deletions test/unit/ODSaviour.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,11 @@ contract UnitODSaviourSaveSafe is ODSaviourSetUp {
ISAFEEngine.SAFE({lockedCollateral: safeStartingCollateralBalance, generatedDebt: safeStartingDebtBalance})
)
);
emit SafeSaved(vaultId, 80 ether);
emit SafeSaved(vaultId, 90 ether);
liquidationEngine.liquidateSAFE(ARB, safeHandler);
assertEq(safeEngine.safes(ARB, safeHandler).lockedCollateral, safeStartingCollateralBalance);
assertEq(safeEngine.safes(ARB, safeHandler).generatedDebt, liquidation.safeDebt);
assertEq(collateralToken.balanceOf(saviourTreasury), startingSaviourBalance - 80 ether);
assertEq(collateralToken.balanceOf(saviourTreasury), startingSaviourBalance - 90 ether);
}

/// test that safe is liquidated without saviour
Expand Down

0 comments on commit 671294d

Please sign in to comment.