ClearingHouse

InternalOpenPositionParamsarrow-up-right

  struct InternalOpenPositionParams(
    address trader
    address baseToken
    bool isBaseToQuote
    bool isExactInput
    bool isClose
    uint256 amount
    uint160 sqrtPriceLimitX96
  )

InternalCheckSlippageParamsarrow-up-right

  struct InternalCheckSlippageParams(
    bool isBaseToQuote
    bool isExactInput
    uint256 base
    uint256 quote
    uint256 oppositeAmountBound
  )

this function is public for testing

setDelegateApprovalarrow-up-right

remove to reduce bytecode size, might add back when we need it

addLiquidityarrow-up-right

Maker can call addLiquidity to provide liquidity on Uniswap V3 pool

Tx will fail if adding base == 0 && quote == 0 / liquidity == 0

  • AddLiquidityParams.useTakerBalance is only accept false now

Parameters:arrow-up-right

Name
Type
Description

params

struct IClearingHouse.AddLiquidityParams

AddLiquidityParams struct

Return Values:arrow-up-right

Name
Type
Description

response

struct IClearingHouse.AddLiquidityResponse

AddLiquidityResponse struct

removeLiquidityarrow-up-right

Maker can call removeLiquidity to remove liquidity

remove liquidity will transfer maker impermanent position to taker position, if liquidity of RemoveLiquidityParams struct is zero, the action will collect fee from pool to maker

Parameters:arrow-up-right

Name
Type
Description

params

struct IClearingHouse.RemoveLiquidityParams

RemoveLiquidityParams struct

Return Values:arrow-up-right

Name
Type
Description

response

struct IClearingHouse.RemoveLiquidityResponse

RemoveLiquidityResponse struct

settleAllFundingarrow-up-right

Settle all markets fundingPayment to owedRealized Pnl

Parameters:arrow-up-right

Name
Type
Description

trader

address

The address of trader

openPositionarrow-up-right

Trader can call openPosition to long/short on baseToken market

  • OpenPositionParams.oppositeAmountBound

    • B2Q + exact input, want more output quote as possible, so we set a lower bound of output quote

    • B2Q + exact output, want less input base as possible, so we set a upper bound of input base

    • Q2B + exact input, want more output base as possible, so we set a lower bound of output base

    • Q2B + exact output, want less input quote as possible, so we set a upper bound of input quote

      when it's set to 0, it will disable slippage protection entirely regardless of exact input or output when it's over or under the bound, it will be reverted

  • OpenPositionParams.sqrtPriceLimitX96

    • B2Q: the price cannot be less than this value after the swap

    • Q2B: the price cannot be greater than this value after the swap

      it will fill the trade until it reaches the price limit but WON'T REVERT when it's set to 0, it will disable price limit; when it's 0 and exact output, the output amount is required to be identical to the param amount

Parameters:arrow-up-right

Name
Type
Description

params

struct IClearingHouse.OpenPositionParams

OpenPositionParams struct

Return Values:arrow-up-right

Name
Type
Description

base

uint256

The amount of baseToken the taker got or spent

quote

uint256

The amount of quoteToken the taker got or spent

openPositionForarrow-up-right

Parameters:arrow-up-right

Name
Type
Description

trader

address

The address of trader

params

struct IClearingHouse.OpenPositionParams

OpenPositionParams struct is the same as openPosition()

Return Values:arrow-up-right

Name
Type
Description

base

uint256

The amount of baseToken the taker got or spent

quote

uint256

The amount of quoteToken the taker got or spent

fee

uint256

The trading fee

closePositionarrow-up-right

Close trader's position

Parameters:arrow-up-right

Name
Type
Description

params

struct IClearingHouse.ClosePositionParams

ClosePositionParams struct

Return Values:arrow-up-right

Name
Type
Description

base

uint256

The amount of baseToken the taker got or spent

quote

uint256

The amount of quoteToken the taker got or spent

If trader is underwater, any one can call liquidate to liquidate this trader

If trader has open orders, need to call cancelAllExcessOrders first If positionSize is greater than maxLiquidatePositionSize, liquidate maxLiquidatePositionSize by default If margin ratio >= 0.5 mmRatio, maxLiquidateRatio = MIN((1, 0.5 totalAbsPositionValue / absPositionValue) If margin ratio < 0.5 mmRatio, maxLiquidateRatio = 1 maxLiquidatePositionSize = positionSize maxLiquidateRatio

Parameters:arrow-up-right

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

positionSize

int256

the position size to be liquidated by liquidator

If trader is underwater, any one can call liquidate to liquidate this trader

If trader has open orders, need to call cancelAllExcessOrders first If positionSize is greater than maxLiquidatePositionSize, liquidate maxLiquidatePositionSize by default If margin ratio >= 0.5 mmRatio, maxLiquidateRatio = MIN((1, 0.5 totalAbsPositionValue / absPositionValue) If margin ratio < 0.5 mmRatio, maxLiquidateRatio = 1 maxLiquidatePositionSize = positionSize maxLiquidateRatio

Parameters:arrow-up-right

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

positionSize

the position size to be liquidated by liquidator

cancelExcessOrdersarrow-up-right

Cancel excess order of a maker

Order id can get from OrderBook.getOpenOrderIds

Parameters:arrow-up-right

Name
Type
Description

maker

address

The address of Maker

baseToken

address

The address of baseToken

orderIds

bytes32[]

The id of the order

cancelAllExcessOrdersarrow-up-right

Cancel all excess orders of a maker if the maker is underwater

This function won't fail if the maker has no order but fails when maker is not underwater

Parameters:arrow-up-right

Name
Type
Description

maker

address

The address of maker

baseToken

address

The address of baseToken

Close all positions and remove all liquidities of a trader in the closed market

Parameters:arrow-up-right

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

Return Values:arrow-up-right

Name
Type
Description

base

uint256

The amount of base token that is closed

quote

uint256

The amount of quote token that is closed

uniswapV3MintCallbackarrow-up-right

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

namings here follow Uniswap's convention

Parameters:arrow-up-right

Name
Type
Description

amount0Owed

uint256

The amount of token0 due to the pool for the minted liquidity

amount1Owed

uint256

The amount of token1 due to the pool for the minted liquidity

data

bytes

Any data passed through by the caller via the IUniswapV3PoolActions#mint call

uniswapV3SwapCallbackarrow-up-right

Called to msg.sender after executing a swap via IUniswapV3Pool#swap.

namings here follow Uniswap's convention

Parameters:arrow-up-right

Name
Type
Description

amount0Delta

int256

The amount of token0 that was sent (negative) or must be received (positive) by the pool by

the end of the swap. If positive, the callback must send that amount of token0 to the pool. |amount1Delta | int256 | The amount of token1 that was sent (negative) or must be received (positive) by the pool by the end of the swap. If positive, the callback must send that amount of token1 to the pool. |data | bytes | Any data passed through by the caller via the IUniswapV3PoolActions#swap call

getQuoteTokenarrow-up-right

Get QuoteToken address

Return Values:arrow-up-right

Name
Type
Description

quoteToken

address

The quote token address

getUniswapV3Factoryarrow-up-right

Get UniswapV3Factory address

Return Values:arrow-up-right

Name
Type
Description

factory

address

UniswapV3Factory address

getClearingHouseConfigarrow-up-right

Get ClearingHouseConfig address

Return Values:arrow-up-right

Name
Type
Description

clearingHouseConfig

address

ClearingHouseConfig address

Get Vault address

Return Values:arrow-up-right

Name
Type
Description

vault

address

Vault address

Get Exchange address

Return Values:arrow-up-right

Name
Type
Description

exchange

address

Exchange address

getOrderBookarrow-up-right

Get OrderBook address

Return Values:arrow-up-right

Name
Type
Description

orderBook

address

OrderBook address

getAccountBalancearrow-up-right

Get AccountBalance address

Return Values:arrow-up-right

Name
Type
Description

accountBalance

address

AccountBalance address

getInsuranceFundarrow-up-right

Get InsuranceFund address

Return Values:arrow-up-right

Name
Type
Description

insuranceFund

address

InsuranceFund address

getDelegateApprovalarrow-up-right

Get DelegateApproval address

Return Values:arrow-up-right

Name
Type
Description

delegateApproval

address

DelegateApproval address

getAccountValuearrow-up-right

Get account value of trader

accountValue = totalCollateralValue + totalUnrealizedPnl, in 18 decimals

Parameters:arrow-up-right

Name
Type
Description

trader

address

The address of trader

Return Values:arrow-up-right

Name
Type
Description

accountValue

int256

The account value of trader

ClearingHouseConfig

setLiquidationPenaltyRatioarrow-up-right

setPartialCloseRatioarrow-up-right

setTwapIntervalarrow-up-right

setMaxMarketsPerAccountarrow-up-right

setSettlementTokenBalanceCaparrow-up-right

setMaxFundingRatearrow-up-right

setMarkPriceMarketTwapIntervalarrow-up-right

setMarkPricePremiumIntervalarrow-up-right

getMaxMarketsPerAccountarrow-up-right

Return Values:arrow-up-right

Name
Type
Description

maxMarketsPerAccount

uint8

Max value of total markets per account

Return Values:arrow-up-right

Name
Type
Description

imRatio

uint24

Initial margin ratio

Return Values:arrow-up-right

Name
Type
Description

mmRatio

uint24

Maintenance margin requirement ratio

getLiquidationPenaltyRatioarrow-up-right

Return Values:arrow-up-right

Name
Type
Description

liquidationPenaltyRatio

uint24

Liquidation penalty ratio

getPartialCloseRatioarrow-up-right

Return Values:arrow-up-right

Name
Type
Description

partialCloseRatio

uint24

Partial close ratio

getTwapIntervalarrow-up-right

Return Values:arrow-up-right

Name
Type
Description

twapInterval

uint32

TwapInterval for funding and prices (market & index) calculations

getSettlementTokenBalanceCaparrow-up-right

Return Values:arrow-up-right

Name
Type
Description

settlementTokenBalanceCap

uint256

Max value of settlement token balance

getMaxFundingRatearrow-up-right

Return Values:arrow-up-right

Name
Type
Description

maxFundingRate

uint24

Max value of funding rate

getMarkPriceConfigarrow-up-right

Return Values:arrow-up-right

Name
Type
Description

marketTwapInterval

uint32

MarketTwapInterval is the interval of market twap used for mark price calculations

premiumInterval

uint32

PremiumInterval is the interval of premium used for mark price calculations

Last updated