@lifi/perps-sdk 31.1.1 → 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 +84 -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/PerpsWsClient.d.ts +3 -1
- package/dist/cjs/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/cjs/websocket/PerpsWsClient.js +71 -0
- package/dist/cjs/websocket/PerpsWsClient.js.map +1 -1
- package/dist/cjs/websocket/WsProviderBase.d.ts +7 -1
- package/dist/cjs/websocket/WsProviderBase.d.ts.map +1 -1
- package/dist/cjs/websocket/WsProviderBase.js +54 -1
- package/dist/cjs/websocket/WsProviderBase.js.map +1 -1
- package/dist/cjs/websocket/types.d.ts +1 -0
- package/dist/cjs/websocket/types.d.ts.map +1 -1
- package/dist/cjs/websocket/wsLog.d.ts +1 -0
- package/dist/cjs/websocket/wsLog.d.ts.map +1 -1
- package/dist/cjs/websocket/wsLog.js +3 -0
- package/dist/cjs/websocket/wsLog.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/PerpsWsClient.d.ts +8 -1
- package/dist/esm/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/esm/websocket/PerpsWsClient.js +76 -0
- package/dist/esm/websocket/PerpsWsClient.js.map +1 -1
- package/dist/esm/websocket/WsProviderBase.d.ts +21 -1
- package/dist/esm/websocket/WsProviderBase.d.ts.map +1 -1
- package/dist/esm/websocket/WsProviderBase.js +67 -0
- package/dist/esm/websocket/WsProviderBase.js.map +1 -1
- package/dist/esm/websocket/types.d.ts +6 -6
- package/dist/esm/websocket/types.d.ts.map +1 -1
- package/dist/esm/websocket/wsLog.d.ts +6 -0
- package/dist/esm/websocket/wsLog.d.ts.map +1 -1
- package/dist/esm/websocket/wsLog.js +8 -0
- package/dist/esm/websocket/wsLog.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/PerpsWsClient.d.ts +8 -1
- package/dist/types/websocket/PerpsWsClient.d.ts.map +1 -1
- package/dist/types/websocket/WsProviderBase.d.ts +21 -1
- package/dist/types/websocket/WsProviderBase.d.ts.map +1 -1
- package/dist/types/websocket/types.d.ts +6 -6
- package/dist/types/websocket/types.d.ts.map +1 -1
- package/dist/types/websocket/wsLog.d.ts +6 -0
- package/dist/types/websocket/wsLog.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/PerpsWsClient.ts +83 -0
- package/src/websocket/WsProviderBase.ts +87 -1
- package/src/websocket/types.ts +6 -6
- package/src/websocket/wsLog.ts +8 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Subscription, SubscriptionEvent } from '@lifi/perps-types';
|
|
1
|
+
import type { MarketContext, Subscription, SubscriptionEvent } from '@lifi/perps-types';
|
|
2
2
|
import type { ProviderGetQuoteParams, QuoteListener } from '../types/provider.js';
|
|
3
3
|
import type { ReconnectingWebSocket } from './ReconnectingWebSocket.js';
|
|
4
4
|
import type { SubscriptionListener, WsProvider, WsStatusListener } from './types.js';
|
|
@@ -12,6 +12,13 @@ import type { SubscriptionListener, WsProvider, WsStatusListener } from './types
|
|
|
12
12
|
* @public
|
|
13
13
|
*/
|
|
14
14
|
export declare const WS_CHANNEL_TEARDOWN_LINGER_MS = 250;
|
|
15
|
+
/**
|
|
16
|
+
* Minimum gap between two `marketsContext` deliveries on one provider. Wire
|
|
17
|
+
* frames inside the gap coalesce; the newest one is delivered when it ends.
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export declare const WS_MARKETS_CONTEXT_INTERVAL_MS = 1000;
|
|
15
22
|
/**
|
|
16
23
|
* Base for per-provider realtime transports. Owns the {@link
|
|
17
24
|
* ReconnectingWebSocket}, the fan-out lifecycle — ref-counted multi-listener
|
|
@@ -34,8 +41,11 @@ export declare abstract class WsProviderBase<TSub = unknown> implements WsProvid
|
|
|
34
41
|
protected readonly providerKey: string;
|
|
35
42
|
private readonly channels;
|
|
36
43
|
private readonly statusListeners;
|
|
44
|
+
private readonly statusObservers;
|
|
37
45
|
private readonly wireSubs;
|
|
38
46
|
constructor(rws: ReconnectingWebSocket, providerKey: string);
|
|
47
|
+
/** Observe transport status without channel acquisition or socket recovery. */
|
|
48
|
+
subscribeStatus(listener: WsStatusListener): () => void;
|
|
39
49
|
/**
|
|
40
50
|
* Acquire a ref-counted subscription to a provider channel and return an
|
|
41
51
|
* idempotent release function. Optional status listeners are reference-counted
|
|
@@ -58,6 +68,16 @@ export declare abstract class WsProviderBase<TSub = unknown> implements WsProvid
|
|
|
58
68
|
close(): void;
|
|
59
69
|
/** Deliver an event to every listener on `key`. */
|
|
60
70
|
protected emit(key: string, event: SubscriptionEvent): void;
|
|
71
|
+
/**
|
|
72
|
+
* Deliver the all-markets context map to the `marketsContext` listeners,
|
|
73
|
+
* at most once per {@link WS_MARKETS_CONTEXT_INTERVAL_MS}. The first call
|
|
74
|
+
* after the channel opens delivers at once. Calls inside the gap keep only
|
|
75
|
+
* the newest `snapshot`, which runs when the gap ends, so one delivered
|
|
76
|
+
* event costs one map build regardless of the wire rate.
|
|
77
|
+
*/
|
|
78
|
+
protected emitMarketsContext(snapshot: () => Record<string, MarketContext>): void;
|
|
79
|
+
private deliverMarketsContext;
|
|
80
|
+
private cancelPendingMarketsContext;
|
|
61
81
|
/**
|
|
62
82
|
* Tear `key` off the wire immediately if no listener holds it — i.e. it is
|
|
63
83
|
* lingering in the deferred-teardown window (or its open is still
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WsProviderBase.d.ts","sourceRoot":"","sources":["../../../src/websocket/WsProviderBase.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"WsProviderBase.d.ts","sourceRoot":"","sources":["../../../src/websocket/WsProviderBase.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,YAAY,EACZ,iBAAiB,EAClB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,KAAK,EACV,sBAAsB,EACtB,aAAa,EACd,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA;AACvE,OAAO,KAAK,EACV,oBAAoB,EACpB,UAAU,EACV,gBAAgB,EACjB,MAAM,YAAY,CAAA;AAGnB;;;;;;;;GAQG;AACH,eAAO,MAAM,6BAA6B,MAAM,CAAA;AAEhD;;;;;GAKG;AACH,eAAO,MAAM,8BAA8B,OAAQ,CAAA;AAqBnD;;;;;;;;;;;;;;;;GAgBG;AACH,8BAAsB,cAAc,CAAC,IAAI,GAAG,OAAO,CAAE,YAAW,UAAU;IACxE,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,qBAAqB,CAAA;IAC7C,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAEtC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkC;IAC3D,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAsC;IACtE,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA8B;IAC9D,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA0B;gBAEvC,GAAG,EAAE,qBAAqB,EAAE,WAAW,EAAE,MAAM;IAc3D,+EAA+E;IAC/E,eAAe,CAAC,QAAQ,EAAE,gBAAgB,GAAG,MAAM,IAAI;IAgBvD;;;;;;OAMG;IACG,SAAS,CACb,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,oBAAoB,EAC9B,QAAQ,CAAC,EAAE,gBAAgB,GAC1B,OAAO,CAAC,MAAM,IAAI,CAAC;IAgCtB;;;;OAIG;IACH,SAAS,IAAI,IAAI;IAOjB;;;;OAIG;IACH,KAAK,IAAI,IAAI;IAkBb,mDAAmD;IACnD,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,iBAAiB,GAAG,IAAI;IAU3D;;;;;;OAMG;IACH,SAAS,CAAC,kBAAkB,CAC1B,QAAQ,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,GAC5C,IAAI;IAaP,OAAO,CAAC,qBAAqB;IAgB7B,OAAO,CAAC,2BAA2B;IAMnC;;;;;;;OAOG;IACH,SAAS,CAAC,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAiBlD;;;;;;OAMG;cACa,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAepE,4EAA4E;IAC5E,SAAS,CAAC,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;YAI5B,UAAU;IAqBxB,qFAAqF;IACrF,QAAQ,CAAC,cAAc,CACrB,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,MAAM,IAAI,CAAC;IAEtB,iFAAiF;IACjF,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM;IAEnD;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,WAAW,CAAC,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,IAAI,CAAC;IAEtE;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAEnE,6DAA6D;IAC7D,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAEnD,mFAAmF;IACnF,SAAS,CAAC,OAAO,IAAI,IAAI;IAEzB,OAAO,CAAC,YAAY;YAYN,cAAc;IAsD5B,OAAO,CAAC,cAAc;CA0CvB"}
|
|
@@ -18,12 +18,10 @@ export type SubscriptionListener = (event: SubscriptionEvent) => void;
|
|
|
18
18
|
/**
|
|
19
19
|
* Health of a provider's underlying WS connection.
|
|
20
20
|
*
|
|
21
|
-
* - `connected` — socket open
|
|
22
|
-
* - `reconnecting` —
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* connection was explicitly closed. The connection is permanently dead and
|
|
26
|
-
* will not recover on its own; the subscription's data is stale. Terminal.
|
|
21
|
+
* - `connected` — socket open. This does not prove channel freshness or authentication.
|
|
22
|
+
* - `reconnecting` — initial connection or automatic recovery in progress.
|
|
23
|
+
* - `disconnected` — retry budget exhausted or explicitly closed.
|
|
24
|
+
* Recovery requires an explicit reconnect.
|
|
27
25
|
*
|
|
28
26
|
* @public
|
|
29
27
|
*/
|
|
@@ -46,6 +44,8 @@ export interface WsProvider {
|
|
|
46
44
|
* `onStatus` receives the provider socket's health transitions.
|
|
47
45
|
*/
|
|
48
46
|
subscribe(sub: Subscription, listener: SubscriptionListener, onStatus?: WsStatusListener): Promise<() => void>;
|
|
47
|
+
/** Observe current socket status and transitions without a channel or reconnect. Release is idempotent. */
|
|
48
|
+
subscribeStatus(listener: WsStatusListener): () => void;
|
|
49
49
|
/**
|
|
50
50
|
* Recover a terminally exhausted connection. Safe no-op unless status is
|
|
51
51
|
* `disconnected`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/websocket/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACxE,OAAO,KAAK,EACV,sBAAsB,EACtB,aAAa,EACd,MAAM,sBAAsB,CAAA;AAE7B;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,YAAY,IAAI,OAAO,CAChE,iBAAiB,EACjB;IAAE,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC,CAAA;CAAE,CAC1B,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAA;AAErE
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/websocket/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACxE,OAAO,KAAK,EACV,sBAAsB,EACtB,aAAa,EACd,MAAM,sBAAsB,CAAA;AAE7B;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,YAAY,IAAI,OAAO,CAChE,iBAAiB,EACjB;IAAE,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC,CAAA;CAAE,CAC1B,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAA;AAErE;;;;;;;;;GASG;AACH,MAAM,MAAM,kBAAkB,GAAG,WAAW,GAAG,cAAc,GAAG,cAAc,CAAA;AAE9E;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,kBAAkB,KAAK,IAAI,CAAA;AAEnE;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IACzB;;;OAGG;IACH,SAAS,CACP,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,oBAAoB,EAC9B,QAAQ,CAAC,EAAE,gBAAgB,GAC1B,OAAO,CAAC,MAAM,IAAI,CAAC,CAAA;IACtB,2GAA2G;IAC3G,eAAe,CAAC,QAAQ,EAAE,gBAAgB,GAAG,MAAM,IAAI,CAAA;IACvD;;;OAGG;IACH,SAAS,IAAI,IAAI,CAAA;IACjB;;;;;;OAMG;IACH,cAAc,CACZ,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,aAAa,GACrB,OAAO,CAAC,MAAM,IAAI,CAAC,CAAA;IACtB,4DAA4D;IAC5D,KAAK,IAAI,IAAI,CAAA;CACd"}
|
|
@@ -58,5 +58,11 @@ export declare const wsLog: {
|
|
|
58
58
|
* one bad listener cannot starve sibling listeners or break socket flow.
|
|
59
59
|
*/
|
|
60
60
|
readonly listenerFailure: (provider: string, listenerType: string, error: unknown) => void;
|
|
61
|
+
/**
|
|
62
|
+
* A provider's `subscribeStatus` threw while a status-only observer attached.
|
|
63
|
+
* That observer receives no transport status; data subscriptions and sibling
|
|
64
|
+
* observers continue.
|
|
65
|
+
*/
|
|
66
|
+
readonly statusObserverFailure: (provider: string, error: unknown) => void;
|
|
61
67
|
};
|
|
62
68
|
//# sourceMappingURL=wsLog.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wsLog.d.ts","sourceRoot":"","sources":["../../../src/websocket/wsLog.ts"],"names":[],"mappings":"AAeA;;;;;;GAMG;AACH,eAAO,MAAM,KAAK;IAChB;;;OAGG;sCACoB,MAAM,OAAO,MAAM,KAAG,IAAI;IAGjD;;;;OAIG;wCACsB,MAAM,SAAS,OAAO,KAAG,IAAI;IAGtD;;;;;OAKG;qCACmB,MAAM,WAAW,MAAM,KAAG,IAAI;IAGpD;;;;OAIG;0CACwB,MAAM,WAAW,MAAM,SAAS,OAAO,KAAG,IAAI;IAMzE;;;;OAIG;uCACqB,MAAM,YAAY,MAAM,KAAG,IAAI;IAKvD;;;;OAIG;oCAES,MAAM,OACX,MAAM,SACJ,MAAM,SACN,OAAO,aACH,MAAM,KAChB,IAAI;IAaP;;;;OAIG;oCACkB,MAAM,OAAO,MAAM,UAAU,MAAM,KAAG,IAAI;IAO/D;;;OAGG;8CAC4B,MAAM,SAAS,OAAO,KAAG,IAAI;IAG5D;;;OAGG;yCAES,MAAM,gBACF,MAAM,SACb,OAAO,KACb,IAAI;
|
|
1
|
+
{"version":3,"file":"wsLog.d.ts","sourceRoot":"","sources":["../../../src/websocket/wsLog.ts"],"names":[],"mappings":"AAeA;;;;;;GAMG;AACH,eAAO,MAAM,KAAK;IAChB;;;OAGG;sCACoB,MAAM,OAAO,MAAM,KAAG,IAAI;IAGjD;;;;OAIG;wCACsB,MAAM,SAAS,OAAO,KAAG,IAAI;IAGtD;;;;;OAKG;qCACmB,MAAM,WAAW,MAAM,KAAG,IAAI;IAGpD;;;;OAIG;0CACwB,MAAM,WAAW,MAAM,SAAS,OAAO,KAAG,IAAI;IAMzE;;;;OAIG;uCACqB,MAAM,YAAY,MAAM,KAAG,IAAI;IAKvD;;;;OAIG;oCAES,MAAM,OACX,MAAM,SACJ,MAAM,SACN,OAAO,aACH,MAAM,KAChB,IAAI;IAaP;;;;OAIG;oCACkB,MAAM,OAAO,MAAM,UAAU,MAAM,KAAG,IAAI;IAO/D;;;OAGG;8CAC4B,MAAM,SAAS,OAAO,KAAG,IAAI;IAG5D;;;OAGG;yCAES,MAAM,gBACF,MAAM,SACb,OAAO,KACb,IAAI;IAMP;;;;OAIG;+CAC6B,MAAM,SAAS,OAAO,KAAG,IAAI;CAGrD,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lifi/perps-sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "33.0.0",
|
|
4
4
|
"description": "LI.FI Perps SDK for perpetuals trading",
|
|
5
5
|
"homepage": "https://github.com/lifinance/perps-sdk",
|
|
6
6
|
"bugs": {
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@lifi/types": "^18.3.0",
|
|
46
46
|
"big.js": "^7.0.1",
|
|
47
47
|
"partysocket": "^1.1.19",
|
|
48
|
-
"@lifi/perps-types": "^
|
|
48
|
+
"@lifi/perps-types": "^28.0.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"viem": ">=2.57.11 <2.57.12"
|
package/src/index.ts
CHANGED
|
@@ -110,6 +110,7 @@ export {
|
|
|
110
110
|
safeResolveCloseSize,
|
|
111
111
|
} from './math/order.js'
|
|
112
112
|
export {
|
|
113
|
+
buildPosition,
|
|
113
114
|
calculateEffectiveLeverage,
|
|
114
115
|
calculateInitialMarginRoe,
|
|
115
116
|
calculateLiquidationDistance,
|
|
@@ -126,6 +127,7 @@ export {
|
|
|
126
127
|
estimateUnrealizedPnl,
|
|
127
128
|
isPositionMarginAdjustable,
|
|
128
129
|
isPositionMarginRemovable,
|
|
130
|
+
remarkPosition,
|
|
129
131
|
safeCalculateEffectiveLeverage,
|
|
130
132
|
safeCalculateInitialMarginRoe,
|
|
131
133
|
safeCalculateLiquidationDistance,
|
|
@@ -370,6 +372,7 @@ export type {
|
|
|
370
372
|
} from './websocket/types.js'
|
|
371
373
|
export {
|
|
372
374
|
WS_CHANNEL_TEARDOWN_LINGER_MS,
|
|
375
|
+
WS_MARKETS_CONTEXT_INTERVAL_MS,
|
|
373
376
|
WsProviderBase,
|
|
374
377
|
} from './websocket/WsProviderBase.js'
|
|
375
378
|
export { wsLog } from './websocket/wsLog.js'
|
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
|
@@ -11,9 +11,16 @@ import { cachePromise } from './cachePromise.js'
|
|
|
11
11
|
import type {
|
|
12
12
|
EventForSubscription,
|
|
13
13
|
SubscriptionListener,
|
|
14
|
+
WsConnectionStatus,
|
|
14
15
|
WsProvider,
|
|
15
16
|
WsStatusListener,
|
|
16
17
|
} from './types.js'
|
|
18
|
+
import { wsLog } from './wsLog.js'
|
|
19
|
+
|
|
20
|
+
interface StatusObserver {
|
|
21
|
+
attach(provider: WsProvider): void
|
|
22
|
+
release(): void
|
|
23
|
+
}
|
|
17
24
|
|
|
18
25
|
/**
|
|
19
26
|
* Call parameters of a {@link WsProviderFactory}.
|
|
@@ -74,6 +81,7 @@ export class PerpsWsClient {
|
|
|
74
81
|
private readonly options: PerpsWsClientOptions
|
|
75
82
|
private providers = new Map<string, WsProvider>()
|
|
76
83
|
private initPromises = new Map<string, Promise<WsProvider>>()
|
|
84
|
+
private readonly statusObservers = new Map<string, Set<StatusObserver>>()
|
|
77
85
|
private discovery: Promise<ProvidersResponse> | undefined
|
|
78
86
|
private closed = false
|
|
79
87
|
|
|
@@ -107,6 +115,71 @@ export class PerpsWsClient {
|
|
|
107
115
|
)
|
|
108
116
|
}
|
|
109
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Observe one provider's transport without creating it or requesting data.
|
|
120
|
+
* Reports the current status synchronously, or `null` before provider creation.
|
|
121
|
+
* Release is idempotent. Client close silently releases observers and rejects new registrations.
|
|
122
|
+
*/
|
|
123
|
+
subscribeStatus(
|
|
124
|
+
provider: string,
|
|
125
|
+
listener: (status: WsConnectionStatus | null) => void
|
|
126
|
+
): () => void {
|
|
127
|
+
this.assertOpen()
|
|
128
|
+
const observers =
|
|
129
|
+
this.statusObservers.get(provider) ?? new Set<StatusObserver>()
|
|
130
|
+
this.statusObservers.set(provider, observers)
|
|
131
|
+
let released = false
|
|
132
|
+
let releaseProvider: (() => void) | undefined
|
|
133
|
+
const notify = (status: WsConnectionStatus | null) => {
|
|
134
|
+
if (released || this.closed) {
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
listener(status)
|
|
139
|
+
} catch (error) {
|
|
140
|
+
wsLog.listenerFailure(provider, 'status', error)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const observer: StatusObserver = {
|
|
144
|
+
attach: (transport) => {
|
|
145
|
+
if (released || this.closed) {
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
let release: () => void
|
|
149
|
+
try {
|
|
150
|
+
release = transport.subscribeStatus(notify)
|
|
151
|
+
} catch (error) {
|
|
152
|
+
wsLog.statusObserverFailure(provider, error)
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
if (released || this.closed) {
|
|
156
|
+
release()
|
|
157
|
+
} else {
|
|
158
|
+
releaseProvider = release
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
release: () => {
|
|
162
|
+
if (released) {
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
released = true
|
|
166
|
+
releaseProvider?.()
|
|
167
|
+
observers.delete(observer)
|
|
168
|
+
if (observers.size === 0) {
|
|
169
|
+
this.statusObservers.delete(provider)
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
}
|
|
173
|
+
observers.add(observer)
|
|
174
|
+
const transport = this.providers.get(provider)
|
|
175
|
+
if (transport) {
|
|
176
|
+
observer.attach(transport)
|
|
177
|
+
} else {
|
|
178
|
+
notify(null)
|
|
179
|
+
}
|
|
180
|
+
return observer.release
|
|
181
|
+
}
|
|
182
|
+
|
|
110
183
|
/**
|
|
111
184
|
* Subscribe to a WebSocket channel, lazily creating the provider's WS
|
|
112
185
|
* connection. Returns an unsubscribe function.
|
|
@@ -168,6 +241,12 @@ export class PerpsWsClient {
|
|
|
168
241
|
*/
|
|
169
242
|
close() {
|
|
170
243
|
this.closed = true
|
|
244
|
+
for (const observers of this.statusObservers.values()) {
|
|
245
|
+
for (const observer of observers) {
|
|
246
|
+
observer.release()
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
this.statusObservers.clear()
|
|
171
250
|
for (const p of this.providers.values()) {
|
|
172
251
|
p.close()
|
|
173
252
|
}
|
|
@@ -239,6 +318,10 @@ export class PerpsWsClient {
|
|
|
239
318
|
client: this.client,
|
|
240
319
|
})
|
|
241
320
|
this.providers.set(provider, wsProvider)
|
|
321
|
+
for (const observer of [...(this.statusObservers.get(provider) ?? [])]) {
|
|
322
|
+
observer.attach(wsProvider)
|
|
323
|
+
}
|
|
324
|
+
this.assertOpen()
|
|
242
325
|
return wsProvider
|
|
243
326
|
}
|
|
244
327
|
|
|
@@ -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
|
/**
|
|
@@ -60,6 +76,7 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
60
76
|
|
|
61
77
|
private readonly channels = new Map<string, ChannelEntry>()
|
|
62
78
|
private readonly statusListeners = new Map<WsStatusListener, number>()
|
|
79
|
+
private readonly statusObservers = new Set<WsStatusListener>()
|
|
63
80
|
private readonly wireSubs = new Map<string, TSub>()
|
|
64
81
|
|
|
65
82
|
constructor(rws: ReconnectingWebSocket, providerKey: string) {
|
|
@@ -76,6 +93,23 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
76
93
|
})
|
|
77
94
|
}
|
|
78
95
|
|
|
96
|
+
/** Observe transport status without channel acquisition or socket recovery. */
|
|
97
|
+
subscribeStatus(listener: WsStatusListener): () => void {
|
|
98
|
+
const notify: WsStatusListener = (status) => {
|
|
99
|
+
if (this.statusObservers.has(notify)) {
|
|
100
|
+
this.callListener('status', listener, status)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
this.statusObservers.add(notify)
|
|
104
|
+
this.rws.onStatus(notify)
|
|
105
|
+
notify(this.rws.getStatus())
|
|
106
|
+
return () => {
|
|
107
|
+
if (this.statusObservers.delete(notify)) {
|
|
108
|
+
this.rws.offStatus(notify)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
79
113
|
/**
|
|
80
114
|
* Acquire a ref-counted subscription to a provider channel and return an
|
|
81
115
|
* idempotent release function. Optional status listeners are reference-counted
|
|
@@ -141,9 +175,14 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
141
175
|
if (entry.pendingTeardown !== undefined) {
|
|
142
176
|
clearTimeout(entry.pendingTeardown)
|
|
143
177
|
}
|
|
178
|
+
this.cancelPendingMarketsContext(entry)
|
|
144
179
|
}
|
|
145
180
|
this.onClose()
|
|
146
181
|
this.rws.close()
|
|
182
|
+
for (const observer of this.statusObservers) {
|
|
183
|
+
this.rws.offStatus(observer)
|
|
184
|
+
}
|
|
185
|
+
this.statusObservers.clear()
|
|
147
186
|
this.channels.clear()
|
|
148
187
|
this.statusListeners.clear()
|
|
149
188
|
this.wireSubs.clear()
|
|
@@ -160,6 +199,50 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
160
199
|
}
|
|
161
200
|
}
|
|
162
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
|
+
|
|
163
246
|
/**
|
|
164
247
|
* Tear `key` off the wire immediately if no listener holds it — i.e. it is
|
|
165
248
|
* lingering in the deferred-teardown window (or its open is still
|
|
@@ -179,6 +262,7 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
179
262
|
if (entry.pendingTeardown !== undefined) {
|
|
180
263
|
clearTimeout(entry.pendingTeardown)
|
|
181
264
|
}
|
|
265
|
+
this.cancelPendingMarketsContext(entry)
|
|
182
266
|
entry.teardown?.()
|
|
183
267
|
this.channels.delete(key)
|
|
184
268
|
return true
|
|
@@ -343,6 +427,7 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
343
427
|
if (entry.listeners.size > 0) {
|
|
344
428
|
return
|
|
345
429
|
}
|
|
430
|
+
entry.pendingSnapshot = undefined
|
|
346
431
|
|
|
347
432
|
if (entry.teardown === undefined) {
|
|
348
433
|
// Open not resolved yet — acquireChannel's post-open check unwinds it.
|
|
@@ -364,6 +449,7 @@ export abstract class WsProviderBase<TSub = unknown> implements WsProvider {
|
|
|
364
449
|
e.pendingTeardown = undefined
|
|
365
450
|
return
|
|
366
451
|
}
|
|
452
|
+
this.cancelPendingMarketsContext(e)
|
|
367
453
|
e.teardown?.()
|
|
368
454
|
this.channels.delete(key)
|
|
369
455
|
}, WS_CHANNEL_TEARDOWN_LINGER_MS)
|
package/src/websocket/types.ts
CHANGED
|
@@ -25,12 +25,10 @@ export type SubscriptionListener = (event: SubscriptionEvent) => void
|
|
|
25
25
|
/**
|
|
26
26
|
* Health of a provider's underlying WS connection.
|
|
27
27
|
*
|
|
28
|
-
* - `connected` — socket open
|
|
29
|
-
* - `reconnecting` —
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* connection was explicitly closed. The connection is permanently dead and
|
|
33
|
-
* will not recover on its own; the subscription's data is stale. Terminal.
|
|
28
|
+
* - `connected` — socket open. This does not prove channel freshness or authentication.
|
|
29
|
+
* - `reconnecting` — initial connection or automatic recovery in progress.
|
|
30
|
+
* - `disconnected` — retry budget exhausted or explicitly closed.
|
|
31
|
+
* Recovery requires an explicit reconnect.
|
|
34
32
|
*
|
|
35
33
|
* @public
|
|
36
34
|
*/
|
|
@@ -59,6 +57,8 @@ export interface WsProvider {
|
|
|
59
57
|
listener: SubscriptionListener,
|
|
60
58
|
onStatus?: WsStatusListener
|
|
61
59
|
): Promise<() => void>
|
|
60
|
+
/** Observe current socket status and transitions without a channel or reconnect. Release is idempotent. */
|
|
61
|
+
subscribeStatus(listener: WsStatusListener): () => void
|
|
62
62
|
/**
|
|
63
63
|
* Recover a terminally exhausted connection. Safe no-op unless status is
|
|
64
64
|
* `disconnected`.
|
package/src/websocket/wsLog.ts
CHANGED
|
@@ -123,4 +123,12 @@ export const wsLog = {
|
|
|
123
123
|
error
|
|
124
124
|
)
|
|
125
125
|
},
|
|
126
|
+
/**
|
|
127
|
+
* A provider's `subscribeStatus` threw while a status-only observer attached.
|
|
128
|
+
* That observer receives no transport status; data subscriptions and sibling
|
|
129
|
+
* observers continue.
|
|
130
|
+
*/
|
|
131
|
+
statusObserverFailure(provider: string, error: unknown): void {
|
|
132
|
+
console.error(`[${provider}:ws] status observer registration failed`, error)
|
|
133
|
+
},
|
|
126
134
|
} as const
|