@somnia-chain/markets-sdk / index / computeBinaryPnl
Function: computeBinaryPnl()
computeBinaryPnl(
fills,balances,market,opts?):BinaryPnl
Defined in: packages/sdk/src/units.ts:408
Realized + unrealized binary PnL for one account, avg-cost basis — a PURE
helper (no indexer/chain dependency). fills are the account's own trades
(from binaryFillsFor); balances its current YES/NO holdings (from
getOutcomeBalances); market supplies decimals + resolution state.
Realized PnL accrues on sells (proceeds − avg cost of the tokens sold).
Unrealized marks the remaining position: to the book-clamped last price
while trading (see markYesPrice; pass opts.bookTop so a live
quote beyond a stale print corrects the mark), and to the settlement payout
once resolved: 1 for the winning outcome, 0 for the loser, and on a void
each leg's share of the market's stored payout vector — a half per side
under the UNIFORM void policy, [p, D−p] at the closing YES price on a
CLOB_SNAPSHOT void that captured a two-sided close.
An unresolved market that has never traded has no mark. When
market.lastPrice is null and opts.bookTop supplies no quote, mark,
value and unrealized are null on both legs, as are the combined
unrealized and total. realized and avgCost stay exact, because
neither depends on a mark.
Errors
Throws InvalidInputError when a void has a present but invalid payout vector. A missing legacy vector keeps the documented half-payout fallback.
Parameters
fills
balances
market
Pick<BinaryMarket, "quoteDecimals" | "lastPrice" | "winningOutcome" | "voided" | "payoutNumerators" | "payoutDenominator">
opts?
bookTop?
Top of the YES book — clamps the mark to live quotes (see markYesPrice).