Skip to content

Commit

Permalink
added 3rd party settler
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Harvey committed Jan 12, 2024
1 parent 7206f26 commit 015a5b6
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions tests/forge/unit/ERC20Pool/ERC20PoolDebtExceedsDeposit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ contract ERC20PoolDebtExceedsDepositTest is ERC20HelperContract {
address internal _borrower2;
address internal _lender;
address internal _attacker;
address internal _settler;

function setUp() external {
_startTest();
Expand Down Expand Up @@ -60,7 +61,11 @@ contract ERC20PoolDebtExceedsDepositTest is ERC20HelperContract {
assertEq(_priceAt(3232), 100.332368143282009890 * 1e18);
}

function testStealReservesWithMargin() external tearDown {
function testStealReservesWithMargin() external {

_settler = makeAddr("settler");
_mintCollateralAndApproveTokens(_settler, 1_000 * 1e18);

// Pool's reserves are already seeded with 50 quote token in setUp()

// assert attacker's balances
Expand Down Expand Up @@ -225,14 +230,30 @@ contract ERC20PoolDebtExceedsDepositTest is ERC20HelperContract {

skip(137 hours);

// 2b. Call settle
// 3rd party settler, snipes the settle
_settle({
from: _attacker,
from: _settler,
borrower: _attacker,
maxDepth: 10,
settledDebt: 42.416885344287936131 * 1e18
});

// 3rd party settler, adds collateral and removes QT
_addCollateral({
from: _settler,
amount: 0.4400000001000000000 * 1e18,
index: 3231,
lpAward: 44.360068569968170018 * 1e18
});

_removeAllLiquidity({
from: _settler,
amount: 43.287456358996402570 * 1e18,
index: 3231,
newLup: 1.0 * 1e18,
lpRedeem: 43.280719726287177705 * 1e18
});

_assertBucket({
index: 3232,
lpBalance: 0 * 1e18,
Expand All @@ -243,28 +264,19 @@ contract ERC20PoolDebtExceedsDepositTest is ERC20HelperContract {

_assertBucket({
index: 3231,
lpBalance: 148.131790867836699762 * 1e18,
collateral: 1.040000000000000000 * 1e18,
deposit: 43.287456358996402570 * 1e18,

lpBalance: 149.211139711517692075 * 1e18,
collateral: 1.480000000100000000 * 1e18,
deposit: 0,
exchangeRate: 1.000155649738540122 * 1e18
});

// 2c. Withdraw the deposit remaing (should be about 50)
// the collateral moved (should be 1.04) from the 100 price bucket (all go to the attacker)
_removeAllLiquidity({
from: _attacker,
amount: 43.287456358996402570 * 1e18,
index: 3231,
newLup: 1.0 * 1e18,
lpRedeem: 43.280719726287177705 * 1e18
});
// 2c. Attacker withdraws remaining collateral

_removeAllCollateral({
from: _attacker,
amount: 1.040000000000000000 * 1e18,
amount: 1.469294121893826788 * 1e18,
index: 3231,
lpRedeem: 104.851071141549522057 * 1e18
lpRedeem: 148.131790867836699762 * 1e18
});

_assertReserveAuction({
Expand All @@ -276,9 +288,10 @@ contract ERC20PoolDebtExceedsDepositTest is ERC20HelperContract {
});

// assert attacker's balances
// attacker profits with 40.878329544797963392 QT
assertEq(_quote.balanceOf(address(_attacker)), 2_000_040.878329544797963392 * 1e18);
assertEq(_collateral.balanceOf(address(_attacker)), 11_000.0 * 1e18);
// attacker looses 2 and change QT
// attacker gains 0.42... Collateral token
assertEq(_quote.balanceOf(address(_attacker)), 1_999_997.590873185801560822 * 1e18);
assertEq(_collateral.balanceOf(address(_attacker)), 11_000.429294121893826788 * 1e18);
}

function testSpendOrigFeePushBadDebtToBorrowers() external tearDown {
Expand Down

0 comments on commit 015a5b6

Please sign in to comment.