Skip to content

Commit

Permalink
Continuation of #1021 - clear TP naming and improved code descriptions (
Browse files Browse the repository at this point in the history
#1027)

* Continuation of #1021 - better naming and improved code descriptions
- change Loan.thresholdPrice and Liquidation.thresholdPrice to unadjustedThresholdPrice naming in order to reflect Collateral factor not applied to these values
- reflect in var names where max unadjusted threshold price is used (Move/Remove param struct)
- natspec update

- rename MAX_NP_TP_RATIO to MAX_BOND_FACTOR

* clear Tp naming
        - t0DebtToCollateral : ( t0Debt / collateral )
        - debtToCollateral :   ( debt / collateral )
        - t0ThresholdPrice :   ( t0Debt / collateral ) * collateralization factor
        - thresholdPrice :     ( debt / collateral ) * collateralization factor

* Fix HeapTest

* Update LoansInfo and LoanInfo methods to return maxT0DebtToCollateral and t0DebtToCollateral

* Fix unit tests

* Update assertLoans in tests and fix htp in BaseHandler

* Update loansInfo and threshold Price in Loans library

* Rename thresholdPrice to debtToCollateral in assertAuction in tests

* updated zerothresholdprice error to zerodebttocollateral

* removed unused _htp import in pool.sol

* updated invariants to include ZeroDebtToCollateral

* Use SafeCast in Loans library

---------

Co-authored-by: prateek105 <[email protected]>
Co-authored-by: Ian Harvey <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2023
1 parent 19b0c3f commit 1736ccf
Show file tree
Hide file tree
Showing 40 changed files with 600 additions and 578 deletions.
60 changes: 41 additions & 19 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 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
25 changes: 12 additions & 13 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 @@ -559,7 +558,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 +738,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 +751,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 +910,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 +919,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 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
46 changes: 23 additions & 23 deletions src/interfaces/pool/commons/IPoolState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ interface IPoolState {

/**
* @notice Returns details of an auction for a given borrower address.
* @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 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, 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 borrower_)
external
Expand All @@ -31,7 +31,7 @@ interface IPoolState {
uint256 kickTime_,
uint256 referencePrice_,
uint256 neutralPrice_,
uint256 thresholdPrice_,
uint256 debtToCollateral_,
address head_,
address next_,
address prev_
Expand Down 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 t0ThresholdPrice_ Borrower t0 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 t0DebtToCollateral_ Borrower t0 debt to collateral.
*/
function loanInfo(
uint256 loanId_
Expand All @@ -211,21 +211,21 @@ interface IPoolState {
view
returns (
address borrower_,
uint256 t0ThresholdPrice_
uint256 t0DebtToCollateral_
);

/**
* @notice Returns information about pool loans.
* @return maxBorrower_ Borrower address with highest threshold price.
* @return maxThresholdPrice_ Highest threshold price in pool.
* @return noOfLoans_ Total number of loans.
* @return maxBorrower_ Borrower address with highest t0 debt to collateral.
* @return maxT0DebtToCollateral_ Highest t0 debt to collateral in pool.
* @return noOfLoans_ Total number of loans.
*/
function loansInfo()
external
view
returns (
address maxBorrower_,
uint256 maxThresholdPrice_,
uint256 maxT0DebtToCollateral_,
uint256 noOfLoans_
);

Expand Down Expand Up @@ -380,8 +380,8 @@ struct LoansState {

/// @dev Struct holding loan state.
struct Loan {
address borrower; // borrower address
uint96 thresholdPrice; // [WAD] Loan's threshold price.
address borrower; // borrower address
uint96 t0DebtToCollateral; // [WAD] Borrower t0 debt to collateral.
}

/// @dev Struct holding borrower state.
Expand Down Expand Up @@ -415,7 +415,7 @@ struct Liquidation {
address next; // next liquidated borrower in auctions queue
uint160 bondSize; // [WAD] liquidation bond size
uint96 neutralPrice; // [WAD] Neutral Price when liquidation was started
uint256 thresholdPrice; // [WAD] Threshold Price when liquidation was started
uint256 debtToCollateral; // [WAD] Borrower debt to collateral, which is used in BPF for kicker's reward calculation
uint256 t0ReserveSettleAmount; // [WAD] Amount of t0Debt that could be settled via reserves in this auction
}

Expand Down
16 changes: 8 additions & 8 deletions src/libraries/external/KickerActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ library KickerActions {
uint256 t0ReserveSettleAmount; // [WAD] Amount of t0Debt that could be settled via reserves in an auction
uint256 borrowerNpTpRatio; // [WAD] borrower NP to TP ratio
uint256 neutralPrice; // [WAD] neutral price recorded in kick action
uint256 htp; // [WAD] highest threshold price in pool
uint256 htp; // [WAD] highest threshold price (including the collateralization factor) in pool
uint256 referencePrice; // [WAD] used to calculate auction start price
uint256 bondFactor; // [WAD] bond factor of kicked auction
uint256 bondSize; // [WAD] bond size of kicked auction
uint256 thresholdPrice; // [WAD] borrower threshold price at kick time
uint256 debtToCollateral; // [WAD] borrower debt to collateral at kick time
}

/// @dev Struct used for `lenderKick` function local vars.
Expand Down Expand Up @@ -346,15 +346,15 @@ library KickerActions {
// which will make it harder for kicker to earn a reward and more likely that the kicker is penalized
_revertIfPriceDroppedBelowLimit(vars.neutralPrice, limitIndex_);

vars.htp = _htp(Loans.getMax(loans_).thresholdPrice, poolState_.inflator);
vars.htp = _htp(Loans.getMax(loans_).t0DebtToCollateral, poolState_.inflator);
vars.referencePrice = Maths.min(Maths.max(vars.htp, vars.neutralPrice), MAX_INFLATED_PRICE);

(vars.bondFactor, vars.bondSize) = _bondParams(
vars.borrowerDebt,
vars.borrowerNpTpRatio
);

vars.thresholdPrice = Maths.wdiv(vars.borrowerDebt, vars.borrowerCollateral);
vars.debtToCollateral = Maths.wdiv(vars.borrowerDebt, vars.borrowerCollateral);

// record liquidation info
_recordAuction(
Expand All @@ -365,7 +365,7 @@ library KickerActions {
vars.bondFactor,
vars.referencePrice,
vars.neutralPrice,
vars.thresholdPrice,
vars.debtToCollateral,
vars.t0ReserveSettleAmount
);

Expand Down Expand Up @@ -427,7 +427,7 @@ library KickerActions {
* @param bondFactor_ Bond factor of the newly kicked auction.
* @param referencePrice_ Used to calculate auction start price.
* @param neutralPrice_ Current pool `Neutral Price`.
* @param thresholdPrice_ Borrower threshold price.
* @param debtToCollateral_ Borrower debt to collateral at time of kick.
* @param t0ReserveSettleAmount_ Amount of t0Debt that could be settled via reserves in auction
*/
function _recordAuction(
Expand All @@ -438,7 +438,7 @@ library KickerActions {
uint256 bondFactor_,
uint256 referencePrice_,
uint256 neutralPrice_,
uint256 thresholdPrice_,
uint256 debtToCollateral_,
uint256 t0ReserveSettleAmount_
) internal {
// record liquidation info
Expand All @@ -447,7 +447,7 @@ library KickerActions {
liquidation_.bondSize = SafeCast.toUint160(bondSize_);
liquidation_.bondFactor = SafeCast.toUint96(bondFactor_);
liquidation_.neutralPrice = SafeCast.toUint96(neutralPrice_);
liquidation_.thresholdPrice = thresholdPrice_;
liquidation_.debtToCollateral = debtToCollateral_;
liquidation_.t0ReserveSettleAmount = t0ReserveSettleAmount_;

// increment number of active auctions
Expand Down
Loading

0 comments on commit 1736ccf

Please sign in to comment.