From 375f52dfe8837e2df8e6604dfa067d066e4043b0 Mon Sep 17 00:00:00 2001 From: grandizzy Date: Wed, 14 Jun 2023 21:40:57 +0300 Subject: [PATCH] Reorg code and revert, fix tests --- src/libraries/external/KickerActions.sol | 17 +++++++------- .../ERC20PoolLiquidationsScaled.t.sol | 22 +++++++++---------- .../ERC721PoolLiquidationsSettle.t.sol | 18 +++++++-------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/libraries/external/KickerActions.sol b/src/libraries/external/KickerActions.sol index 606e96434..2a70fc63a 100644 --- a/src/libraries/external/KickerActions.sol +++ b/src/libraries/external/KickerActions.sol @@ -154,18 +154,19 @@ library KickerActions { ) external returns ( KickResult memory kickResult_ ) { + KickWithDepositLocalVars memory vars; + + vars.bucketPrice = _priceAt(index_); + vars.currentLup = Deposits.getLup(deposits_, poolState_.debt); + + // revert if the bucket price is below LUP + if (vars.bucketPrice < vars.currentLup) revert PriceBelowLUP(); + Bucket storage bucket = buckets_[index_]; Lender storage lender = bucket.lenders[msg.sender]; - KickWithDepositLocalVars memory vars; - vars.lenderLP = bucket.bankruptcyTime < lender.depositTime ? lender.lps : 0; vars.bucketDeposit = Deposits.valueAt(deposits_, index_); - vars.bucketPrice = _priceAt(index_); - vars.currentLup = Deposits.getLup(deposits_, poolState_.debt); - - // revert if the bucket price used to kick and remove is below LUP - if (vars.bucketPrice < vars.currentLup) revert PriceBelowLUP(); // calculate amount lender is entitled in current bucket (based on lender LP in bucket) vars.entitledAmount = Buckets.lpToQuoteTokens( @@ -183,7 +184,7 @@ library KickerActions { // revert if no entitled amount if (vars.entitledAmount == 0) revert InsufficientLiquidity(); - // kick borrower + // kick top borrower kickResult_ = _kick( auctions_, deposits_, diff --git a/tests/forge/unit/ERC20Pool/ERC20PoolLiquidationsScaled.t.sol b/tests/forge/unit/ERC20Pool/ERC20PoolLiquidationsScaled.t.sol index aaabe44e2..0378d590f 100644 --- a/tests/forge/unit/ERC20Pool/ERC20PoolLiquidationsScaled.t.sol +++ b/tests/forge/unit/ERC20Pool/ERC20PoolLiquidationsScaled.t.sol @@ -175,8 +175,8 @@ contract ERC20PoolLiquidationsScaledTest is ERC20DSTestPlus { function testSettleAuctionWithoutTakes( uint8 collateralPrecisionDecimals_, uint8 quotePrecisionDecimals_, - uint16 startBucketId_) external tearDown - { + uint16 startBucketId_ + ) external tearDown { uint256 boundColPrecision = bound(uint256(collateralPrecisionDecimals_), 6, 18); uint256 boundQuotePrecision = bound(uint256(quotePrecisionDecimals_), 6, 18); uint256 startBucketId = bound(uint256(startBucketId_), 1000, 6388); @@ -193,10 +193,10 @@ contract ERC20PoolLiquidationsScaledTest is ERC20DSTestPlus { assertLt(_borrowerCollateralization(_borrower), 1e18); // Kick an auction and wait for a meaningful price - assertEq(_quote.balanceOf(_bidder), 200_000 * _quoteTokenPrecision); + assertEq(_quote.balanceOf(_bidder), 300_000 * _quoteTokenPrecision); _kick(_borrower, _bidder); // assert bidder locked balance in auction bond - assertLt(_quote.balanceOf(_bidder), 200_000 * _quoteTokenPrecision); + assertLt(_quote.balanceOf(_bidder), 300_000 * _quoteTokenPrecision); (uint256 auctionPrice, uint256 auctionDebt, uint256 auctionCollateral) = _advanceAuction(9 hours); assertGt(auctionPrice, 0); @@ -227,10 +227,10 @@ contract ERC20PoolLiquidationsScaledTest is ERC20DSTestPlus { if ( bondTransferAmount * _pool.quoteTokenScale() < claimableBond ) roundingDiff = 1; // ensure bidders can still withdraw their bonds - assertLt(_quote.balanceOf(_bidder), 200_000 * _quoteTokenPrecision); + assertLt(_quote.balanceOf(_bidder), 300_000 * _quoteTokenPrecision); changePrank(_bidder); _pool.withdrawBonds(_bidder, type(uint256).max); - assertEq(_quote.balanceOf(_bidder), 200_000 * _quoteTokenPrecision - roundingDiff); + assertEq(_quote.balanceOf(_bidder), 300_000 * _quoteTokenPrecision - roundingDiff); } function testLiquidationKickWithDeposit( @@ -299,14 +299,14 @@ contract ERC20PoolLiquidationsScaledTest is ERC20DSTestPlus { _pool.kickWithDeposit(bucketId, MAX_FENWICK_INDEX); _checkAuctionStateUponKick(lender); - // confirm user has redeemed some of their LP to post liquidation bond + // confirm user doesn't redeemed any of their LP to post liquidation bond (uint256 lenderLP, ) = _pool.lenderInfo(bucketId, lender); - assertLt(lenderLP, lastLenderLP); + assertEq(lenderLP, lastLenderLP); - // confirm deposit has been removed from bucket + // confirm deposit wasn't removed from bucket (, uint256 bucketDeposit, , uint256 bucketLP, , ) = _poolUtils.bucketInfo(address(_pool), bucketId); - assertLt(bucketDeposit, lastBucketDeposit); - assertLt(bucketLP, lastBucketLP); + assertTrue(bucketDeposit >= lastBucketDeposit); + assertEq(bucketLP, lastBucketLP); } function _checkAuctionStateUponKick(address kicker) internal { diff --git a/tests/forge/unit/ERC721Pool/ERC721PoolLiquidationsSettle.t.sol b/tests/forge/unit/ERC721Pool/ERC721PoolLiquidationsSettle.t.sol index db6999e7d..6fbcbd921 100644 --- a/tests/forge/unit/ERC721Pool/ERC721PoolLiquidationsSettle.t.sol +++ b/tests/forge/unit/ERC721Pool/ERC721PoolLiquidationsSettle.t.sol @@ -144,7 +144,7 @@ contract ERC721PoolLiquidationsSettleTest is ERC721HelperContract { PoolParams({ htp: 0, lup: 3_863.654368867279344664 * 1e18, - poolSize: 12_997.115384615384614000 * 1e18, + poolSize: 16_000 * 1e18, pledgedCollateral: 5 * 1e18, encumberedCollateral: 2.624293841728065377 * 1e18, poolDebt: 10_139.364366784136304617 * 1e18, @@ -172,8 +172,8 @@ contract ERC721PoolLiquidationsSettleTest is ERC721HelperContract { borrowerCollateralization: 2.286329337285291739 * 1e18 }); - assertEq(_quote.balanceOf(address(_pool)), 6_000 * 1e18); - assertEq(_quote.balanceOf(_lender), 104_000 * 1e18); + assertEq(_quote.balanceOf(address(_pool)), 9_002.884615384615386000 * 1e18); // increased by bonds size + assertEq(_quote.balanceOf(_lender), 100_997.115384615384614000 * 1e18); // decreased by bonds size assertEq(_quote.balanceOf(_borrower), 5_100 * 1e18); assertEq(_quote.balanceOf(_borrower2), 13_000 * 1e18); } @@ -249,7 +249,7 @@ contract ERC721PoolLiquidationsSettleTest is ERC721HelperContract { PoolParams({ htp: 0, lup: MAX_PRICE, - poolSize: 2_861.685489213148775844 * 1e18, + poolSize: 5_864.570104597764159383 * 1e18, pledgedCollateral: 1 * 1e18, encumberedCollateral: 0, poolDebt: 0, @@ -301,8 +301,8 @@ contract ERC721PoolLiquidationsSettleTest is ERC721HelperContract { exchangeRate: 1.000000000005475091 * 1e18 }); - assertEq(_quote.balanceOf(address(_pool)), 6_000 * 1e18); - assertEq(_quote.balanceOf(_lender), 104_000 * 1e18); + assertEq(_quote.balanceOf(address(_pool)), 9_002.884615384615386000 * 1e18); + assertEq(_quote.balanceOf(_lender), 100_997.115384615384614000 * 1e18); assertEq(_quote.balanceOf(_borrower), 5_100 * 1e18); assertEq(_quote.balanceOf(_borrower2), 13_000 * 1e18); @@ -365,10 +365,10 @@ contract ERC721PoolLiquidationsSettleTest is ERC721HelperContract { _assertBucket({ index: 2500, - lpBalance: 1_860.819465331537769873 * 1e18, + lpBalance: 4_863.128335182565063307 * 1e18, collateral: 0, - deposit: 1_861.382770397052268844 * 1e18, - exchangeRate: 1.000302718816096508 * 1e18 + deposit: 4_864.324200136395380383 * 1e18, + exchangeRate: 1.000245904461368780 * 1e18 }); }