@somnia-chain/markets-sdk


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

Function: computePositionPnL()

computePositionPnL(events, balances, market, oneCollateral, opts?): BinaryPositionPnL

Defined in: packages/sdk/src/derivedReads.ts:431

Fold a PnLEvent stream (oldest-first) + current balances into a BinaryPositionPnL, avg-cost basis, RAW units. oneCollateral = 10^quoteDecimals. Prices arrive in YES terms; a NO event is re-expressed to NO terms (oneCollateral − yesPrice) here so the two books stay separate.

An unresolved market that has never traded has no mark price. When market.lastPrice is null and opts.bookTop supplies no quote, markPrice, markValue and unrealizedPnl are null on both legs and on the total. balance, costBasis, avgCost and realizedPnl stay exact, because none of them depends on a mark.

Errors

Throws InvalidInputError when a void has a present but invalid payout vector. A legacy row with both vector fields absent keeps the documented half-payout fallback.

Parameters

events

PnLEvent[]

balances

balanceYes

bigint

balanceNo

bigint

market

Pick<BinaryMarket, "quoteDecimals" | "lastPrice" | "winningOutcome" | "voided" | "payoutNumerators" | "payoutDenominator">

oneCollateral

bigint

opts?

bookTop?

YesBookTop

Top of the YES book — clamps the mark to live quotes (see markYesPrice).

Returns

BinaryPositionPnL