Skip to content

Commit

Permalink
Include 1.04 COLLATERALIZATION_FACTOR (#1021)
Browse files Browse the repository at this point in the history
* Include 1.04 COLLATERALIZATION_FACTOR
in neutral price used in kicks

* fixed `testDepositTakeAndSettleByBucketTakeSubsetPool`

* fixed testDepositTakeAndSettleByRegularTakeSubsetPool

* Moved `wmul` inside `mulDiv`

* fix position, rewards, and poolInfoUtils tests

* fix pool factory emit; fix borrowAndRepay tests

* updated ERC20PoolBorrowTest, ERC20PoolCollateralTest, and ERC20PoolDebtExceedsDepositTest

* rounding update

* updated ERC20PoolQuoteTokenTest and ERC20PoolPrecisionTest

* Fix ERC721 unit tests

* Fix ERC20PoolLiquidationsArbTakeTest and ERC20PoolLiquidationsDepositTakeTest

* Fix ERC20PoolFactory, ERC20PoolLenderKick and ERC20PoolKick

* Fix ERC20PoolLiquidationSettleFuzzyTest and ERC20PoolLiquidationTakeFuzzyTest

* Fix ERC20PoolLiquidationsMisc

* Fix ERC20PoolLiquidationsLowPriceCollateralTest, ERC20PoolLiquidationsTakeAndRepayAllDebtInPoolTest and PositionManagerERC20PoolTest

* Fix ERC20PoolLiquidationsSettle test

* Fix testTakeWithFlashLoan test

* Fix ERC20PoolLiquidationsTakeTest

* Include COLLATERALIZATION_FACTOR in loanInfo

* Remove redundant code from testTakeAndSettle that was already been tested in other tests

* Update testTakeAndSettle

---------

Co-authored-by: mwc <[email protected]>
Co-authored-by: Mike <[email protected]>
Co-authored-by: Ed Noepel <[email protected]>
Co-authored-by: grandizzy <[email protected]>
Co-authored-by: prateek105 <[email protected]>
  • Loading branch information
6 people authored Dec 12, 2023
1 parent 1ff4991 commit d4270e9
Show file tree
Hide file tree
Showing 32 changed files with 845 additions and 920 deletions.
2 changes: 1 addition & 1 deletion src/PoolInfoUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ contract PoolInfoUtils {
uint256 npTpRatio;
(t0Debt, collateral_, npTpRatio) = pool.borrowerInfo(borrower_);

t0Np_ = collateral_ == 0 ? 0 : Math.mulDiv(t0Debt, npTpRatio, collateral_);
t0Np_ = collateral_ == 0 ? 0 : Math.mulDiv(Maths.wmul(t0Debt, COLLATERALIZATION_FACTOR), npTpRatio, collateral_);

debt_ = Maths.ceilWmul(t0Debt, pendingInflator);
}
Expand Down
2 changes: 1 addition & 1 deletion src/base/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
Loan memory loan = Loans.getByIndex(loans, loanId_);
return (
loan.borrower,
loan.thresholdPrice
Maths.wmul(loan.thresholdPrice, COLLATERALIZATION_FACTOR)
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/pool/commons/IPoolState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ interface IPoolState {

/**
* @notice Returns information about a loan in the pool.
* @param loanId_ Loan's id within loan heap. Max loan is position `1`.
* @return borrower_ Borrower address at the given position.
* @return thresholdPrice_ Borrower threshold price in pool.
* @param loanId_ Loan's id within loan heap. Max loan is position `1`.
* @return borrower_ Borrower address at the given position.
* @return t0ThresholdPrice_ Borrower t0 threshold price in pool.
*/
function loanInfo(
uint256 loanId_
Expand All @@ -211,7 +211,7 @@ interface IPoolState {
view
returns (
address borrower_,
uint256 thresholdPrice_
uint256 t0ThresholdPrice_
);

/**
Expand Down
3 changes: 2 additions & 1 deletion src/libraries/external/KickerActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ library KickerActions {
// neutral price = Tp * Np to Tp ratio
// neutral price is capped at 50 * max pool price
vars.neutralPrice = Maths.min(
Math.mulDiv(vars.borrowerDebt, vars.borrowerNpTpRatio, vars.borrowerCollateral),
Math.mulDiv(Maths.wmul(vars.borrowerDebt, COLLATERALIZATION_FACTOR),
vars.borrowerNpTpRatio, vars.borrowerCollateral),
MAX_INFLATED_PRICE
);
// check if NP is not less than price at the limit index provided by the kicker - done to prevent frontrunning kick auction call with a large amount of loan
Expand Down
2 changes: 1 addition & 1 deletion tests/forge/interactions/BalancerUniswapExample.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ contract BalancerUniswapTaker {

// take auction from Ajna pool, give USDC, receive WETH
IAjnaPool(decoded.ajnaPool).take(decoded.borrower, decoded.maxAmount, address(this), new bytes(0));
uint256 usdcBalanceAfterTake = 81974358;
uint256 usdcBalanceAfterTake = 81253332;
assert(tokens[0].balanceOf(address(this)) == usdcBalanceAfterTake); // USDC balance after Ajna take
assert(tokens[1].balanceOf(address(this)) == 2000000000000000000); // WETH balance after Ajna take

Expand Down
6 changes: 3 additions & 3 deletions tests/forge/interactions/ERC20TakeWithExternalLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ contract ERC20TakeWithExternalLiquidityTest is Test {
})
);
vm.expectEmit(true, true, false, true);
emit Take(_borrower, 18.025641486856350208 * 1e18, 2.0 * 1e18, 0.201532798513255896 * 1e18, true);
emit Take(_borrower, 18.746667146330604216 * 1e18, 2.0 * 1e18, 0.209594110453786132 * 1e18, true);
taker.take(tokens, amounts, data);

assertGt(usdc.balanceOf(address(this)), 0); // could vary
Expand All @@ -126,7 +126,7 @@ contract ERC20TakeWithExternalLiquidityTest is Test {
// call take using taker contract
bytes memory data = abi.encode(address(_ajnaPool));
vm.expectEmit(true, true, false, true);
emit Take(_borrower, 18.025641486856350208 * 1e18, 2.0 * 1e18, 0.201532798513255896 * 1e18, true);
emit Take(_borrower, 18.746667146330604216 * 1e18, 2.0 * 1e18, 0.209594110453786132 * 1e18, true);
_ajnaPool.take(_borrower, takeAmount, address(taker), data);

// confirm we earned some quote token
Expand All @@ -148,7 +148,7 @@ contract ERC20TakeWithExternalLiquidityTest is Test {
_ajnaPool.take(_borrower, 1_001 * 1e18, _lender1, new bytes(0));

// _lender is has QT deducted from balance
assertEq(usdc.balanceOf(_lender), 119_999.999999926981758794 * 1e18);
assertEq(usdc.balanceOf(_lender), 119_999.999999926981037768 * 1e18);
assertEq(weth.balanceOf(_lender), 0);

// callee, _lender1 receives CT from take
Expand Down
12 changes: 6 additions & 6 deletions tests/forge/interactions/ERC721TakeWithExternalLiquidity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ contract ERC721TakeWithExternalLiquidityTest is ERC721HelperContract {
// call take using taker contract
bytes memory data = abi.encode(address(_pool));
vm.expectEmit(true, true, false, true);
uint256 quoteTokenPaid = 1_082.785034492073132320 * 1e18;
uint256 quoteTokenPaid = 1_126.096435871756057616 * 1e18;
uint256 collateralPurchased = 2 * 1e18;
uint256 bondChange = 12.105904710718649453 * 1e18;
uint256 bondChange = 12.590140899147395432 * 1e18;
emit Take(_borrower, quoteTokenPaid, collateralPurchased, bondChange, true);
_pool.take(_borrower, 2, address(taker), data);

// confirm we earned some quote token
assertEq(_quote.balanceOf(address(taker)), 417.214965507926867680 * 1e18);
assertEq(_quote.balanceOf(address(taker)), 373.903564128243942384 * 1e18);
}

function testTakeNFTCalleeDiffersFromSender() external {
Expand All @@ -110,14 +110,14 @@ contract ERC721TakeWithExternalLiquidityTest is ERC721HelperContract {
changePrank(_lender);
bytes memory data = abi.encode(address(_pool));
vm.expectEmit(true, true, false, true);
uint256 quoteTokenPaid = 1_082.785034492073132320 * 1e18;
uint256 quoteTokenPaid = 1_126.096435871756057616 * 1e18;
uint256 collateralPurchased = 2 * 1e18;
uint256 bondChange = 12.105904710718649453 * 1e18;
uint256 bondChange = 12.590140899147395432 * 1e18;
emit Take(_borrower, quoteTokenPaid, collateralPurchased, bondChange, true);
_pool.take(_borrower, 2, address(taker), data);

// _lender is msg.sender, QT & CT balances post take
assertEq(_quote.balanceOf(_lender), 48_895.437905421641180379 * 1e18);
assertEq(_quote.balanceOf(_lender), 48_852.126504041958255083 * 1e18);
assertEq(_quote.balanceOf(address(taker)), 1_500.0 * 1e18); // QT is increased as NFTTakeExample contract sells the NFT
}
}
18 changes: 9 additions & 9 deletions tests/forge/invariants/base/BasicInvariants.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -241,27 +241,27 @@ abstract contract BasicInvariants is BaseInvariants {
/************************/

function _invariant_L1_L2_L3() internal view {
(address borrower, uint256 tp) = _pool.loanInfo(0);
(address borrower, uint256 t0Tp) = _pool.loanInfo(0);

// first loan in loan heap should be 0
require(borrower == address(0), "Loan Invariant L2");
require(tp == 0, "Loan Invariant L2");
require(t0Tp == 0, "Loan Invariant L2");

( , , uint256 totalLoans) = _pool.loansInfo();

for (uint256 loanId = 1; loanId < totalLoans; loanId++) {
(borrower, tp) = _pool.loanInfo(loanId);
(borrower, t0Tp) = _pool.loanInfo(loanId);

// borrower address and threshold price should not 0
require(borrower != address(0), "Loan Invariant L1");
require(tp != 0, "Loan Invariant L1");
require(t0Tp != 0, "Loan Invariant L1");

// tp of a loan at index 'i' in loan array should be greater than equals to loans at index '2i' and '2i+1'
(, uint256 tp1) = _pool.loanInfo(2 * loanId);
(, uint256 tp2) = _pool.loanInfo(2 * loanId + 1);
// t0Tp of a loan at index 'i' in loan array should be greater than equals to loans at index '2i' and '2i+1'
(, uint256 t0Tp1) = _pool.loanInfo(2 * loanId);
(, uint256 t0Tp2) = _pool.loanInfo(2 * loanId + 1);

require(tp >= tp1, "Loan Invariant L3");
require(tp >= tp2, "Loan Invariant L3");
require(t0Tp >= t0Tp1, "Loan Invariant L3");
require(t0Tp >= t0Tp2, "Loan Invariant L3");
}
}

Expand Down
14 changes: 7 additions & 7 deletions tests/forge/unit/ERC20Pool/ERC20PoolBorrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ contract ERC20PoolBorrowTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: expectedDebt,
borrowerCollateral: 50 * 1e18,
borrowert0Np: 467.406425053964329248 * 1e18,
borrowert0Np: 486.102682056122902418 * 1e18,
borrowerCollateralization: 6.818094832771756370 * 1e18
});

Expand Down Expand Up @@ -378,7 +378,7 @@ contract ERC20PoolBorrowTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: expectedDebt,
borrowerCollateral: 60 * 1e18,
borrowert0Np: 389.505354211636941040 * 1e18,
borrowert0Np: 405.085568380102418682 * 1e18,
borrowerCollateralization: 8.171632970402482385 * 1e18
});
_assertLenderInterest(liquidityAdded, 22.041594239314643404 * 1e18);
Expand Down Expand Up @@ -415,7 +415,7 @@ contract ERC20PoolBorrowTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: expectedDebt,
borrowerCollateral: 50 * 1e18,
borrowert0Np: 467.170821671076429268 * 1e18,
borrowert0Np: 485.857654537919486439 * 1e18,
borrowerCollateralization: 6.800465336646754158 * 1e18
});
_assertLenderInterest(liquidityAdded, 47.030993626904794487 * 1e18);
Expand Down Expand Up @@ -449,7 +449,7 @@ contract ERC20PoolBorrowTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: expectedDebt,
borrowerCollateral: 50 * 1e18,
borrowert0Np: 469.453463878474116266 * 1e18,
borrowert0Np: 488.231602433613080917 * 1e18,
borrowerCollateralization: 6.790328096027958520 * 1e18
});
_assertLenderInterest(liquidityAdded, 74.559258911307822676 * 1e18);
Expand Down Expand Up @@ -480,7 +480,7 @@ contract ERC20PoolBorrowTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: expectedDebt,
borrowerCollateral: 50 * 1e18,
borrowert0Np: 469.453463878474116266 * 1e18,
borrowert0Np: 488.231602433613080917 * 1e18,
borrowerCollateralization: 6.779194583993119727 * 1e18
});
_assertLenderInterest(liquidityAdded, 104.888719493548276695 * 1e18);
Expand Down Expand Up @@ -509,7 +509,7 @@ contract ERC20PoolBorrowTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: expectedDebt,
borrowerCollateral: 50 * 1e18,
borrowert0Np: 469.453463878474116266 * 1e18,
borrowert0Np: 488.231602433613080917 * 1e18,
borrowerCollateralization: 6.766968803727464027 * 1e18
});
}
Expand Down Expand Up @@ -917,7 +917,7 @@ contract ERC20PoolBorrowTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: 500.480769230769231 * 1e18,
borrowerCollateral: 50 * 1e18,
borrowert0Np: 11.128724406046769744 * 1e18,
borrowert0Np: 11.573873382288640533 * 1e18,
borrowerCollateralization: 289.230741805752310899 * 1e18
});

Expand Down
12 changes: 6 additions & 6 deletions tests/forge/unit/ERC20Pool/ERC20PoolCollateral.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ contract ERC20PoolCollateralTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: 21_020.192307692307702000 * 1e18,
borrowerCollateral: 100 * 1e18,
borrowert0Np: 233.703212526982164624 * 1e18,
borrowert0Np: 243.051341028061451209 * 1e18,
borrowerCollateralization: 13.63618966554351274 * 1e18
});

Expand Down Expand Up @@ -146,7 +146,7 @@ contract ERC20PoolCollateralTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: 21_049.006823139002918431 * 1e18,
borrowerCollateral: 50 * 1e18,
borrowert0Np: 467.406425053964329248 * 1e18,
borrowert0Np: 486.102682056122902418 * 1e18,
borrowerCollateralization: 6.808761371077973858 * 1e18
});

Expand Down Expand Up @@ -182,7 +182,7 @@ contract ERC20PoolCollateralTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: 21_049.006823139002918431 * 1e18,
borrowerCollateral: 7.343479566252432930 * 1e18,
borrowert0Np: 3_198.079075140710730815 * 1e18,
borrowert0Np: 3_326.002238146339160048 * 1e18,
borrowerCollateralization: 1 * 1e18
});

Expand Down Expand Up @@ -250,7 +250,7 @@ contract ERC20PoolCollateralTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: 21_020.192307692307702000 * 1e18,
borrowerCollateral: 100 * 1e18,
borrowert0Np: 233.703212526982164624 * 1e18,
borrowert0Np: 243.051341028061451209 * 1e18,
borrowerCollateralization: 13.63618966554351274 * 1e18
});

Expand All @@ -275,7 +275,7 @@ contract ERC20PoolCollateralTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: 21_049.006823139002918431 * 1e18,
borrowerCollateral: 50 * 1e18,
borrowert0Np: 467.406425053964329248 * 1e18,
borrowert0Np: 486.102682056122902418 * 1e18,
borrowerCollateralization: 6.808761371077973858 * 1e18
});

Expand All @@ -297,7 +297,7 @@ contract ERC20PoolCollateralTest is ERC20HelperContract {
borrower: _borrower,
borrowerDebt: 21_049.006823139002918431 * 1e18,
borrowerCollateral: 7.343479566252432930 * 1e18,
borrowert0Np: 3_198.079075140710730815 * 1e18,
borrowert0Np: 3_326.002238146339160048 * 1e18,
borrowerCollateralization: 1 * 1e18
});

Expand Down
Loading

0 comments on commit d4270e9

Please sign in to comment.