Skip to content

Commit

Permalink
Reorg code and revert, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Jun 14, 2023
1 parent e98167b commit 375f52d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
17 changes: 9 additions & 8 deletions src/libraries/external/KickerActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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_,
Expand Down
22 changes: 11 additions & 11 deletions tests/forge/unit/ERC20Pool/ERC20PoolLiquidationsScaled.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions tests/forge/unit/ERC721Pool/ERC721PoolLiquidationsSettle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
});
}

Expand Down

0 comments on commit 375f52d

Please sign in to comment.