@lifi/perps-sdk 0.0.0-preview-c2a413ab → 0.0.0-preview-be3da034

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.
Files changed (41) hide show
  1. package/README.md +33 -1
  2. package/dist/cjs/index.d.ts +2 -2
  3. package/dist/cjs/index.d.ts.map +1 -1
  4. package/dist/cjs/index.js +6 -5
  5. package/dist/cjs/index.js.map +1 -1
  6. package/dist/cjs/math/position.d.ts +2 -0
  7. package/dist/cjs/math/position.d.ts.map +1 -1
  8. package/dist/cjs/math/position.js +54 -0
  9. package/dist/cjs/math/position.js.map +1 -1
  10. package/dist/cjs/version.d.ts +1 -1
  11. package/dist/cjs/version.js +1 -1
  12. package/dist/cjs/websocket/WsProviderBase.d.ts +1 -5
  13. package/dist/cjs/websocket/WsProviderBase.d.ts.map +1 -1
  14. package/dist/cjs/websocket/WsProviderBase.js +1 -32
  15. package/dist/cjs/websocket/WsProviderBase.js.map +1 -1
  16. package/dist/esm/index.d.ts +2 -2
  17. package/dist/esm/index.d.ts.map +1 -1
  18. package/dist/esm/index.js +2 -2
  19. package/dist/esm/index.js.map +1 -1
  20. package/dist/esm/math/position.d.ts +21 -0
  21. package/dist/esm/math/position.d.ts.map +1 -1
  22. package/dist/esm/math/position.js +71 -0
  23. package/dist/esm/math/position.js.map +1 -1
  24. package/dist/esm/version.d.ts +1 -1
  25. package/dist/esm/version.js +1 -1
  26. package/dist/esm/websocket/WsProviderBase.d.ts +1 -18
  27. package/dist/esm/websocket/WsProviderBase.d.ts.map +1 -1
  28. package/dist/esm/websocket/WsProviderBase.js +0 -44
  29. package/dist/esm/websocket/WsProviderBase.js.map +1 -1
  30. package/dist/types/index.d.ts +2 -2
  31. package/dist/types/index.d.ts.map +1 -1
  32. package/dist/types/math/position.d.ts +21 -0
  33. package/dist/types/math/position.d.ts.map +1 -1
  34. package/dist/types/version.d.ts +1 -1
  35. package/dist/types/websocket/WsProviderBase.d.ts +1 -18
  36. package/dist/types/websocket/WsProviderBase.d.ts.map +1 -1
  37. package/package.json +2 -2
  38. package/src/index.ts +2 -1
  39. package/src/math/position.ts +90 -0
  40. package/src/version.ts +1 -1
  41. package/src/websocket/WsProviderBase.ts +1 -63
@@ -594,3 +594,93 @@ export function isPositionMarginRemovable(position: Position): boolean {
594
594
  PositionMarginAdjustment.ADD_AND_REMOVE
595
595
  )
596
596
  }
597
+
598
+ const isPositiveDecimalString = (value: unknown): value is string =>
599
+ isDecimalString(value) && isDecimalStringGreaterThan(value, '0')
600
+
601
+ /**
602
+ * Complete a mapped position with its calculated fields. A venue
603
+ * `positionValue` or `returnOnEquity` on the input stays. Each other field
604
+ * comes from its formula, and is absent when an input is absent or not a
605
+ * decimal string, or when `markPrice` or `marginUsed` is not above zero.
606
+ * Never throws and never warns.
607
+ *
608
+ * @public
609
+ */
610
+ export function buildPosition(position: Position): Position {
611
+ const { size, markPrice, liquidationPrice, unrealizedPnl, marginUsed } =
612
+ position
613
+ const hasMarkPrice = isPositiveDecimalString(markPrice)
614
+ const hasMargin = isPositiveDecimalString(marginUsed)
615
+ const positionValue =
616
+ position.positionValue ??
617
+ (hasMarkPrice && isDecimalString(size)
618
+ ? calculateNotionalValue(size, markPrice)
619
+ : undefined)
620
+ const returnOnEquity =
621
+ position.returnOnEquity ??
622
+ (hasMargin && isDecimalString(unrealizedPnl)
623
+ ? calculateRoe(unrealizedPnl, marginUsed)
624
+ : undefined)
625
+ const effectiveLeverage =
626
+ hasMargin && isDecimalString(positionValue)
627
+ ? calculateEffectiveLeverage({
628
+ positionValueUsd: positionValue,
629
+ marginUsd: marginUsed,
630
+ })
631
+ : undefined
632
+ const liquidationDistance =
633
+ hasMarkPrice && isDecimalString(liquidationPrice)
634
+ ? calculateLiquidationDistance({
635
+ liquidationPrice,
636
+ currentPrice: markPrice,
637
+ })
638
+ : undefined
639
+ return {
640
+ ...position,
641
+ ...(positionValue === undefined ? {} : { positionValue }),
642
+ ...(returnOnEquity === undefined ? {} : { returnOnEquity }),
643
+ ...(effectiveLeverage === undefined ? {} : { effectiveLeverage }),
644
+ ...(liquidationDistance === undefined ? {} : { liquidationDistance }),
645
+ }
646
+ }
647
+
648
+ /**
649
+ * The position at another mark price, as a new object: `markPrice`,
650
+ * `positionValue`, `unrealizedPnl`, `returnOnEquity`, `effectiveLeverage` and
651
+ * `liquidationDistance` calculated again by {@link buildPosition}. Gives
652
+ * `position` itself when `markPrice` is not a positive decimal string.
653
+ * `unrealizedPnl` keeps the input figure when `entryPrice` or `size` is not a
654
+ * decimal string.
655
+ *
656
+ * @public
657
+ */
658
+ export function remarkPosition(
659
+ position: Position,
660
+ markPrice: string
661
+ ): Position {
662
+ if (!isPositiveDecimalString(markPrice)) {
663
+ return position
664
+ }
665
+ const {
666
+ positionValue: _positionValue,
667
+ returnOnEquity: _returnOnEquity,
668
+ effectiveLeverage: _effectiveLeverage,
669
+ liquidationDistance: _liquidationDistance,
670
+ ...fields
671
+ } = position
672
+ const { entryPrice, size } = position
673
+ return buildPosition({
674
+ ...fields,
675
+ markPrice,
676
+ unrealizedPnl:
677
+ isDecimalString(entryPrice) && isDecimalString(size)
678
+ ? estimateUnrealizedPnl({
679
+ entryPrice,
680
+ markPrice,
681
+ size,
682
+ isLong: position.side === PositionSide.LONG,
683
+ })
684
+ : position.unrealizedPnl,
685
+ })
686
+ }
package/src/version.ts CHANGED
@@ -10,4 +10,4 @@ export const name = '@lifi/perps-sdk'
10
10
  *
11
11
  * @public
12
12
  */
13
- export const version = '0.0.0-preview-c2a413ab'
13
+ export const version = '0.0.0-preview-be3da034'
@@ -1,8 +1,4 @@
1
- import type {
2
- MarketContext,
3
- Subscription,
4
- SubscriptionEvent,
5
- } from '@lifi/perps-types'
1
+ import type { Subscription, SubscriptionEvent } from '@lifi/perps-types'
6
2
  import type {
7
3
  ProviderGetQuoteParams,
8
4
  QuoteListener,
@@ -26,14 +22,6 @@ import { wsLog } from './wsLog.js'
26
22
  */
27
23
  export const WS_CHANNEL_TEARDOWN_LINGER_MS = 250
28
24
 
29
- /**
30
- * Minimum gap between two `marketsContext` deliveries on one provider. Wire
31
- * frames inside the gap coalesce; the newest one is delivered when it ends.
32
- *
33
- * @public
34
- */
35
- export const WS_MARKETS_CONTEXT_INTERVAL_MS = 1_000
36
-
37
25
  interface ChannelEntry {
38
26
  /**
39
27
  * Per-listener ref count — a `Map`, not a `Set`, because the same listener
@@ -47,10 +35,6 @@ interface ChannelEntry {
47
35
  opening?: Promise<() => void>
48
36
  /** Deferred-teardown timer, set on the 1→0 transition; cleared if re-subscribed within the linger. */
49
37
  pendingTeardown?: ReturnType<typeof setTimeout>
50
- /** `marketsContext` gap timer, set on each delivery; the next delivery waits for it. */
51
- throttle?: ReturnType<typeof setTimeout>
52
- /** Newest `marketsContext` snapshot producer that arrived inside the gap. */
53
- pendingSnapshot?: () => Record<string, MarketContext>
54
38
  }
55
39
 
56
40
  /**
@@ -175,7 +159,6 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
175
159
  if (entry.pendingTeardown !== undefined) {
176
160
  clearTimeout(entry.pendingTeardown)
177
161
  }
178
- this.cancelPendingMarketsContext(entry)
179
162
  }
180
163
  this.onClose()
181
164
  this.rws.close()
@@ -199,50 +182,6 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
199
182
  }
200
183
  }
201
184
 
202
- /**
203
- * Deliver the all-markets context map to the `marketsContext` listeners,
204
- * at most once per {@link WS_MARKETS_CONTEXT_INTERVAL_MS}. The first call
205
- * after the channel opens delivers at once. Calls inside the gap keep only
206
- * the newest `snapshot`, which runs when the gap ends, so one delivered
207
- * event costs one map build regardless of the wire rate.
208
- */
209
- protected emitMarketsContext(
210
- snapshot: () => Record<string, MarketContext>
211
- ): void {
212
- const key = this.toKey({ channel: 'marketsContext', dex: this.providerKey })
213
- const entry = this.channels.get(key)
214
- if (entry === undefined || entry.listeners.size === 0) {
215
- return
216
- }
217
- if (entry.throttle !== undefined) {
218
- entry.pendingSnapshot = snapshot
219
- return
220
- }
221
- this.deliverMarketsContext(key, entry, snapshot)
222
- }
223
-
224
- private deliverMarketsContext(
225
- key: string,
226
- entry: ChannelEntry,
227
- snapshot: () => Record<string, MarketContext>
228
- ): void {
229
- entry.pendingSnapshot = undefined
230
- entry.throttle = setTimeout(() => {
231
- entry.throttle = undefined
232
- const pending = entry.pendingSnapshot
233
- if (pending !== undefined && this.channels.get(key) === entry) {
234
- this.deliverMarketsContext(key, entry, pending)
235
- }
236
- }, WS_MARKETS_CONTEXT_INTERVAL_MS)
237
- this.emit(key, { channel: 'marketsContext', data: snapshot() })
238
- }
239
-
240
- private cancelPendingMarketsContext(entry: ChannelEntry): void {
241
- clearTimeout(entry.throttle)
242
- entry.throttle = undefined
243
- entry.pendingSnapshot = undefined
244
- }
245
-
246
185
  /**
247
186
  * Tear `key` off the wire immediately if no listener holds it — i.e. it is
248
187
  * lingering in the deferred-teardown window (or its open is still
@@ -426,7 +365,6 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
426
365
  if (entry.listeners.size > 0) {
427
366
  return
428
367
  }
429
- this.cancelPendingMarketsContext(entry)
430
368
 
431
369
  if (entry.teardown === undefined) {
432
370
  // Open not resolved yet — acquireChannel's post-open check unwinds it.