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

The factor to controls the pace of the increase in the premium the leverage liquidation premium function.

uint256 private constant LEVERAGE_LIQUIDATION_BREAK_EVEN_FACTOR = 5;

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 activeLiquidityScalerInQ72,
uint256 sqrtPriceMinInQ72,
uint256 sqrtPriceMaxInQ72
) internal pure returns (uint256 netDebtInLAssets, uint256 netCollateralInLAssets, bool netDebtX);

checkSaturationPremiums

function checkSaturationPremiums(
ISaturationAndGeometricTWAPState saturationAndGeometricTWAPState,
Validation.InputParams memory inputParams,
address borrower,
uint256 depositLToTransferInLAssets,
uint256 depositXToTransferInXAssets,
uint256 depositYToTransferInYAssets
) external view;

liquidateLeverageCalcDeltaAndPremium

Calculate the amount to be closed (from both deposit and borrow) and premium to be paid. 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 BB and a scaler SS that sets the pace at which the premium increased, in code we call this LEVERAGE_LIQUIDATION_BREAK_EVEN_FACTOR, 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,
bool depositXAndY,
bool repayXAndY
) external pure returns (LeveragedLiquidationParams memory leveragedLiquidationParams);

Parameters

NameTypeDescription
inputParamsValidation.InputParamsThe params representing the position of the borrower.
depositXAndYboolFlag indicating whether the liquidator is taking deposit of X and Y.
repayXAndYboolFlag indicating whether the liquidator is repaying borrow of X and Y.

Returns

NameTypeDescription
leveragedLiquidationParamsLeveragedLiquidationParamsa struct of type LeveragedLiquidationParams containing the amounts to be closed and the premium to be paid.

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 activeLiquidityScalerInQ72,
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

Calculate the maximum premium the liquidator should receive based on the LTV of the borrower. maxPremiumInBips is linear in between the following points

0<=LTV<START_NEGATIVE_PREMIUM_LTV_BIPS=>maxPremiumInBips=0START_NEGATIVE_PREMIUM_LTV_BIPS=LTV=>maxPremiumInBips==0(negativepremium)START_PREMIUM_LTV_BIPS=LTV=>maxPremiumInBips==1(nopremium)0.9=LTV=>maxPremiumInBips==1/0.9(fullpremium)\begin{gather*} 0 <= LTV < START\_NEGATIVE\_PREMIUM\_LTV\_BIPS => maxPremiumInBips = 0 \\ START\_NEGATIVE\_PREMIUM\_LTV\_BIPS = LTV => maxPremiumInBips == 0 \quad (negative premium) \\ START\_PREMIUM\_LTV\_BIPS = LTV => maxPremiumInBips == 1 \quad (no premium) \\ 0.9 = LTV => maxPremiumInBips == 1/0.9 \quad (full premium) \end{gather*}

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.

calcSaturationPremiumBips

Calculate the premium the saturation liquidator is receiving given the borrowers deposit and the depositToTransfer to the liquidator. The end premium is the max of the premiums in L, X, Y If no saturation liq is requested (liquidationParams.saturationDepositLToBeTransferred==liquidationParams.saturationDepositXToBeTransferred==liquidationParams.saturationDepositYToBeTransferred==0), the premium will be 0

function calcSaturationPremiumBips(
Validation.InputParams memory inputParams,
uint256 depositLToTransferInLAssets,
uint256 depositXToTransferInXAssets,
uint256 depositYToTransferInYAssets
) internal pure returns (uint256 premiumInBips);

Parameters

NameTypeDescription
inputParamsValidation.InputParamsThe params containing the position of the borrower.
depositLToTransferInLAssetsuint256
depositXToTransferInXAssetsuint256
depositYToTransferInYAssetsuint256

Returns

NameTypeDescription
premiumInBipsuint256The premium being received by the liquidator.

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();

NotEnoughRepaidForLiquidation

error NotEnoughRepaidForLiquidation();

TooMuchDepositToTransferForLeverageLiquidation

error TooMuchDepositToTransferForLeverageLiquidation();

LiquidationMutation

error LiquidationMutation();

Structs

LeveragedLiquidationParams

struct LeveragedLiquidationParams {
uint256 closeInLAssets;
uint256 closeInXAssets;
uint256 closeInYAssets;
uint256 premiumInLAssets;
uint256 premiumLInXAssets;
uint256 premiumLInYAssets;
bool badDebt;
}