@somnia-chain/markets-sdk


@somnia-chain/markets-sdk / index / perpMarkForPnl

Function: perpMarkForPnl()

perpMarkForPnl(state): object

Defined in: packages/sdk/src/perp/state.ts:87

The price to mark a perp position at, and whether it came from the mark feed.

Extracted and tested rather than written inline at the call site, because the inline version is a one-line regression waiting to happen. getPerpState reads the mark via tryGetMarkPrice, which REPORTS staleness rather than reverting on it — so the naive markPrice - entryPrice takes the meaningless 0 price word from a stale feed and produces a 100% loss on every open position. An earlier getMarkPrice() read reverted instead, which failed loudly; making the read more robust made the derived number silently worse until this guard.

The index price is the fallback rather than "no value": it is a separate oracle the pool already trusts for funding, and dropping positions out of a portfolio view is its own kind of wrong. Callers that want a MARK READING (rather than something to mark against) should report undefined on staleness instead — see fetchFundingRate.

Parameters

state

Pick<PerpStateOnchain, "markPrice" | "markPriceOk" | "indexPrice">

Returns

object

price

price: bigint

fromIndex

fromIndex: boolean