Skip to main content

Liquidation

Git Source

State Variables

START_NEGATIVE_PREMIUM_LTV_BIPS

uint256 internal constant START_NEGATIVE_PREMIUM_LTV_BIPS = 6000;

START_PREMIUM_LTV_BIPS

uint256 private constant START_PREMIUM_LTV_BIPS = 7500;

NEGATIVE_PREMIUM_SLOPE_IN_BIPS

uint256 private constant NEGATIVE_PREMIUM_SLOPE_IN_BIPS = 66_667;

NEGATIVE_PREMIUM_INTERCEPT_IN_BIPS

uint256 private constant NEGATIVE_PREMIUM_INTERCEPT_IN_BIPS = 40_000;

POSITIVE_PREMIUM_SLOPE_IN_BIPS

uint256 private constant POSITIVE_PREMIUM_SLOPE_IN_BIPS = 7408;

POSITIVE_PREMIUM_INTERCEPT_IN_BIPS

uint256 private constant POSITIVE_PREMIUM_INTERCEPT_IN_BIPS = 4444;

LEVERAGE_LIQUIDATION_BREAK_EVEN_FACTOR_MAG2

This factor brings a leveraged position just below the leverage liquidation threshold

uint256 internal constant LEVERAGE_LIQUIDATION_BREAK_EVEN_FACTOR_MAG2 = 10;

LEVERAGE_LIQUIDATION_FACTOR

Leverage liquidation factor derived from max allowed leverage scaled by the break-even factor

uint256 internal constant LEVERAGE_LIQUIDATION_FACTOR =
ALLOWED_LIQUIDITY_LEVERAGE * MAG2 / LEVERAGE_LIQUIDATION_BREAK_EVEN_FACTOR_MAG2;

MAX_PREMIUM_IN_BIPS

uint256 internal constant MAX_PREMIUM_IN_BIPS = 11_111;

HARD

uint256 internal constant HARD = 0;

SATURATION

uint256 internal constant SATURATION = 1;

LEVERAGE

uint256 internal constant LEVERAGE = 2;

Functions

verifyHardLiquidation

function verifyHardLiquidation(
ISaturationAndGeometricTWAPState saturationState,
address pairAddress,
Validation.InputParams memory inputParams,
uint256[6] memory proposedLiquidation,
address borrower
) external view returns (uint256[6] memory partialLiquidation, bool badDebt);

checkHardPremiums

function checkHardPremiums(
uint256 repaidDebtInL,
uint256 seizedCollateralValueInL,
uint256 maxAllowedPremiumBips
) internal pure;

calculateNetDebtAndSeizedDeposits

function calculateNetDebtAndSeizedDeposits(
uint256[6] memory proposedLiquidation,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72
) internal pure returns (uint256 netDebtInLAssets, uint256 netCollateralInLAssets, bool netDebtX);

checkSaturationPremiums

function checkSaturationPremiums(
ISaturationAndGeometricTWAPState saturationAndGeometricTWAPState,
Validation.InputParams memory inputParams,
address borrower
) external view returns (uint256 seizeLAssets, uint256 seizeXAssets, uint256 seizeYAssets);

liquidateLeverageCalcDeltaAndPremium

Calculate weighted leverage liquidation repayments and seized deposits.

*The first three return indices are seized deposits, the last three are required repayments, and all values are in each token's native units. The formula for the premium is calculated with the average net borrow of X and Y BB and the net deposit of X and Y DD and a scaler SS that sets the pace at which the premium increased, in code we call this LEVERAGE_LIQUIDATION_BREAK_EVEN_FACTOR_MAG2, and allowed leverage ALAL, ALLOWED_LIQUIDITY_LEVERAGE:

premium={S(BDAL1AL) if BD>AL1AL0 otherwise premium = \begin{cases} S \left( \frac{B}{D} - \frac{AL - 1}{AL} \right) \text { if } \frac{B}{D} > \frac{AL - 1}{AL} \\ 0 \text { otherwise } \end{cases}

This can be visualized here. The premium is a percentage of the total deposit. If the premium is low enough, then we we attempt to deleverage the position such that the premium and closed part of the position leaves it under the leveraged threshold. If this is not possible, then all of the users deposit will be transferred to the liquidator and there will be bad debt. Note that the de leveraging relies on the min and max tick to be equal, so the result may not be a valid amount of leverage using a min and max price as is done in the Validation library.*

function liquidateLeverageCalcDeltaAndPremium(
Validation.InputParams memory inputParams
) external pure returns (uint256[6] memory leveragedLiquidationParams, bool badDebt);

Parameters

NameTypeDescription
inputParamsValidation.InputParamsThe params representing the position of the borrower.

Returns

NameTypeDescription
leveragedLiquidationParamsuint256[6]Array indexed by DEPOSIT_L, DEPOSIT_X, DEPOSIT_Y, BORROW_L, BORROW_X, BORROW_Y.
badDebtboolWhether the leverage liquidation leaves bad debt to burn.

calculateLeverageLiquidationAsset

Calculate the amount of an asset to be liquidated in a leverage liquidation.

we use the min so amounts don't exceed balances

function calculateLeverageLiquidationAsset(
uint256 userAsset,
uint256 totalShareLAsset,
uint256 totalLAsset,
bool rounding
) internal pure returns (uint256);

Parameters

NameTypeDescription
userAssetuint256The amount of the asset held by the user.
totalShareLAssetuint256The portion getting seized or repaid in L assets, or fraction of the total borrowed l or deposited l respectively.
totalLAssetuint256The total borrowed l or deposited l respectively.
roundingboolWhether to round up the result, we round up for the borrow legs to make sure enough is repaid to cover the seized deposits.

calcHardMaxPremiumInBips

Calculate the maximum premium the liquidator may receive given the LTV of the borrower.

We min the result to favor the borrower.

function calcHardMaxPremiumInBips(
uint256[6] memory validatedLiquidation,
uint256 activeLiquidityAssets,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72
) internal pure returns (uint256 maxPremiumInBips);

Returns

NameTypeDescription
maxPremiumInBipsuint256The max premium allowed to be received by the liquidator.

calcHardPremiumInBips

Calculate the premium being afforded to the liquidator given the repay and depositToTransfer amounts.

We use prices to maximize the premiumInBips to favor the borrower

function calcHardPremiumInBips(
uint256 repaidDebtInL,
uint256 seizedCollateralValueInL
) internal pure returns (uint256 premiumInBips);

Parameters

NameTypeDescription
repaidDebtInLuint256The amount of debt being repaid in L assets.
seizedCollateralValueInLuint256The value of the collateral being seized in L assets.

Returns

NameTypeDescription
premiumInBipsuint256The premium being received by the liquidator.

convertLtvToPremium

Calculates the maximum premium the liquidator should receive based on borrower LTV. ltvBips and the returned premium are both denominated in bips. The premium is flat below the negative-premium threshold, then follows the two linear segments used by the implementation:

P(ltv)={0ltvSTART_NEGATIVE_PREMIUM_LTV_BIPSNEGATIVE_PREMIUM_SLOPE_IN_BIPSltvBIPSNEGATIVE_PREMIUM_INTERCEPT_IN_BIPSSTART_NEGATIVE_PREMIUM_LTV_BIPS<ltv<START_PREMIUM_LTV_BIPSPOSITIVE_PREMIUM_SLOPE_IN_BIPSltvBIPS+POSITIVE_PREMIUM_INTERCEPT_IN_BIPSSTART_PREMIUM_LTV_BIPSltvP(ltv)= \begin{cases} 0 & ltv \le START\_NEGATIVE\_PREMIUM\_LTV\_BIPS \\ \left\lfloor\frac{NEGATIVE\_PREMIUM\_SLOPE\_IN\_BIPS\cdot ltv}{BIPS}\right\rfloor - NEGATIVE\_PREMIUM\_INTERCEPT\_IN\_BIPS & START\_NEGATIVE\_PREMIUM\_LTV\_BIPS < ltv < START\_PREMIUM\_LTV\_BIPS \\ \left\lfloor\frac{POSITIVE\_PREMIUM\_SLOPE\_IN\_BIPS\cdot ltv}{BIPS}\right\rfloor + POSITIVE\_PREMIUM\_INTERCEPT\_IN\_BIPS & START\_PREMIUM\_LTV\_BIPS \le ltv \end{cases}

internal for testing only

function convertLtvToPremium(
uint256 ltvBips
) internal pure returns (uint256 maxPremiumInBips);

Parameters

NameTypeDescription
ltvBipsuint256LTV of the borrower.

Returns

NameTypeDescription
maxPremiumInBipsuint256The maximum premium for the liquidator.

calcSaturationSeizedAssets

function calcSaturationSeizedAssets(
uint256 depositedLAssets,
uint256 depositedXAssets,
uint256 depositedYAssets,
uint256 premiumInBips
) internal pure returns (uint256 seizedLAssets, uint256 seizedXAssets, uint256 seizedYAssets);

calcSaturationMaxPremiumInBips

Calculate the max premium the saturation liquidator can receive given position of account.

function calcSaturationMaxPremiumInBips(
ISaturationAndGeometricTWAPState saturationAndGeometricTWAPState,
Validation.InputParams memory inputParams,
address account
) internal view returns (uint256 maxPremiumBips);

Parameters

NameTypeDescription
saturationAndGeometricTWAPStateISaturationAndGeometricTWAPStateThe contract containing the saturation state.
inputParamsValidation.InputParamsThe params containing the position of account.
accountaddressThe account of the borrower.

Returns

NameTypeDescription
maxPremiumBipsuint256The max premium for the liquidator.

Errors

LiquidationPremiumTooHigh

error LiquidationPremiumTooHigh();

LiquidationZeroPremium

error LiquidationZeroPremium();

NotEnoughRepaidForLiquidation

error NotEnoughRepaidForLiquidation();

TooMuchDepositToTransferForLeverageLiquidation

error TooMuchDepositToTransferForLeverageLiquidation();

LiquidationMutation

error LiquidationMutation();