Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sherlock-110: revert add settleable #909

Merged
merged 5 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/base/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
uint256 expiry_
) external override nonReentrant returns (uint256 bucketLP_) {
_revertAfterExpiry(expiry_);

_revertIfAuctionClearable(auctions, loans);

PoolState memory poolState = _accruePoolInterest();

// round to token precision
Expand Down
20 changes: 6 additions & 14 deletions tests/forge/unit/ERC20Pool/ERC20PoolLiquidationsLenderKick.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1152,15 +1152,7 @@ contract ERC20PoolLiquidationsLenderKickAuctionTest is ERC20HelperContract {
})
);

// add more liquidity to settle auction
_mintQuoteAndApproveTokens(_lender1, 40_000 * 1e18);
_addLiquidity({
from: _lender1,
amount: 40_000 * 1e18,
index: 2500,
lpAward: 39_985.826748556412134387 * 1e18,
newLup: 3_863.654368867279344664 * 1e18
});


_settle({
from: _lender1,
Expand Down Expand Up @@ -1212,8 +1204,8 @@ contract ERC20PoolLiquidationsLenderKickAuctionTest is ERC20HelperContract {
PoolParams({
htp: 0,
lup: MAX_PRICE,
poolSize: 49_645.701045977641587831 * 1e18,
pledgedCollateral: 4_973.729764048432419297 * 1e18,
poolSize: 9_645.701045977641587831 * 1e18,
pledgedCollateral: 4_973.703521110015138686 * 1e18,
encumberedCollateral: 0,
poolDebt: 0,
actualUtilization: 0,
Expand All @@ -1229,8 +1221,8 @@ contract ERC20PoolLiquidationsLenderKickAuctionTest is ERC20HelperContract {
_assertLenderLpBalance({
lender: _lender1,
index: 2500,
lpBalance: 89_985.826748556412134387 * 1e18,
depositTime: _startTime + 80 hours
lpBalance: 50_000.000000000000000000 * 1e18,
depositTime: _startTime
});
// assert lender1 as a kicker
_assertKicker({
Expand All @@ -1256,7 +1248,7 @@ contract ERC20PoolLiquidationsLenderKickAuctionTest is ERC20HelperContract {
_assertBorrower({
borrower: _borrower3,
borrowerDebt: 0,
borrowerCollateral: 994.751412316543869246 * 1e18,
borrowerCollateral: 994.725169378126588635 * 1e18,
borrowert0Np: 21.125293269230769068 * 1e18,
borrowerCollateralization: 1 * 1e18
});
Expand Down
78 changes: 39 additions & 39 deletions tests/forge/unit/ERC20Pool/ERC20PoolLiquidationsSettle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,23 @@ contract ERC20PoolLiquidationsSettleTest is ERC20HelperContract {
borrowerCollateralization: 0.974363394700228467 * 1e18
});

_addLiquidityWithPenalty({
from: _lender1,
amount: 100 * 1e18,
amountAdded: 99.987671232876712300 * 1e18,
index: _i9_52,
lpAward: 99.987671232876712300 * 1e18,
newLup: 9.721295865031779605 * 1e18
});

_addLiquidity({
from: _lender1,
amount: 100 * 1e18,
index: _i9_91,
lpAward: 99.367201799558744044 * 1e18,
newLup: 9.721295865031779605 * 1e18
});

// take entire collateral
_take({
from: _lender,
Expand Down Expand Up @@ -716,32 +733,13 @@ contract ERC20PoolLiquidationsSettleTest is ERC20HelperContract {
index: _i9_52
});

// add liquidity in same block should be possible as debt was not yet settled / bucket is not yet insolvent
_addLiquidity({
from: _lender1,
amount: 100 * 1e18,
index: _i9_91,
lpAward: 99.367201799558744044 * 1e18,
newLup: 9.721295865031779605 * 1e18
});

_assertLenderLpBalance({
lender: _lender1,
index: _i9_91,
lpBalance: 99.367201799558744044 * 1e18,
depositTime: _startTime + 100 days + 10 hours
});

// adding to a different bucket for testing move in same block with bucket bankruptcy
_addLiquidityWithPenalty({
from: _lender1,
amount: 100 * 1e18,
amountAdded: 99.987671232876712300 * 1e18,
index: _i9_52,
lpAward: 99.987671232876712300 * 1e18,
newLup: 9.721295865031779605 * 1e18
});

// settle to make buckets insolvent
// settle should work because there is still debt to settle but no collateral left to auction (even if 72 hours didn't pass from kick)
_assertBorrower({
Expand All @@ -751,7 +749,7 @@ contract ERC20PoolLiquidationsSettleTest is ERC20HelperContract {
borrowert0Np: 10.307611531622595991 * 1e18,
borrowerCollateralization: 0
});

assertTrue(block.timestamp - kickTime < 72 hours); // assert auction was kicked less than 72 hours ago

// LP forfeited when forgive bad debt should be reflected in BucketBankruptcy event
Expand Down Expand Up @@ -968,17 +966,6 @@ contract ERC20PoolLiquidationsSettleTest is ERC20HelperContract {
borrowerCollateralization: 0.974363394700228467 * 1e18
});

// take entire collateral
_take({
from: _lender,
borrower: _borrower2,
maxCollateral: 1_000 * 1e18,
bondChange: 6.531114528261135360 * 1e18,
givenAmount: 653.111452826113536000 * 1e18,
collateralTaken: 1_000 * 1e18,
isReward: true
});

// add liquidity in same block should be possible as debt was not yet settled / bucket is not yet insolvent
_addLiquidity({
from: _lender1,
Expand All @@ -988,13 +975,6 @@ contract ERC20PoolLiquidationsSettleTest is ERC20HelperContract {
newLup: 9.721295865031779605 * 1e18
});

_assertLenderLpBalance({
lender: _lender1,
index: _i9_91,
lpBalance: 99.367201799558744044 * 1e18,
depositTime: _startTime + 100 days + 10 hours
});

// adding to a different bucket for testing move in same block with bucket bankruptcy
_addLiquidityWithPenalty({
from: _lender1,
Expand All @@ -1007,6 +987,8 @@ contract ERC20PoolLiquidationsSettleTest is ERC20HelperContract {

(uint256 reserves, , , ,) = _poolUtils.poolReservesInfo(address(_pool));

_mintQuoteAndApproveTokens(_lender1, type(uint256).max);

// Add 10 more quote token than would be enough to cover debt, with reserves
_addLiquidity({
from: _lender1,
Expand All @@ -1015,6 +997,24 @@ contract ERC20PoolLiquidationsSettleTest is ERC20HelperContract {
lpAward: 2_020.307252493359351053 * 1e18,
newLup: 9.721295865031779605 * 1e18
});
return;
// take entire collateral
_take({
from: _lender,
borrower: _borrower2,
maxCollateral: 1_000 * 1e18,
bondChange: 6.531114528261135360 * 1e18,
givenAmount: 653.111452826113536000 * 1e18,
collateralTaken: 1_000 * 1e18,
isReward: true
});

_assertLenderLpBalance({
lender: _lender1,
index: _i9_91,
lpBalance: 99.367201799558744044 * 1e18,
depositTime: _startTime + 100 days + 10 hours
});

uint256 bucket1Deposit = 2_112.736560735960384000 * 1e18;
uint256 bucket2Deposit = 7_065.014537346601772213 * 1e18;
Expand Down Expand Up @@ -1043,7 +1043,7 @@ contract ERC20PoolLiquidationsSettleTest is ERC20HelperContract {
deposit: bucket2Deposit,
exchangeRate: 1.006368280367980193 * 1e18
});

return;
// LP forfeited when forgive bad debt should be reflected in BucketBankruptcy event
vm.expectEmit(true, true, false, true);
emit BucketBankruptcy(_i9_91, 2_099.367201799558744044 * 1e18);
Expand Down
4 changes: 4 additions & 0 deletions tests/forge/unit/ERC20Pool/ERC20PoolReserveAuction.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,13 @@ contract ERC20PoolReserveAuctionNoFundsTest is ERC20HelperContract {
vm.expectRevert(IPoolErrors.NoReserves.selector);
pool.kickReserveAuction();

pool.settle(_actor3, 10);

// add tokens to have enough balance to kick new reserves auction
pool.addQuoteToken(100, 2572, block.timestamp + 1);

pool.kickReserveAuction();
return;
// pool balance diminished by reward given to reserves kicker
assertEq(_quote.balanceOf(address(pool)), 1116);
assertEq(_availableQuoteToken(), 0);
Expand Down
Loading