@metamask-previews/perps-controller 22.3.0-preview-693aef2bf → 22.3.0-preview-91dd16c
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/CHANGELOG.md +8 -0
- package/dist/PerpsController-method-action-types.d.ts +19 -1
- package/dist/PerpsController-method-action-types.d.ts.map +1 -1
- package/dist/PerpsController-method-action-types.js.map +1 -1
- package/dist/PerpsController.d.ts +16 -1
- package/dist/PerpsController.d.ts.map +1 -1
- package/dist/PerpsController.js +32 -0
- package/dist/PerpsController.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.d.ts +10 -1
- package/dist/providers/AggregatedPerpsProvider.d.ts.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.js +20 -0
- package/dist/providers/AggregatedPerpsProvider.js.map +1 -1
- package/dist/providers/HyperLiquidProvider.d.ts +10 -1
- package/dist/providers/HyperLiquidProvider.d.ts.map +1 -1
- package/dist/providers/HyperLiquidProvider.js +20 -0
- package/dist/providers/HyperLiquidProvider.js.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.ts +24 -1
- package/dist/services/HyperLiquidSubscriptionService.d.ts.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.js +131 -0
- package/dist/services/HyperLiquidSubscriptionService.js.map +1 -1
- package/dist/types/index.d.ts +48 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CaipAccountId } from '@metamask/utils';
|
|
2
|
+
import type { TwapHistoryResponse, ActiveAssetDataWsEvent, AssetCtxsWsEvent } from '@nktkas/hyperliquid';
|
|
2
3
|
import type { HyperLiquidAbstractionMode } from '../types/hyperliquid-types.js';
|
|
3
4
|
import type { Position, OrderFill, Order, TwapOrder, SubscribePricesParams, SubscribePositionsParams, SubscribeOrderFillsParams, SubscribeOrdersParams, SubscribeTwapOrdersParams, SubscribeAccountParams, SubscribeOICapsParams, SubscribeOrderBookParams, PerpsPlatformDependencies } from '../types/index.js';
|
|
4
5
|
import type { HyperLiquidClientService } from './HyperLiquidClientService.js';
|
|
@@ -254,6 +255,28 @@ export declare class HyperLiquidSubscriptionService {
|
|
|
254
255
|
* @returns Cleanup function to unsubscribe
|
|
255
256
|
*/
|
|
256
257
|
subscribeToOrderBook(params: SubscribeOrderBookParams): () => void;
|
|
258
|
+
/**
|
|
259
|
+
* Stream the venue's `activeAssetData` for one account and market. Each
|
|
260
|
+
* caller gets its own venue subscription, which restoreSubscriptions()
|
|
261
|
+
* reopens on a replacement transport and clearAll() retires. The venue
|
|
262
|
+
* pushes a new frame when the market's position, leverage or mark price
|
|
263
|
+
* changes.
|
|
264
|
+
*
|
|
265
|
+
* @param params - Subscription parameters.
|
|
266
|
+
* @param params.symbol - Market symbol, including any DEX prefix.
|
|
267
|
+
* @param params.accountId - Optional CAIP account ID; defaults to the selected account.
|
|
268
|
+
* @param params.callback - Receives each frame for this account and market.
|
|
269
|
+
* @param params.onError - Receives a failure to open the subscription, or
|
|
270
|
+
* one that stops it after it opened, once transient failures have been
|
|
271
|
+
* retried on the same transport.
|
|
272
|
+
* @returns Cleanup function to unsubscribe.
|
|
273
|
+
*/
|
|
274
|
+
subscribeToActiveAssetData(params: {
|
|
275
|
+
symbol: string;
|
|
276
|
+
accountId?: CaipAccountId;
|
|
277
|
+
callback: (data: ActiveAssetDataWsEvent) => void;
|
|
278
|
+
onError?: (error: Error) => void;
|
|
279
|
+
}): () => void;
|
|
257
280
|
/**
|
|
258
281
|
* Restore all active subscriptions after WebSocket reconnection
|
|
259
282
|
* Re-establishes WebSocket subscriptions for all active subscribers
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HyperLiquidSubscriptionService.d.ts","sourceRoot":"","sources":["../../src/services/HyperLiquidSubscriptionService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"HyperLiquidSubscriptionService.d.ts","sourceRoot":"","sources":["../../src/services/HyperLiquidSubscriptionService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,KAAK,EAMV,mBAAmB,EAEnB,sBAAsB,EAItB,gBAAgB,EAMjB,MAAM,qBAAqB,CAAC;AAO7B,OAAO,KAAK,EAEV,0BAA0B,EAC3B,MAAM,+BAA+B,CAAC;AAGvC,OAAO,KAAK,EAEV,QAAQ,EACR,SAAS,EACT,KAAK,EAEL,SAAS,EACT,qBAAqB,EACrB,wBAAwB,EACxB,yBAAyB,EACzB,qBAAqB,EACrB,yBAAyB,EACzB,sBAAsB,EACtB,qBAAqB,EACrB,wBAAwB,EAGxB,yBAAyB,EAG1B,MAAM,mBAAmB,CAAC;AAgC3B,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAC9E,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAE9E;;;GAGG;AACH,KAAK,kBAAkB,GAAG,IAAI,CAC5B,wBAAwB,EACxB,qBAAqB,GAAG,2BAA2B,CACpD,CAAC;AA0BF;;;GAGG;AACH,qBAAa,8BAA8B;;IA2XzC,YACE,aAAa,EAAE,wBAAwB,EACvC,aAAa,EAAE,kBAAkB,EACjC,oBAAoB,EAAE,yBAAyB,EAC/C,WAAW,CAAC,EAAE,OAAO,EACrB,WAAW,CAAC,EAAE,MAAM,EAAE,EACtB,gBAAgB,CAAC,EAAE,MAAM,EAAE,EAC3B,gBAAgB,CAAC,EAAE,MAAM,EAAE,EAC3B,mBAAmB,CAAC,EAAE,MAAM,EAC5B,mBAAmB,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,EAa9C;IAwMD;;;;;;;;OAQG;IACI,eAAe,CACpB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE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,CAAC,MAAM,EAAE;QACxC,MAAM,EAAE,MAAM,CAAC;QACf,SAAS,CAAC,EAAE,aAAa,CAAC;QAC1B,QAAQ,EAAE,CAAC,IAAI,EAAE,sBAAsB,KAAK,IAAI,CAAC;QACjD,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;KAClC,GAAG,MAAM,IAAI,CAwIb;IAsJD;;;;;;OAMG;IACU,oBAAoB,IAAI,OAAO,CAAC,IAAI,CAAC,CAuQjD;IAED;;OAEG;IACI,QAAQ,IAAI,IAAI,CAsPtB;CACF"}
|
|
@@ -84,6 +84,10 @@ export class HyperLiquidSubscriptionService {
|
|
|
84
84
|
// assetCtxs can serve prices again until a fresh snapshot arrives.
|
|
85
85
|
#fastAssetCtxsCoins = new Set();
|
|
86
86
|
#globalActiveAssetSubscriptions = new Map();
|
|
87
|
+
// Per-caller activeAssetData streams (available to trade). The service owns
|
|
88
|
+
// them so restoreSubscriptions() can reopen them on a replacement transport
|
|
89
|
+
// and clearAll() can retire pending and open ones.
|
|
90
|
+
#activeAssetDataStreams = new Set();
|
|
87
91
|
// Track in-progress activeAssetCtx subscription promises to prevent leaks
|
|
88
92
|
// when cleanup fires before the async subscription resolves (#28141)
|
|
89
93
|
#pendingActiveAssetPromises = new Map();
|
|
@@ -3448,6 +3452,127 @@ export class HyperLiquidSubscriptionService {
|
|
|
3448
3452
|
}
|
|
3449
3453
|
};
|
|
3450
3454
|
}
|
|
3455
|
+
/**
|
|
3456
|
+
* Stream the venue's `activeAssetData` for one account and market. Each
|
|
3457
|
+
* caller gets its own venue subscription, which restoreSubscriptions()
|
|
3458
|
+
* reopens on a replacement transport and clearAll() retires. The venue
|
|
3459
|
+
* pushes a new frame when the market's position, leverage or mark price
|
|
3460
|
+
* changes.
|
|
3461
|
+
*
|
|
3462
|
+
* @param params - Subscription parameters.
|
|
3463
|
+
* @param params.symbol - Market symbol, including any DEX prefix.
|
|
3464
|
+
* @param params.accountId - Optional CAIP account ID; defaults to the selected account.
|
|
3465
|
+
* @param params.callback - Receives each frame for this account and market.
|
|
3466
|
+
* @param params.onError - Receives a failure to open the subscription, or
|
|
3467
|
+
* one that stops it after it opened, once transient failures have been
|
|
3468
|
+
* retried on the same transport.
|
|
3469
|
+
* @returns Cleanup function to unsubscribe.
|
|
3470
|
+
*/
|
|
3471
|
+
subscribeToActiveAssetData(params) {
|
|
3472
|
+
const { symbol, accountId, callback, onError } = params;
|
|
3473
|
+
let subscription;
|
|
3474
|
+
let retired = false;
|
|
3475
|
+
// Bumped on every (re)open: a pending or open attempt from an older
|
|
3476
|
+
// transport, or one that was retired, drops its frames and subscription.
|
|
3477
|
+
let attempt = 0;
|
|
3478
|
+
const unsubscribe = (sub) => {
|
|
3479
|
+
sub.unsubscribe().catch((error) => {
|
|
3480
|
+
this.#logErrorUnlessClearing(ensureError(error, 'HyperLiquidSubscriptionService.subscribeToActiveAssetData'), this.#getErrorContext('subscribeToActiveAssetData.unsubscribe', {
|
|
3481
|
+
symbol,
|
|
3482
|
+
}));
|
|
3483
|
+
});
|
|
3484
|
+
};
|
|
3485
|
+
const open = (retryAttempt = 1) => {
|
|
3486
|
+
attempt += 1;
|
|
3487
|
+
const thisAttempt = attempt;
|
|
3488
|
+
const isCurrent = () => !retired && thisAttempt === attempt;
|
|
3489
|
+
// reopen() has released any previous subscription.
|
|
3490
|
+
subscription = undefined;
|
|
3491
|
+
// Same policy as the assetCtxs subscribeWithRetry: a transient SDK
|
|
3492
|
+
// failure retries on the same transport (3 attempts, attempt * 500 ms);
|
|
3493
|
+
// anything else, or the last attempt, reaches the caller's onError.
|
|
3494
|
+
const fail = (error, failedAttempt) => {
|
|
3495
|
+
if (!isCurrent()) {
|
|
3496
|
+
return;
|
|
3497
|
+
}
|
|
3498
|
+
// Retire this attempt so its late frames and errors are dropped.
|
|
3499
|
+
attempt += 1;
|
|
3500
|
+
const retryGeneration = attempt;
|
|
3501
|
+
subscription = undefined;
|
|
3502
|
+
const safeError = ensureError(error, 'HyperLiquidSubscriptionService.subscribeToActiveAssetData');
|
|
3503
|
+
if (failedAttempt < 3 &&
|
|
3504
|
+
!this.#isClearing &&
|
|
3505
|
+
this.#isTransientSdkError(safeError)) {
|
|
3506
|
+
const retryDelayMs = failedAttempt * 500;
|
|
3507
|
+
this.#deps.debugLogger.log('Transient activeAssetData subscription failure, retrying', {
|
|
3508
|
+
symbol,
|
|
3509
|
+
attempt: failedAttempt,
|
|
3510
|
+
retryDelayMs,
|
|
3511
|
+
error: safeError.message,
|
|
3512
|
+
});
|
|
3513
|
+
setTimeout(() => {
|
|
3514
|
+
// A reopen, cleanup or clearAll since the failure wins.
|
|
3515
|
+
if (!retired && retryGeneration === attempt && !this.#isClearing) {
|
|
3516
|
+
open(failedAttempt + 1);
|
|
3517
|
+
}
|
|
3518
|
+
}, retryDelayMs);
|
|
3519
|
+
return;
|
|
3520
|
+
}
|
|
3521
|
+
this.#logErrorUnlessClearing(safeError, this.#getErrorContext('subscribeToActiveAssetData', { symbol }));
|
|
3522
|
+
onError?.(safeError);
|
|
3523
|
+
};
|
|
3524
|
+
(async () => {
|
|
3525
|
+
await this.#clientService.ensureSubscriptionClient(this.#walletService.createWalletAdapter());
|
|
3526
|
+
const subscriptionClient = this.#clientService.getSubscriptionClient();
|
|
3527
|
+
if (!subscriptionClient) {
|
|
3528
|
+
throw new Error('Subscription client not available');
|
|
3529
|
+
}
|
|
3530
|
+
const user = await this.#walletService.getUserAddressWithDefault(accountId);
|
|
3531
|
+
if (!isCurrent()) {
|
|
3532
|
+
return;
|
|
3533
|
+
}
|
|
3534
|
+
const sub = await subscriptionClient.activeAssetData({ user, coin: symbol }, (data) => {
|
|
3535
|
+
if (!isCurrent() ||
|
|
3536
|
+
data.coin !== symbol ||
|
|
3537
|
+
data.user.toLowerCase() !== user.toLowerCase()) {
|
|
3538
|
+
return;
|
|
3539
|
+
}
|
|
3540
|
+
callback(data);
|
|
3541
|
+
}, {
|
|
3542
|
+
// A failure after the open (a rejected resubscription, a
|
|
3543
|
+
// terminated connection): the SDK has already dropped the
|
|
3544
|
+
// subscription. It had opened, so retry with a fresh budget.
|
|
3545
|
+
onError: (error) => fail(error, 1),
|
|
3546
|
+
});
|
|
3547
|
+
if (!isCurrent()) {
|
|
3548
|
+
unsubscribe(sub);
|
|
3549
|
+
return;
|
|
3550
|
+
}
|
|
3551
|
+
subscription = sub;
|
|
3552
|
+
})().catch((error) => fail(error, retryAttempt));
|
|
3553
|
+
};
|
|
3554
|
+
const stream = {
|
|
3555
|
+
reopen: () => {
|
|
3556
|
+
// A retry can open on the replacement transport before restore
|
|
3557
|
+
// reaches this stream; release it instead of orphaning it.
|
|
3558
|
+
if (subscription) {
|
|
3559
|
+
unsubscribe(subscription);
|
|
3560
|
+
}
|
|
3561
|
+
open();
|
|
3562
|
+
},
|
|
3563
|
+
retire: () => {
|
|
3564
|
+
retired = true;
|
|
3565
|
+
this.#activeAssetDataStreams.delete(stream);
|
|
3566
|
+
if (subscription) {
|
|
3567
|
+
unsubscribe(subscription);
|
|
3568
|
+
subscription = undefined;
|
|
3569
|
+
}
|
|
3570
|
+
},
|
|
3571
|
+
};
|
|
3572
|
+
this.#activeAssetDataStreams.add(stream);
|
|
3573
|
+
open();
|
|
3574
|
+
return stream.retire;
|
|
3575
|
+
}
|
|
3451
3576
|
/**
|
|
3452
3577
|
* Process raw L2Book data into OrderBookData format
|
|
3453
3578
|
* Calculates cumulative totals, notional values, and spread metrics
|
|
@@ -3670,6 +3795,9 @@ export class HyperLiquidSubscriptionService {
|
|
|
3670
3795
|
});
|
|
3671
3796
|
}));
|
|
3672
3797
|
}
|
|
3798
|
+
// Reopen activeAssetData streams on the replacement transport. A failure
|
|
3799
|
+
// reaches the caller's onError.
|
|
3800
|
+
this.#activeAssetDataStreams.forEach((stream) => stream.reopen());
|
|
3673
3801
|
// Re-establish user data subscriptions if there are user data subscribers
|
|
3674
3802
|
if (this.#positionSubscribers.size > 0 ||
|
|
3675
3803
|
this.#orderSubscribers.size > 0 ||
|
|
@@ -3812,6 +3940,9 @@ export class HyperLiquidSubscriptionService {
|
|
|
3812
3940
|
this.#accountSubscribers.clear();
|
|
3813
3941
|
this.#marketDataSubscribers.clear();
|
|
3814
3942
|
this.#orderBookSubscribers.clear();
|
|
3943
|
+
// Retire activeAssetData streams: pending opens drop their subscription
|
|
3944
|
+
// and open ones stop delivering frames.
|
|
3945
|
+
this.#activeAssetDataStreams.forEach((stream) => stream.retire());
|
|
3815
3946
|
// Clear order fill subscriptions; generations were bumped above.
|
|
3816
3947
|
this.#orderFillSubscriptions.forEach((subscription) => {
|
|
3817
3948
|
subscription.unsubscribe().catch(() => {
|