@somnia-chain/markets-sdk / index / pnlEventsFor
Function: pnlEventsFor()
pnlEventsFor(
account,fills,routerActions):PnLEvent[]
Defined in: packages/sdk/src/derivedReads.ts:403
Derive the PnLEvent stream for account from raw FillRows
(order-book fills) + RouterActionRecords (mint/merge complete sets),
merged into ONE oldest-first timeline so the avg-cost roll sees mints and fills
in the order they happened.
ORDERED BY CHAIN POSITION, not by input order: block, then log index, compared as
exact integers and read from each row's id. timestamp takes no part - it is
metadata, and two events routinely share one second, so the order rows arrive in is
NOT the order they happened (fills are fetched newest-first) and a buy and a sell in
one second would otherwise fold sell-first and lose the realization to the clamp.
Fills whose side isn't bridged yet are skipped (can't attribute an outcome).
Mint/merge use the record's amount (each outcome's set size). Redeem actions are
ignored (they settle the position at payout, they don't change cost basis of a
still-open book).
SCOPE IS THE CALLER'S JOB: no market filter is applied here, so pass ONE
market's fills, selected by FillRow.market (never by pool — see there).
Errors
Throws InvalidInputError when a row carries a malformed number. Two cases:
- an
idthat is not`${blockNumber}_${logIndex}`, so the row cannot be placed in chain order; - a
quantity,fillPriceor mint/mergeamountthat is not a number.
EVERY one of those must be an UNSIGNED DECIMAL run — BigInt alone would read "",
a sign, or an 0x/0o/0b prefix, silently sorting a row somewhere it does not
belong or folding a quantity nobody wrote. timestamp is NOT read: it is metadata,
and ordering on it would let a wrong one override the position it disagrees with.
Rows from getUserFills and the router-action reads always satisfy this; a caller
assembling its own rows must. Nothing is defaulted, because a defaulted value
silently changes an average-cost roll and returns a wrong basis.
Parameters
account
string
fills
FillRow[]
routerActions
Returns
PnLEvent[]