@lifi/perps-sdk 32.0.0 → 33.0.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/README.md +45 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +7 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math/position.d.ts +2 -0
- package/dist/cjs/math/position.d.ts.map +1 -1
- package/dist/cjs/math/position.js +53 -0
- 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 +12 -0
- package/dist/esm/math/position.d.ts.map +1 -1
- package/dist/esm/math/position.js +61 -0
- 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 +12 -0
- 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 +3 -0
- package/src/math/position.ts +90 -0
- package/src/version.ts +1 -1
- package/src/websocket/WsProviderBase.ts +65 -1
package/src/math/position.ts
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
} from '../decimal/arithmetic.js'
|
|
25
25
|
import { DivBig } from '../decimal/big.js'
|
|
26
26
|
import { isDecimalStringGreaterThan } from '../decimal/compare.js'
|
|
27
|
+
import { roundDecimalString } from '../decimal/convert.js'
|
|
27
28
|
import {
|
|
28
29
|
bigToDecimalString,
|
|
29
30
|
decimalStringToBig,
|
|
@@ -594,3 +595,92 @@ export function isPositionMarginRemovable(position: Position): boolean {
|
|
|
594
595
|
PositionMarginAdjustment.ADD_AND_REMOVE
|
|
595
596
|
)
|
|
596
597
|
}
|
|
598
|
+
|
|
599
|
+
const isPositiveDecimalString = (value: unknown): value is string =>
|
|
600
|
+
isDecimalString(value) && isDecimalStringGreaterThan(value, '0')
|
|
601
|
+
|
|
602
|
+
/** Complete a position, retaining explicit venue value and ROE.
|
|
603
|
+
* Rebuild leverage and liquidation distance with half-up rounding to two decimals.
|
|
604
|
+
* Unusable inputs omit calculated fields. Never throws or warns.
|
|
605
|
+
* @public
|
|
606
|
+
*/
|
|
607
|
+
export function buildPosition(position: Position): Position {
|
|
608
|
+
const {
|
|
609
|
+
effectiveLeverage: _effectiveLeverage,
|
|
610
|
+
liquidationDistance: _liquidationDistance,
|
|
611
|
+
...fields
|
|
612
|
+
} = position
|
|
613
|
+
const built: Position = fields
|
|
614
|
+
const { size, markPrice, liquidationPrice, unrealizedPnl, marginUsed } =
|
|
615
|
+
position
|
|
616
|
+
const hasMarkPrice = isPositiveDecimalString(markPrice)
|
|
617
|
+
const hasMargin = isPositiveDecimalString(marginUsed)
|
|
618
|
+
if (
|
|
619
|
+
built.positionValue === undefined &&
|
|
620
|
+
hasMarkPrice &&
|
|
621
|
+
isDecimalString(size)
|
|
622
|
+
) {
|
|
623
|
+
built.positionValue = calculateNotionalValue(size, markPrice)
|
|
624
|
+
}
|
|
625
|
+
if (
|
|
626
|
+
built.returnOnEquity === undefined &&
|
|
627
|
+
hasMargin &&
|
|
628
|
+
isDecimalString(unrealizedPnl)
|
|
629
|
+
) {
|
|
630
|
+
built.returnOnEquity = calculateRoe(unrealizedPnl, marginUsed)
|
|
631
|
+
}
|
|
632
|
+
if (hasMargin && isDecimalString(built.positionValue)) {
|
|
633
|
+
built.effectiveLeverage = roundDecimalString(
|
|
634
|
+
calculateEffectiveLeverage({
|
|
635
|
+
positionValueUsd: built.positionValue,
|
|
636
|
+
marginUsd: marginUsed,
|
|
637
|
+
}),
|
|
638
|
+
2,
|
|
639
|
+
'round'
|
|
640
|
+
)
|
|
641
|
+
}
|
|
642
|
+
if (hasMarkPrice && isPositiveDecimalString(liquidationPrice)) {
|
|
643
|
+
built.liquidationDistance = roundDecimalString(
|
|
644
|
+
calculateLiquidationDistance({
|
|
645
|
+
liquidationPrice,
|
|
646
|
+
currentPrice: markPrice,
|
|
647
|
+
}),
|
|
648
|
+
2,
|
|
649
|
+
'round'
|
|
650
|
+
)
|
|
651
|
+
}
|
|
652
|
+
return built
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/** Recalculate mark-dependent fields using the input snapshot's margin.
|
|
656
|
+
* Invalid marks return the input. Invalid entry prices or sizes retain its PnL.
|
|
657
|
+
* The input is unchanged, and valid marks return a new position.
|
|
658
|
+
* @public
|
|
659
|
+
*/
|
|
660
|
+
export function remarkPosition(
|
|
661
|
+
position: Position,
|
|
662
|
+
markPrice: string
|
|
663
|
+
): Position {
|
|
664
|
+
if (!isPositiveDecimalString(markPrice)) {
|
|
665
|
+
return position
|
|
666
|
+
}
|
|
667
|
+
const {
|
|
668
|
+
positionValue: _positionValue,
|
|
669
|
+
returnOnEquity: _returnOnEquity,
|
|
670
|
+
...fields
|
|
671
|
+
} = position
|
|
672
|
+
const { entryPrice, size } = position
|
|
673
|
+
return buildPosition({
|
|
674
|
+
...fields,
|
|
675
|
+
markPrice,
|
|
676
|
+
unrealizedPnl:
|
|
677
|
+
isPositiveDecimalString(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)
|