Skip to content

Commit

Permalink
Merge branch 'develop' into merge-rc9-feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek105 committed Dec 12, 2023
2 parents 85ecbe9 + 912b1f6 commit 088431e
Show file tree
Hide file tree
Showing 34 changed files with 901 additions and 937 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
11 changes: 6 additions & 5 deletions src/base/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ import {
_revertIfAuctionDebtLocked,
_revertIfAuctionClearable,
_revertAfterExpiry,
_revertIfAuctionPriceBelow
_revertIfAuctionPriceBelow,
_revertIfActiveAuctions
} from '../libraries/helpers/RevertsHelper.sol';

import { Buckets } from '../libraries/internal/Buckets.sol';
Expand Down Expand Up @@ -160,7 +161,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {

_revertIfAuctionClearable(auctions, loans);

_revertIfAuctionPriceBelow(index_, auctions);
_revertIfAuctionPriceBelow(auctions, index_);

PoolState memory poolState = _accruePoolInterest();

Expand Down Expand Up @@ -196,7 +197,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {

_revertIfAuctionClearable(auctions, loans);

_revertIfAuctionPriceBelow(toIndex_, auctions);
_revertIfAuctionPriceBelow(auctions, toIndex_);

PoolState memory poolState = _accruePoolInterest();

Expand Down Expand Up @@ -400,7 +401,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
* @dev - `KickReserveAuction`
*/
function kickReserveAuction() external override nonReentrant {
_revertIfAuctionClearable(auctions, loans);
_revertIfActiveAuctions(auctions);

// start a new claimable reserve auction, passing in relevant parameters such as the current pool size, debt, balance, and inflator value
KickerActions.kickReserveAuction(
Expand Down Expand Up @@ -910,7 +911,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
16 changes: 8 additions & 8 deletions src/libraries/external/TakerActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,6 @@ library TakerActions {

_rewardTake(auctions_, liquidation, vars_);

emit Take(
params_.borrower,
vars_.quoteTokenAmount,
vars_.collateralAmount,
vars_.bondChange,
vars_.isRewarded
);

if (params_.poolType == uint8(PoolType.ERC721)) {
// slither-disable-next-line divide-before-multiply
uint256 collateralTaken = (vars_.collateralAmount / 1e18) * 1e18; // solidity rounds down, so if 2.5 it will be 2.5 / 1 = 2
Expand All @@ -397,6 +389,14 @@ library TakerActions {
}
}
}

emit Take(
params_.borrower,
vars_.quoteTokenAmount,
vars_.collateralAmount,
vars_.bondChange,
vars_.isRewarded
);
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/libraries/helpers/RevertsHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ import { Maths } from '../internal/Maths.sol';
if (newPrice_ < _priceAt(limitIndex_)) revert LimitIndexExceeded();
}

/**
/**
* @notice Check if provided price is above current auction price.
* @notice Prevents manipulative deposits and arbTakes.
* @dev Reverts with `AddAboveAuctionPrice` if price is above head of auction queue.
* @param index_ Identifies bucket price to be compared with current auction price.
* @param auctions_ Auctions data.
* @param index_ Identifies bucket price to be compared with current auction price.
*/
function _revertIfAuctionPriceBelow(
uint256 index_,
AuctionsState storage auctions_
AuctionsState storage auctions_,
uint256 index_
) view {
address head = auctions_.head;
if (head != address(0)) {
Expand All @@ -94,6 +94,18 @@ import { Maths } from '../internal/Maths.sol';
}
}

/**
* @notice Check if there are still active / non settled auctions in pool.
* @notice Prevents kicking reserves auctions until all pending auctions are fully settled.
* @dev Reverts with `AuctionNotCleared`.
* @param auctions_ Auctions data.
*/
function _revertIfActiveAuctions(
AuctionsState storage auctions_
) view {
if (auctions_.noOfAuctions != 0) revert AuctionNotCleared();
}

/**
* @notice Check if expiration provided by user has met or exceeded current block height timestamp.
* @notice Prevents stale transactions interacting with the pool at potentially unfavorable prices.
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 088431e

Please sign in to comment.