Query Trader Info
PendingFundingPayment
// the following is the workaround unless we expose Exchange._getFundingGrowthGlobalAndTwaps()
const lastTraderWhoEmitFundingUpdated
const fundingGrowthGlobal.fundingGrowthGlobal = AccountBalance.getAccountinfo(lastTraderWhoEmitFundingUpdatedFromThatMarket, baseToken).lastTwPremiumGrowthGlobalX96
// now we're fetching the info from the trader we want to calculate the pnl
const traderLastTwPremiumGrowthGlobalX96 = AccountBalance.getAccountinfo(trader, baseToken).lastTwPremiumGrowthGlobalX96
const tradersTakerPosSize = AccountBalance.getTakerPositionSize(trader, baseToken)
// repeat the math of Funding.calcPendingFundingPaymentWithLiquidityCoefficient
const takerPendingFundingPaymentByMarket = tradersTakerPosSize * ((fundingGrowthGlobal.twPremiumX96 - traderLastTwPremiumGrowthGlobalX96) / 2^96 ) / 15mins
const makerFundingPayment = Exchange.getPendingFundingPayment(trader, baseToken) - takerPendingFundingPaymentByMarket
Exchange.getPendingFundingPayment(trader, baseToken)
is negative if receiving funding payment
Taker or Maker's UnrealizedPnl by Market
const takerPositionSize = AccountBalance.getTakerPositionSize(trader, baseToken)
const makerImpermanentPositionSize = AccountBalance.getTotalPositionSize(trader, baseToken) - takerPositionSize
const takerOpenNotional = AccountBalance.getTakerOpenNotional(trader, baseToken)
const makerOpenNotional = AccountBalance.getTotalOpenNotional(trader, baseToken) - takerOpenNotional
const takerUnrealizedPnl = takerPositionSize * indexPrice + takerOpenNotional
const makerUnrealizedPnl = makerImpermanentPositionSize * indexPrice + makerOpenNotional
Realized PnL
- by event
- observe
PnlRealized
event fromAccountBalance
(less preferred, harder to get market)- it will be emitted anytime when addLiquidity, removeLiquidity, openPosition, closePosition, liquidate, cancelExcessOrder and settleAllFunding. It could emit multiple event in 1 action
- ex. when liquidating a position, it can
- emit PnlRealized first for the funding
- emit another one for IF (taking 10% fee as IF’s profit)
- realized the position pnl
- emit PnlRealized for trader’s liquidation fee (loss)
- also emit enother one for the liquidator (profit)
- PositionChanged +
FundingPaymentSettled
+ PositionLiquidated + RealizedMakerFee- PositionChanged.realizedPnl +
FundingPaymentSettled
.fundingPayment + PositionLiquidated.liquidationFee
- PositionChanged.realizedPnl +
- observe
- by contract
- every time a contract call (ex.
openPosition
), storeowedRealizedPnl
before and after the openPosition by callingAccountBalance.getPnlAndPendingFee
- every time a contract call (ex.
Margin Ratio
There are 2 ways to do it:
Use our npm package
@perp/sdk-curie
and call Positions'getAccountMarginRatio
function. Check code snippet.Get data from contracts and calculate them. The formula will be the same as in our
@perp/sdk-curie
.
const accountValue = await Vault.getAccountValue(trader)
const totalAbsPositionValue = await AccountBalance.getTotalAbsPositionValue(trader)
const marginRatio = accountValue / totalAbsPositionValue