Skip to main content

IExchange

SwapParams

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

SwapResponse

  struct SwapResponse(
uint256 base
uint256 quote
int256 exchangedPositionSize
int256 exchangedPositionNotional
uint256 fee
uint256 insuranceFundFee
int256 pnlToBeRealized
uint256 sqrtPriceAfterX96
int24 tick
bool isPartialClose
)

SwapCallbackData

  struct SwapCallbackData(
address trader
address baseToken
address pool
uint24 uniswapFeeRatio
uint256 fee
)

RealizePnlParams

  struct RealizePnlParams(
address trader
address baseToken
int256 base
int256 quote
)

Functions

swap

  function swap(
struct IExchange.SwapParams params
) external returns (struct IExchange.SwapResponse swapResponse)

The actual swap function

can only be called from ClearingHouse

Parameters:

NameTypeDescription
paramsstruct IExchange.SwapParamsThe parameters of the swap

Return Values:

NameTypeDescription
swapResponsestruct IExchange.SwapResponseThe result of the swap

settleFunding

  function settleFunding(
) external returns (int256 fundingPayment, struct Funding.Growth fundingGrowthGlobal)

Settle the funding payment for the time interval since the last settlement

This function should be called at the beginning of every high-level function, such as openPosition() while it doesn't matter who calls this function this function 1. settles personal funding payment 2. updates global funding growth personal funding payment is settled whenever there is pending funding payment the global funding growth update only happens once per unique timestamp (not blockNumber, due to Arbitrum)

Return Values:

NameTypeDescription
fundingPaymentint256the funding payment of a trader in one market should be settled into owned realized Pnl
fundingGrowthGlobalstruct Funding.Growththe up-to-date globalFundingGrowth, usually used for later calculations

getMaxTickCrossedWithinBlock

  function getMaxTickCrossedWithinBlock(
address baseToken
) external returns (uint24 maxTickCrossedWithinBlock)

Get the max ticks allowed to be crossed within a block when reducing position

Parameters:

NameTypeDescription
baseTokenaddressAddress of the base token

Return Values:

NameTypeDescription
maxTickCrossedWithinBlockuint24The max ticks allowed to be crossed within a block when reducing position

getAllPendingFundingPayment

  function getAllPendingFundingPayment(
) external returns (int256 pendingFundingPayment)

Get all the pending funding payment for a trader

Return Values:

NameTypeDescription
pendingFundingPaymentint256The pending funding payment of the trader.

Positive value means the trader pays funding, negative value means the trader receives funding.

isOverPriceSpread

  function isOverPriceSpread(
address baseToken
) external returns (bool)

Check if current price spread between market price and index twap is over maximum price spread.

Parameters:

NameTypeDescription
baseTokenaddressAddress of the base token

Return Values:

NameTypeDescription
trueboolif over the maximum price spread

getPendingFundingPayment

  function getPendingFundingPayment(
) external returns (int256 pendingFundingPayment)

Get the pending funding payment for a trader in a given market

this is the view version of _updateFundingGrowth()

Return Values:

NameTypeDescription
pendingFundingPaymentint256The pending funding payment of a trader in one market,

including liquidity & balance coefficients. Positive value means the trader pays funding, negative value means the trader receives funding.

getSqrtMarkTwapX96

  function getSqrtMarkTwapX96(
address baseToken,
uint32 twapInterval
) external returns (uint160 sqrtMarkTwapX96)

Deprecated function, will be removed in the next release, use getSqrtMarketTwapX96() instead Get the square root of the market twap price with the given time interval

The return value is a X96 number

Parameters:

NameTypeDescription
baseTokenaddressAddress of the base token
twapIntervaluint32The time interval in seconds

Return Values:

NameTypeDescription
sqrtMarkTwapX96uint160The square root of the market twap price

getSqrtMarketTwapX96

  function getSqrtMarketTwapX96(
address baseToken,
uint32 twapInterval
) external returns (uint160 sqrtMarketTwapX96)

Get the square root of the market twap price with the given time interval

The return value is a X96 number

Parameters:

NameTypeDescription
baseTokenaddressAddress of the base token
twapIntervaluint32The time interval in seconds

Return Values:

NameTypeDescription
sqrtMarketTwapX96uint160The square root of the market twap price

getPnlToBeRealized

  function getPnlToBeRealized(
struct IExchange.RealizePnlParams params
) external returns (int256 pnlToBeRealized)

Get the pnl that can be realized if trader reduce position

This function normally won't be needed by traders, but it might be useful for 3rd party

Parameters:

NameTypeDescription
paramsstruct IExchange.RealizePnlParamsThe params needed to do the query, encoded as RealizePnlParams in calldata

Return Values:

NameTypeDescription
pnlToBeRealizedint256The pnl that can be realized if trader reduce position

getOrderBook

  function getOrderBook(
) external returns (address orderBook)

Get OrderBook contract address

Return Values:

NameTypeDescription
orderBookaddressOrderBook contract address

getAccountBalance

  function getAccountBalance(
) external returns (address accountBalance)

Get AccountBalance contract address

Return Values:

NameTypeDescription
accountBalanceaddressAccountBalance contract address

getClearingHouseConfig

  function getClearingHouseConfig(
) external returns (address clearingHouse)

Get ClearingHouseConfig contract address

Return Values:

NameTypeDescription
clearingHouseaddressClearingHouseConfig contract address

Events

FundingUpdated

  event FundingUpdated(
address baseToken,
uint256 marketTwap,
uint256 indexTwap
)

Emitted when the global funding growth is updated

Parameters:

NameTypeDescription
baseTokenaddressAddress of the base token
marketTwapuint256The market twap price when the funding growth is updated
indexTwapuint256The index twap price when the funding growth is updated

MaxTickCrossedWithinBlockChanged

  event MaxTickCrossedWithinBlockChanged(
address baseToken,
uint24 maxTickCrossedWithinBlock
)

Emitted when maxTickCrossedWithinBlock is updated

Parameters:

NameTypeDescription
baseTokenaddressAddress of the base token
maxTickCrossedWithinBlockuint24Max tick allowed to be crossed within block when reducing position

AccountBalanceChanged

  event AccountBalanceChanged(
address accountBalance
)

Emitted when accountBalance is updated

Parameters:

NameTypeDescription
accountBalanceaddressThe address of accountBalance contract