Skip to content

Commit

Permalink
Merge branch 'develop' into remove-rewards-mgr
Browse files Browse the repository at this point in the history
  • Loading branch information
EdNoepel committed Dec 18, 2023
2 parents ca5661e + b826e7f commit c541940
Show file tree
Hide file tree
Showing 76 changed files with 2,048 additions and 1,470 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# (-include to ignore error if it does not exist)
-include .env && source ./tests/forge/invariants/scenarios/scenario-${SCENARIO}.sh

CONTRACT_EXCLUDES="RegressionTest|Panic|RealWorld|Trading"
CONTRACT_EXCLUDES="RegressionTest|Panic|RealWorld|Trading|Rewards"
TEST_EXCLUDES="testLoad|invariant|test_regression"

all: clean install build
Expand Down
66 changes: 44 additions & 22 deletions src/PoolInfoUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
_minDebtAmount,
_priceAt,
_reserveAuctionPrice,
_htp,
MAX_FENWICK_INDEX,
MIN_PRICE,
COLLATERALIZATION_FACTOR
Expand Down Expand Up @@ -56,7 +57,7 @@ contract PoolInfoUtils {
* @return price_ Current price of the auction. (`WAD`)
* @return neutralPrice_ Price at which bond holder is neither rewarded nor penalized. (`WAD`)
* @return referencePrice_ Price used to determine auction start price. (`WAD`)
* @return thresholdPrice_ Threshold Price when liquidation was started. (`WAD`)
* @return debtToCollateral_ Borrower debt to collateral at time of kick. (`WAD`)
* @return bondFactor_ The factor used for calculating bond size. (`WAD`)
*/
function auctionStatus(address ajnaPool_, address borrower_)
Expand All @@ -70,7 +71,7 @@ contract PoolInfoUtils {
uint256 price_,
uint256 neutralPrice_,
uint256 referencePrice_,
uint256 thresholdPrice_,
uint256 debtToCollateral_,
uint256 bondFactor_
)
{
Expand All @@ -81,7 +82,7 @@ contract PoolInfoUtils {
kickTime_,
referencePrice_,
neutralPrice_,
thresholdPrice_, , , ) = IPool(ajnaPool_).auctionInfo(borrower_);
debtToCollateral_, , , ) = IPool(ajnaPool_).auctionInfo(borrower_);

if (kickTime_ != 0) {
(debtToCover_, collateral_, ) = this.borrowerInfo(ajnaPool_, borrower_);
Expand All @@ -97,18 +98,18 @@ contract PoolInfoUtils {
/**
* @notice Returns details of an auction for a given borrower address.
* @dev Calls and returns all values from pool.auctionInfo().
* @param ajnaPool_ Address of `Ajna` pool.
* @param borrower_ Address of the borrower that is liquidated.
* @return kicker_ Address of the kicker that is kicking the auction.
* @return bondFactor_ The factor used for calculating bond size.
* @return bondSize_ The bond amount in quote token terms.
* @return kickTime_ Time the liquidation was initiated.
* @return referencePrice_ Price used to determine auction start price.
* @return neutralPrice_ `Neutral Price` of auction.
* @return thresholdPrice_ Threshold Price when liquidation was started.
* @return head_ Address of the head auction.
* @return next_ Address of the next auction in queue.
* @return prev_ Address of the prev auction in queue.
* @param ajnaPool_ Address of `Ajna` pool.
* @param borrower_ Address of the borrower that is liquidated.
* @return kicker_ Address of the kicker that is kicking the auction.
* @return bondFactor_ The factor used for calculating bond size.
* @return bondSize_ The bond amount in quote token terms.
* @return kickTime_ Time the liquidation was initiated.
* @return referencePrice_ Price used to determine auction start price.
* @return neutralPrice_ `Neutral Price` of auction.
* @return debtToCollateral_ Borrower debt to collateral at time of kick, which is used in BPF for kicker's reward calculation.
* @return head_ Address of the head auction.
* @return next_ Address of the next auction in queue.
* @return prev_ Address of the prev auction in queue.
*/
function auctionInfo(address ajnaPool_, address borrower_) external view returns (
address kicker_,
Expand All @@ -117,7 +118,7 @@ contract PoolInfoUtils {
uint256 kickTime_,
uint256 referencePrice_,
uint256 neutralPrice_,
uint256 thresholdPrice_,
uint256 debtToCollateral_,
address head_,
address next_,
address prev_
Expand Down Expand Up @@ -258,9 +259,18 @@ contract PoolInfoUtils {
hpbIndex_ = pool.depositIndex(1);
hpb_ = _priceAt(hpbIndex_);

(, uint256 maxThresholdPrice,) = pool.loansInfo();
(, uint256 maxT0DebtToCollateral,) = pool.loansInfo();

(
uint256 inflator,
uint256 inflatorUpdate
) = pool.inflatorInfo();

(uint256 interestRate, ) = pool.interestRateInfo();

htp_ = maxThresholdPrice;
uint256 pendingInflator = PoolCommons.pendingInflator(inflator, inflatorUpdate, interestRate);

htp_ = _htp(maxT0DebtToCollateral, pendingInflator);
htpIndex_ = htp_ >= MIN_PRICE ? _indexOf(htp_) : MAX_FENWICK_INDEX;
lupIndex_ = pool.depositIndex(debt);
lup_ = _priceAt(lupIndex_);
Expand All @@ -277,7 +287,7 @@ contract PoolInfoUtils {
(
uint256 bondEscrowed,
uint256 unclaimedReserve,
,
, ,
) = pool.reservesInfo();
uint256 escrowedAmounts = bondEscrowed + unclaimedReserve;

Expand Down Expand Up @@ -313,7 +323,7 @@ contract PoolInfoUtils {

uint256 quoteTokenBalance = IERC20Token(pool.quoteTokenAddress()).balanceOf(ajnaPool_) * pool.quoteTokenScale();

(uint256 bondEscrowed, uint256 unclaimedReserve, uint256 auctionKickTime, ) = pool.reservesInfo();
(uint256 bondEscrowed, uint256 unclaimedReserve, uint256 auctionKickTime, uint256 lastKickedReserves, ) = pool.reservesInfo();

// due to rounding issues, especially in Auction.settle, this can be slighly negative
if (poolDebt + quoteTokenBalance >= poolSize + bondEscrowed + unclaimedReserve) {
Expand All @@ -329,7 +339,7 @@ contract PoolInfoUtils {
);

claimableReservesRemaining_ = unclaimedReserve;
auctionPrice_ = _reserveAuctionPrice(auctionKickTime);
auctionPrice_ = _reserveAuctionPrice(auctionKickTime, lastKickedReserves);
timeRemaining_ = 3 days - Maths.min(3 days, block.timestamp - auctionKickTime);
}

Expand Down Expand Up @@ -463,7 +473,19 @@ contract PoolInfoUtils {
function htp(
address ajnaPool_
) external view returns (uint256 htp_) {
(, htp_, ) = IPool(ajnaPool_).loansInfo();
IPool pool = IPool(ajnaPool_);

(, uint256 maxT0DebtToCollateral,) = pool.loansInfo();

(
uint256 inflator,
uint256 inflatorUpdate
) = pool.inflatorInfo();

(uint256 interestRate, ) = pool.interestRateInfo();
uint256 pendingInflator = PoolCommons.pendingInflator(inflator, inflatorUpdate, interestRate);

htp_ = _htp(maxT0DebtToCollateral, pendingInflator);
}

/**
Expand Down
33 changes: 17 additions & 16 deletions src/base/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import {
import {
COLLATERALIZATION_FACTOR,
_determineInflatorState,
_htp,
_priceAt,
_roundToScale
} from '../libraries/helpers/PoolHelper.sol';
Expand Down Expand Up @@ -204,10 +203,10 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
_revertIfAuctionDebtLocked(deposits, poolState.t0DebtInAuction, fromIndex_, poolState.inflator);

MoveQuoteParams memory moveParams;
moveParams.maxAmountToMove = maxAmount_;
moveParams.fromIndex = fromIndex_;
moveParams.toIndex = toIndex_;
moveParams.thresholdPrice = Loans.getMax(loans).thresholdPrice;
moveParams.maxAmountToMove = maxAmount_;
moveParams.fromIndex = fromIndex_;
moveParams.toIndex = toIndex_;
moveParams.maxT0DebtToCollateral = Loans.getMax(loans).t0DebtToCollateral;

uint256 newLup;
(
Expand Down Expand Up @@ -247,9 +246,9 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
deposits,
poolState,
RemoveQuoteParams({
maxAmount: Maths.min(maxAmount_, _availableQuoteToken()),
index: index_,
thresholdPrice: Loans.getMax(loans).thresholdPrice
maxAmount: Maths.min(maxAmount_, _availableQuoteToken()),
index: index_,
maxT0DebtToCollateral: Loans.getMax(loans).t0DebtToCollateral
})
);

Expand Down Expand Up @@ -396,7 +395,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
* @dev update `reserveAuction.latestBurnEventEpoch` and burn event `timestamp` state
* @dev === Reverts on ===
* @dev 2 weeks not passed `ReserveAuctionTooSoon()`
* @dev unsettled liquidation `AuctionNotCleared()`
* @dev unsettled liquidation `AuctionActive()`
* @dev === Emit events ===
* @dev - `KickReserveAuction`
*/
Expand Down Expand Up @@ -428,7 +427,8 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
uint256 ajnaRequired;
(amount_, ajnaRequired) = TakerActions.takeReserves(
reserveAuction,
maxAmount_
maxAmount_,
_getArgUint256(QUOTE_SCALE)
);

// burn required number of ajna tokens to take quote from reserves
Expand Down Expand Up @@ -559,7 +559,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
emaState,
deposits,
poolState_,
Loans.getMax(loans).thresholdPrice,
Loans.getMax(loans).t0DebtToCollateral,
elapsed
) returns (uint256 newInflator, uint256 newInterest) {
poolState_.inflator = newInflator;
Expand Down Expand Up @@ -739,7 +739,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
uint256 kickTime_,
uint256 referencePrice_,
uint256 neutralPrice_,
uint256 thresholdPrice_,
uint256 debtToCollateral_,
address head_,
address next_,
address prev_
Expand All @@ -752,7 +752,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
liquidation.kickTime,
liquidation.referencePrice,
liquidation.neutralPrice,
liquidation.thresholdPrice,
liquidation.debtToCollateral,
auctions.head,
liquidation.next,
liquidation.prev
Expand Down Expand Up @@ -911,7 +911,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
Loan memory loan = Loans.getByIndex(loans, loanId_);
return (
loan.borrower,
Maths.wmul(loan.thresholdPrice, COLLATERALIZATION_FACTOR)
loan.t0DebtToCollateral
);
}

Expand All @@ -920,7 +920,7 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
Loan memory maxLoan = Loans.getMax(loans);
return (
maxLoan.borrower,
_htp(maxLoan.thresholdPrice, inflatorState.inflator),
maxLoan.t0DebtToCollateral,
Loans.noOfLoans(loans)
);
}
Expand All @@ -931,11 +931,12 @@ abstract contract Pool is Clone, ReentrancyGuard, Multicall, IPool {
}

/// @inheritdoc IPoolState
function reservesInfo() external view override returns (uint256, uint256, uint256, uint256) {
function reservesInfo() external view override returns (uint256, uint256, uint256, uint256, uint256) {
return (
auctions.totalBondEscrowed,
reserveAuction.unclaimed,
reserveAuction.kicked,
reserveAuction.lastKickedReserves,
reserveAuction.totalInterestEarned
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/pool/commons/IPoolErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ interface IPoolErrors {
error TransferToSameOwner();

/**
* @notice The threshold price of the loan to be inserted in loans heap is zero.
* @notice The DebtToCollateral of the loan to be inserted in loans heap is zero.
*/
error ZeroThresholdPrice();
error ZeroDebtToCollateral();

}
14 changes: 7 additions & 7 deletions src/interfaces/pool/commons/IPoolInternals.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ struct AddQuoteParams {

/// @dev Struct used to hold parameters for `LenderAction.moveQuoteToken` action.
struct MoveQuoteParams {
uint256 fromIndex; // the deposit index from where amount is moved
uint256 maxAmountToMove; // [WAD] max amount to move between deposits
uint256 toIndex; // the deposit index where amount is moved to
uint256 thresholdPrice; // [WAD] max threshold price in pool
uint256 fromIndex; // the deposit index from where amount is moved
uint256 maxAmountToMove; // [WAD] max amount to move between deposits
uint256 toIndex; // the deposit index where amount is moved to
uint256 maxT0DebtToCollateral; // [WAD] max t0 debt to collateral in pool
}

/// @dev Struct used to hold parameters for `LenderAction.removeQuoteToken` action.
struct RemoveQuoteParams {
uint256 index; // the deposit index from where amount is removed
uint256 maxAmount; // [WAD] max amount to be removed
uint256 thresholdPrice; // [WAD] max threshold price in pool
uint256 index; // the deposit index from where amount is removed
uint256 maxAmount; // [WAD] max amount to be removed
uint256 maxT0DebtToCollateral; // [WAD] max t0 debt to collateral in pool
}

/*************************************/
Expand Down
Loading

0 comments on commit c541940

Please sign in to comment.