Skip to main content

IClearingHouse

AddLiquidityParams

  struct AddLiquidityParams(
address baseToken
uint256 base
uint256 quote
int24 lowerTick
int24 upperTick
uint256 minBase
uint256 minQuote
bool useTakerBalance
uint256 deadline
)

RemoveLiquidityParams

  struct RemoveLiquidityParams(
address baseToken
int24 lowerTick
int24 upperTick
uint128 liquidity
uint256 minBase
uint256 minQuote
uint256 deadline
)

AddLiquidityResponse

  struct AddLiquidityResponse(
uint256 base
uint256 quote
uint256 fee
uint256 liquidity
)

RemoveLiquidityResponse

  struct RemoveLiquidityResponse(
uint256 base
uint256 quote
uint256 fee
)

OpenPositionParams

  struct OpenPositionParams(
address baseToken
bool isBaseToQuote
bool isExactInput
uint256 amount
uint256 oppositeAmountBound
uint256 deadline
uint160 sqrtPriceLimitX96
bytes32 referralCode
)

ClosePositionParams

  struct ClosePositionParams(
address baseToken
uint160 sqrtPriceLimitX96
uint256 oppositeAmountBound
uint256 deadline
bytes32 referralCode
)

CollectPendingFeeParams

  struct CollectPendingFeeParams(
address trader
address baseToken
int24 lowerTick
int24 upperTick
)

Functions

addLiquidity

  function addLiquidity(
struct IClearingHouse.AddLiquidityParams params
) external returns (struct IClearingHouse.AddLiquidityResponse response)

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:

NameTypeDescription
paramsstruct IClearingHouse.AddLiquidityParamsAddLiquidityParams struct

Return Values:

NameTypeDescription
responsestruct IClearingHouse.AddLiquidityResponseAddLiquidityResponse struct

removeLiquidity

  function removeLiquidity(
struct IClearingHouse.RemoveLiquidityParams params
) external returns (struct IClearingHouse.RemoveLiquidityResponse response)

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:

NameTypeDescription
paramsstruct IClearingHouse.RemoveLiquidityParamsRemoveLiquidityParams struct

Return Values:

NameTypeDescription
responsestruct IClearingHouse.RemoveLiquidityResponseRemoveLiquidityResponse struct

settleAllFunding

  function settleAllFunding(
address trader
) external

Settle all markets fundingPayment to owedRealized Pnl

Parameters:

NameTypeDescription
traderaddressThe address of trader

openPosition

  function openPosition(
struct IClearingHouse.OpenPositionParams params
) external returns (uint256 base, uint256 quote)

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:

NameTypeDescription
paramsstruct IClearingHouse.OpenPositionParamsOpenPositionParams struct

Return Values:

NameTypeDescription
baseuint256The amount of baseToken the taker got or spent
quoteuint256The amount of quoteToken the taker got or spent

openPositionFor

  function openPositionFor(
address trader,
struct IClearingHouse.OpenPositionParams params
) external returns (uint256 base, uint256 quote, uint256 fee)

Parameters:

NameTypeDescription
traderaddressThe address of trader
paramsstruct IClearingHouse.OpenPositionParamsOpenPositionParams struct is the same as openPosition()

Return Values:

NameTypeDescription
baseuint256The amount of baseToken the taker got or spent
quoteuint256The amount of quoteToken the taker got or spent
feeuint256The trading fee

closePosition

  function closePosition(
struct IClearingHouse.ClosePositionParams params
) external returns (uint256 base, uint256 quote)

Close trader's position

Parameters:

NameTypeDescription
paramsstruct IClearingHouse.ClosePositionParamsClosePositionParams struct

Return Values:

NameTypeDescription
baseuint256The amount of baseToken the taker got or spent
quoteuint256The amount of quoteToken the taker got or spent

liquidate

  function liquidate(
address trader,
address baseToken,
int256 positionSize
) external

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:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken
positionSizeint256the position size to be liquidated by liquidator

liquidate

  function liquidate(
address trader,
address baseToken
) external

liquidate trader's position and will liquidate the max possible position size

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:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

cancelExcessOrders

  function cancelExcessOrders(
address maker,
address baseToken,
bytes32[] orderIds
) external

Cancel excess order of a maker

Order id can get from OrderBook.getOpenOrderIds

Parameters:

NameTypeDescription
makeraddressThe address of Maker
baseTokenaddressThe address of baseToken
orderIdsbytes32[]The id of the order

cancelAllExcessOrders

  function cancelAllExcessOrders(
address maker,
address baseToken
) external

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:

NameTypeDescription
makeraddressThe address of maker
baseTokenaddressThe address of baseToken

quitMarket

  function quitMarket(
address trader,
address baseToken
) external returns (uint256 base, uint256 quote)

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

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
baseuint256The amount of base token that is closed
quoteuint256The amount of quote token that is closed

getAccountValue

  function getAccountValue(
address trader
) external returns (int256 accountValue)

Get account value of trader

accountValue = totalCollateralValue + totalUnrealizedPnl, in 18 decimals

Parameters:

NameTypeDescription
traderaddressThe address of trader

Return Values:

NameTypeDescription
accountValueint256The account value of trader

getQuoteToken

  function getQuoteToken(
) external returns (address quoteToken)

Get QuoteToken address

Return Values:

NameTypeDescription
quoteTokenaddressThe quote token address

getUniswapV3Factory

  function getUniswapV3Factory(
) external returns (address factory)

Get UniswapV3Factory address

Return Values:

NameTypeDescription
factoryaddressUniswapV3Factory address

getClearingHouseConfig

  function getClearingHouseConfig(
) external returns (address clearingHouseConfig)

Get ClearingHouseConfig address

Return Values:

NameTypeDescription
clearingHouseConfigaddressClearingHouseConfig address

getVault

  function getVault(
) external returns (address vault)

Get Vault address

Return Values:

NameTypeDescription
vaultaddressVault address

getExchange

  function getExchange(
) external returns (address exchange)

Get Exchange address

Return Values:

NameTypeDescription
exchangeaddressExchange address

getOrderBook

  function getOrderBook(
) external returns (address orderBook)

Get OrderBook address

Return Values:

NameTypeDescription
orderBookaddressOrderBook address

getAccountBalance

  function getAccountBalance(
) external returns (address accountBalance)

Get AccountBalance address

Return Values:

NameTypeDescription
accountBalanceaddressAccountBalance address

getInsuranceFund

  function getInsuranceFund(
) external returns (address insuranceFund)

Get InsuranceFund address

Return Values:

NameTypeDescription
insuranceFundaddressInsuranceFund address

getDelegateApproval

  function getDelegateApproval(
) external returns (address delegateApproval)

Get DelegateApproval address

Return Values:

NameTypeDescription
delegateApprovaladdressDelegateApproval address

Events

ReferredPositionChanged

  event ReferredPositionChanged(
bytes32 referralCode
)

Emitted when open position with non-zero referral code

Parameters:

NameTypeDescription
referralCodebytes32The referral code by partners

PositionLiquidated

  event PositionLiquidated(
address trader,
address baseToken,
uint256 positionNotional,
uint256 positionSize,
uint256 liquidationFee,
address liquidator
)

Emitted when taker position is being liquidated

Parameters:

NameTypeDescription
traderaddressThe trader who has been liquidated
baseTokenaddressVirtual base token(ETH, BTC, etc...) address
positionNotionaluint256The cost of position
positionSizeuint256The size of position
liquidationFeeuint256The fee of liquidate
liquidatoraddressThe address of liquidator

LiquidityChanged

  event LiquidityChanged(
address maker,
address baseToken,
address quoteToken,
int24 lowerTick,
int24 upperTick,
int256 base,
int256 quote,
int128 liquidity,
uint256 quoteFee
)

Emitted when maker's liquidity of a order changed

Parameters:

NameTypeDescription
makeraddressThe one who provide liquidity
baseTokenaddressThe address of virtual base token(ETH, BTC, etc...)
quoteTokenaddressThe address of virtual USD token
lowerTickint24The lower tick of the position in which to add liquidity
upperTickint24The upper tick of the position in which to add liquidity
baseint256The amount of base token added (> 0) / removed (< 0) as liquidity; fees not included
quoteint256The amount of quote token added ... (same as the above)
liquidityint128The amount of liquidity unit added (> 0) / removed (< 0)
quoteFeeuint256The amount of quote token the maker received as fees

PositionChanged

  event PositionChanged(
address trader,
address baseToken,
int256 exchangedPositionSize,
int256 exchangedPositionNotional,
uint256 fee,
int256 openNotional,
int256 realizedPnl,
uint256 sqrtPriceAfterX96
)

Emitted when taker's position is being changed

Parameters:

NameTypeDescription
traderaddressTrader address
baseTokenaddressThe address of virtual base token(ETH, BTC, etc...)
exchangedPositionSizeint256The actual amount swap to uniswapV3 pool
exchangedPositionNotionalint256The cost of position, include fee
feeuint256The fee of open/close position
openNotionalint256The cost of open/close position, < 0: long, > 0: short
realizedPnlint256The realized Pnl after open/close position
sqrtPriceAfterX96uint256The sqrt price after swap, in X96

PositionClosed

  event PositionClosed(
address trader,
address baseToken,
int256 closedPositionSize,
int256 closedPositionNotional,
int256 openNotional,
int256 realizedPnl,
uint256 closedPrice
)

Emitted when taker close her position in closed market

Parameters:

NameTypeDescription
traderaddressTrader address
baseTokenaddressThe address of virtual base token(ETH, BTC, etc...)
closedPositionSizeint256Trader's position size in closed market
closedPositionNotionalint256Trader's position notional in closed market, based on closed price
openNotionalint256The cost of open/close position, < 0: long, > 0: short
realizedPnlint256The realized Pnl after close position
closedPriceuint256The close price of position

FundingPaymentSettled

  event FundingPaymentSettled(
address trader,
address baseToken,
int256 fundingPayment
)

Emitted when settling a trader's funding payment

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of virtual base token(ETH, BTC, etc...)
fundingPaymentint256The fundingPayment of trader on baseToken market, > 0: payment, < 0 : receipt

TrustedForwarderChanged

  event TrustedForwarderChanged(
address forwarder
)

Emitted when trusted forwarder address changed

TrustedForward is only used for metaTx

Parameters:

NameTypeDescription
forwarderaddressThe trusted forwarder address

DelegateApprovalChanged

  event DelegateApprovalChanged(
address delegateApproval
)

Emitted when DelegateApproval address changed

Parameters:

NameTypeDescription
delegateApprovaladdressThe address of DelegateApproval