@lifi/perps-sdk 0.0.0-preview-be3da034 → 0.0.0-preview-5f7146b6
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/README.md +1 -33
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +5 -6
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math/position.d.ts +0 -2
- package/dist/cjs/math/position.d.ts.map +1 -1
- package/dist/cjs/math/position.js +0 -54
- package/dist/cjs/math/position.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/websocket/WsProviderBase.d.ts +5 -1
- package/dist/cjs/websocket/WsProviderBase.d.ts.map +1 -1
- package/dist/cjs/websocket/WsProviderBase.js +34 -1
- package/dist/cjs/websocket/WsProviderBase.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/math/position.d.ts +0 -21
- package/dist/esm/math/position.d.ts.map +1 -1
- package/dist/esm/math/position.js +0 -71
- package/dist/esm/math/position.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/websocket/WsProviderBase.d.ts +18 -1
- package/dist/esm/websocket/WsProviderBase.d.ts.map +1 -1
- package/dist/esm/websocket/WsProviderBase.js +46 -0
- package/dist/esm/websocket/WsProviderBase.js.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math/position.d.ts +0 -21
- package/dist/types/math/position.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/dist/types/websocket/WsProviderBase.d.ts +18 -1
- package/dist/types/websocket/WsProviderBase.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -2
- package/src/math/position.ts +0 -90
- package/src/version.ts +1 -1
- package/src/websocket/WsProviderBase.ts +65 -1
package/src/math/position.ts
CHANGED
|
@@ -594,93 +594,3 @@ 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
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
MarketContext,
|
|
3
|
+
Subscription,
|
|
4
|
+
SubscriptionEvent,
|
|
5
|
+
} from '@lifi/perps-types'
|
|
2
6
|
import type {
|
|
3
7
|
ProviderGetQuoteParams,
|
|
4
8
|
QuoteListener,
|
|
@@ -22,6 +26,14 @@ import { wsLog } from './wsLog.js'
|
|
|
22
26
|
*/
|
|
23
27
|
export const WS_CHANNEL_TEARDOWN_LINGER_MS = 250
|
|
24
28
|
|
|
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
|
+
|
|
25
37
|
interface ChannelEntry {
|
|
26
38
|
/**
|
|
27
39
|
* Per-listener ref count — a `Map`, not a `Set`, because the same listener
|
|
@@ -35,6 +47,10 @@ interface ChannelEntry {
|
|
|
35
47
|
opening?: Promise<() => void>
|
|
36
48
|
/** Deferred-teardown timer, set on the 1→0 transition; cleared if re-subscribed within the linger. */
|
|
37
49
|
pendingTeardown?: ReturnType<typeof setTimeout>
|
|
50
|
+
/** `marketsContext` gap timer, set on each delivery and cleared with the channel; 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>
|
|
38
54
|
}
|
|
39
55
|
|
|
40
56
|
/**
|
|
@@ -159,6 +175,7 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
159
175
|
if (entry.pendingTeardown !== undefined) {
|
|
160
176
|
clearTimeout(entry.pendingTeardown)
|
|
161
177
|
}
|
|
178
|
+
this.cancelPendingMarketsContext(entry)
|
|
162
179
|
}
|
|
163
180
|
this.onClose()
|
|
164
181
|
this.rws.close()
|
|
@@ -182,6 +199,50 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
182
199
|
}
|
|
183
200
|
}
|
|
184
201
|
|
|
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
|
+
|
|
185
246
|
/**
|
|
186
247
|
* Tear `key` off the wire immediately if no listener holds it — i.e. it is
|
|
187
248
|
* lingering in the deferred-teardown window (or its open is still
|
|
@@ -201,6 +262,7 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
201
262
|
if (entry.pendingTeardown !== undefined) {
|
|
202
263
|
clearTimeout(entry.pendingTeardown)
|
|
203
264
|
}
|
|
265
|
+
this.cancelPendingMarketsContext(entry)
|
|
204
266
|
entry.teardown?.()
|
|
205
267
|
this.channels.delete(key)
|
|
206
268
|
return true
|
|
@@ -365,6 +427,7 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
365
427
|
if (entry.listeners.size > 0) {
|
|
366
428
|
return
|
|
367
429
|
}
|
|
430
|
+
entry.pendingSnapshot = undefined
|
|
368
431
|
|
|
369
432
|
if (entry.teardown === undefined) {
|
|
370
433
|
// Open not resolved yet — acquireChannel's post-open check unwinds it.
|
|
@@ -386,6 +449,7 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
386
449
|
e.pendingTeardown = undefined
|
|
387
450
|
return
|
|
388
451
|
}
|
|
452
|
+
this.cancelPendingMarketsContext(e)
|
|
389
453
|
e.teardown?.()
|
|
390
454
|
this.channels.delete(key)
|
|
391
455
|
}, WS_CHANNEL_TEARDOWN_LINGER_MS)
|