Skip to main content

OrderBook

InternalAddLiquidityToOrderParams

  struct InternalAddLiquidityToOrderParams(
address maker
address baseToken
address pool
int24 lowerTick
int24 upperTick
uint256 feeGrowthGlobalX128
uint128 liquidity
uint256 base
uint256 quote
struct Funding.Growth globalFundingGrowth
)

InternalRemoveLiquidityParams

  struct InternalRemoveLiquidityParams(
address maker
address baseToken
address pool
bytes32 orderId
int24 lowerTick
int24 upperTick
uint128 liquidity
)

InternalSwapStep

  struct InternalSwapStep(
uint160 initialSqrtPriceX96
int24 nextTick
bool isNextTickInitialized
uint160 nextSqrtPriceX96
uint256 amountIn
uint256 amountOut
uint256 fee
)

Functions

initialize

  function initialize(
) external

setExchange

  function setExchange(
) external

addLiquidity

  function addLiquidity(
struct IOrderBook.AddLiquidityParams params
) external returns (struct IOrderBook.AddLiquidityResponse)

Add liquidity logic

Only used by ClearingHouse contract

Parameters:

NameTypeDescription
paramsstruct IOrderBook.AddLiquidityParamsAdd liquidity params, detail on IOrderBook.AddLiquidityParams

Return Values:

NameTypeDescription
responsestruct IOrderBook.AddLiquidityResponseAdd liquidity response, detail on IOrderBook.AddLiquidityResponse

removeLiquidity

  function removeLiquidity(
struct IOrderBook.RemoveLiquidityParams params
) external returns (struct IOrderBook.RemoveLiquidityResponse)

Remove liquidity logic, only used by ClearingHouse contract

Parameters:

NameTypeDescription
paramsstruct IOrderBook.RemoveLiquidityParamsRemove liquidity params, detail on IOrderBook.RemoveLiquidityParams

Return Values:

NameTypeDescription
responsestruct IOrderBook.RemoveLiquidityResponseRemove liquidity response, detail on IOrderBook.RemoveLiquidityResponse

updateFundingGrowthAndLiquidityCoefficientInFundingPayment

  function updateFundingGrowthAndLiquidityCoefficientInFundingPayment(
address trader,
address baseToken,
struct Funding.Growth fundingGrowthGlobal
) external returns (int256 liquidityCoefficientInFundingPayment)

This is the non-view version of getLiquidityCoefficientInFundingPayment(), only can be called by Exchange contract

Parameters:

NameTypeDescription
traderaddressThe trader address
baseTokenaddressThe base token address
fundingGrowthGlobalstruct Funding.GrowthThe funding growth info, detail on Funding.Growth

Return Values:

NameTypeDescription
liquidityCoefficientInFundingPaymentint256the funding payment of all orders/liquidity of a maker

updateOrderDebt

  function updateOrderDebt(
) external

uniswapV3MintCallback

  function uniswapV3MintCallback(
uint256 amount0Owed,
uint256 amount1Owed,
bytes data
) external

Called to msg.sender after minting liquidity to a position from IUniswapV3Pool#mint.

In the implementation you must pay the pool tokens owed for the minted liquidity. The caller of this method must be checked to be a UniswapV3Pool deployed by the canonical UniswapV3Factory.

Parameters:

NameTypeDescription
amount0Oweduint256The amount of token0 due to the pool for the minted liquidity
amount1Oweduint256The amount of token1 due to the pool for the minted liquidity
databytesAny data passed through by the caller via the IUniswapV3PoolActions#mint call

replaySwap

  function replaySwap(
struct IOrderBook.ReplaySwapParams params
) external returns (struct IOrderBook.ReplaySwapResponse)

Replay the swap and get the swap result (price impact and swap fee), only can be called by Exchange contract;

ReplaySwapResponse.insuranceFundFee = fee * insuranceFundFeeRatio

Parameters:

NameTypeDescription
paramsstruct IOrderBook.ReplaySwapParamsReplaySwap params, detail on IOrderBook.ReplaySwapParams

Return Values:

NameTypeDescription
responsestruct IOrderBook.ReplaySwapResponseThe swap result encoded in ReplaySwapResponse

getExchange

  function getExchange(
) external returns (address)

Get Exchange contract address

Return Values:

NameTypeDescription
exchangeaddressThe Exchange contract address

getOpenOrderIds

  function getOpenOrderIds(
address trader,
address baseToken
) external returns (bytes32[])

Get open order ids of a trader in the given market

Parameters:

NameTypeDescription
traderaddressThe trader address
baseTokenaddressThe base token address

Return Values:

NameTypeDescription
orderIdsbytes32[]The open order ids

getOpenOrderById

  function getOpenOrderById(
bytes32 orderId
) external returns (struct OpenOrder.Info)

Get open order info by given order id

Parameters:

NameTypeDescription
orderIdbytes32The order id

Return Values:

NameTypeDescription
infostruct OpenOrder.InfoThe open order info encoded in OpenOrder.Info

getOpenOrder

  function getOpenOrder(
address trader,
address baseToken,
int24 upperTick,
int24 lowerTick
) external returns (struct OpenOrder.Info)

Get open order info by given base token, upper tick and lower tick

Parameters:

NameTypeDescription
traderaddressThe trader address
baseTokenaddressThe base token address
upperTickint24The upper tick
lowerTickint24The lower tick

Return Values:

NameTypeDescription
infostruct OpenOrder.Infohe open order info encoded in OpenOrder.Info

hasOrder

  function hasOrder(
address trader,
address[] tokens
) external returns (bool)

Check if the specified trader has order in given markets

Parameters:

NameTypeDescription
traderaddressThe trader address
tokensaddress[]The base token addresses

Return Values:

NameTypeDescription
hasOrderboolTrue if the trader has order in given markets

getTotalQuoteBalanceAndPendingFee

  function getTotalQuoteBalanceAndPendingFee(
address trader,
address[] baseTokens
) external returns (int256 totalQuoteAmountInPools, uint256 totalPendingFee)

Get the total quote token amount and pending fees of all orders in given markets

Parameters:

NameTypeDescription
traderaddressThe trader address
baseTokensaddress[]The base token addresses

Return Values:

NameTypeDescription
totalQuoteAmountInPoolsint256The total quote token amount
totalPendingFeeuint256The total pending fees in the orders

getTotalTokenAmountInPoolAndPendingFee

  function getTotalTokenAmountInPoolAndPendingFee(
address trader,
address baseToken,
bool fetchBase
) external returns (uint256 tokenAmount, uint256 pendingFee)

Get the total token amount (quote or base) and pending fees of all orders in the given market

Parameters:

NameTypeDescription
traderaddressThe trader address
baseTokenaddressThe base token addresses
fetchBaseboolTrue if fetch base token amount, false if fetch quote token amount

Return Values:

NameTypeDescription
tokenAmountuint256The total quote/base token amount
totalPendingFeeuint256The total pending fees in the orders

getLiquidityCoefficientInFundingPayment

  function getLiquidityCoefficientInFundingPayment(
) external returns (int256 liquidityCoefficientInFundingPayment)

Get the pending funding payment of all orders in the given market

This is the view version of updateFundingGrowthAndLiquidityCoefficientInFundingPayment(), so only part of the funding payment will be returned. Use it with caution because it does not return all the pending funding payment of orders. Normally you won't need to use this function

Return Values:

NameTypeDescription
liquidityCoefficientInFundingPaymentint256the funding payment of all orders/liquidity of a maker

getPendingFee

  function getPendingFee(
address trader,
address baseToken,
int24 lowerTick,
int24 upperTick
) external returns (uint256)

Get the pending fees of a order

Parameters:

NameTypeDescription
traderaddressThe trader address
baseTokenaddressThe base token address
lowerTickint24The lower tick
upperTickint24The upper tick

Return Values:

NameTypeDescription
feeuint256The pending fees

getTotalOrderDebt

  function getTotalOrderDebt(
address trader,
address baseToken,
bool fetchBase
) public returns (uint256)

Get the total debt token amount (base or quote) of all orders in the given market

Parameters:

NameTypeDescription
traderaddressThe trader address
baseTokenaddressThe base token address
fetchBaseboolTrue if fetch base token amount, false if fetch quote token amount

Return Values:

NameTypeDescription
debtAmountuint256The total debt token amount