Skip to main content

LiquidityPool.sol

LiquidityPool​

struct LiquidityPool {
mapping(uint16 => struct LiquidityBin) _longBins;
mapping(uint16 => struct LiquidityBin) _shortBins;
}

Represents a collection of long and short liquidity bins

LiquidityPoolLib​

Library for managing liquidity bins in an LiquidityPool

LiquidityBinEarningAccumulated​

event LiquidityBinEarningAccumulated(uint16 feeRate, bytes1 binType, uint256 earning)

Emitted when earning is accumulated for a liquidity bin.

  • Parameters:

    NameTypeDescription
    feeRateuint16The fee rate of the bin.
    binTypebytes1The type of the bin ("L" for long, "S" for short).
    earninguint256The accumulated earning.

_proportionalPositionParamValue​

struct _proportionalPositionParamValue {
int256 qty;
uint256 takerMargin;
}

_validTradingFeeRate​

modifier _validTradingFeeRate(int16 tradingFeeRate)

Modifier to validate the trading fee rate.

  • Parameters:

    NameTypeDescription
    tradingFeeRateint16The trading fee rate to validate.

initialize​

function initialize(struct LiquidityPool self) internal

Initializes the LiquidityPool.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool.

settle​

function settle(struct LiquidityPool self, struct LpContext ctx, int16[] feeRates) internal

Settles the liquidity bins in the LiquidityPool.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool.
    ctxstruct LpContextThe LpContext object.
    feeRatesint16[]The feeRate list of liquidity bin to settle.

settleAll​

function settleAll(struct LiquidityPool self, struct LpContext ctx) internal

Settles the liquidity bins in the LiquidityPool.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool.
    ctxstruct LpContextThe LpContext object.

prepareBinMargins​

function prepareBinMargins(struct LiquidityPool self, struct LpContext ctx, int256 qty, uint256 makerMargin, uint256 minimumBinMargin) internal returns (struct BinMargin[])

Prepares bin margins based on the given quantity and maker margin.

_This function prepares bin margins by performing the following steps:

 1. Calculates the appropriate bin margins
for each trading fee rate based on the provided quantity and maker margin.
2. Iterates through the target bins based on the quantity,
finds the minimum available fee rate,
and determines the upper bound for calculating bin margins.
3. Iterates from the minimum fee rate until the upper bound,
assigning the remaining maker margin to the bins until it is exhausted.
4. Creates an array of BinMargin structs
containing the trading fee rate and corresponding margin amount for each bin.
Throws an error with the code `Errors.NOT_ENOUGH_FREE_LIQUIDITY` if there is not enough free liquidity._
  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool.
    ctxstruct LpContextThe LpContext data struct
    qtyint256The quantity of the position.
    makerMarginuint256The maker margin of the position.
    minimumBinMarginuint256
  • Return Values:

    NameTypeDescription
    [0]struct BinMargin[]binMargins An array of BinMargin representing the calculated bin margins.

acceptOpenPosition​

function acceptOpenPosition(struct LiquidityPool self, struct LpContext ctx, struct Position position) internal

Accepts an open position and opens corresponding liquidity bins.

This function calculates the target liquidity bins based on the position quantity. It prepares the bin margins and divides the position parameters accordingly. Then, it opens the liquidity bins with the corresponding parameters and trading fees.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    ctxstruct LpContextThe LpContext object.
    positionstruct PositionThe Position object representing the open position.

acceptClosePosition​

function acceptClosePosition(struct LiquidityPool self, struct LpContext ctx, struct Position position) internal

Accepts a close position request and closes the corresponding liquidity bins.

This function calculates the target liquidity bins based on the position quantity. It retrieves the maker margin and bin margins from the position. Then, it divides the position parameters to match the bin margins. Finally, it closes the liquidity bins with the provided parameters.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    ctxstruct LpContextThe LpContext object.
    positionstruct PositionThe Position object representing the close position request.

acceptClaimPosition​

function acceptClaimPosition(struct LiquidityPool self, struct LpContext ctx, struct Position position, int256 realizedPnl) internal

Accepts a claim position request and processes the corresponding liquidity bins based on the realized position pnl.

This function verifies if the absolute value of the realized position pnl is within the acceptable margin range. It retrieves the target liquidity bins based on the position quantity and the bin margins from the position. Then, it divides the position parameters to match the bin margins. Depending on the value of the realized position pnl, it either claims the position fully or partially. The claimed pnl is distributed among the liquidity bins according to their respective margins. Throws an error with the code Errors.EXCEED_MARGIN_RANGE if the realized profit or loss does not falls within the acceptable margin range.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    ctxstruct LpContextThe LpContext object.
    positionstruct PositionThe Position object representing the position to claim.
    realizedPnlint256The realized position pnl (taker side).

acceptAddLiquidity​

function acceptAddLiquidity(struct LiquidityPool self, struct LpContext ctx, int16 tradingFeeRate, uint256 amount) internal

Accepts an add liquidity request and processes the liquidity bin corresponding to the given trading fee rate.

This function validates the trading fee rate and calls the acceptAddLiquidity function on the target liquidity bin.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    ctxstruct LpContextThe LpContext object.
    tradingFeeRateint16The trading fee rate associated with the liquidity bin.
    amountuint256The amount of liquidity to add.

acceptClaimLiquidity​

function acceptClaimLiquidity(struct LiquidityPool self, struct LpContext ctx, int16 tradingFeeRate, uint256 amount, uint256 oracleVersion) internal returns (uint256)

Accepts a claim liquidity request and processes the liquidity bin corresponding to the given trading fee rate.

This function validates the trading fee rate and calls the acceptClaimLiquidity function on the target liquidity bin.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    ctxstruct LpContextThe LpContext object.
    tradingFeeRateint16The trading fee rate associated with the liquidity bin.
    amountuint256The amount of liquidity to claim. (should be the same as the one used in acceptAddLiquidity)
    oracleVersionuint256The oracle version used for the claim. (should be the oracle version when call acceptAddLiquidity)
  • Return Values:

    NameTypeDescription
    [0]uint256The amount of liquidity (CLB tokens) received as a result of the liquidity claim.

acceptRemoveLiquidity​

function acceptRemoveLiquidity(struct LiquidityPool self, struct LpContext ctx, int16 tradingFeeRate, uint256 clbTokenAmount) internal

Accepts a remove liquidity request and processes the liquidity bin corresponding to the given trading fee rate.

This function validates the trading fee rate and calls the acceptRemoveLiquidity function on the target liquidity bin.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    ctxstruct LpContextThe LpContext object.
    tradingFeeRateint16The trading fee rate associated with the liquidity bin.
    clbTokenAmountuint256The amount of CLB tokens to remove.

acceptWithdrawLiquidity​

function acceptWithdrawLiquidity(struct LiquidityPool self, struct LpContext ctx, int16 tradingFeeRate, uint256 clbTokenAmount, uint256 oracleVersion) internal returns (uint256 amount, uint256 burnedCLBTokenAmount)

Accepts a withdraw liquidity request and processes the liquidity bin corresponding to the given trading fee rate.

This function validates the trading fee rate and calls the acceptWithdrawLiquidity function on the target liquidity bin.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    ctxstruct LpContextThe LpContext object.
    tradingFeeRateint16The trading fee rate associated with the liquidity bin.
    clbTokenAmountuint256The amount of CLB tokens to withdraw. (should be the same as the one used in acceptRemoveLiquidity)
    oracleVersionuint256The oracle version used for the withdrawal. (should be the oracle version when call acceptRemoveLiquidity)
  • Return Values:

    NameTypeDescription
    amountuint256The amount of base tokens withdrawn
    burnedCLBTokenAmountuint256the amount of CLB tokens burned.

getBinLiquidity​

function getBinLiquidity(struct LiquidityPool self, int16 tradingFeeRate) internal view returns (uint256 amount)

Retrieves the total liquidity amount in base tokens for the specified trading fee rate.

This function retrieves the liquidity bin based on the trading fee rate and calls the liquidity function on it.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    tradingFeeRateint16The trading fee rate associated with the liquidity bin.
  • Return Values:

    NameTypeDescription
    amountuint256The total liquidity amount in base tokens.

getBinFreeLiquidity​

function getBinFreeLiquidity(struct LiquidityPool self, int16 tradingFeeRate) internal view returns (uint256 amount)

Retrieves the free liquidity amount in base tokens for the specified trading fee rate.

This function retrieves the liquidity bin based on the trading fee rate and calls the freeLiquidity function on it.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    tradingFeeRateint16The trading fee rate associated with the liquidity bin.
  • Return Values:

    NameTypeDescription
    amountuint256The free liquidity amount in base tokens.

distributeEarning​

function distributeEarning(struct LiquidityPool self, uint256 earning, uint256 marketBalance) internal

Distributes earnings among the liquidity bins.

This function distributes the earnings among the liquidity bins, proportional to their total balances. It iterates through the trading fee rates and distributes the proportional amount of earnings to each bin based on its total balance relative to the market balance.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe LiquidityPool storage.
    earninguint256The total earnings to be distributed.
    marketBalanceuint256The market balance.

binValue​

function binValue(struct LiquidityPool self, struct LpContext ctx, int16 _tradingFeeRate) internal view returns (uint256 value)

Retrieves the value of a specific bin in the LiquidityPool storage for the provided trading fee rate.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool storage.
    ctxstruct LpContextThe LP context containing relevant information for the calculation.
    _tradingFeeRateint16The trading fee rate for which to calculate the bin value.
  • Return Values:

    NameTypeDescription
    valueuint256The value of the specified bin.

pendingLiquidity​

function pendingLiquidity(struct LiquidityPool self, int16 tradingFeeRate) internal view returns (struct PendingLiquidity)

Retrieves the pending liquidity information for a specific trading fee rate from a LiquidityPool.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool struct.
    tradingFeeRateint16The trading fee rate for which to retrieve the pending liquidity.
  • Return Values:

    NameTypeDescription
    [0]struct PendingLiquiditypendingLiquidity An instance of PendingLiquidity representing the pending liquidity information.

claimableLiquidity​

function claimableLiquidity(struct LiquidityPool self, int16 tradingFeeRate, uint256 oracleVersion) internal view returns (struct ClaimableLiquidity)

Retrieves the claimable liquidity information for a specific trading fee rate and oracle version from a LiquidityPool.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool struct.
    tradingFeeRateint16The trading fee rate for which to retrieve the claimable liquidity.
    oracleVersionuint256The oracle version for which to retrieve the claimable liquidity.
  • Return Values:

    NameTypeDescription
    [0]struct ClaimableLiquidityclaimableLiquidity An instance of ClaimableLiquidity representing the claimable liquidity information.

liquidityBinStatuses​

function liquidityBinStatuses(struct LiquidityPool self, struct LpContext ctx) internal view returns (struct LiquidityBinStatus[])

Retrieves the liquidity bin statuses for the LiquidityPool using the provided context.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe LiquidityPool storage instance.
    ctxstruct LpContextThe LpContext containing the necessary context for calculating the bin statuses.
  • Return Values:

    NameTypeDescription
    [0]struct LiquidityBinStatus[]stats An array of LiquidityBinStatus representing the liquidity bin statuses.

pendingPosition​

function pendingPosition(struct LiquidityPool self, int16 tradingFeeRate) internal view returns (struct PendingPosition)

Retrieves the pending position information for a specific trading fee rate from a LiquidityPool.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool struct.
    tradingFeeRateint16The trading fee rate for which to retrieve the pending position.
  • Return Values:

    NameTypeDescription
    [0]struct PendingPositionpendingPositin An instance of PendingPositin representing the pending position information.

closingPosition​

function closingPosition(struct LiquidityPool self, int16 tradingFeeRate) internal view returns (struct ClosingPosition)

Retrieves the closing position information for a specific trading fee rate from a LiquidityPool.

  • Parameters:

    NameTypeDescription
    selfstruct LiquidityPoolThe reference to the LiquidityPool struct.
    tradingFeeRateint16The trading fee rate for which to retrieve the pending position.
  • Return Values:

    NameTypeDescription
    [0]struct ClosingPositionclosingPositin An instance of ClosingPositin representing the closing position information.