Skip to main content

IAccountBalance

Functions

modifyTakerBalance

  function modifyTakerBalance(
address trader,
address baseToken,
int256 base,
int256 quote
) external returns (int256 takerPositionSize, int256 takerOpenNotional)

Modify trader account balance

Only used by ClearingHouse contract

Parameters:

NameTypeDescription
traderaddressThe address of the trader
baseTokenaddressThe address of the baseToken
baseint256Modified amount of base
quoteint256Modified amount of quote

Return Values:

NameTypeDescription
takerPositionSizeint256Taker position size after modified
takerOpenNotionalint256Taker open notional after modified

modifyOwedRealizedPnl

  function modifyOwedRealizedPnl(
address trader,
int256 amount
) external

Modify trader owedRealizedPnl

Only used by ClearingHouse contract

Parameters:

NameTypeDescription
traderaddressThe address of the trader
amountint256Modified amount of owedRealizedPnl

settleOwedRealizedPnl

  function settleOwedRealizedPnl(
address trader
) external returns (int256 pnl)

Settle owedRealizedPnl

Only used by Vault.withdraw()

Parameters:

NameTypeDescription
traderaddressThe address of the trader

Return Values:

NameTypeDescription
pnlint256Settled owedRealizedPnl

settleQuoteToOwedRealizedPnl

  function settleQuoteToOwedRealizedPnl(
address trader,
address baseToken,
int256 amount
) external

Modify trader owedRealizedPnl

Only used by ClearingHouse contract

Parameters:

NameTypeDescription
traderaddressThe address of the trader
baseTokenaddressThe address of the baseToken
amountint256Settled quote amount

settleBalanceAndDeregister

  function settleBalanceAndDeregister(
address trader,
address baseToken,
int256 takerBase,
int256 takerQuote,
int256 realizedPnl,
int256 makerFee
) external

Settle account balance and deregister base token

Only used by ClearingHouse contract

Parameters:

NameTypeDescription
traderaddressThe address of the trader
baseTokenaddressThe address of the baseToken
takerBaseint256Modified amount of taker base
takerQuoteint256Modified amount of taker quote
realizedPnlint256Amount of pnl realized
makerFeeint256Amount of maker fee collected from pool

registerBaseToken

  function registerBaseToken(
address trader,
address baseToken
) external

Every time a trader's position value is checked, the base token list of this trader will be traversed; thus, this list should be kept as short as possible

Only used by ClearingHouse contract

Parameters:

NameTypeDescription
traderaddressThe address of the trader
baseTokenaddressThe address of the trader's base token

deregisterBaseToken

  function deregisterBaseToken(
address trader,
address baseToken
) external

Deregister baseToken from trader accountInfo

Only used by ClearingHouse contract, this function is expensive, due to for loop

Parameters:

NameTypeDescription
traderaddressThe address of the trader
baseTokenaddressThe address of the trader's base token

updateTwPremiumGrowthGlobal

  function updateTwPremiumGrowthGlobal(
address trader,
address baseToken,
int256 lastTwPremiumGrowthGlobalX96
) external

Update trader Twap premium info

Only used by ClearingHouse contract

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken
lastTwPremiumGrowthGlobalX96int256The last Twap Premium

settlePositionInClosedMarket

  function settlePositionInClosedMarket(
address trader,
address baseToken
) external returns (int256 positionNotional, int256 openNotional, int256 realizedPnl, uint256 closedPrice)

Settle trader's PnL in closed market

Only used by ClearingHouse

Parameters:

NameTypeDescription
traderaddressThe address of the trader
baseTokenaddressThe address of the trader's base token

Return Values:

NameTypeDescription
positionNotionalint256Taker's position notional settled with closed price
openNotionalint256Taker's open notional
realizedPnlint256Settled realized pnl
closedPriceuint256The closed price of the closed market

getClearingHouseConfig

  function getClearingHouseConfig(
) external returns (address clearingHouseConfig)

Get ClearingHouseConfig address

Return Values:

NameTypeDescription
clearingHouseConfigaddressThe address of ClearingHouseConfig

getOrderBook

  function getOrderBook(
) external returns (address orderBook)

Get OrderBook address

Return Values:

NameTypeDescription
orderBookaddressThe address of OrderBook

getVault

  function getVault(
) external returns (address vault)

Get Vault address

Return Values:

NameTypeDescription
vaultaddressThe address of Vault

getBaseTokens

  function getBaseTokens(
address trader
) external returns (address[] baseTokens)

Get trader registered baseTokens

Parameters:

NameTypeDescription
traderaddressThe address of trader

Return Values:

NameTypeDescription
baseTokensaddress[]The array of baseToken address

getAccountInfo

  function getAccountInfo(
address trader,
address baseToken
) external returns (struct AccountMarket.Info traderAccountInfo)

Get trader account info

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
traderAccountInfostruct AccountMarket.InfoThe baseToken account info of trader

getTakerOpenNotional

  function getTakerOpenNotional(
address trader,
address baseToken
) external returns (int256 openNotional)

Get taker cost of trader's baseToken

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
openNotionalint256The taker cost of trader's baseToken

getTotalOpenNotional

  function getTotalOpenNotional(
address trader,
address baseToken
) external returns (int256 totalOpenNotional)

Get total cost of trader's baseToken

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
totalOpenNotionalint256the amount of quote token paid for a position when opening

getTotalDebtValue

  function getTotalDebtValue(
address trader
) external returns (uint256 totalDebtValue)

Get total debt value of trader

Total debt value will relate to Vault.getFreeCollateral()

Parameters:

NameTypeDescription
traderaddressThe address of trader

Return Values:

NameTypeDescription
totalDebtValueuint256The debt value of trader

getMarginRequirementForLiquidation

  function getMarginRequirementForLiquidation(
address trader
) external returns (int256 marginRequirementForLiquidation)

Get margin requirement to check whether trader will be able to liquidate

This is different from Vault._getTotalMarginRequirement(), which is for freeCollateral calculation

Parameters:

NameTypeDescription
traderaddressThe address of trader

Return Values:

NameTypeDescription
marginRequirementForLiquidationint256It is compared with ClearingHouse.getAccountValue which is also an int

getPnlAndPendingFee

  function getPnlAndPendingFee(
address trader
) external returns (int256 owedRealizedPnl, int256 unrealizedPnl, uint256 pendingFee)

Get owedRealizedPnl, unrealizedPnl and pending fee

Parameters:

NameTypeDescription
traderaddressThe address of trader

Return Values:

NameTypeDescription
owedRealizedPnlint256the pnl realized already but stored temporarily in AccountBalance
unrealizedPnlint256the pnl not yet realized
pendingFeeuint256the pending fee of maker earned

hasOrder

  function hasOrder(
address trader
) external returns (bool)

Check trader has open order in open/closed market.

Parameters:

NameTypeDescription
traderaddressThe address of trader

Return Values:

NameTypeDescription
Trueboolof false

getBase

  function getBase(
address trader,
address baseToken
) external returns (int256 baseAmount)

Get trader base amount

base amount = takerPositionSize - orderBaseDebt

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
baseAmountint256The base amount of trader's baseToken market

getQuote

  function getQuote(
address trader,
address baseToken
) external returns (int256 quoteAmount)

Get trader quote amount

quote amount = takerOpenNotional - orderQuoteDebt

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
quoteAmountint256The quote amount of trader's baseToken market

getTakerPositionSize

  function getTakerPositionSize(
address trader,
address baseToken
) external returns (int256 takerPositionSize)

Get taker position size of trader's baseToken market

This will only has taker position, can get maker impermanent position through getTotalPositionSize

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
takerPositionSizeint256The taker position size of trader's baseToken market

getTotalPositionSize

  function getTotalPositionSize(
address trader,
address baseToken
) external returns (int256 totalPositionSize)

Get total position size of trader's baseToken market

total position size = taker position size + maker impermanent position size

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
totalPositionSizeint256The total position size of trader's baseToken market

getTotalPositionValue

  function getTotalPositionValue(
address trader,
address baseToken
) external returns (int256 totalPositionValue)

Get total position value of trader's baseToken market

A negative returned value is only be used when calculating pnl, we use mark price to calc position value

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
totalPositionValueint256Total position value of trader's baseToken market

getTotalAbsPositionValue

  function getTotalAbsPositionValue(
address trader
) external returns (uint256 totalAbsPositionValue)

Get all market position abs value of trader

Parameters:

NameTypeDescription
traderaddressThe address of trader

Return Values:

NameTypeDescription
totalAbsPositionValueuint256Sum up positions value of every market

getLiquidatablePositionSize

  function getLiquidatablePositionSize(
address trader,
address baseToken,
int256 accountValue
) external returns (int256)

Get liquidatable position size of trader's baseToken market

Parameters:

NameTypeDescription
traderaddressThe address of trader
baseTokenaddressThe address of baseToken
accountValueint256The account value of trader

Return Values:

NameTypeDescription
liquidatablePositionSizeint256The liquidatable position size of trader's baseToken market

getMarkPrice

  function getMarkPrice(
address baseToken
) external returns (uint256)

Get mark price of baseToken market

Mark price is the median of three prices as below.

   1. current market price
2. market twap with 30 mins
3. index price + premium with 15 mins

If the parameters to calculate mark price are not set, returns index twap instead for backward compatible If the market is paused, returns index twap instead, that will be the index twap while pausing market

Parameters:

NameTypeDescription
baseTokenaddressThe address of baseToken

Return Values:

NameTypeDescription
priceuint256The mark price of baseToken market

Events

VaultChanged

  event VaultChanged(
address vault
)

Parameters:

NameTypeDescription
vaultaddressThe address of the vault contract

PnlRealized

  event PnlRealized(
address trader,
int256 amount
)

Emit whenever a trader's owedRealizedPnl is updated

Parameters:

NameTypeDescription
traderaddressThe address of the trader
amountint256The amount changed