📉Auto-Deleveraging

Auto Deleveraging (ADL) is a risk management mechanism used to balance the market when the unrealized profit of positions exceeds a certain threshold compared to the available liquidity pool. This document outlines the process of checking for ADL conditions and executing ADL.


Checking ADL Conditions

To determine if a market can be subject to ADL, you can call the getPnlFactor function in the MarketUtils library:

solidity

function getPnlFactor(  
    MarketId _id,  
    address _market,  
    address _vault,  
    uint256 _indexPrice,  
    uint256 _collateralPrice,  
    uint256 _collateralBaseUnit,  
    bool _isLong  
) public view returns (int256 pnlFactor);

This function calculates the ratio of unrealized PnL to the pool balance.

Parameters:

  • MarketId _id: The ID of the market to check.

  • address _market: The address of the market contract.

  • address _vault: The address of the vault contract.

  • uint256 _indexPrice: The current index price of the asset.

  • uint256 _collateralPrice: The current price of the collateral token.

  • uint256 _collateralBaseUnit: The base unit of the collateral token.

  • bool _isLong: Whether to check long or short positions.

Returns:

  • int256 pnlFactor: The ratio of unrealized PnL to pool balance, represented as a signed integer.

Note: The maximum PnL to pool ratio for all markets is 45% or 0.45e18. If pnlFactor > 0.45e18, then an ADL can be executed.


Finding the Next ADL Target

To determine which position should be targeted next for ADL, use the getNextAdlTarget function in the Position library:

solidity

function getNextAdlTarget(  
    MarketId _id,  
    ITradeStorage tradeStorage,  
    string memory _ticker,  
    uint256 _indexPrice,  
    uint256 _indexBaseUnit,  
    uint256 _totalPoolSizeUsd,  
    bool _isLong  
) external view returns (bytes32 positionKey);

This function calculates the ADL Target Score for each open position and returns the position key with the highest score.

Parameters:

  • MarketId _id: The ID of the market to check for ADL targets.

  • ITradeStorage tradeStorage: The interface to the trade storage contract.

  • string memory _ticker: The ticker symbol of the asset in the market.

  • uint256 _indexPrice: The current index price of the asset to 30 decimal places. E.g $2500 = 2.5e33.

  • uint256 _indexBaseUnit: The base unit of the index price.

  • uint256 _totalPoolSizeUsd: The total size of the liquidity pool in USD to 30 decimal places. e.g 1 USD = 1e30.

  • bool _isLong: Whether to check long or short positions.

Returns:

  • bytes32 positionKey: The key of the position with the highest ADL Target Score.

Note: This function should not be used on-chain due to its looping nature. It's intended for off-chain use to determine the optimal position for ADL.


Executing ADL

Executing an ADL is a two-step process:

Step 1: Request Execution Pricing

Before executing ADL, you must first call the requestExecutionPricing function on the Router smart contract:

function requestExecutionPricing(  
    MarketId _id,  
    bytes32 _key,  
    bool _isPositionKey  
) external payable returns (bytes32 priceRequestKey);

Parameters:

  • MarketId _id: The ID of the market where the ADL will be executed.

  • bytes32 _key: The position key of the position to be deleveraged.

  • bool _isPositionKey: Should be set to true for ADL operations.

Step 2: Execute ADL

After requesting the execution pricing and waiting for a price to be signed, you can proceed to execute the ADL by calling the executeAdl function on the PositionManager contract:

solidity

function executeAdl(  
    MarketId _id,  
    bytes32 _requestKey,  
    bytes32 _positionKey  
) external payable;

Parameters:

  • MarketId _id: The ID of the market where the ADL is being executed.

  • bytes32 _requestKey: The price request key returned from the requestExecutionPricing function.

  • bytes32 _positionKey: The position key of the position to be deleveraged.

Important Notes:

Only the person who requested the pricing for an order should be able to initiate the ADL, up until a certain time buffer.

After that time buffer, any user should be able to initiate the ADL.

The caller needs to call Router.requestExecutionPricing before and provide a valid requestKey.

By following these steps and utilizing the provided functions, you can effectively monitor market conditions for ADL scenarios and execute ADL when necessary to maintain market balance.

Last updated