Perp v2
Perp v2
  • About Perpetual Protocol
    • Social Links
    • Governance
    • v2 Era Ecosystem
    • PERP Token
  • Terms of Service
  • General
    • Wallets
    • Transfering Assets
    • Deposit & Withdrawal
    • Perpetual + Uniswap
    • FAQs
    • Guides
    • Legacy Reward Programs
    • Security
  • Hot Tub
  • Trading on Perp v2
    • How It Works
    • UI Overview
    • Perpetual Futures Basics
    • Trading Quick-start
    • Opening and Closing Positions
    • Account Value & More
    • Funding Payments
    • Order Types
    • Trading Fees and Gas Fees
    • Managing Risk
    • Multi-collateral
    • More docs
  • Providing Liquidity
    • Basics
    • Introduction to Maker UI
    • Add / Remove Liquidity
    • Estimated Fees and Rewards APR
    • More on LPing
    • Tools for LPs
  • Perp v2 Specs
  • Developer Docs
    • Developer FAQs
    • Source Code
    • Protocol Attributes
    • Contract guide
      • AccountBalance
      • BaseToken
      • ClearingHouse
      • CollateralManager
      • DelegateApproval
      • Exchange
      • InsuranceFund
      • MarketRegistry
      • OrderBook
      • QuoteToken
      • Vault
      • VirtualToken
    • Interface Contracts
      • IAccountBalance
      • IBaseToken
      • IClearingHouse
      • IClearingHouseConfig
      • IClearingHouseConfigEvent
      • ICollateralManager
      • IDelegateApproval
      • IERC20Metadata
      • IExchange
      • IIndexPrice
      • IInsuranceFund
      • IMarketRegistry
      • IOrderBook
      • IVault
      • IVirtualToken
    • Integration guide
    • Dev tools
Powered by GitBook
On this page
  • Functions​
  • Events​

Was this helpful?

  1. Developer Docs
  2. Interface Contracts

IAccountBalance

Last updated 1 year ago

Was this helpful?

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:

Name
Type
Description

trader

address

The address of the trader

baseToken

address

The address of the baseToken

base

int256

Modified amount of base

quote

int256

Modified amount of quote

Return Values:

Name
Type
Description

takerPositionSize

int256

Taker position size after modified

takerOpenNotional

int256

Taker open notional after modified

  function modifyOwedRealizedPnl(
    address trader,
    int256 amount
  ) external

Modify trader owedRealizedPnl

Only used by ClearingHouse contract

Name
Type
Description

trader

address

The address of the trader

amount

int256

Modified amount of owedRealizedPnl

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

Settle owedRealizedPnl

Only used by Vault.withdraw()

Name
Type
Description

trader

address

The address of the trader

Name
Type
Description

pnl

int256

Settled owedRealizedPnl

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

Modify trader owedRealizedPnl

Only used by ClearingHouse contract

Name
Type
Description

trader

address

The address of the trader

baseToken

address

The address of the baseToken

amount

int256

Settled quote amount

  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

Name
Type
Description

trader

address

The address of the trader

baseToken

address

The address of the baseToken

takerBase

int256

Modified amount of taker base

takerQuote

int256

Modified amount of taker quote

realizedPnl

int256

Amount of pnl realized

makerFee

int256

Amount of maker fee collected from pool

  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

Name
Type
Description

trader

address

The address of the trader

baseToken

address

The address of the trader's base token

  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

Name
Type
Description

trader

address

The address of the trader

baseToken

address

The address of the trader's base token

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

Update trader Twap premium info

Only used by ClearingHouse contract

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

lastTwPremiumGrowthGlobalX96

int256

The last Twap Premium

  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

Name
Type
Description

trader

address

The address of the trader

baseToken

address

The address of the trader's base token

Name
Type
Description

positionNotional

int256

Taker's position notional settled with closed price

openNotional

int256

Taker's open notional

realizedPnl

int256

Settled realized pnl

closedPrice

uint256

The closed price of the closed market

  function getClearingHouseConfig(
  ) external returns (address clearingHouseConfig)

Get ClearingHouseConfig address

Name
Type
Description

clearingHouseConfig

address

The address of ClearingHouseConfig

  function getOrderBook(
  ) external returns (address orderBook)

Get OrderBook address

Name
Type
Description

orderBook

address

The address of OrderBook

  function getVault(
  ) external returns (address vault)

Get Vault address

Name
Type
Description

vault

address

The address of Vault

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

Get trader registered baseTokens

Name
Type
Description

trader

address

The address of trader

Name
Type
Description

baseTokens

address[]

The array of baseToken address

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

Get trader account info

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

Name
Type
Description

traderAccountInfo

struct AccountMarket.Info

The baseToken account info of trader

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

Get taker cost of trader's baseToken

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

Name
Type
Description

openNotional

int256

The taker cost of trader's baseToken

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

Get total cost of trader's baseToken

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

Name
Type
Description

totalOpenNotional

int256

the amount of quote token paid for a position when opening

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

Get total debt value of trader

Total debt value will relate to Vault.getFreeCollateral()

Name
Type
Description

trader

address

The address of trader

Name
Type
Description

totalDebtValue

uint256

The debt value of trader

  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

Name
Type
Description

trader

address

The address of trader

Name
Type
Description

marginRequirementForLiquidation

int256

It is compared with ClearingHouse.getAccountValue which is also an int

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

Get owedRealizedPnl, unrealizedPnl and pending fee

Name
Type
Description

trader

address

The address of trader

Name
Type
Description

owedRealizedPnl

int256

the pnl realized already but stored temporarily in AccountBalance

unrealizedPnl

int256

the pnl not yet realized

pendingFee

uint256

the pending fee of maker earned

  function hasOrder(
    address trader
  ) external returns (bool)

Check trader has open order in open/closed market.

Name
Type
Description

trader

address

The address of trader

Name
Type
Description

True

bool

of false

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

Get trader base amount

base amount = takerPositionSize - orderBaseDebt

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

Name
Type
Description

baseAmount

int256

The base amount of trader's baseToken market

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

Get trader quote amount

quote amount = takerOpenNotional - orderQuoteDebt

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

Name
Type
Description

quoteAmount

int256

The quote amount of trader's baseToken market

  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

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

Name
Type
Description

takerPositionSize

int256

The taker position size of trader's baseToken market

  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

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

Name
Type
Description

totalPositionSize

int256

The total position size of trader's baseToken market

  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

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

Name
Type
Description

totalPositionValue

int256

Total position value of trader's baseToken market

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

Get all market position abs value of trader

Name
Type
Description

trader

address

The address of trader

Name
Type
Description

totalAbsPositionValue

uint256

Sum up positions value of every market

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

Get liquidatable position size of trader's baseToken market

Name
Type
Description

trader

address

The address of trader

baseToken

address

The address of baseToken

accountValue

int256

The account value of trader

Name
Type
Description

liquidatablePositionSize

int256

The liquidatable position size of trader's baseToken market

  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

Name
Type
Description

baseToken

address

The address of baseToken

Name
Type
Description

price

uint256

The mark price of baseToken market

  event VaultChanged(
    address vault
  )
Name
Type
Description

vault

address

The address of the vault contract

  event PnlRealized(
    address trader,
    int256 amount
  )

Emit whenever a trader's owedRealizedPnl is updated

Name
Type
Description

trader

address

The address of the trader

amount

int256

The amount changed

modifyOwedRealizedPnl

Parameters:

settleOwedRealizedPnl

Parameters:

Return Values:

settleQuoteToOwedRealizedPnl

Parameters:

settleBalanceAndDeregister

Parameters:

registerBaseToken

Parameters:

deregisterBaseToken

Parameters:

updateTwPremiumGrowthGlobal

Parameters:

settlePositionInClosedMarket

Parameters:

Return Values:

getClearingHouseConfig

Return Values:

getOrderBook

Return Values:

getVault

Return Values:

getBaseTokens

Parameters:

Return Values:

getAccountInfo

Parameters:

Return Values:

getTakerOpenNotional

Parameters:

Return Values:

getTotalOpenNotional

Parameters:

Return Values:

getTotalDebtValue

Parameters:

Return Values:

getMarginRequirementForLiquidation

Parameters:

Return Values:

getPnlAndPendingFee

Parameters:

Return Values:

hasOrder

Parameters:

Return Values:

getBase

Parameters:

Return Values:

getQuote

Parameters:

Return Values:

getTakerPositionSize

Parameters:

Return Values:

getTotalPositionSize

Parameters:

Return Values:

getTotalPositionValue

Parameters:

Return Values:

getTotalAbsPositionValue

Parameters:

Return Values:

getLiquidatablePositionSize

Parameters:

Return Values:

getMarkPrice

Parameters:

Return Values:

Events

VaultChanged

Parameters:

PnlRealized

Parameters:

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​