Skip to content

Commit

Permalink
Merge pull request #234 from euler-xyz/missing-commits
Browse files Browse the repository at this point in the history
test: add ltv tests
  • Loading branch information
dglowinski authored May 20, 2024
2 parents 8a71e42 + bd45037 commit 0f2b108
Showing 1 changed file with 158 additions and 0 deletions.
158 changes: 158 additions & 0 deletions test/unit/evault/modules/Vault/ltv.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,162 @@ contract VaultTest_LTV is EVaultTestBase {
//borrowLTV > liquidationLTV
eTST.setLTV(address(eTST2), 0.2e4, 0.1e4, 0);
}

function test_borrowLTV() public {
startHoax(depositor);
assetTST.mint(depositor, 100e18);
assetTST.approve(address(eTST), type(uint256).max);
eTST.deposit(100e18, depositor);

startHoax(borrower);
assetTST2.mint(borrower, 100e18);
assetTST2.approve(address(eTST2), type(uint256).max);
eTST2.deposit(100e18, borrower);
evc.enableCollateral(borrower, address(eTST2));
evc.enableController(borrower, address(eTST));

startHoax(address(this));
eTST.setLTV(address(eTST2), 0.2e4, 0.9e4, 0);

uint256 borrowLTV = eTST.LTVBorrow(address(eTST2));
assertEq(borrowLTV, 0.2e4);

uint256 snapshot = vm.snapshot();

(uint256 collateralValue,) = eTST.accountLiquidity(borrower, false);
assertEq(collateralValue, eTST2.balanceOf(borrower) * borrowLTV / 1e4);

startHoax(borrower);
vm.expectRevert(Errors.E_AccountLiquidity.selector);
eTST.borrow(collateralValue, borrower);

eTST.borrow(collateralValue - 1, borrower);

vm.revertTo(snapshot);

startHoax(address(this));
eTST.setLTV(address(eTST2), 0.1e4, 0.9e4, 0);

borrowLTV = eTST.LTVBorrow(address(eTST2));
assertEq(borrowLTV, 0.1e4);

(collateralValue,) = eTST.accountLiquidity(borrower, false);
assertEq(collateralValue, eTST2.balanceOf(borrower) * borrowLTV / 1e4);

startHoax(borrower);
vm.expectRevert(Errors.E_AccountLiquidity.selector);
eTST.borrow(collateralValue, borrower);

eTST.borrow(collateralValue - 1, borrower);

vm.revertTo(snapshot);

startHoax(address(this));
eTST.setLTV(address(eTST2), 0, 0.9e4, 0);

borrowLTV = eTST.LTVBorrow(address(eTST2));
assertEq(borrowLTV, 0);

(collateralValue,) = eTST.accountLiquidity(borrower, false);
assertEq(collateralValue, eTST2.balanceOf(borrower) * borrowLTV / 1e4);

startHoax(borrower);
vm.expectRevert(Errors.E_AccountLiquidity.selector);
eTST.borrow(1, borrower);

vm.revertTo(snapshot);

startHoax(address(this));
eTST.setLTV(address(eTST2), 0.1e4, 0.2e4, 100);

borrowLTV = eTST.LTVBorrow(address(eTST2));
assertEq(borrowLTV, 0.1e4);

(collateralValue,) = eTST.accountLiquidity(borrower, false);
assertEq(collateralValue, eTST2.balanceOf(borrower) * borrowLTV / 1e4);

startHoax(borrower);
vm.expectRevert(Errors.E_AccountLiquidity.selector);
eTST.borrow(collateralValue, borrower);

skip(50);

borrowLTV = eTST.LTVBorrow(address(eTST2));
assertEq(borrowLTV, 0.1e4);

vm.expectRevert(Errors.E_AccountLiquidity.selector);
eTST.borrow(collateralValue, borrower);

eTST.borrow(collateralValue - 1, borrower);
}

function test_liqudationLTV() public {
eTST.setLTV(address(eTST2), 0.3e4, 0.3e4, 0);

startHoax(depositor);
assetTST.mint(depositor, 20e18);
assetTST.approve(address(eTST), type(uint256).max);
eTST.deposit(20e18, depositor);

startHoax(borrower);
assetTST2.mint(borrower, 20e18);
assetTST2.approve(address(eTST2), type(uint256).max);
eTST2.deposit(20e18, borrower);
evc.enableCollateral(borrower, address(eTST2));
evc.enableController(borrower, address(eTST));
eTST.borrow(5e18, borrower);

//check account borrowing collateral value
uint256 borrowLTV = eTST.LTVBorrow(address(eTST2));
assertEq(borrowLTV, 3000);

(uint256 collateralValue, uint256 liabilityValue) = eTST.accountLiquidity(borrower, false);
assertEq(collateralValue, eTST2.balanceOf(borrower) * borrowLTV / 1e4);

startHoax(address(this));
eTST.setLTV(address(eTST2), 0.15e4, 0.15e4, 100);

borrowLTV = eTST.LTVBorrow(address(eTST2));
assertEq(borrowLTV, 1500);

(collateralValue, liabilityValue) = eTST.accountLiquidity(borrower, false);
assertEq(collateralValue, eTST2.balanceOf(borrower) * borrowLTV / 1e4);
assertLe(collateralValue * 1e18 / liabilityValue, 1e18); // HS < 1

//check account liquidation collateral value
uint256 liqudationLTV = eTST.LTVLiquidation(address(eTST2));
assertEq(liqudationLTV, 3000);

(collateralValue, liabilityValue) = eTST.accountLiquidity(borrower, true);
assertEq(collateralValue, eTST2.balanceOf(borrower) * liqudationLTV / 1e4);

(uint256 maxRepay, uint256 maxYield) = eTST.checkLiquidation(depositor, borrower, address(eTST2));
assertEq(maxRepay, 0);
assertEq(maxYield, 0);

skip(20);

liqudationLTV = eTST.LTVLiquidation(address(eTST2));
assertEq(liqudationLTV, 2700);

(collateralValue, liabilityValue) = eTST.accountLiquidity(borrower, true);
assertEq(collateralValue, eTST2.balanceOf(borrower) * liqudationLTV / 1e4);

(maxRepay, maxYield) = eTST.checkLiquidation(depositor, borrower, address(eTST2));
assertEq(maxRepay, 0);
assertEq(maxYield, 0);

skip(40);

liqudationLTV = eTST.LTVLiquidation(address(eTST2));
assertEq(liqudationLTV, 2100);

(collateralValue, liabilityValue) = eTST.accountLiquidity(borrower, true);
assertEq(collateralValue, eTST2.balanceOf(borrower) * liqudationLTV / 1e4);
assertLe(collateralValue * 1e18 / liabilityValue, 1e18); // HS < 1

(maxRepay, maxYield) = eTST.checkLiquidation(depositor, borrower, address(eTST2));
assertNotEq(maxRepay, 0);
assertNotEq(maxYield, 0);
}
}

0 comments on commit 0f2b108

Please sign in to comment.