Skip to content

Commit

Permalink
change test params
Browse files Browse the repository at this point in the history
  • Loading branch information
daopunk committed Jul 3, 2024
1 parent f8bf2dc commit 31edfb2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 70 deletions.
134 changes: 66 additions & 68 deletions test/e2e/E2ELiquidation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import {ISAFEEngine} from '@interfaces/ISAFEEngine.sol';
import {IOracleRelayer} from '@interfaces/IOracleRelayer.sol';
import {Math, RAY} from '@libraries/Math.sol';
import {DelayedOracleForTest} from '@test/mocks/DelayedOracleForTest.sol';
import {Common, COLLAT, DEBT, TKN} from '@test/e2e/Common.t.sol';
import {Common, COLLAT, DEBT, RETH} from '@test/e2e/Common.t.sol';

uint256 constant MINUS_0_5_PERCENT_PER_HOUR = 999_998_607_628_240_588_157_433_861;
uint256 constant DEPOSIT = 135 ether + 1; // 136% collateralized
uint256 constant MINT = 100 ether;
uint256 constant DEFAULT_DEVALUATION = 0.2 ether;
uint256 constant USER_AMOUNT = 1000 ether;
uint256 constant DEPOSIT = 0.5 ether; // $1000 worth of RETH (1 RETH = $2000)
uint256 constant MINT = 740 ether; // $740 worth of OD (135% over-collateralization)
uint256 constant USER_AMOUNT = 500 ether;

contract E2ELiquidation is Common {
using Math for uint256;
Expand All @@ -34,76 +33,70 @@ contract E2ELiquidation is Common {
address public bobProxy;
uint256 public initialSystemCoinSupply;

IERC20 public reth;

mapping(address proxy => uint256 safeId) public vaults;

function setUp() public virtual override {
super.setUp();
_refreshCData(TKN);
refreshCData(RETH);
reth = IERC20(address(collateral[RETH]));

aliceProxy = _userVaultSetup(TKN, alice, USER_AMOUNT, 'AliceProxy');
aliceProxy = userVaultSetup(RETH, alice, USER_AMOUNT, 'AliceProxy');
aliceNFV = vault721.getNfvState(vaults[aliceProxy]);
_depositCollateralAndGenDebt(TKN, vaults[aliceProxy], DEPOSIT, MINT, aliceProxy);
depositCollateralAndGenDebt(RETH, vaults[aliceProxy], DEPOSIT, MINT, aliceProxy);

bobProxy = _userVaultSetup(TKN, bob, USER_AMOUNT, 'BobProxy');
bobProxy = userVaultSetup(RETH, bob, USER_AMOUNT, 'BobProxy');
bobNFV = vault721.getNfvState(vaults[bobProxy]);
_depositCollateralAndGenDebt(TKN, vaults[bobProxy], DEPOSIT * 3, MINT * 3, bobProxy);
depositCollateralAndGenDebt(RETH, vaults[bobProxy], DEPOSIT * 3, MINT * 3, bobProxy);

initialSystemCoinSupply = systemCoin.totalSupply();

_refreshCData(TKN);

_collateralDevaluation(TKN, DEFAULT_DEVALUATION);
auctionId = liquidationEngine.liquidateSAFE(TKN, aliceNFV.safeHandler);

vm.prank(bob);
systemCoin.approve(bobProxy, USER_AMOUNT);
}

function testBuyCollateral1() public {
// CAH holds all 136 ether of collateral after liquidation and before auction
_logWadCollateralAuctionHouseTokenCollateral(TKN);
assertEq(safeEngine.tokenCollateral(TKN, address(collateralAuctionHouse[TKN])), DEPOSIT);

// alice has no collateral after liquidation
assertEq(safeEngine.tokenCollateral(TKN, aliceNFV.safeHandler), 0);

// bob's non-deposited collateral balance before collateral auction
uint256 _externalCollateralBalanceBob = collateral[TKN].balanceOf(bob);

// alice + bob systemCoin supply
assertEq(initialSystemCoinSupply, systemCoin.totalSupply());

// bob to buy alice's liquidated collateral
_buyCollateral(TKN, auctionId, 0, MINT, bobProxy);

// alice systemCoin supply burned in collateral auction
assertEq(systemCoin.totalSupply(), initialSystemCoinSupply - MINT);

// bob's non-deposited collateral balance after collateral auction
uint256 _externalCollateralGain = collateral[TKN].balanceOf(bob) - _externalCollateralBalanceBob;
emit log_named_uint('_externalCollateralGain -------', _externalCollateralGain);

// coinBalance of accountingEngine: +100 ether
_logWadAccountingEngineCoinAndDebtBalance();

// CAH still holds 60 ether of collateral after auction, because more collateral needs to be sold
_logWadCollateralAuctionHouseTokenCollateral(TKN);
assertEq(safeEngine.tokenCollateral(TKN, address(collateralAuctionHouse[TKN])), DEPOSIT - _externalCollateralGain);

// alice's tokenCollateral balance after the auction the initial deposit minus the auctioned collateral
assertEq(safeEngine.tokenCollateral(TKN, aliceNFV.safeHandler), 0);
function testAssumptions() public {
assertEq(reth.balanceOf(alice), USER_AMOUNT - DEPOSIT);
assertEq(reth.balanceOf(bob), USER_AMOUNT - DEPOSIT * 3);
assertEq(systemCoin.totalSupply(), 1480 ether);
readDelayedPrice(RETH);
collateralDevaluation(RETH, 200 ether); // 10% devaulation; 135% => 125% over-collateralization
readDelayedPrice(RETH);
emitRatio(RETH, aliceNFV.safeHandler);
liquidationEngine.liquidateSAFE(RETH, aliceNFV.safeHandler);
}

/**
* @dev initial setup
* RETH: $2000
* OD = $1
* Liquidation Penalty: 5%
* Liquidation Ratio: 125%
* Safety Ration: 135%
*
* @notice scenario
* User deposit $1000 worth of RETH (0.5 ether) and borrows $740 OD (135% ratio)
*
*/
function testLiquidation() public {}

// HELPER FUNCTIONS
function readDelayedPrice(bytes32 _cType) public returns (uint256) {
uint256 _p = delayedOracle[_cType].read();
emit log_named_uint('CType Price Read', _p);
return _p;
}

function _collateralDevaluation(bytes32 _cType, uint256 _devaluation) internal {
function collateralDevaluation(bytes32 _cType, uint256 _devaluation) public returns (uint256) {
uint256 _p = delayedOracle[_cType].read();
DelayedOracleForTest(address(delayedOracle[_cType])).setPriceAndValidity(_p - _devaluation, true);
oracleRelayer.updateCollateralPrice(_cType);
_refreshCData(_cType);
refreshCData(_cType);
return delayedOracle[_cType].read();
}

function _refreshCData(bytes32 _cType) internal {
function refreshCData(bytes32 _cType) public {
cTypeData = safeEngine.cData(_cType);
liquidationPrice = cTypeData.liquidationPrice;
accumulatedRate = cTypeData.accumulatedRate;
Expand All @@ -113,20 +106,20 @@ contract E2ELiquidation is Common {
safetyCRatio = oracleParams.safetyCRatio;
}

function _userVaultSetup(
function userVaultSetup(
bytes32 _cType,
address _user,
uint256 _amount,
string memory _name
) internal returns (address _proxy) {
_proxy = _deployOrFind(_user);
_mintToken(_cType, _user, _amount, _proxy);
) public returns (address _proxy) {
_proxy = deployOrFind(_user);
mintToken(_cType, _user, _amount, _proxy);
vm.label(_proxy, _name);
vm.prank(_proxy);
vaults[_proxy] = safeManager.openSAFE(_cType, _proxy);
}

function _mintToken(bytes32 _cType, address _account, uint256 _amount, address _okAccount) internal {
function mintToken(bytes32 _cType, address _account, uint256 _amount, address _okAccount) public {
vm.startPrank(_account);
deal(address(collateral[_cType]), _account, _amount);
if (_okAccount != address(0)) {
Expand All @@ -135,7 +128,7 @@ contract E2ELiquidation is Common {
vm.stopPrank();
}

function _deployOrFind(address _owner) internal returns (address) {
function deployOrFind(address _owner) public returns (address) {
address proxy = vault721.getProxy(_owner);
if (proxy == address(0)) {
return address(vault721.build(_owner));
Expand All @@ -144,13 +137,13 @@ contract E2ELiquidation is Common {
}
}

function _depositCollateralAndGenDebt(
function depositCollateralAndGenDebt(
bytes32 _cType,
uint256 _safeId,
uint256 _collatAmount,
uint256 _deltaWad,
address _proxy
) internal {
) public {
vm.startPrank(ODProxy(_proxy).OWNER());
bytes memory _payload = abi.encodeWithSelector(
basicActions.lockTokenCollateralAndGenerateDebt.selector,
Expand All @@ -165,13 +158,13 @@ contract E2ELiquidation is Common {
vm.stopPrank();
}

function _buyCollateral(
function buyCollateral(
bytes32 _cType,
uint256 _auctionId,
uint256 _minCollateral,
uint256 _bid,
address _proxy
) internal {
) public {
vm.startPrank(ODProxy(_proxy).OWNER());
bytes memory _payload = abi.encodeWithSelector(
collateralBidActions.buyCollateral.selector,
Expand All @@ -186,14 +179,19 @@ contract E2ELiquidation is Common {
vm.stopPrank();
}

function _logWadAccountingEngineCoinAndDebtBalance() internal {
emit log_named_uint('_accountingEngineCoinBalance --', safeEngine.coinBalance(address(accountingEngine)) / RAY);
emit log_named_uint('_accountingEngineDebtBalance --', safeEngine.debtBalance(address(accountingEngine)) / RAY);
function getSAFE(bytes32 _cType, address _safe) public view returns (uint256 _collateral, uint256 _debt) {
ISAFEEngine.SAFE memory _safeData = safeEngine.safes(_cType, _safe);
_collateral = _safeData.lockedCollateral;
_debt = _safeData.generatedDebt;
}

function _logWadCollateralAuctionHouseTokenCollateral(bytes32 _cType) internal {
emit log_named_uint(
'_CAH_tokenCollateral ----------', safeEngine.tokenCollateral(_cType, address(collateralAuctionHouse[_cType]))
);
function getRatio(bytes32 _cType, uint256 _collateral, uint256 _debt) public view returns (uint256 _ratio) {
_ratio = _collateral.wmul(oracleRelayer.cParams(_cType).oracle.read()).wdiv(_debt.wmul(accumulatedRate));
}

function emitRatio(bytes32 _cType, address _safe) public returns (uint256 _ratio) {
(uint256 _collateral, uint256 _debt) = getSAFE(_cType, _safe);
_ratio = getRatio(_cType, _collateral, _debt);
emit log_named_uint('CType to Debt Ratio', _ratio);
}
}
2 changes: 1 addition & 1 deletion test/e2e/TestParams.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ abstract contract TestParams is Contracts, Params {

_liquidationEngineCParams[_cType] = ILiquidationEngine.LiquidationEngineCollateralParams({
collateralAuctionHouse: address(collateralAuctionHouse[_cType]),
liquidationPenalty: 1.1e18, // WAD
liquidationPenalty: 1.05e18, // WAD
liquidationQuantity: 100_000e45 // RAD
});

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/TestParams.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ abstract contract TestParams is Contracts, Params {

_liquidationEngineCParams[_cType] = ILiquidationEngine.LiquidationEngineCollateralParams({
collateralAuctionHouse: address(collateralAuctionHouse[_cType]),
liquidationPenalty: 1.1e18, // WAD
liquidationPenalty: 1.05e18, // WAD
liquidationQuantity: 100_000e45 // RAD
});

Expand Down

0 comments on commit 31edfb2

Please sign in to comment.