update lending contract
This commit is contained in:
@@ -156,7 +156,6 @@ contract YtLendingTest is Test {
|
||||
borrowPerYearInterestRateSlopeHigh: BORROW_RATE_HIGH,
|
||||
borrowPerYearInterestRateBase: BORROW_RATE_BASE,
|
||||
storeFrontPriceFactor: STORE_FRONT_PRICE_FACTOR,
|
||||
trackingIndexScale: 1e15,
|
||||
baseBorrowMin: uint104(BASE_BORROW_MIN),
|
||||
targetReserves: uint104(TARGET_RESERVES),
|
||||
assetConfigs: assetConfigs
|
||||
@@ -319,7 +318,7 @@ contract YtLendingTest is Test {
|
||||
|
||||
// 借款 $16,000 USDC(80% LTV)
|
||||
uint256 borrowAmount = 16000e6;
|
||||
lending.borrow(borrowAmount);
|
||||
lending.withdraw(borrowAmount);
|
||||
vm.stopPrank();
|
||||
|
||||
// 验证
|
||||
@@ -335,7 +334,7 @@ contract YtLendingTest is Test {
|
||||
// Alice 尝试无抵押借款
|
||||
vm.prank(alice);
|
||||
vm.expectRevert(ILending.InsufficientCollateral.selector);
|
||||
lending.borrow(1000e6);
|
||||
lending.withdraw(1000e6);
|
||||
}
|
||||
|
||||
function test_09_Borrow_FailBelowMinimum() public {
|
||||
@@ -345,7 +344,7 @@ contract YtLendingTest is Test {
|
||||
|
||||
// 尝试借款低于最小值 (< 100 USDC)
|
||||
vm.expectRevert(ILending.BorrowTooSmall.selector);
|
||||
lending.borrow(50e6);
|
||||
lending.withdraw(50e6);
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
@@ -361,7 +360,7 @@ contract YtLendingTest is Test {
|
||||
// Bob 存入 10 YTToken,借 8,000 USDC
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(8000e6);
|
||||
lending.withdraw(8000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
// 时间前进 365 天
|
||||
@@ -398,7 +397,7 @@ contract YtLendingTest is Test {
|
||||
// Bob 借款
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(8000e6);
|
||||
lending.withdraw(8000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
// 每月触发一次利息累积(模拟复利)
|
||||
@@ -426,7 +425,7 @@ contract YtLendingTest is Test {
|
||||
// Bob 存入 10 YTToken (价值 $20,000),借 10,000 USDC
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(10000e6);
|
||||
lending.withdraw(10000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
// LTV = 50%,健康
|
||||
@@ -441,7 +440,7 @@ contract YtLendingTest is Test {
|
||||
// Bob 存入 10 YTToken,借 16,000 USDC(80% LTV)
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(16000e6);
|
||||
lending.withdraw(16000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
// YTToken 价格暴跌到 $1,800
|
||||
@@ -463,7 +462,7 @@ contract YtLendingTest is Test {
|
||||
// 1. Bob 建立借款头寸
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18); // 10 YTToken @ $2000 = $20,000
|
||||
lending.borrow(16000e6); // $16,000(80% LTV)
|
||||
lending.withdraw(16000e6); // $16,000(80% LTV)
|
||||
vm.stopPrank();
|
||||
|
||||
// 2. 计算精确的清算价格(1e30 精度)
|
||||
@@ -507,7 +506,7 @@ contract YtLendingTest is Test {
|
||||
// 1. Bob 建立不良头寸
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18); // 10 YTToken @ $2000 = $20,000
|
||||
lending.borrow(16000e6); // $16,000
|
||||
lending.withdraw(16000e6); // $16,000
|
||||
vm.stopPrank();
|
||||
|
||||
// 2. YTToken 价格跌到 $1,750
|
||||
@@ -544,12 +543,12 @@ contract YtLendingTest is Test {
|
||||
// 1. Bob 和 Charlie 都建立不良头寸
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(16000e6);
|
||||
lending.withdraw(16000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
vm.startPrank(charlie);
|
||||
lending.supplyCollateral(address(ytVault), 5e18);
|
||||
lending.borrow(8000e6);
|
||||
lending.withdraw(8000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
// 2. 价格下跌
|
||||
@@ -581,7 +580,7 @@ contract YtLendingTest is Test {
|
||||
// 1. 先清算一个账户,产生抵押品库存
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(16000e6);
|
||||
lending.withdraw(16000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
ytFactory.updateVaultPrices(address(ytVault), 1750e30);
|
||||
@@ -619,7 +618,7 @@ contract YtLendingTest is Test {
|
||||
// 设置清算库存
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(16000e6);
|
||||
lending.withdraw(16000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
ytFactory.updateVaultPrices(address(ytVault), 1750e30);
|
||||
@@ -649,7 +648,7 @@ contract YtLendingTest is Test {
|
||||
// Bob 小额借款
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18); // 10 YTToken @ $2000 = $20,000
|
||||
lending.borrow(100e6); // 只借 $100
|
||||
lending.withdraw(100e6); // 只借 $100
|
||||
vm.stopPrank();
|
||||
|
||||
// 让时间流逝以累积利息,增加 reserves
|
||||
@@ -701,7 +700,7 @@ contract YtLendingTest is Test {
|
||||
// Bob 借 5,000 USDC
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(5000e6);
|
||||
lending.withdraw(5000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
// reserves = balance - totalSupply + totalBorrow
|
||||
@@ -719,7 +718,7 @@ contract YtLendingTest is Test {
|
||||
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(8000e6);
|
||||
lending.withdraw(8000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
// 时间流逝
|
||||
@@ -746,7 +745,7 @@ contract YtLendingTest is Test {
|
||||
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(8000e6);
|
||||
lending.withdraw(8000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
vm.warp(block.timestamp + 365 days);
|
||||
@@ -799,7 +798,7 @@ contract YtLendingTest is Test {
|
||||
// Bob 借 8,000 USDC
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(8000e6);
|
||||
lending.withdraw(8000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
// 利用率 = 8000 / 10000 = 80%
|
||||
@@ -813,7 +812,7 @@ contract YtLendingTest is Test {
|
||||
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(5000e6);
|
||||
lending.withdraw(5000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
uint64 supplyRate = lending.getSupplyRate();
|
||||
@@ -831,7 +830,7 @@ contract YtLendingTest is Test {
|
||||
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(8000e6);
|
||||
lending.withdraw(8000e6);
|
||||
vm.stopPrank();
|
||||
|
||||
uint64 borrowRate = lending.getBorrowRate();
|
||||
@@ -869,7 +868,7 @@ contract YtLendingTest is Test {
|
||||
lending.supplyCollateral(address(ytVault), 10e18); // $20,000
|
||||
|
||||
// 借款 $16,000(正好 80%)
|
||||
lending.borrow(16000e6);
|
||||
lending.withdraw(16000e6);
|
||||
|
||||
// 应该成功
|
||||
assertEq(lending.borrowBalanceOf(alice), 16000e6, "Should borrow at max LTV");
|
||||
@@ -887,7 +886,7 @@ contract YtLendingTest is Test {
|
||||
|
||||
// 尝试借 $16,001(超过 80%)
|
||||
vm.expectRevert(ILending.InsufficientCollateral.selector);
|
||||
lending.borrow(16001e6);
|
||||
lending.withdraw(16001e6);
|
||||
vm.stopPrank();
|
||||
}
|
||||
|
||||
@@ -899,7 +898,7 @@ contract YtLendingTest is Test {
|
||||
// Alice 借款后尝试取出抵押品
|
||||
vm.startPrank(alice);
|
||||
lending.supplyCollateral(address(ytVault), 10e18);
|
||||
lending.borrow(16000e6);
|
||||
lending.withdraw(16000e6);
|
||||
|
||||
// 尝试取出 1 YTToken 会破坏抵押率
|
||||
vm.expectRevert(ILending.InsufficientCollateral.selector);
|
||||
@@ -929,13 +928,13 @@ contract YtLendingTest is Test {
|
||||
// 2. Bob 抵押借款
|
||||
vm.startPrank(bob);
|
||||
lending.supplyCollateral(address(ytVault), 20e18); // $40,000
|
||||
lending.borrow(30000e6); // 75% LTV
|
||||
lending.withdraw(30000e6); // 75% LTV
|
||||
vm.stopPrank();
|
||||
|
||||
// 3. Charlie 也抵押借款(更激进,容易被清算)
|
||||
vm.startPrank(charlie);
|
||||
lending.supplyCollateral(address(ytVault), 5e18); // $10,000
|
||||
lending.borrow(7900e6); // 79% LTV
|
||||
lending.withdraw(7900e6); // 79% LTV
|
||||
vm.stopPrank();
|
||||
|
||||
// 4. 时间流逝,利息累积
|
||||
|
||||
Reference in New Issue
Block a user