@reyaxyz/common 0.380.1 → 0.382.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts CHANGED
@@ -1144,7 +1144,7 @@ type SpotPair<T extends string> = T extends `${infer Base}USDMARK`
1144
1144
  : never;
1145
1145
 
1146
1146
  /**
1147
- * Base assets of the RWA perpetual markets (ids 76-94; registration is a separate per-chain release gate). Stork publishes each as a pair of
1147
+ * Base assets of the RWA perpetual markets (ids 76-94 and 96; registration is a separate per-chain release gate). Stork publishes each as a pair of
1148
1148
  * USDC-quoted feeds, so neither is ever divided by USDCUSD.
1149
1149
  */
1150
1150
  export type RwaStorkAsset =
@@ -1166,7 +1166,8 @@ export type RwaStorkAsset =
1166
1166
  | 'INTC'
1167
1167
  | 'HOOD'
1168
1168
  | 'AMD'
1169
- | 'MSTR';
1169
+ | 'MSTR'
1170
+ | 'SKHY';
1170
1171
 
1171
1172
  /**
1172
1173
  * Stork actual-mark feed. It is each RWA market's on-chain `oracleNodeId`, the
@@ -226,6 +226,83 @@ export function calculatePositionFundingSinceChange(
226
226
  return mulWad(divWad(funding.minus(lastFunding), multiplier), base.negated());
227
227
  }
228
228
 
229
+ /**
230
+ * Raw E18 funding on an open position since its session (the contract's
231
+ * `position.session`) opened, positive received: the funding settled at its
232
+ * fills since then (positions.session_settled_funding, RET-281) plus
233
+ * calculatePositionFundingSinceChange. Null while the settled part is unknown
234
+ * (a session opened before the indexer kept it, not yet backfilled).
235
+ */
236
+ export function calculatePositionFundingSinceOpen(
237
+ position: RealizedBalancePosition & {
238
+ session_settled_funding?: { toString(): string } | null;
239
+ },
240
+ trackers: RealizedBalanceTrackers | undefined,
241
+ ): BigNumber | null {
242
+ const settled = position.session_settled_funding;
243
+ if (settled === null || settled === undefined) return null;
244
+ return new BigNumber(settled.toString()).plus(
245
+ calculatePositionFundingSinceChange(position, trackers),
246
+ );
247
+ }
248
+
249
+ /** A stored position's fields `nextSessionSettledFunding` reads (raw E18). */
250
+ export type SessionFundingPosition = {
251
+ base: { toString(): string } | null;
252
+ funding_value: { toString(): string } | null;
253
+ base_multiplier: { toString(): string } | null;
254
+ session: { toString(): string } | null;
255
+ session_settled_funding?: { toString(): string } | null;
256
+ };
257
+
258
+ /**
259
+ * positions.session_settled_funding after a fill (RET-281), as the
260
+ * perp-execution indexer's upsert computes it (persistPerpExecutionCte.ts):
261
+ * raw E18 funding settled into the position at its fills since its current
262
+ * session (the contract's `position.session`) opened, positive received.
263
+ *
264
+ * - No stored position, or a new session (opened from flat or flipped): 0.
265
+ * The opening fill's settlement belonged to the previous position. A
266
+ * stored position from before the `session` column has none to compare:
267
+ * there flat before, or the other side, opens one.
268
+ * - Within a session: plus computeFundingCashflow of the stored position
269
+ * against the fill's post-fill funding value, which within a session is
270
+ * the value it settled at.
271
+ * - Unknown (null) stays unknown; a flat position or a zero multiplier
272
+ * accrues nothing.
273
+ */
274
+ export function nextSessionSettledFunding(
275
+ previous: SessionFundingPosition | undefined,
276
+ next: Pick<SessionFundingPosition, 'session' | 'funding_value' | 'base'>,
277
+ ): string | null {
278
+ if (!previous) return '0';
279
+ const base =
280
+ previous.base === null ? null : new BigNumber(previous.base.toString());
281
+ if (previous.session !== null && previous.session !== undefined) {
282
+ if (previous.session.toString() !== String(next.session)) return '0';
283
+ } else if (
284
+ base === null ||
285
+ base.isZero() ||
286
+ base.gt(0) !== new BigNumber(String(next.base)).gt(0)
287
+ ) {
288
+ return '0';
289
+ }
290
+ const settled = previous.session_settled_funding;
291
+ if (settled === null || settled === undefined) return null;
292
+ const multiplier = new BigNumber(previous.base_multiplier?.toString() ?? 0);
293
+ if (base === null || base.isZero() || !multiplier.gt(0)) {
294
+ return settled.toString();
295
+ }
296
+ // As the SQL: a stored position without a funding value is unknown.
297
+ if (previous.funding_value === null) return null;
298
+ const delta = integer(next.funding_value, 'fill funding').minus(
299
+ integer(previous.funding_value, 'position funding'),
300
+ );
301
+ return new BigNumber(settled.toString())
302
+ .plus(mulWad(divWad(delta, multiplier), base.negated()))
303
+ .toFixed();
304
+ }
305
+
229
306
  /**
230
307
  * Returns raw E18 PnL aligned to collateral units. The instrument floors EACH
231
308
  * market's PnL to quote-token precision before Core aggregates collateral info.