diff --git a/script/Liquidate.s.sol b/script/Liquidate.s.sol index eafc26ef..c6db7184 100644 --- a/script/Liquidate.s.sol +++ b/script/Liquidate.s.sol @@ -16,7 +16,8 @@ contract LiquidateScript is Script, Logger { Size size = Size(payable(sizeContractAddress)); - LiquidateParams memory params = LiquidateParams({debtPositionId: 0, minimumCollateralProfit: 0}); + LiquidateParams memory params = + LiquidateParams({debtPositionId: 0, minimumCollateralProfit: 0, deadline: block.timestamp}); vm.startBroadcast(deployerPrivateKey); size.liquidate(params); diff --git a/test/local/actions/BuyCreditMarket.t.sol b/test/local/actions/BuyCreditMarket.t.sol index f1657bec..2db9b581 100644 --- a/test/local/actions/BuyCreditMarket.t.sol +++ b/test/local/actions/BuyCreditMarket.t.sol @@ -366,7 +366,7 @@ contract BuyCreditMarketLendTest is BaseTest { ); } - function testFuzz_BuyCreditMarket_buyCreditMarket_exactAmountOut_parametric( + function testFuzz_BuyCreditMarket_buyCreditMarket_exactAmountOut_specification( uint256 A1, uint256 A2, uint256 deltaT1, @@ -457,7 +457,7 @@ contract BuyCreditMarketLendTest is BaseTest { ); } - function testFuzz_BuyCreditMarket_buyCreditMarket_exactAmountIn_parametric( + function testFuzz_BuyCreditMarket_buyCreditMarket_exactAmountIn_specification( uint256 V1, uint256 V2, uint256 deltaT1, diff --git a/test/local/actions/Compensate.t.sol b/test/local/actions/Compensate.t.sol index d8a49106..451678a1 100644 --- a/test/local/actions/Compensate.t.sol +++ b/test/local/actions/Compensate.t.sol @@ -9,6 +9,7 @@ import {RESERVED_ID} from "@src/libraries/LoanLibrary.sol"; import {SellCreditMarketParams} from "@src/libraries/actions/SellCreditMarket.sol"; import {SetUserConfiguration, SetUserConfigurationParams} from "@src/libraries/actions/SetUserConfiguration.sol"; +import {Math} from "@src/libraries/Math.sol"; import {BuyCreditMarketParams} from "@src/libraries/actions/BuyCreditMarket.sol"; import {CompensateParams} from "@src/libraries/actions/Compensate.sol"; import {BaseTest} from "@test/BaseTest.sol"; @@ -444,15 +445,16 @@ contract CompensateTest is BaseTest { uint256 tenor = 73 days; uint256 dueDate = startDate + 73 days; uint256 amount = 1000e6; - uint256 swapFee1 = size.getSwapFee(amount, tenor); uint256 debtPositionId = _sellCreditMarket(alice, bob, RESERVED_ID, amount, tenor, false); + uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue; + uint256 apr = size.getLoanOfferAPR(bob, tenor); + uint256 r = Math.aprToRatePerTenor(apr, tenor); + uint256 swapFee1 = size.getSwapFee(Math.mulDivUp(futureValue, 1e18, 1e18 + r), tenor); uint256 creditPositionId = size.getCreditPositionIdsByDebtPositionId(debtPositionId)[0]; assertEq(_state().feeRecipient.borrowATokenBalance, swapFee1); assertEq(_state().alice.borrowATokenBalance, amount); - uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue; - uint256 aliceCollateralBefore = _state().alice.collateralTokenBalance; vm.warp(block.timestamp + 30 days); diff --git a/test/local/actions/Liquidate.t.sol b/test/local/actions/Liquidate.t.sol index 7c9058ba..539c1bcb 100644 --- a/test/local/actions/Liquidate.t.sol +++ b/test/local/actions/Liquidate.t.sol @@ -97,6 +97,7 @@ contract LiquidateTest is BaseTest { _buyCreditLimit(alice, block.timestamp + 365 days, YieldCurveHelper.pointCurve(365 days, 0.03e18)); uint256 amount = 15e6; uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, amount, 365 days, false); + uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue; _setPrice(0.1e18); @@ -107,7 +108,7 @@ contract LiquidateTest is BaseTest { Vars memory _before = _state(); - uint256 liquidatorProfit = _liquidate(liquidator, debtPositionId, 0); + uint256 liquidatorProfit = _liquidate(liquidator, debtPositionId); Vars memory _after = _state(); @@ -116,7 +117,7 @@ contract LiquidateTest is BaseTest { assertEq( _after.feeRecipient.borrowATokenBalance, _before.feeRecipient.borrowATokenBalance, - size.getSwapFee(amount, 365 days) + size.getSwapFee(Math.mulDivUp(futureValue, 1e18, 1.03e18), 365 days) ); assertEq( _after.feeRecipient.collateralTokenBalance, diff --git a/test/local/actions/Multicall.t.sol b/test/local/actions/Multicall.t.sol index 19e607be..bac5e407 100644 --- a/test/local/actions/Multicall.t.sol +++ b/test/local/actions/Multicall.t.sol @@ -175,8 +175,9 @@ contract MulticallTest is BaseTest { function test_Multicall_multicall_bypasses_cap_if_it_is_to_reduce_debt() public { _setPrice(1e18); + _updateConfig("swapFeeAPR", 0); uint256 amount = 100e6; - uint256 cap = amount + size.getSwapFee(100e6, 365 days); + uint256 cap = amount; _updateConfig("borrowATokenCap", cap); _deposit(alice, usdc, cap); diff --git a/test/local/actions/SelfLiquidate.t.sol b/test/local/actions/SelfLiquidate.t.sol index 4c264ef7..e26bb1c7 100644 --- a/test/local/actions/SelfLiquidate.t.sol +++ b/test/local/actions/SelfLiquidate.t.sol @@ -35,7 +35,7 @@ contract SelfLiquidateTest is BaseTest { size.debtTokenAmountToCollateralTokenAmount(size.getDebtPosition(debtPositionId).futureValue); vm.expectRevert(); - _liquidate(liquidator, debtPositionId, debtInCollateralToken); + _liquidate(liquidator, debtPositionId, debtInCollateralToken, block.timestamp); Vars memory _before = _state(); @@ -133,7 +133,7 @@ contract SelfLiquidateTest is BaseTest { size.debtTokenAmountToCollateralTokenAmount(size.getDebtPosition(debtPositionId).futureValue); vm.expectRevert(); - _liquidate(liquidator, debtPositionId, futureValueInCollateralToken); + _liquidate(liquidator, debtPositionId, futureValueInCollateralToken, block.timestamp); Vars memory _before = _state(); diff --git a/test/local/actions/SellCreditMarket.t.sol b/test/local/actions/SellCreditMarket.t.sol index 9476632a..c66dd6ed 100644 --- a/test/local/actions/SellCreditMarket.t.sol +++ b/test/local/actions/SellCreditMarket.t.sol @@ -33,20 +33,17 @@ contract SellCreditMarketTest is BaseTest { uint256 amount = 100e6; uint256 tenor = 365 days; - uint256 futureValue = Math.mulDivUp(amount, (PERCENT + 0.03e18), PERCENT); uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, amount, tenor, false); - - uint256 futureValueOpening = Math.mulDivUp(futureValue, size.riskConfig().crOpening, PERCENT); - uint256 minimumCollateral = size.debtTokenAmountToCollateralTokenAmount(futureValueOpening); - uint256 swapFee = size.getSwapFee(amount, tenor); + uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue; + uint256 issuanceValue = Math.mulDivDown(futureValue, PERCENT, PERCENT + 0.03e18); + uint256 swapFee = size.getSwapFee(issuanceValue, tenor); Vars memory _after = _state(); - assertGt(_before.bob.collateralTokenBalance, minimumCollateral); assertEq(_after.alice.borrowATokenBalance, _before.alice.borrowATokenBalance - amount - swapFee); assertEq(_after.bob.borrowATokenBalance, _before.bob.borrowATokenBalance + amount); assertEq(_after.variablePool.collateralTokenBalance, _before.variablePool.collateralTokenBalance); - assertEq(_after.bob.debtBalance, size.getDebtPosition(debtPositionId).futureValue); + assertEq(_after.bob.debtBalance, futureValue); } function testFuzz_SellCreditMarket_sellCreditMarket_used_to_borrow(uint256 amount, uint256 apr, uint256 tenor) @@ -66,22 +63,21 @@ contract SellCreditMarketTest is BaseTest { Vars memory _before = _state(); - uint256 rate = uint256(Math.aprToRatePerTenor(apr, tenor)); - uint256 debt = Math.mulDivUp(amount, (PERCENT + rate), PERCENT); + uint256 rate = Math.aprToRatePerTenor(apr, tenor); uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, amount, tenor, false); - uint256 debtOpening = Math.mulDivUp(debt, size.riskConfig().crOpening, PERCENT); - uint256 minimumCollateral = size.debtTokenAmountToCollateralTokenAmount(debtOpening); + uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue; + uint256 issuanceValue = Math.mulDivDown(futureValue, PERCENT, PERCENT + rate); + Vars memory _after = _state(); - assertGt(_before.bob.collateralTokenBalance, minimumCollateral); assertEq( _after.alice.borrowATokenBalance, - _before.alice.borrowATokenBalance - amount - size.getSwapFee(amount, tenor) + _before.alice.borrowATokenBalance - amount - size.getSwapFee(issuanceValue, tenor) ); assertEq(_after.bob.borrowATokenBalance, _before.bob.borrowATokenBalance + amount); assertEq(_after.variablePool.collateralTokenBalance, _before.variablePool.collateralTokenBalance); - assertEq(_after.bob.debtBalance, size.getDebtPosition(debtPositionId).futureValue); + assertEq(_after.bob.debtBalance, futureValue); } function test_SellCreditMarket_sellCreditMarket_fragmentation() public { @@ -516,15 +512,15 @@ contract SellCreditMarketTest is BaseTest { Vars memory _before = _state(); - _sellCreditMarket(bob, alice, RESERVED_ID, cash, tenor, false); + uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, cash, tenor, false); + uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue; uint256 swapFeePercent = Math.mulDivUp(size.feeConfig().swapFeeAPR, tenor, 365 days); + uint256 r = Math.aprToRatePerTenor(apr, tenor); + uint256 swapFee = Math.mulDivUp(futureValue, swapFeePercent, PERCENT + r); Vars memory _after = _state(); - assertEq( - _after.alice.borrowATokenBalance, - _before.alice.borrowATokenBalance - cash - Math.mulDivUp(cash, swapFeePercent, PERCENT) - ); + assertEq(_after.alice.borrowATokenBalance, _before.alice.borrowATokenBalance - cash - swapFee); assertEq(_after.bob.borrowATokenBalance, _before.bob.borrowATokenBalance + cash); }