Skip to content

Commit

Permalink
Memorialize borrower threshold price at time of kick to calculate bpf…
Browse files Browse the repository at this point in the history
… on take
  • Loading branch information
prateek105 committed Nov 10, 2023
1 parent 50aa42f commit 020fa96
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/interfaces/pool/commons/IPoolState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,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
}

/// @dev Struct holding kicker state.
Expand Down
4 changes: 3 additions & 1 deletion src/libraries/external/KickerActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import {
MAX_INFLATED_PRICE,
_bondParams,
_bpf,
_claimableReserves,
_isCollateralized,
_priceAt,
Expand Down Expand Up @@ -334,6 +333,9 @@ library KickerActions {
vars.neutralPrice
);

// store borrower Threshold price
liquidation.thresholdPrice = Maths.wdiv(vars.borrowerDebt, vars.borrowerCollateral);

// update escrowed bonds balances and get the difference needed to cover bond (after using any kick claimable funds if any)
kickResult_.amountToCoverBond = _updateEscrowedBonds(auctions_, vars.bondSize);

Expand Down
7 changes: 1 addition & 6 deletions src/libraries/external/TakerActions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ library TakerActions {
vars_ = _prepareTake(
liquidation,
borrower_.t0Debt,
borrower_.collateral,
params_.inflator
);

Expand Down Expand Up @@ -428,7 +427,6 @@ library TakerActions {
vars_= _prepareTake(
liquidation,
borrower_.t0Debt,
borrower_.collateral,
params_.inflator
);

Expand Down Expand Up @@ -681,14 +679,12 @@ library TakerActions {
* - loan is not in auction NoAuction()
* @param liquidation_ Liquidation struct holding auction details.
* @param t0Debt_ Borrower t0 debt.
* @param collateral_ Borrower collateral.
* @param inflator_ The pool's inflator, used to calculate borrower debt.
* @return vars The prepared vars for take action.
*/
function _prepareTake(
Liquidation memory liquidation_,
uint256 t0Debt_,
uint256 collateral_,
uint256 inflator_
) internal view returns (TakeLocalVars memory vars) {

Expand All @@ -704,8 +700,7 @@ library TakerActions {
vars.auctionPrice = _auctionPrice(liquidation_.referencePrice, kickTime);
vars.bondFactor = liquidation_.bondFactor;
vars.bpf = _bpf(
vars.borrowerDebt,
collateral_,
liquidation_.thresholdPrice,
neutralPrice,
liquidation_.bondFactor,
vars.auctionPrice
Expand Down
20 changes: 8 additions & 12 deletions src/libraries/helpers/PoolHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -372,32 +372,28 @@ import { Maths } from '../internal/Maths.sol';
/**
* @notice Calculates bond penalty factor.
* @dev Called in kick and take.
* @param debt_ Borrower debt.
* @param collateral_ Borrower collateral.
* @param neutralPrice_ `NP` of auction.
* @param bondFactor_ Factor used to determine bondSize.
* @param auctionPrice_ Auction price at the time of call.
* @return bpf_ Factor used in determining bond `reward` (positive) or `penalty` (negative).
* @param thresholdPrice_ Borrower tp at time of kick.
* @param neutralPrice_ `NP` of auction.
* @param bondFactor_ Factor used to determine bondSize.
* @param auctionPrice_ Auction price at the time of call.
* @return bpf_ Factor used in determining bond `reward` (positive) or `penalty` (negative).
*/
function _bpf(
uint256 debt_,
uint256 collateral_,
uint256 thresholdPrice_,
uint256 neutralPrice_,
uint256 bondFactor_,
uint256 auctionPrice_
) pure returns (int256) {
int256 thresholdPrice = int256(Maths.wdiv(debt_, collateral_));

int256 sign;
if (thresholdPrice < int256(neutralPrice_)) {
if (int256(thresholdPrice_) < int256(neutralPrice_)) {
// BPF = BondFactor * min(1, max(-1, (neutralPrice - price) / (neutralPrice - thresholdPrice)))
sign = Maths.minInt(
1e18,
Maths.maxInt(
-1 * 1e18,
PRBMathSD59x18.div(
int256(neutralPrice_) - int256(auctionPrice_),
int256(neutralPrice_) - thresholdPrice
int256(neutralPrice_) - int256(thresholdPrice_)
)
)
);
Expand Down
13 changes: 7 additions & 6 deletions tests/forge/unit/Auctions.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ contract AuctionsTest is DSTestPlus {
uint256 collateral = 1000 * 1e18;
uint256 neutralPrice = 15 * 1e18;
uint256 bondFactor = 0.1 * 1e18;
uint256 thresholdPrice = Maths.wdiv(debt, collateral);

assertEq(_bpf(debt, collateral, neutralPrice, bondFactor, price), 0.1 * 1e18);
assertEq(_bpf(9000 * 1e18, collateral, neutralPrice, bondFactor, price), 0.083333333333333333 * 1e18);
assertEq(_bpf(debt, collateral, neutralPrice, bondFactor, 9.5 * 1e18), 0.1 * 1e18);
assertEq(_bpf(9000 * 1e18, collateral, neutralPrice, bondFactor, 9.5 * 1e18), 0.091666666666666667 * 1e18);
assertEq(_bpf(9000 * 1e18, collateral, 10 * 1e18, bondFactor, 10.5 * 1e18), -0.05 * 1e18);
assertEq(_bpf(debt, collateral, 5 * 1e18, bondFactor, 10.5 * 1e18), -0.1 * 1e18);
assertEq(_bpf(Maths.wdiv(debt, collateral), neutralPrice, bondFactor, price), 0.1 * 1e18);
assertEq(_bpf(Maths.wdiv(9000 * 1e18, collateral), neutralPrice, bondFactor, price), 0.083333333333333333 * 1e18);
assertEq(_bpf(Maths.wdiv(debt, collateral), neutralPrice, bondFactor, 9.5 * 1e18), 0.1 * 1e18);
assertEq(_bpf(Maths.wdiv(9000 * 1e18, collateral), neutralPrice, bondFactor, 9.5 * 1e18), 0.091666666666666667 * 1e18);
assertEq(_bpf(Maths.wdiv(9000 * 1e18, collateral), 10 * 1e18, bondFactor, 10.5 * 1e18), -0.05 * 1e18);
assertEq(_bpf(Maths.wdiv(debt, collateral), 5 * 1e18, bondFactor, 10.5 * 1e18), -0.1 * 1e18);
}

/**
Expand Down

0 comments on commit 020fa96

Please sign in to comment.