@metamask-previews/perps-controller 25.0.0-preview-c785ff081 → 26.1.0-preview-037c8a905
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 +115 -4
- package/dist/PerpsController-method-action-types.d.ts +149 -3
- 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 +132 -6
- package/dist/PerpsController.d.ts.map +1 -1
- package/dist/PerpsController.js +769 -33
- package/dist/PerpsController.js.map +1 -1
- package/dist/constants/eventNames.d.ts +63 -0
- package/dist/constants/eventNames.d.ts.map +1 -1
- package/dist/constants/eventNames.js +73 -0
- package/dist/constants/eventNames.js.map +1 -1
- package/dist/constants/hyperLiquidConfig.d.ts +15 -0
- package/dist/constants/hyperLiquidConfig.d.ts.map +1 -1
- package/dist/constants/hyperLiquidConfig.js +14 -0
- package/dist/constants/hyperLiquidConfig.js.map +1 -1
- package/dist/constants/lighterConfig.d.ts +5 -0
- package/dist/constants/lighterConfig.d.ts.map +1 -1
- package/dist/constants/lighterConfig.js +5 -0
- package/dist/constants/lighterConfig.js.map +1 -1
- package/dist/constants/perpsConfig.d.ts +19 -1
- package/dist/constants/perpsConfig.d.ts.map +1 -1
- package/dist/constants/perpsConfig.js +22 -1
- package/dist/constants/perpsConfig.js.map +1 -1
- package/dist/index.d.ts +11 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/internal/accountReturnOnEquity.d.ts +12 -0
- package/dist/internal/accountReturnOnEquity.d.ts.map +1 -0
- package/dist/internal/accountReturnOnEquity.js +18 -0
- package/dist/internal/accountReturnOnEquity.js.map +1 -0
- package/dist/internal/hyperLiquidHip3Rebalance.d.ts +14 -0
- package/dist/internal/hyperLiquidHip3Rebalance.d.ts.map +1 -0
- package/dist/internal/hyperLiquidHip3Rebalance.js +18 -0
- package/dist/internal/hyperLiquidHip3Rebalance.js.map +1 -0
- package/dist/internal/marketNumericValues.d.ts +10 -0
- package/dist/internal/marketNumericValues.d.ts.map +1 -0
- package/dist/internal/marketNumericValues.js +9 -0
- package/dist/internal/marketNumericValues.js.map +1 -0
- package/dist/internal/perpsDisplaySymbol.d.ts +12 -0
- package/dist/internal/perpsDisplaySymbol.d.ts.map +1 -0
- package/dist/internal/perpsDisplaySymbol.js +21 -0
- package/dist/internal/perpsDisplaySymbol.js.map +1 -0
- package/dist/perpsErrorCodes.d.ts +1 -0
- package/dist/perpsErrorCodes.d.ts.map +1 -1
- package/dist/perpsErrorCodes.js +1 -0
- package/dist/perpsErrorCodes.js.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.d.ts +54 -1
- package/dist/providers/AggregatedPerpsProvider.d.ts.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.js +153 -0
- package/dist/providers/AggregatedPerpsProvider.js.map +1 -1
- package/dist/providers/HyperLiquidProvider.d.ts +72 -4
- package/dist/providers/HyperLiquidProvider.d.ts.map +1 -1
- package/dist/providers/HyperLiquidProvider.js +749 -157
- package/dist/providers/HyperLiquidProvider.js.map +1 -1
- package/dist/providers/LighterProvider.d.ts +9 -1
- package/dist/providers/LighterProvider.d.ts.map +1 -1
- package/dist/providers/LighterProvider.js +28 -5
- package/dist/providers/LighterProvider.js.map +1 -1
- package/dist/services/DataLakeService.d.ts.map +1 -1
- package/dist/services/DataLakeService.js +7 -28
- package/dist/services/DataLakeService.js.map +1 -1
- package/dist/services/HyperLiquidClientService.d.ts +8 -0
- package/dist/services/HyperLiquidClientService.d.ts.map +1 -1
- package/dist/services/HyperLiquidClientService.js +65 -0
- package/dist/services/HyperLiquidClientService.js.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.ts +30 -1
- package/dist/services/HyperLiquidSubscriptionService.d.ts.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.js +97 -17
- package/dist/services/HyperLiquidSubscriptionService.js.map +1 -1
- package/dist/services/LighterNotificationEnrollmentService.d.ts +40 -0
- package/dist/services/LighterNotificationEnrollmentService.d.ts.map +1 -0
- package/dist/services/LighterNotificationEnrollmentService.js +102 -0
- package/dist/services/LighterNotificationEnrollmentService.js.map +1 -0
- package/dist/services/MarketDataService.d.ts +15 -1
- package/dist/services/MarketDataService.d.ts.map +1 -1
- package/dist/services/MarketDataService.js +43 -1
- package/dist/services/MarketDataService.js.map +1 -1
- package/dist/services/StreamChannelManager.d.ts +139 -0
- package/dist/services/StreamChannelManager.d.ts.map +1 -0
- package/dist/services/StreamChannelManager.js +475 -0
- package/dist/services/StreamChannelManager.js.map +1 -0
- package/dist/services/TerminalMarketService.d.ts.map +1 -1
- package/dist/services/TerminalMarketService.js +7 -0
- package/dist/services/TerminalMarketService.js.map +1 -1
- package/dist/services/TradingService.d.ts +2 -0
- package/dist/services/TradingService.d.ts.map +1 -1
- package/dist/services/TradingService.js +11 -3
- package/dist/services/TradingService.js.map +1 -1
- package/dist/services/agentSigner.d.ts +8 -0
- package/dist/services/agentSigner.d.ts.map +1 -1
- package/dist/services/agentSigner.js +13 -0
- package/dist/services/agentSigner.js.map +1 -1
- package/dist/services/getOptionalBearerToken.d.ts +12 -0
- package/dist/services/getOptionalBearerToken.d.ts.map +1 -0
- package/dist/services/getOptionalBearerToken.js +22 -0
- package/dist/services/getOptionalBearerToken.js.map +1 -0
- package/dist/types/hyperliquid-types.d.ts +9 -1
- package/dist/types/hyperliquid-types.d.ts.map +1 -1
- package/dist/types/hyperliquid-types.js +10 -0
- package/dist/types/hyperliquid-types.js.map +1 -1
- package/dist/types/index.d.ts +445 -14
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/utils/accountAggregation.d.ts +17 -0
- package/dist/utils/accountAggregation.d.ts.map +1 -1
- package/dist/utils/accountAggregation.js +21 -5
- package/dist/utils/accountAggregation.js.map +1 -1
- package/dist/utils/accountUtils.d.ts +31 -10
- package/dist/utils/accountUtils.d.ts.map +1 -1
- package/dist/utils/accountUtils.js +68 -28
- package/dist/utils/accountUtils.js.map +1 -1
- package/dist/utils/closeCalculations.d.ts +17 -4
- package/dist/utils/closeCalculations.d.ts.map +1 -1
- package/dist/utils/closeCalculations.js +23 -7
- package/dist/utils/closeCalculations.js.map +1 -1
- package/dist/utils/hyperLiquidAdapter.d.ts +10 -1
- package/dist/utils/hyperLiquidAdapter.d.ts.map +1 -1
- package/dist/utils/hyperLiquidAdapter.js +17 -1
- package/dist/utils/hyperLiquidAdapter.js.map +1 -1
- package/dist/utils/hyperLiquidPositionPreview.d.ts +133 -9
- package/dist/utils/hyperLiquidPositionPreview.d.ts.map +1 -1
- package/dist/utils/hyperLiquidPositionPreview.js +264 -29
- package/dist/utils/hyperLiquidPositionPreview.js.map +1 -1
- package/dist/utils/index.d.ts +4 -2
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/lighterAdapter.d.ts.map +1 -1
- package/dist/utils/lighterAdapter.js +46 -9
- package/dist/utils/lighterAdapter.js.map +1 -1
- package/dist/utils/lighterClientOrderTag.d.ts +23 -0
- package/dist/utils/lighterClientOrderTag.d.ts.map +1 -0
- package/dist/utils/lighterClientOrderTag.js +43 -0
- package/dist/utils/lighterClientOrderTag.js.map +1 -0
- package/dist/utils/marketDataTransform.d.ts.map +1 -1
- package/dist/utils/marketDataTransform.js +8 -0
- package/dist/utils/marketDataTransform.js.map +1 -1
- package/dist/utils/marketUtils.d.ts +1 -2
- package/dist/utils/marketUtils.d.ts.map +1 -1
- package/dist/utils/marketUtils.js +7 -17
- package/dist/utils/marketUtils.js.map +1 -1
- package/dist/utils/orderBookGrouping.d.ts +35 -5
- package/dist/utils/orderBookGrouping.d.ts.map +1 -1
- package/dist/utils/orderBookGrouping.js +62 -6
- package/dist/utils/orderBookGrouping.js.map +1 -1
- package/dist/utils/perpsClientCopy.d.ts.map +1 -1
- package/dist/utils/perpsClientCopy.js +4 -0
- package/dist/utils/perpsClientCopy.js.map +1 -1
- package/dist/utils/perpsDiskPersistence.d.ts.map +1 -1
- package/dist/utils/perpsDiskPersistence.js +10 -0
- package/dist/utils/perpsDiskPersistence.js.map +1 -1
- package/dist/utils/perpsFormatters.d.ts +31 -0
- package/dist/utils/perpsFormatters.d.ts.map +1 -1
- package/dist/utils/perpsFormatters.js +26 -0
- package/dist/utils/perpsFormatters.js.map +1 -1
- package/dist/utils/sortMarkets.d.ts +13 -5
- package/dist/utils/sortMarkets.d.ts.map +1 -1
- package/dist/utils/sortMarkets.js +60 -38
- package/dist/utils/sortMarkets.js.map +1 -1
- package/docs/architecture.md +70 -38
- package/docs/client-contract.md +42 -17
- package/docs/contracts/events.md +34 -30
- package/docs/formatting.md +2 -0
- package/docs/venues/lighter/capabilities.md +18 -15
- package/package.json +3 -3
package/dist/PerpsController.js
CHANGED
|
@@ -5,7 +5,7 @@ import { hasProperty } from '@metamask/utils';
|
|
|
5
5
|
import { v4 as uuidv4 } from 'uuid';
|
|
6
6
|
import { VISIBLE_CANDLE_COUNT_CONFIG, } from './constants/chartConfig.js';
|
|
7
7
|
import { PERPS_EVENT_PROPERTY, PERPS_EVENT_VALUE, } from './constants/eventNames.js';
|
|
8
|
-
import { canonicalizeHyperLiquidDexes, MAINNET_HIP3_CONFIG, TESTNET_HIP3_CONFIG, USDC_SYMBOL, } from './constants/hyperLiquidConfig.js';
|
|
8
|
+
import { canonicalizeHyperLiquidDexes, isCrossMarginSupported, MAINNET_HIP3_CONFIG, TESTNET_HIP3_CONFIG, USDC_SYMBOL, } from './constants/hyperLiquidConfig.js';
|
|
9
9
|
import { PerpsMeasurementName } from './constants/performanceMetrics.js';
|
|
10
10
|
import { PERPS_CONSTANTS, MARKET_SORTING_CONFIG, buildProviderCacheKey, MAX_SLIPPAGE_BOUNDS, DEFAULT_PERPS_MODE, DEFAULT_ORDER_BOOK_PREFERENCES, DEFAULT_PRO_LAYOUT_PREFERENCES, DEFAULT_SELECTED_ORDER_TYPE, } from './constants/perpsConfig.js';
|
|
11
11
|
import { PERPS_ERROR_CODES } from './perpsErrorCodes.js';
|
|
@@ -20,10 +20,12 @@ import { DataLakeService } from './services/DataLakeService.js';
|
|
|
20
20
|
import { DepositService } from './services/DepositService.js';
|
|
21
21
|
import { EligibilityService } from './services/EligibilityService.js';
|
|
22
22
|
import { FeatureFlagConfigurationService } from './services/FeatureFlagConfigurationService.js';
|
|
23
|
+
import { LighterNotificationEnrollmentService } from './services/LighterNotificationEnrollmentService.js';
|
|
23
24
|
import { MarketDataService } from './services/MarketDataService.js';
|
|
24
25
|
import { isMissingActionHandlerError } from './services/missingActionHandler.js';
|
|
25
26
|
import { isProviderOnTestnet } from './services/providerNetwork.js';
|
|
26
27
|
import { RewardsIntegrationService } from './services/RewardsIntegrationService.js';
|
|
28
|
+
import { StreamChannelManager } from './services/StreamChannelManager.js';
|
|
27
29
|
import { TerminalMarketService } from './services/TerminalMarketService.js';
|
|
28
30
|
import { TradingService } from './services/TradingService.js';
|
|
29
31
|
// PerpsStreamChannelKey removed: using string for channel keys (PerpsStreamManager.pauseChannel takes string)
|
|
@@ -478,6 +480,26 @@ const metadata = {
|
|
|
478
480
|
* Number of public trades `getPublicTrades` returns when no limit is given.
|
|
479
481
|
*/
|
|
480
482
|
const PUBLIC_TRADES_DEFAULT_LIMIT = 50;
|
|
483
|
+
/** `subscribeTo*` streams that read an account rather than a market. */
|
|
484
|
+
const ACCOUNT_STREAMS = new Set([
|
|
485
|
+
'account',
|
|
486
|
+
'availableToTrade',
|
|
487
|
+
'chaseOrderLists',
|
|
488
|
+
'feeRates',
|
|
489
|
+
'liveTwapOrders',
|
|
490
|
+
'marginAdjustmentLimits',
|
|
491
|
+
'oiCaps',
|
|
492
|
+
'orderFills',
|
|
493
|
+
'orders',
|
|
494
|
+
'positions',
|
|
495
|
+
'tradingAgentValidity',
|
|
496
|
+
'twapOrders',
|
|
497
|
+
]);
|
|
498
|
+
/**
|
|
499
|
+
* Provider part of the key of a channel subscribed while no provider is
|
|
500
|
+
* active. The channel opens on the next successful initialization.
|
|
501
|
+
*/
|
|
502
|
+
const PENDING_PROVIDER_CHANNEL_ID = 'pending';
|
|
481
503
|
const MESSENGER_EXPOSED_METHODS = [
|
|
482
504
|
'approveSubscriptionBuilderFee',
|
|
483
505
|
'calculateFees',
|
|
@@ -529,6 +551,8 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
529
551
|
'getOrderBookPreferences',
|
|
530
552
|
'getOrderCapabilities',
|
|
531
553
|
'getMarginModeLock',
|
|
554
|
+
'getMarginAdjustmentLimits',
|
|
555
|
+
'getMarketMeta',
|
|
532
556
|
'getScalePriceLadder',
|
|
533
557
|
'getOrderFills',
|
|
534
558
|
'getOrders',
|
|
@@ -537,6 +561,7 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
537
561
|
'getPositions',
|
|
538
562
|
'getProviderHealth',
|
|
539
563
|
'getPublicTrades',
|
|
564
|
+
'getStreamSnapshot',
|
|
540
565
|
'getSelectedOrderType',
|
|
541
566
|
'getRecoveredDispatches',
|
|
542
567
|
'getAttachedOrderGroups',
|
|
@@ -564,12 +589,14 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
564
589
|
'placeOrder',
|
|
565
590
|
'prepareTradingWallet',
|
|
566
591
|
'previewPositionModify',
|
|
592
|
+
'estimateCrossLiquidation',
|
|
567
593
|
'reconnect',
|
|
568
594
|
'recordMarketViewed',
|
|
569
595
|
'refreshEligibility',
|
|
570
596
|
'replacePositionTPSLLeg',
|
|
571
597
|
'resetFirstTimeUserState',
|
|
572
598
|
'resetSelectedPaymentToken',
|
|
599
|
+
'resubscribeStreams',
|
|
573
600
|
'getMaxSlippage',
|
|
574
601
|
'setMaxSlippage',
|
|
575
602
|
'setOrderBookPreferences',
|
|
@@ -596,8 +623,12 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
596
623
|
'subscribeToAvailableToTrade',
|
|
597
624
|
'subscribeToCandles',
|
|
598
625
|
'subscribeToChaseOrderLists',
|
|
626
|
+
'subscribeToConnectionLatency',
|
|
599
627
|
'subscribeToConnectionState',
|
|
628
|
+
'subscribeToFeeRates',
|
|
600
629
|
'subscribeToLiveTwapOrders',
|
|
630
|
+
'subscribeToMarginAdjustmentLimits',
|
|
631
|
+
'subscribeToMarketMeta',
|
|
601
632
|
'subscribeToOICaps',
|
|
602
633
|
'subscribeToOrderBook',
|
|
603
634
|
'subscribeToOrderFills',
|
|
@@ -606,6 +637,7 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
606
637
|
'subscribeToPrices',
|
|
607
638
|
'subscribeToProviderHealth',
|
|
608
639
|
'subscribeToPublicTrades',
|
|
640
|
+
'subscribeToTradingAgentValidity',
|
|
609
641
|
'subscribeToTwapOrders',
|
|
610
642
|
'suspendChaseOrders',
|
|
611
643
|
'switchProvider',
|
|
@@ -761,6 +793,18 @@ export class PerpsController extends BaseController {
|
|
|
761
793
|
#unpersistedWatchlistAdds = new Map();
|
|
762
794
|
#userDiskWrite = Promise.resolve();
|
|
763
795
|
#providerSubscriptions = new Set();
|
|
796
|
+
/** The shared stream layer behind every `subscribeTo*` method. */
|
|
797
|
+
#streams = new StreamChannelManager({
|
|
798
|
+
subscribeToConnectionState: (listener) => this.subscribeToConnectionState(listener),
|
|
799
|
+
onError: (error) => this.#logError(ensureError(error, 'PerpsController.streams'), this.#getErrorContext('streams')),
|
|
800
|
+
});
|
|
801
|
+
#providerChannelIds = new WeakMap();
|
|
802
|
+
/**
|
|
803
|
+
* Channel sources being opened. Their provider subscriptions are not
|
|
804
|
+
* tracked for aggregated rebinding: the channel rebinds them itself.
|
|
805
|
+
*/
|
|
806
|
+
#openingChannelSources = 0;
|
|
807
|
+
#nextProviderChannelId = 0;
|
|
764
808
|
// A failed automatic rollback does not end the consumers' subscription context.
|
|
765
809
|
#hasPendingSubscriptionRecovery = false;
|
|
766
810
|
// Store options for dependency injection (allows core package to inject platform-specific services)
|
|
@@ -771,6 +815,7 @@ export class PerpsController extends BaseController {
|
|
|
771
815
|
#accountService;
|
|
772
816
|
#eligibilityService;
|
|
773
817
|
#dataLakeService;
|
|
818
|
+
#lighterNotificationEnrollmentService;
|
|
774
819
|
#depositService;
|
|
775
820
|
#featureFlagConfigurationService;
|
|
776
821
|
#rewardsIntegrationService;
|
|
@@ -813,6 +858,8 @@ export class PerpsController extends BaseController {
|
|
|
813
858
|
this.#accountService = new AccountService(infrastructure, messenger);
|
|
814
859
|
this.#eligibilityService = new EligibilityService(infrastructure);
|
|
815
860
|
this.#dataLakeService = new DataLakeService(infrastructure, messenger);
|
|
861
|
+
this.#lighterNotificationEnrollmentService =
|
|
862
|
+
new LighterNotificationEnrollmentService(infrastructure, messenger);
|
|
816
863
|
this.#depositService = new DepositService(infrastructure, messenger);
|
|
817
864
|
this.#featureFlagConfigurationService = new FeatureFlagConfigurationService(infrastructure);
|
|
818
865
|
this.#rewardsIntegrationService = new RewardsIntegrationService(infrastructure, messenger);
|
|
@@ -879,10 +926,16 @@ export class PerpsController extends BaseController {
|
|
|
879
926
|
});
|
|
880
927
|
}
|
|
881
928
|
};
|
|
882
|
-
this.messenger.subscribe('AccountsController:selectedAccountChange', () =>
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
929
|
+
this.messenger.subscribe('AccountsController:selectedAccountChange', () => {
|
|
930
|
+
updateSelectedTradingAccount(getSelectedEvmAccountFromMessenger(this.messenger));
|
|
931
|
+
this.#rebindStreams();
|
|
932
|
+
});
|
|
933
|
+
this.messenger.subscribe('AccountTreeController:selectedAccountGroupChange', (groupId) => {
|
|
934
|
+
updateSelectedTradingAccount(groupId === ''
|
|
935
|
+
? null
|
|
936
|
+
: getSelectedEvmAccountFromMessenger(this.messenger));
|
|
937
|
+
this.#rebindStreams();
|
|
938
|
+
});
|
|
886
939
|
this.providers = new Map();
|
|
887
940
|
// Migrate old persisted data without accountAddress
|
|
888
941
|
this.#migrateRequestsIfNeeded();
|
|
@@ -1683,6 +1736,7 @@ export class PerpsController extends BaseController {
|
|
|
1683
1736
|
async #withStreamPause(operation, channels) {
|
|
1684
1737
|
const pausedChannels = [];
|
|
1685
1738
|
const { streamManager } = this.#options.infrastructure;
|
|
1739
|
+
const releases = channels.map((channel) => this.#streams.pause(channel));
|
|
1686
1740
|
// Pause emission on specified channels (WebSocket stays connected)
|
|
1687
1741
|
// Track which channels successfully paused to ensure proper cleanup
|
|
1688
1742
|
for (const channel of channels) {
|
|
@@ -1704,6 +1758,9 @@ export class PerpsController extends BaseController {
|
|
|
1704
1758
|
return await operation();
|
|
1705
1759
|
}
|
|
1706
1760
|
finally {
|
|
1761
|
+
for (const release of releases) {
|
|
1762
|
+
release();
|
|
1763
|
+
}
|
|
1707
1764
|
// Resume only channels that were successfully paused
|
|
1708
1765
|
for (const channel of pausedChannels) {
|
|
1709
1766
|
try {
|
|
@@ -1800,6 +1857,8 @@ export class PerpsController extends BaseController {
|
|
|
1800
1857
|
this.#reinitializationOperationPromise = operation;
|
|
1801
1858
|
return () => {
|
|
1802
1859
|
this.#isReinitializing = false;
|
|
1860
|
+
// Streams wait for the provider until the whole operation ends.
|
|
1861
|
+
this.#rebindStreams();
|
|
1803
1862
|
resolveOperation();
|
|
1804
1863
|
if (this.#reinitializationOperationPromise === operation) {
|
|
1805
1864
|
this.#reinitializationOperationPromise = null;
|
|
@@ -1890,6 +1949,7 @@ export class PerpsController extends BaseController {
|
|
|
1890
1949
|
if (preserveSubscriptions) {
|
|
1891
1950
|
this.#restoreProviderSubscriptions();
|
|
1892
1951
|
}
|
|
1952
|
+
this.#rebindStreams();
|
|
1893
1953
|
return; // Exit retry loop on success
|
|
1894
1954
|
}
|
|
1895
1955
|
catch (error) {
|
|
@@ -1961,6 +2021,7 @@ export class PerpsController extends BaseController {
|
|
|
1961
2021
|
?.subscriptionBuilderAddressMainnet,
|
|
1962
2022
|
onChaseOrderMaxDistanceReached: this.#publishChaseOrderMaxDistanceReached,
|
|
1963
2023
|
getAgentSigner: this.#agentBindings.resolve,
|
|
2024
|
+
hasAgentSource: this.#agentBindings.hasSource,
|
|
1964
2025
|
onAgentRejected: (account, agentAddress) => {
|
|
1965
2026
|
this.#agentBindings.release(account, agentAddress);
|
|
1966
2027
|
this.#options.clientConfig?.providerCredentials?.hyperliquid?.onAgentRejected?.(account, agentAddress);
|
|
@@ -2503,6 +2564,356 @@ export class PerpsController extends BaseController {
|
|
|
2503
2564
|
return this.#getUnavailableMarginModeLock('provider_unavailable', resolvedProviderId);
|
|
2504
2565
|
}
|
|
2505
2566
|
}
|
|
2567
|
+
/**
|
|
2568
|
+
* Get how much margin an isolated position can take in (`maxAdd`, what the
|
|
2569
|
+
* venue's isolated margin update accepts) and release (`maxRemove`),
|
|
2570
|
+
* through the active provider route used by order placement. Never throws;
|
|
2571
|
+
* failures report an unavailable status.
|
|
2572
|
+
*
|
|
2573
|
+
* @param params - Market and optional provider route.
|
|
2574
|
+
* @returns The limits, or a typed unavailable result.
|
|
2575
|
+
*/
|
|
2576
|
+
async getMarginAdjustmentLimits(params) {
|
|
2577
|
+
try {
|
|
2578
|
+
const activeProvider = await this.#getActiveProviderWhenReady();
|
|
2579
|
+
if (this.#hasConflictingProviderRoute(params.providerId, activeProvider) ||
|
|
2580
|
+
!activeProvider.getMarginAdjustmentLimits) {
|
|
2581
|
+
return { status: 'unavailable', reason: 'provider' };
|
|
2582
|
+
}
|
|
2583
|
+
return await activeProvider.getMarginAdjustmentLimits(params);
|
|
2584
|
+
}
|
|
2585
|
+
catch (error) {
|
|
2586
|
+
this.#debugLog('PerpsController: Margin adjustment limits unavailable', {
|
|
2587
|
+
error: ensureError(error, 'PerpsController.getMarginAdjustmentLimits')
|
|
2588
|
+
.message,
|
|
2589
|
+
});
|
|
2590
|
+
return { status: 'unavailable', reason: 'provider' };
|
|
2591
|
+
}
|
|
2592
|
+
}
|
|
2593
|
+
async getMarketMeta(params) {
|
|
2594
|
+
const provider = await this.#getActiveProviderWhenReady();
|
|
2595
|
+
const providerId = params?.providerId;
|
|
2596
|
+
if (this.#hasConflictingProviderRoute(providerId, provider)) {
|
|
2597
|
+
throw new Error(PERPS_ERROR_CODES.PROVIDER_NOT_FOUND);
|
|
2598
|
+
}
|
|
2599
|
+
const symbol = params?.symbol;
|
|
2600
|
+
const metas = await this.#readMarketMetas(provider, providerId, symbol === undefined ? undefined : { symbols: [symbol] });
|
|
2601
|
+
if (symbol === undefined) {
|
|
2602
|
+
return metas;
|
|
2603
|
+
}
|
|
2604
|
+
const meta = metas.find((item) => item.symbol.toLowerCase() === symbol.toLowerCase());
|
|
2605
|
+
if (!meta) {
|
|
2606
|
+
throw new Error(PERPS_ERROR_CODES.MARKET_NOT_FOUND);
|
|
2607
|
+
}
|
|
2608
|
+
return meta;
|
|
2609
|
+
}
|
|
2610
|
+
/**
|
|
2611
|
+
* Read one venue's market list as metadata. The aggregated list drops a
|
|
2612
|
+
* failed venue, so the routed venue is read alone: a failure rejects
|
|
2613
|
+
* instead of reading as missing markets.
|
|
2614
|
+
*
|
|
2615
|
+
* @param provider - Active provider.
|
|
2616
|
+
* @param providerId - Optional explicit route.
|
|
2617
|
+
* @param marketsParams - Optional market filter and cache bypass.
|
|
2618
|
+
* @returns The venue's markets as metadata.
|
|
2619
|
+
*/
|
|
2620
|
+
async #readMarketMetas(provider, providerId, marketsParams) {
|
|
2621
|
+
const markets = provider instanceof AggregatedPerpsProvider
|
|
2622
|
+
? await provider.getProviderMarkets(provider.getWriteProviderId(providerId), marketsParams)
|
|
2623
|
+
: await provider.getMarkets(marketsParams);
|
|
2624
|
+
return markets.map((market) => ({
|
|
2625
|
+
symbol: market.name,
|
|
2626
|
+
marginTiers: market.marginTiers ?? null,
|
|
2627
|
+
isDelisted: market.isDelisted === true,
|
|
2628
|
+
maxLeverage: market.maxLeverage,
|
|
2629
|
+
onlyIsolated: !isCrossMarginSupported(market),
|
|
2630
|
+
}));
|
|
2631
|
+
}
|
|
2632
|
+
/**
|
|
2633
|
+
* Stream every market's metadata (margin tiers, delisting, max leverage,
|
|
2634
|
+
* Cross support) from the routed venue. The callback receives the current
|
|
2635
|
+
* list at once, from the provider's session cache, then a new list each
|
|
2636
|
+
* time the market data preload refreshes (`startMarketDataPreload`), which
|
|
2637
|
+
* re-reads the venue's metadata. It adds no timer of its own. A failed read
|
|
2638
|
+
* goes to `onError` and leaves the last list current; an empty list is
|
|
2639
|
+
* never reported for a failure. In aggregated mode an unrouted stream
|
|
2640
|
+
* rebinds to the new default provider.
|
|
2641
|
+
*
|
|
2642
|
+
* @param params - Optional provider route, callback and error handler.
|
|
2643
|
+
* @returns Cleanup function.
|
|
2644
|
+
*/
|
|
2645
|
+
subscribeToMarketMeta(params) {
|
|
2646
|
+
return this.#subscribeToChannel('marketMeta', params, (channelParams) => this.#openMarketMeta(channelParams));
|
|
2647
|
+
}
|
|
2648
|
+
/**
|
|
2649
|
+
* Open the venue subscription behind a `marketMeta` channel.
|
|
2650
|
+
*
|
|
2651
|
+
* @param params - The channel's parameters.
|
|
2652
|
+
* @returns A cleanup function to remove the subscription.
|
|
2653
|
+
*/
|
|
2654
|
+
#openMarketMeta(params) {
|
|
2655
|
+
const provider = this.getActiveProviderOrNull();
|
|
2656
|
+
if (!provider ||
|
|
2657
|
+
this.#hasConflictingProviderRoute(params.providerId, provider)) {
|
|
2658
|
+
params.onError?.(new Error(PERPS_ERROR_CODES.PROVIDER_NOT_FOUND));
|
|
2659
|
+
return () => undefined;
|
|
2660
|
+
}
|
|
2661
|
+
const subscriber = { params, lastList: null };
|
|
2662
|
+
const open = (nextProvider) => {
|
|
2663
|
+
subscriber.provider = nextProvider;
|
|
2664
|
+
subscriber.lastList = null;
|
|
2665
|
+
this.#marketMetaSubscribers.add(subscriber);
|
|
2666
|
+
this.#emitMarketMeta(subscriber, false).catch(() => undefined);
|
|
2667
|
+
return () => {
|
|
2668
|
+
this.#marketMetaSubscribers.delete(subscriber);
|
|
2669
|
+
};
|
|
2670
|
+
};
|
|
2671
|
+
return this.#trackProviderSubscription(provider, open);
|
|
2672
|
+
}
|
|
2673
|
+
/**
|
|
2674
|
+
* Read the subscriber's venue and report its list when it changed.
|
|
2675
|
+
*
|
|
2676
|
+
* @param subscriber - Market metadata subscriber.
|
|
2677
|
+
* @param skipCache - Re-read the venue's metadata instead of the cache.
|
|
2678
|
+
*/
|
|
2679
|
+
async #emitMarketMeta(subscriber, skipCache) {
|
|
2680
|
+
const { provider, params } = subscriber;
|
|
2681
|
+
if (!provider) {
|
|
2682
|
+
return;
|
|
2683
|
+
}
|
|
2684
|
+
try {
|
|
2685
|
+
const metas = await this.#readMarketMetas(provider, params.providerId, skipCache ? { skipCache: true } : undefined);
|
|
2686
|
+
// A subscriber that left, or rebound to another provider, during the
|
|
2687
|
+
// read gets nothing; an empty read is a failure, not a delisting.
|
|
2688
|
+
if (!this.#marketMetaSubscribers.has(subscriber) ||
|
|
2689
|
+
subscriber.provider !== provider) {
|
|
2690
|
+
return;
|
|
2691
|
+
}
|
|
2692
|
+
if (metas.length === 0) {
|
|
2693
|
+
throw new Error(PERPS_ERROR_CODES.MARKETS_FAILED);
|
|
2694
|
+
}
|
|
2695
|
+
const list = JSON.stringify(metas);
|
|
2696
|
+
if (list === subscriber.lastList) {
|
|
2697
|
+
return;
|
|
2698
|
+
}
|
|
2699
|
+
subscriber.lastList = list;
|
|
2700
|
+
params.callback(metas);
|
|
2701
|
+
}
|
|
2702
|
+
catch (error) {
|
|
2703
|
+
if (this.#marketMetaSubscribers.has(subscriber)) {
|
|
2704
|
+
params.onError?.(ensureError(error, 'PerpsController.subscribeToMarketMeta'));
|
|
2705
|
+
}
|
|
2706
|
+
}
|
|
2707
|
+
}
|
|
2708
|
+
/** Re-read every market metadata subscriber's venue after a preload. */
|
|
2709
|
+
#refreshMarketMetaSubscribers() {
|
|
2710
|
+
for (const subscriber of this.#marketMetaSubscribers) {
|
|
2711
|
+
this.#emitMarketMeta(subscriber, true).catch(() => undefined);
|
|
2712
|
+
}
|
|
2713
|
+
}
|
|
2714
|
+
/**
|
|
2715
|
+
* Stream an isolated position's margin adjustment limits from the routed
|
|
2716
|
+
* provider's live account and position streams, with no venue reads. The
|
|
2717
|
+
* callback receives the current limits at once, then each change: the
|
|
2718
|
+
* same result `getMarginAdjustmentLimits` returns. A provider without the
|
|
2719
|
+
* stream, or a route that conflicts with the active provider, reports
|
|
2720
|
+
* `unavailable`/`provider` once.
|
|
2721
|
+
*
|
|
2722
|
+
* @param params - Market, optional route, callback and error handler.
|
|
2723
|
+
* @returns Cleanup function.
|
|
2724
|
+
*/
|
|
2725
|
+
subscribeToMarginAdjustmentLimits(params) {
|
|
2726
|
+
return this.#subscribeToChannel('marginAdjustmentLimits', params, (channelParams) => this.#openMarginAdjustmentLimits(channelParams));
|
|
2727
|
+
}
|
|
2728
|
+
/**
|
|
2729
|
+
* Open the venue subscription behind a `marginAdjustmentLimits` channel.
|
|
2730
|
+
*
|
|
2731
|
+
* @param params - The channel's parameters.
|
|
2732
|
+
* @returns A cleanup function to remove the subscription.
|
|
2733
|
+
*/
|
|
2734
|
+
#openMarginAdjustmentLimits(params) {
|
|
2735
|
+
const unsupported = () => {
|
|
2736
|
+
params.callback({ status: 'unavailable', reason: 'provider' });
|
|
2737
|
+
return () => undefined;
|
|
2738
|
+
};
|
|
2739
|
+
const provider = this.getActiveProviderOrNull();
|
|
2740
|
+
if (!provider ||
|
|
2741
|
+
this.#hasConflictingProviderRoute(params.providerId, provider)) {
|
|
2742
|
+
return unsupported();
|
|
2743
|
+
}
|
|
2744
|
+
try {
|
|
2745
|
+
return this.#trackProviderSubscription(provider, (nextProvider) => this.#hasConflictingProviderRoute(params.providerId, nextProvider) ||
|
|
2746
|
+
!nextProvider.subscribeToMarginAdjustmentLimits
|
|
2747
|
+
? unsupported()
|
|
2748
|
+
: nextProvider.subscribeToMarginAdjustmentLimits(params));
|
|
2749
|
+
}
|
|
2750
|
+
catch (error) {
|
|
2751
|
+
this.#logError(ensureError(error, 'PerpsController.subscribeToMarginAdjustmentLimits'), this.#getErrorContext('subscribeToMarginAdjustmentLimits', { symbol: params.symbol, providerId: params.providerId }, CONNECTION_ERROR_TAGS));
|
|
2752
|
+
return unsupported();
|
|
2753
|
+
}
|
|
2754
|
+
}
|
|
2755
|
+
/**
|
|
2756
|
+
* Stream the routed provider's WebSocket keepalive round trip, in whole
|
|
2757
|
+
* milliseconds. The callback receives the last measurement at once when
|
|
2758
|
+
* there is one, then each new one, at the keepalive's own cadence. A
|
|
2759
|
+
* provider without a keepalive measurement never calls back.
|
|
2760
|
+
*
|
|
2761
|
+
* @param params - Optional provider route, callback and error handler.
|
|
2762
|
+
* @returns Cleanup function.
|
|
2763
|
+
*/
|
|
2764
|
+
subscribeToConnectionLatency(params) {
|
|
2765
|
+
return this.#subscribeToChannel('connectionLatency', params, (channelParams) => this.#openConnectionLatency(channelParams));
|
|
2766
|
+
}
|
|
2767
|
+
/**
|
|
2768
|
+
* Open the venue subscription behind a `connectionLatency` channel.
|
|
2769
|
+
*
|
|
2770
|
+
* @param params - The channel's parameters.
|
|
2771
|
+
* @returns A cleanup function to remove the subscription.
|
|
2772
|
+
*/
|
|
2773
|
+
#openConnectionLatency(params) {
|
|
2774
|
+
const provider = this.getActiveProviderOrNull();
|
|
2775
|
+
if (!provider ||
|
|
2776
|
+
this.#hasConflictingProviderRoute(params.providerId, provider)) {
|
|
2777
|
+
return () => undefined;
|
|
2778
|
+
}
|
|
2779
|
+
try {
|
|
2780
|
+
return this.#trackProviderSubscription(provider, (nextProvider) => this.#hasConflictingProviderRoute(params.providerId, nextProvider)
|
|
2781
|
+
? () => undefined
|
|
2782
|
+
: (nextProvider.subscribeToConnectionLatency?.(params) ??
|
|
2783
|
+
(() => undefined)));
|
|
2784
|
+
}
|
|
2785
|
+
catch (error) {
|
|
2786
|
+
params.onError?.(ensureError(error, 'PerpsController.subscribeToConnectionLatency'));
|
|
2787
|
+
return () => undefined;
|
|
2788
|
+
}
|
|
2789
|
+
}
|
|
2790
|
+
/**
|
|
2791
|
+
* Stream a market's post-discount fee rates: the `calculateFees` results
|
|
2792
|
+
* for a taker and a maker fill. The callback receives one read at once,
|
|
2793
|
+
* then a new read only on events: a fill on the account's own fills stream
|
|
2794
|
+
* (its volume tier can change), an account switch, a default provider
|
|
2795
|
+
* change in aggregated mode, and `invalidateSubscriptionBenefits`. There is
|
|
2796
|
+
* no timer. A failed read goes to `onError` and leaves the last rates
|
|
2797
|
+
* current; an unchanged read is not delivered.
|
|
2798
|
+
*
|
|
2799
|
+
* @param params - Market, optional order type, route, account, callback
|
|
2800
|
+
* and error handler.
|
|
2801
|
+
* @returns Cleanup function.
|
|
2802
|
+
*/
|
|
2803
|
+
subscribeToFeeRates(params) {
|
|
2804
|
+
return this.#subscribeToChannel('feeRates', params, (channelParams) => this.#openFeeRates(channelParams));
|
|
2805
|
+
}
|
|
2806
|
+
/**
|
|
2807
|
+
* Open the reads behind a `feeRates` channel.
|
|
2808
|
+
*
|
|
2809
|
+
* @param params - The channel's parameters.
|
|
2810
|
+
* @returns A cleanup function to stop the reads.
|
|
2811
|
+
*/
|
|
2812
|
+
#openFeeRates(params) {
|
|
2813
|
+
const provider = this.getActiveProviderOrNull();
|
|
2814
|
+
if (!provider) {
|
|
2815
|
+
params.onError?.(new Error(PERPS_ERROR_CODES.PROVIDER_NOT_FOUND));
|
|
2816
|
+
return () => undefined;
|
|
2817
|
+
}
|
|
2818
|
+
const { symbol, orderType, providerId, accountId } = params;
|
|
2819
|
+
let closed = false;
|
|
2820
|
+
let readId = 0;
|
|
2821
|
+
let last;
|
|
2822
|
+
const read = () => {
|
|
2823
|
+
readId += 1;
|
|
2824
|
+
const id = readId;
|
|
2825
|
+
const quote = async (isMaker) => await this.calculateFees({
|
|
2826
|
+
symbol,
|
|
2827
|
+
providerId,
|
|
2828
|
+
isMaker,
|
|
2829
|
+
orderType: orderType ?? (isMaker ? 'limit' : 'market'),
|
|
2830
|
+
});
|
|
2831
|
+
Promise.all([quote(false), quote(true)])
|
|
2832
|
+
.then(([taker, maker]) => {
|
|
2833
|
+
// Only the newest read is reported, so an older one never wins.
|
|
2834
|
+
if (closed || id !== readId) {
|
|
2835
|
+
return;
|
|
2836
|
+
}
|
|
2837
|
+
const rates = { symbol, taker, maker };
|
|
2838
|
+
const key = JSON.stringify(rates);
|
|
2839
|
+
if (key !== last) {
|
|
2840
|
+
last = key;
|
|
2841
|
+
params.callback(rates);
|
|
2842
|
+
}
|
|
2843
|
+
})
|
|
2844
|
+
.catch((error) => {
|
|
2845
|
+
if (!closed && id === readId) {
|
|
2846
|
+
params.onError?.(ensureError(error, 'PerpsController.subscribeToFeeRates'));
|
|
2847
|
+
}
|
|
2848
|
+
});
|
|
2849
|
+
};
|
|
2850
|
+
this.#feeRateRefreshers.add(read);
|
|
2851
|
+
// Re-read when the aggregated default provider changes as well.
|
|
2852
|
+
const stopRoute = this.#trackProviderSubscription(provider, () => {
|
|
2853
|
+
read();
|
|
2854
|
+
return () => undefined;
|
|
2855
|
+
});
|
|
2856
|
+
const stopFills = this.subscribeToOrderFills({
|
|
2857
|
+
accountId,
|
|
2858
|
+
callback: (_fills, isSnapshot) => {
|
|
2859
|
+
if (!isSnapshot) {
|
|
2860
|
+
read();
|
|
2861
|
+
}
|
|
2862
|
+
},
|
|
2863
|
+
});
|
|
2864
|
+
// An account switch rebinds the channel, which reopens this read.
|
|
2865
|
+
return () => {
|
|
2866
|
+
closed = true;
|
|
2867
|
+
this.#feeRateRefreshers.delete(read);
|
|
2868
|
+
stopRoute();
|
|
2869
|
+
stopFills();
|
|
2870
|
+
};
|
|
2871
|
+
}
|
|
2872
|
+
/**
|
|
2873
|
+
* Report whether the selected (or given) account's trading agent is valid
|
|
2874
|
+
* for the routed venue (`PerpsTradingAgentValidity`). The callback receives
|
|
2875
|
+
* one read at once, then a new value on events only: an agent set or
|
|
2876
|
+
* cleared (`setAgentSigner`, `clearAgentSigners`), a venue rejection of the
|
|
2877
|
+
* agent, an account switch, and the agent's `validUntil` expiry, a single
|
|
2878
|
+
* timeout at that deadline. A venue without an agent step reports
|
|
2879
|
+
* `not_required`.
|
|
2880
|
+
*
|
|
2881
|
+
* @param params - Optional account and route, callback and error handler.
|
|
2882
|
+
* @returns Cleanup function.
|
|
2883
|
+
*/
|
|
2884
|
+
subscribeToTradingAgentValidity(params) {
|
|
2885
|
+
return this.#subscribeToChannel('tradingAgentValidity', params, (channelParams) => this.#openTradingAgentValidity(channelParams));
|
|
2886
|
+
}
|
|
2887
|
+
/**
|
|
2888
|
+
* Open the venue subscription behind a `tradingAgentValidity` channel.
|
|
2889
|
+
*
|
|
2890
|
+
* @param params - The channel's parameters.
|
|
2891
|
+
* @returns A cleanup function to remove the subscription.
|
|
2892
|
+
*/
|
|
2893
|
+
#openTradingAgentValidity(params) {
|
|
2894
|
+
const provider = this.getActiveProviderOrNull();
|
|
2895
|
+
if (!provider ||
|
|
2896
|
+
this.#hasConflictingProviderRoute(params.providerId, provider)) {
|
|
2897
|
+
params.onError?.(new Error(PERPS_ERROR_CODES.PROVIDER_NOT_FOUND));
|
|
2898
|
+
return () => undefined;
|
|
2899
|
+
}
|
|
2900
|
+
const open = (nextProvider) => {
|
|
2901
|
+
if (!nextProvider.subscribeToTradingAgentValidity) {
|
|
2902
|
+
params.callback({ status: 'not_required' });
|
|
2903
|
+
return () => undefined;
|
|
2904
|
+
}
|
|
2905
|
+
return nextProvider.subscribeToTradingAgentValidity(params);
|
|
2906
|
+
};
|
|
2907
|
+
try {
|
|
2908
|
+
// An account switch rebinds the channel, which reads the new account's
|
|
2909
|
+
// agent afresh.
|
|
2910
|
+
return this.#trackProviderSubscription(provider, open);
|
|
2911
|
+
}
|
|
2912
|
+
catch (error) {
|
|
2913
|
+
params.onError?.(ensureError(error, 'PerpsController.subscribeToTradingAgentValidity'));
|
|
2914
|
+
return () => undefined;
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2506
2917
|
#getUnavailableMarginModeLock(reason, providerId) {
|
|
2507
2918
|
return providerId
|
|
2508
2919
|
? { status: 'unavailable', providerId, reason }
|
|
@@ -2691,7 +3102,7 @@ export class PerpsController extends BaseController {
|
|
|
2691
3102
|
context: this.#createServiceContext('placeOrder', {
|
|
2692
3103
|
saveTradeConfiguration: (symbol, leverage, providerId) => this.saveTradeConfiguration(symbol, leverage, providerId),
|
|
2693
3104
|
}),
|
|
2694
|
-
reportOrderToDataLake: (dataLakeParams) => this.reportOrderToDataLake(dataLakeParams),
|
|
3105
|
+
reportOrderToDataLake: (dataLakeParams) => this.reportOrderToDataLake(dataLakeParams, provider),
|
|
2695
3106
|
});
|
|
2696
3107
|
if (result.success) {
|
|
2697
3108
|
this.clearPendingTradeConfiguration(capturedParams.symbol, capturedParams.providerId);
|
|
@@ -3026,7 +3437,7 @@ export class PerpsController extends BaseController {
|
|
|
3026
3437
|
context: this.#createServiceContext('closePosition', {
|
|
3027
3438
|
getPositions: () => this.getPositions(),
|
|
3028
3439
|
}),
|
|
3029
|
-
reportOrderToDataLake: (dataLakeParams) => this.reportOrderToDataLake(dataLakeParams),
|
|
3440
|
+
reportOrderToDataLake: (dataLakeParams) => this.reportOrderToDataLake(dataLakeParams, provider),
|
|
3030
3441
|
});
|
|
3031
3442
|
}
|
|
3032
3443
|
/**
|
|
@@ -4312,6 +4723,10 @@ export class PerpsController extends BaseController {
|
|
|
4312
4723
|
'hip3ConfigVersion',
|
|
4313
4724
|
]);
|
|
4314
4725
|
#preloadTimer = null;
|
|
4726
|
+
/** Market metadata streams, refreshed after each market data preload. */
|
|
4727
|
+
#marketMetaSubscribers = new Set();
|
|
4728
|
+
/** Fee rate streams, re-read when subscription benefits are invalidated. */
|
|
4729
|
+
#feeRateRefreshers = new Set();
|
|
4315
4730
|
#preloadStartRequested = false;
|
|
4316
4731
|
#isPreloading = false;
|
|
4317
4732
|
#marketPreloadQueued = false;
|
|
@@ -4407,7 +4822,9 @@ export class PerpsController extends BaseController {
|
|
|
4407
4822
|
this.#previousIsTestnet = this.state.isTestnet;
|
|
4408
4823
|
this.#previousHip3ConfigVersion = this.state.hip3ConfigVersion;
|
|
4409
4824
|
// Immediate preload
|
|
4410
|
-
this.#performMarketDataPreload()
|
|
4825
|
+
this.#performMarketDataPreload()
|
|
4826
|
+
.then(() => this.#refreshMarketMetaSubscribers())
|
|
4827
|
+
.catch(() => {
|
|
4411
4828
|
/* fire-and-forget */
|
|
4412
4829
|
});
|
|
4413
4830
|
this.#performUserDataPreload().catch(() => {
|
|
@@ -4415,7 +4832,9 @@ export class PerpsController extends BaseController {
|
|
|
4415
4832
|
});
|
|
4416
4833
|
// Periodic refresh
|
|
4417
4834
|
this.#preloadTimer = setInterval(() => {
|
|
4418
|
-
this.#performMarketDataPreload()
|
|
4835
|
+
this.#performMarketDataPreload()
|
|
4836
|
+
.then(() => this.#refreshMarketMetaSubscribers())
|
|
4837
|
+
.catch(() => {
|
|
4419
4838
|
/* fire-and-forget */
|
|
4420
4839
|
});
|
|
4421
4840
|
this.#performUserDataPreload().catch(() => {
|
|
@@ -4446,7 +4865,9 @@ export class PerpsController extends BaseController {
|
|
|
4446
4865
|
// No need to clear user data cache — per-provider keys include the
|
|
4447
4866
|
// network, so different networks don't collide. Re-preload will
|
|
4448
4867
|
// populate the new key.
|
|
4449
|
-
this.#performMarketDataPreload()
|
|
4868
|
+
this.#performMarketDataPreload()
|
|
4869
|
+
.then(() => this.#refreshMarketMetaSubscribers())
|
|
4870
|
+
.catch(() => {
|
|
4450
4871
|
/* fire-and-forget */
|
|
4451
4872
|
});
|
|
4452
4873
|
this.#performUserDataPreload().catch(() => {
|
|
@@ -4679,7 +5100,9 @@ export class PerpsController extends BaseController {
|
|
|
4679
5100
|
this.#isPreloading = false;
|
|
4680
5101
|
if (this.#marketPreloadQueued && this.#preloadTimer) {
|
|
4681
5102
|
this.#marketPreloadQueued = false;
|
|
4682
|
-
this.#performMarketDataPreload()
|
|
5103
|
+
this.#performMarketDataPreload()
|
|
5104
|
+
.then(() => this.#refreshMarketMetaSubscribers())
|
|
5105
|
+
.catch(() => {
|
|
4683
5106
|
// Background preload is best-effort.
|
|
4684
5107
|
});
|
|
4685
5108
|
}
|
|
@@ -4985,9 +5408,10 @@ export class PerpsController extends BaseController {
|
|
|
4985
5408
|
});
|
|
4986
5409
|
}
|
|
4987
5410
|
/**
|
|
4988
|
-
* Project the
|
|
5411
|
+
* Project the position that would remain after a proposed order.
|
|
4989
5412
|
* Margin and liquidation availability are independent: a missing liquidation
|
|
4990
|
-
* does not hide a valid margin projection.
|
|
5413
|
+
* does not hide a valid margin projection. Hyperliquid projects Cross
|
|
5414
|
+
* positions against the account's Cross collateral.
|
|
4991
5415
|
*
|
|
4992
5416
|
* @param params - Live position plus the proposed order.
|
|
4993
5417
|
* @returns Discriminated preview of the resulting position.
|
|
@@ -5001,6 +5425,28 @@ export class PerpsController extends BaseController {
|
|
|
5001
5425
|
context,
|
|
5002
5426
|
});
|
|
5003
5427
|
}
|
|
5428
|
+
/**
|
|
5429
|
+
* Estimate the liquidation price of the Cross position an order would
|
|
5430
|
+
* leave: an opening, or an add, reduce or flip of the market's position.
|
|
5431
|
+
* Never rejects: `price`, `none` when no price liquidates it at the
|
|
5432
|
+
* account's collateral, or `unavailable` with a typed reason.
|
|
5433
|
+
*
|
|
5434
|
+
* @param params - The proposed Cross order.
|
|
5435
|
+
* @returns The typed estimate.
|
|
5436
|
+
*/
|
|
5437
|
+
async estimateCrossLiquidation(params) {
|
|
5438
|
+
// Not initialized or reinitializing: no provider to ask. A direct-mode
|
|
5439
|
+
// route that names another venue would answer for the wrong venue.
|
|
5440
|
+
const provider = this.getActiveProviderOrNull();
|
|
5441
|
+
if (!provider ||
|
|
5442
|
+
this.#hasConflictingProviderRoute(params.providerId, provider)) {
|
|
5443
|
+
return { status: 'unavailable', reason: 'provider' };
|
|
5444
|
+
}
|
|
5445
|
+
return this.#marketDataService.estimateCrossLiquidation({
|
|
5446
|
+
provider,
|
|
5447
|
+
params,
|
|
5448
|
+
});
|
|
5449
|
+
}
|
|
5004
5450
|
/**
|
|
5005
5451
|
* Calculate maintenance margin for a specific asset
|
|
5006
5452
|
* Returns a percentage (e.g., 0.0125 for 1.25%)
|
|
@@ -5240,7 +5686,9 @@ export class PerpsController extends BaseController {
|
|
|
5240
5686
|
// The state-change listener may have already fired during reinit
|
|
5241
5687
|
// but was skipped due to the #isReinitializing guard.
|
|
5242
5688
|
if (this.#preloadTimer) {
|
|
5243
|
-
this.#performMarketDataPreload()
|
|
5689
|
+
this.#performMarketDataPreload()
|
|
5690
|
+
.then(() => this.#refreshMarketMetaSubscribers())
|
|
5691
|
+
.catch(() => {
|
|
5244
5692
|
/* fire-and-forget */
|
|
5245
5693
|
});
|
|
5246
5694
|
this.#performUserDataPreload().catch(() => {
|
|
@@ -5407,7 +5855,9 @@ export class PerpsController extends BaseController {
|
|
|
5407
5855
|
completeReinitialization();
|
|
5408
5856
|
// Re-trigger preload now that reinit is complete.
|
|
5409
5857
|
if (this.#preloadTimer) {
|
|
5410
|
-
this.#performMarketDataPreload()
|
|
5858
|
+
this.#performMarketDataPreload()
|
|
5859
|
+
.then(() => this.#refreshMarketMetaSubscribers())
|
|
5860
|
+
.catch(() => {
|
|
5411
5861
|
/* fire-and-forget */
|
|
5412
5862
|
});
|
|
5413
5863
|
this.#performUserDataPreload().catch(() => {
|
|
@@ -5614,17 +6064,16 @@ export class PerpsController extends BaseController {
|
|
|
5614
6064
|
*
|
|
5615
6065
|
* @param provider - Initially active provider.
|
|
5616
6066
|
* @param open - Opens this subscription on a provider.
|
|
5617
|
-
* @param onInvalidate - Invalidates a cached value while the route is detached.
|
|
5618
6067
|
* @returns Cleanup that always removes the current binding.
|
|
5619
6068
|
*/
|
|
5620
|
-
#trackProviderSubscription(provider, open
|
|
5621
|
-
if (provider.protocolId !== 'aggregated'
|
|
6069
|
+
#trackProviderSubscription(provider, open) {
|
|
6070
|
+
if (provider.protocolId !== 'aggregated' ||
|
|
6071
|
+
this.#openingChannelSources > 0) {
|
|
5622
6072
|
return open(provider);
|
|
5623
6073
|
}
|
|
5624
6074
|
const subscription = {
|
|
5625
6075
|
open,
|
|
5626
6076
|
unsubscribe: open(provider),
|
|
5627
|
-
onInvalidate,
|
|
5628
6077
|
};
|
|
5629
6078
|
this.#providerSubscriptions.add(subscription);
|
|
5630
6079
|
return () => {
|
|
@@ -5656,15 +6105,10 @@ export class PerpsController extends BaseController {
|
|
|
5656
6105
|
catch (error) {
|
|
5657
6106
|
this.#logProviderSubscriptionError(error);
|
|
5658
6107
|
}
|
|
5659
|
-
if (!discard && this.#providerSubscriptions.has(subscription)) {
|
|
5660
|
-
try {
|
|
5661
|
-
subscription.onInvalidate?.();
|
|
5662
|
-
}
|
|
5663
|
-
catch (error) {
|
|
5664
|
-
this.#logProviderSubscriptionError(error);
|
|
5665
|
-
}
|
|
5666
|
-
}
|
|
5667
6108
|
}
|
|
6109
|
+
// Stream channels stop before their provider disconnects; readers stay
|
|
6110
|
+
// and the next successful initialization reopens them.
|
|
6111
|
+
this.#streams.rebind((key) => key.providerId === PENDING_PROVIDER_CHANNEL_ID, (key) => ({ ...key, providerId: PENDING_PROVIDER_CHANNEL_ID }));
|
|
5668
6112
|
}
|
|
5669
6113
|
/** Reattach surviving registrations after rollback or a later recovery. */
|
|
5670
6114
|
#restoreProviderSubscriptions() {
|
|
@@ -5700,6 +6144,148 @@ export class PerpsController extends BaseController {
|
|
|
5700
6144
|
#logProviderSubscriptionError(error) {
|
|
5701
6145
|
this.#logError(ensureError(error, 'PerpsController.providerSubscriptionRollback'), this.#getErrorContext('providerSubscriptionRollback'));
|
|
5702
6146
|
}
|
|
6147
|
+
/**
|
|
6148
|
+
* Join the shared channel for one `subscribeTo*` call, opening the venue
|
|
6149
|
+
* subscription when it is the first reader. The key comes from
|
|
6150
|
+
* `#getChannelKey`. A call made while no provider is active joins a pending
|
|
6151
|
+
* channel that opens on the next successful initialization.
|
|
6152
|
+
*
|
|
6153
|
+
* @param stream - Stream name.
|
|
6154
|
+
* @param params - The caller's parameters.
|
|
6155
|
+
* @param open - Opens the venue subscription for the channel.
|
|
6156
|
+
* @param source - Replay and REST seed behaviour of the stream.
|
|
6157
|
+
* @param source.replay - Replay the last value to later readers.
|
|
6158
|
+
* @param source.read - REST read seeding the channel and run once per drop.
|
|
6159
|
+
* @returns Cleanup that leaves the channel.
|
|
6160
|
+
*/
|
|
6161
|
+
#subscribeToChannel(stream, params, open, source = {}) {
|
|
6162
|
+
const { callback, onError, throttleMs, onReset, ...options } = params;
|
|
6163
|
+
const { read } = source;
|
|
6164
|
+
return this.#streams.subscribe(this.#getChannelKey(stream, options), {
|
|
6165
|
+
...source,
|
|
6166
|
+
read: read
|
|
6167
|
+
? async (key) => key.providerId === PENDING_PROVIDER_CHANNEL_ID
|
|
6168
|
+
? undefined
|
|
6169
|
+
: await read()
|
|
6170
|
+
: undefined,
|
|
6171
|
+
open: (emit, fail, key) => {
|
|
6172
|
+
// A pending channel opens when `#rebindStreams` finds a provider.
|
|
6173
|
+
if (key.providerId === PENDING_PROVIDER_CHANNEL_ID) {
|
|
6174
|
+
return () => undefined;
|
|
6175
|
+
}
|
|
6176
|
+
this.#openingChannelSources += 1;
|
|
6177
|
+
try {
|
|
6178
|
+
return open({
|
|
6179
|
+
...params,
|
|
6180
|
+
throttleMs: undefined,
|
|
6181
|
+
onReset: undefined,
|
|
6182
|
+
callback: emit,
|
|
6183
|
+
// Every reader of the channel hears its failures, not only the
|
|
6184
|
+
// reader that opened it.
|
|
6185
|
+
onError: fail,
|
|
6186
|
+
});
|
|
6187
|
+
}
|
|
6188
|
+
finally {
|
|
6189
|
+
this.#openingChannelSources -= 1;
|
|
6190
|
+
}
|
|
6191
|
+
},
|
|
6192
|
+
}, { callback, onError, throttleMs, onReset });
|
|
6193
|
+
}
|
|
6194
|
+
/**
|
|
6195
|
+
* The channel key of a stream: every parameter but the callbacks and
|
|
6196
|
+
* `throttleMs` (each reader throttles on its own), the active provider
|
|
6197
|
+
* instance, and for account streams the account, the selected one when none
|
|
6198
|
+
* is given. `prices` symbols are sorted so their order does not split
|
|
6199
|
+
* channels.
|
|
6200
|
+
*
|
|
6201
|
+
* @param stream - Stream name.
|
|
6202
|
+
* @param options - The stream's parameters without callbacks.
|
|
6203
|
+
* @returns The channel key.
|
|
6204
|
+
*/
|
|
6205
|
+
#getChannelKey(stream, options) {
|
|
6206
|
+
const provider = this.getActiveProviderOrNull();
|
|
6207
|
+
const { symbols } = options;
|
|
6208
|
+
return {
|
|
6209
|
+
stream,
|
|
6210
|
+
providerId: provider
|
|
6211
|
+
? String(this.#getProviderChannelId(provider))
|
|
6212
|
+
: PENDING_PROVIDER_CHANNEL_ID,
|
|
6213
|
+
accountId: ACCOUNT_STREAMS.has(stream)
|
|
6214
|
+
? this.#getChannelAccountId(options)
|
|
6215
|
+
: undefined,
|
|
6216
|
+
options: stream === 'prices' && Array.isArray(symbols)
|
|
6217
|
+
? { ...options, symbols: symbols.map(String).sort() }
|
|
6218
|
+
: options,
|
|
6219
|
+
};
|
|
6220
|
+
}
|
|
6221
|
+
/**
|
|
6222
|
+
* Move channels whose provider or selected account changed to the current
|
|
6223
|
+
* one: readers keep their subscription, receive `onReset`, then the new
|
|
6224
|
+
* context's data. Pending channels open here. Does nothing while no
|
|
6225
|
+
* provider is active.
|
|
6226
|
+
*/
|
|
6227
|
+
#rebindStreams() {
|
|
6228
|
+
if (!this.getActiveProviderOrNull()) {
|
|
6229
|
+
return;
|
|
6230
|
+
}
|
|
6231
|
+
const nextKey = (key) => this.#getChannelKey(key.stream, key.options ?? {});
|
|
6232
|
+
this.#streams.rebind((key) => {
|
|
6233
|
+
const next = nextKey(key);
|
|
6234
|
+
return (next.providerId === key.providerId && next.accountId === key.accountId);
|
|
6235
|
+
}, nextKey);
|
|
6236
|
+
}
|
|
6237
|
+
/**
|
|
6238
|
+
* The last value a stream's channel delivered, for a synchronous first
|
|
6239
|
+
* render (such as React's `useSyncExternalStore`). Uses the same channel
|
|
6240
|
+
* as a `subscribeTo*` call with these parameters.
|
|
6241
|
+
*
|
|
6242
|
+
* @param stream - Stream name.
|
|
6243
|
+
* @param params - The `subscribeTo*` parameters; callbacks and throttle are
|
|
6244
|
+
* ignored.
|
|
6245
|
+
* @returns The callback arguments last delivered, or `undefined` when the
|
|
6246
|
+
* channel is not open or has no value yet.
|
|
6247
|
+
*/
|
|
6248
|
+
getStreamSnapshot(stream, params) {
|
|
6249
|
+
const { callback: _callback, onError: _onError, onReset: _onReset, throttleMs: _throttleMs, ...options } = params;
|
|
6250
|
+
return this.#streams.getLast(this.#getChannelKey(stream, options));
|
|
6251
|
+
}
|
|
6252
|
+
/**
|
|
6253
|
+
* Stop and restart every open stream's venue subscription, keeping readers
|
|
6254
|
+
* and last values. For a socket that reports open but no longer delivers,
|
|
6255
|
+
* such as after an app returns to the foreground.
|
|
6256
|
+
*/
|
|
6257
|
+
resubscribeStreams() {
|
|
6258
|
+
this.#streams.reopenAll();
|
|
6259
|
+
}
|
|
6260
|
+
/**
|
|
6261
|
+
* Account part of an account stream's channel key: the explicit account,
|
|
6262
|
+
* else the selected one.
|
|
6263
|
+
*
|
|
6264
|
+
* @param options - The stream's parameters without callbacks.
|
|
6265
|
+
* @returns The account key.
|
|
6266
|
+
*/
|
|
6267
|
+
#getChannelAccountId(options) {
|
|
6268
|
+
if ('accountId' in options && typeof options.accountId === 'string') {
|
|
6269
|
+
return options.accountId;
|
|
6270
|
+
}
|
|
6271
|
+
const selected = getSelectedEvmAccountFromMessenger(this.messenger);
|
|
6272
|
+
return `selected:${selected?.address.toLowerCase() ?? 'none'}`;
|
|
6273
|
+
}
|
|
6274
|
+
/**
|
|
6275
|
+
* Stable id of a provider instance for channel keys.
|
|
6276
|
+
*
|
|
6277
|
+
* @param provider - Provider instance.
|
|
6278
|
+
* @returns Its id.
|
|
6279
|
+
*/
|
|
6280
|
+
#getProviderChannelId(provider) {
|
|
6281
|
+
let id = this.#providerChannelIds.get(provider);
|
|
6282
|
+
if (id === undefined) {
|
|
6283
|
+
this.#nextProviderChannelId += 1;
|
|
6284
|
+
id = this.#nextProviderChannelId;
|
|
6285
|
+
this.#providerChannelIds.set(provider, id);
|
|
6286
|
+
}
|
|
6287
|
+
return id;
|
|
6288
|
+
}
|
|
5703
6289
|
// Live data delegation (NO Redux) - delegates to active provider
|
|
5704
6290
|
/**
|
|
5705
6291
|
* Subscribe to live price updates
|
|
@@ -5708,6 +6294,15 @@ export class PerpsController extends BaseController {
|
|
|
5708
6294
|
* @returns A cleanup function to remove the subscription.
|
|
5709
6295
|
*/
|
|
5710
6296
|
subscribeToPrices(params) {
|
|
6297
|
+
return this.#subscribeToChannel('prices', params, (channelParams) => this.#openPrices(channelParams));
|
|
6298
|
+
}
|
|
6299
|
+
/**
|
|
6300
|
+
* Open the venue subscription behind a `prices` channel.
|
|
6301
|
+
*
|
|
6302
|
+
* @param params - The channel's parameters.
|
|
6303
|
+
* @returns A cleanup function to remove the subscription.
|
|
6304
|
+
*/
|
|
6305
|
+
#openPrices(params) {
|
|
5711
6306
|
const provider = this.getActiveProviderOrNull();
|
|
5712
6307
|
if (!provider) {
|
|
5713
6308
|
return () => {
|
|
@@ -5733,6 +6328,19 @@ export class PerpsController extends BaseController {
|
|
|
5733
6328
|
* @returns A cleanup function to remove the subscription.
|
|
5734
6329
|
*/
|
|
5735
6330
|
subscribeToPositions(params) {
|
|
6331
|
+
return this.#subscribeToChannel('positions', params, (channelParams) => this.#openPositions(channelParams), {
|
|
6332
|
+
read: async () => params.accountId === undefined
|
|
6333
|
+
? [await this.getPositions(), params.includeSnapshotStatus]
|
|
6334
|
+
: undefined,
|
|
6335
|
+
});
|
|
6336
|
+
}
|
|
6337
|
+
/**
|
|
6338
|
+
* Open the venue subscription behind a `positions` channel.
|
|
6339
|
+
*
|
|
6340
|
+
* @param params - The channel's parameters.
|
|
6341
|
+
* @returns A cleanup function to remove the subscription.
|
|
6342
|
+
*/
|
|
6343
|
+
#openPositions(params) {
|
|
5736
6344
|
const provider = this.getActiveProviderOrNull();
|
|
5737
6345
|
if (!provider) {
|
|
5738
6346
|
return () => {
|
|
@@ -5758,6 +6366,15 @@ export class PerpsController extends BaseController {
|
|
|
5758
6366
|
* @returns A cleanup function to remove the subscription.
|
|
5759
6367
|
*/
|
|
5760
6368
|
subscribeToOrderFills(params) {
|
|
6369
|
+
return this.#subscribeToChannel('orderFills', params, (channelParams) => this.#openOrderFills(channelParams), { replay: false });
|
|
6370
|
+
}
|
|
6371
|
+
/**
|
|
6372
|
+
* Open the venue subscription behind a `orderFills` channel.
|
|
6373
|
+
*
|
|
6374
|
+
* @param params - The channel's parameters.
|
|
6375
|
+
* @returns A cleanup function to remove the subscription.
|
|
6376
|
+
*/
|
|
6377
|
+
#openOrderFills(params) {
|
|
5761
6378
|
const provider = this.getActiveProviderOrNull();
|
|
5762
6379
|
if (!provider) {
|
|
5763
6380
|
return () => {
|
|
@@ -5787,6 +6404,15 @@ export class PerpsController extends BaseController {
|
|
|
5787
6404
|
* @returns A cleanup function to remove the subscription.
|
|
5788
6405
|
*/
|
|
5789
6406
|
subscribeToTwapOrders(params) {
|
|
6407
|
+
return this.#subscribeToChannel('twapOrders', params, (channelParams) => this.#openTwapOrders(channelParams));
|
|
6408
|
+
}
|
|
6409
|
+
/**
|
|
6410
|
+
* Open the venue subscription behind a `twapOrders` channel.
|
|
6411
|
+
*
|
|
6412
|
+
* @param params - The channel's parameters.
|
|
6413
|
+
* @returns A cleanup function to remove the subscription.
|
|
6414
|
+
*/
|
|
6415
|
+
#openTwapOrders(params) {
|
|
5790
6416
|
const provider = this.getActiveProviderOrNull();
|
|
5791
6417
|
if (!provider?.subscribeToTwapOrders) {
|
|
5792
6418
|
return () => {
|
|
@@ -5794,7 +6420,7 @@ export class PerpsController extends BaseController {
|
|
|
5794
6420
|
};
|
|
5795
6421
|
}
|
|
5796
6422
|
try {
|
|
5797
|
-
return this.#trackProviderSubscription(provider, (nextProvider) => nextProvider.subscribeToTwapOrders?.(params) ?? (() => undefined)
|
|
6423
|
+
return this.#trackProviderSubscription(provider, (nextProvider) => nextProvider.subscribeToTwapOrders?.(params) ?? (() => undefined));
|
|
5798
6424
|
}
|
|
5799
6425
|
catch (error) {
|
|
5800
6426
|
this.#logError(ensureError(error, 'PerpsController.subscribeToTwapOrders'), this.#getErrorContext('subscribeToTwapOrders', {
|
|
@@ -5815,6 +6441,15 @@ export class PerpsController extends BaseController {
|
|
|
5815
6441
|
* @returns A cleanup function that stops reads and the stream.
|
|
5816
6442
|
*/
|
|
5817
6443
|
subscribeToLiveTwapOrders(params) {
|
|
6444
|
+
return this.#subscribeToChannel('liveTwapOrders', params, (channelParams) => this.#openLiveTwapOrders(channelParams));
|
|
6445
|
+
}
|
|
6446
|
+
/**
|
|
6447
|
+
* Open the venue subscription behind a `liveTwapOrders` channel.
|
|
6448
|
+
*
|
|
6449
|
+
* @param params - The channel's parameters.
|
|
6450
|
+
* @returns A cleanup function to remove the subscription.
|
|
6451
|
+
*/
|
|
6452
|
+
#openLiveTwapOrders(params) {
|
|
5818
6453
|
return createLiveTwapOrderList({
|
|
5819
6454
|
source: {
|
|
5820
6455
|
getTwapOrders: async () => await this.getTwapOrders(),
|
|
@@ -5887,6 +6522,15 @@ export class PerpsController extends BaseController {
|
|
|
5887
6522
|
* @returns A cleanup function that stops reads.
|
|
5888
6523
|
*/
|
|
5889
6524
|
subscribeToChaseOrderLists(params) {
|
|
6525
|
+
return this.#subscribeToChannel('chaseOrderLists', params, (channelParams) => this.#openChaseOrderLists(channelParams));
|
|
6526
|
+
}
|
|
6527
|
+
/**
|
|
6528
|
+
* Open the venue subscription behind a `chaseOrderLists` channel.
|
|
6529
|
+
*
|
|
6530
|
+
* @param params - The channel's parameters.
|
|
6531
|
+
* @returns A cleanup function to remove the subscription.
|
|
6532
|
+
*/
|
|
6533
|
+
#openChaseOrderLists(params) {
|
|
5890
6534
|
return createLiveChaseOrderList({
|
|
5891
6535
|
source: {
|
|
5892
6536
|
getChaseOrders: async () => await this.getChaseOrders(),
|
|
@@ -5942,6 +6586,23 @@ export class PerpsController extends BaseController {
|
|
|
5942
6586
|
* @returns A cleanup function to remove the subscription.
|
|
5943
6587
|
*/
|
|
5944
6588
|
subscribeToOrders(params) {
|
|
6589
|
+
return this.#subscribeToChannel('orders', params, (channelParams) => this.#openOrders(channelParams), {
|
|
6590
|
+
read: async () => params.accountId === undefined
|
|
6591
|
+
? [
|
|
6592
|
+
await this.getOpenOrders({ throwOnUnavailable: true }),
|
|
6593
|
+
undefined,
|
|
6594
|
+
params.includeSnapshotStatus,
|
|
6595
|
+
]
|
|
6596
|
+
: undefined,
|
|
6597
|
+
});
|
|
6598
|
+
}
|
|
6599
|
+
/**
|
|
6600
|
+
* Open the venue subscription behind a `orders` channel.
|
|
6601
|
+
*
|
|
6602
|
+
* @param params - The channel's parameters.
|
|
6603
|
+
* @returns A cleanup function to remove the subscription.
|
|
6604
|
+
*/
|
|
6605
|
+
#openOrders(params) {
|
|
5945
6606
|
const provider = this.getActiveProviderOrNull();
|
|
5946
6607
|
if (!provider) {
|
|
5947
6608
|
return () => {
|
|
@@ -6008,6 +6669,15 @@ export class PerpsController extends BaseController {
|
|
|
6008
6669
|
* @returns A cleanup function to remove the subscription.
|
|
6009
6670
|
*/
|
|
6010
6671
|
subscribeToAccount(params) {
|
|
6672
|
+
return this.#subscribeToChannel('account', params, (channelParams) => this.#openAccount(channelParams));
|
|
6673
|
+
}
|
|
6674
|
+
/**
|
|
6675
|
+
* Open the venue subscription behind a `account` channel.
|
|
6676
|
+
*
|
|
6677
|
+
* @param params - The channel's parameters.
|
|
6678
|
+
* @returns A cleanup function to remove the subscription.
|
|
6679
|
+
*/
|
|
6680
|
+
#openAccount(params) {
|
|
6011
6681
|
const provider = this.getActiveProviderOrNull();
|
|
6012
6682
|
if (!provider) {
|
|
6013
6683
|
return () => {
|
|
@@ -6125,7 +6795,7 @@ export class PerpsController extends BaseController {
|
|
|
6125
6795
|
}
|
|
6126
6796
|
throw error;
|
|
6127
6797
|
}
|
|
6128
|
-
}
|
|
6798
|
+
});
|
|
6129
6799
|
}
|
|
6130
6800
|
catch (error) {
|
|
6131
6801
|
this.#logError(ensureError(error, 'PerpsController.subscribeToAccount'), this.#getErrorContext('subscribeToAccount', {
|
|
@@ -6144,6 +6814,16 @@ export class PerpsController extends BaseController {
|
|
|
6144
6814
|
* @returns A cleanup function to remove the subscription.
|
|
6145
6815
|
*/
|
|
6146
6816
|
subscribeToOrderBook(params) {
|
|
6817
|
+
// A replaced transport does not restore the book; the channel reopens it.
|
|
6818
|
+
return this.#subscribeToChannel('orderBook', params, (channelParams) => this.#openOrderBook(channelParams), { reopenOnReconnect: true });
|
|
6819
|
+
}
|
|
6820
|
+
/**
|
|
6821
|
+
* Open the venue subscription behind a `orderBook` channel.
|
|
6822
|
+
*
|
|
6823
|
+
* @param params - The channel's parameters.
|
|
6824
|
+
* @returns A cleanup function to remove the subscription.
|
|
6825
|
+
*/
|
|
6826
|
+
#openOrderBook(params) {
|
|
6147
6827
|
const provider = this.getActiveProviderOrNull();
|
|
6148
6828
|
if (!provider) {
|
|
6149
6829
|
return () => {
|
|
@@ -6178,6 +6858,15 @@ export class PerpsController extends BaseController {
|
|
|
6178
6858
|
* @returns A cleanup function to remove the subscription.
|
|
6179
6859
|
*/
|
|
6180
6860
|
subscribeToAvailableToTrade(params) {
|
|
6861
|
+
return this.#subscribeToChannel('availableToTrade', params, (channelParams) => this.#openAvailableToTrade(channelParams));
|
|
6862
|
+
}
|
|
6863
|
+
/**
|
|
6864
|
+
* Open the venue subscription behind a `availableToTrade` channel.
|
|
6865
|
+
*
|
|
6866
|
+
* @param params - The channel's parameters.
|
|
6867
|
+
* @returns A cleanup function to remove the subscription.
|
|
6868
|
+
*/
|
|
6869
|
+
#openAvailableToTrade(params) {
|
|
6181
6870
|
const noop = () => {
|
|
6182
6871
|
// No-op: value unavailable for this route
|
|
6183
6872
|
};
|
|
@@ -6190,7 +6879,7 @@ export class PerpsController extends BaseController {
|
|
|
6190
6879
|
return this.#trackProviderSubscription(provider, (nextProvider) => this.#hasConflictingProviderRoute(params.providerId, nextProvider)
|
|
6191
6880
|
? () => undefined
|
|
6192
6881
|
: (nextProvider.subscribeToAvailableToTrade?.(params) ??
|
|
6193
|
-
(() => undefined))
|
|
6882
|
+
(() => undefined)));
|
|
6194
6883
|
}
|
|
6195
6884
|
catch (error) {
|
|
6196
6885
|
this.#logError(ensureError(error, 'PerpsController.subscribeToAvailableToTrade'), this.#getErrorContext('subscribeToAvailableToTrade', { symbol: params.symbol, providerId: params.providerId }, CONNECTION_ERROR_TAGS));
|
|
@@ -6264,6 +6953,17 @@ export class PerpsController extends BaseController {
|
|
|
6264
6953
|
* @returns A cleanup function to remove the subscription.
|
|
6265
6954
|
*/
|
|
6266
6955
|
subscribeToPublicTrades(params) {
|
|
6956
|
+
return this.#subscribeToChannel('publicTrades', params, (channelParams) => this.#openPublicTrades(channelParams),
|
|
6957
|
+
// A replaced transport does not restore trades; the channel reopens it.
|
|
6958
|
+
{ replay: false, reopenOnReconnect: true });
|
|
6959
|
+
}
|
|
6960
|
+
/**
|
|
6961
|
+
* Open the venue subscription behind a `publicTrades` channel.
|
|
6962
|
+
*
|
|
6963
|
+
* @param params - The channel's parameters.
|
|
6964
|
+
* @returns A cleanup function to remove the subscription.
|
|
6965
|
+
*/
|
|
6966
|
+
#openPublicTrades(params) {
|
|
6267
6967
|
const noop = () => {
|
|
6268
6968
|
// No-op: no public trade feed for this route
|
|
6269
6969
|
};
|
|
@@ -6314,6 +7014,15 @@ export class PerpsController extends BaseController {
|
|
|
6314
7014
|
* @returns A cleanup function to remove the subscription.
|
|
6315
7015
|
*/
|
|
6316
7016
|
subscribeToCandles(params) {
|
|
7017
|
+
return this.#subscribeToChannel('candles', params, (channelParams) => this.#openCandles(channelParams));
|
|
7018
|
+
}
|
|
7019
|
+
/**
|
|
7020
|
+
* Open the venue subscription behind a `candles` channel.
|
|
7021
|
+
*
|
|
7022
|
+
* @param params - The channel's parameters.
|
|
7023
|
+
* @returns A cleanup function to remove the subscription.
|
|
7024
|
+
*/
|
|
7025
|
+
#openCandles(params) {
|
|
6317
7026
|
const provider = this.getActiveProviderOrNull();
|
|
6318
7027
|
if (!provider) {
|
|
6319
7028
|
return () => {
|
|
@@ -6342,6 +7051,15 @@ export class PerpsController extends BaseController {
|
|
|
6342
7051
|
* @returns A cleanup function to remove the subscription.
|
|
6343
7052
|
*/
|
|
6344
7053
|
subscribeToOICaps(params) {
|
|
7054
|
+
return this.#subscribeToChannel('oiCaps', params, (channelParams) => this.#openOICaps(channelParams));
|
|
7055
|
+
}
|
|
7056
|
+
/**
|
|
7057
|
+
* Open the venue subscription behind a `oiCaps` channel.
|
|
7058
|
+
*
|
|
7059
|
+
* @param params - The channel's parameters.
|
|
7060
|
+
* @returns A cleanup function to remove the subscription.
|
|
7061
|
+
*/
|
|
7062
|
+
#openOICaps(params) {
|
|
6345
7063
|
const provider = this.getActiveProviderOrNull();
|
|
6346
7064
|
if (!provider) {
|
|
6347
7065
|
return () => {
|
|
@@ -6565,6 +7283,10 @@ export class PerpsController extends BaseController {
|
|
|
6565
7283
|
*/
|
|
6566
7284
|
invalidateSubscriptionBenefits() {
|
|
6567
7285
|
this.#rewardsIntegrationService.invalidateSubscriptionBenefits();
|
|
7286
|
+
// The discount the fee rates were priced with may have changed.
|
|
7287
|
+
for (const refresh of this.#feeRateRefreshers) {
|
|
7288
|
+
refresh();
|
|
7289
|
+
}
|
|
6568
7290
|
}
|
|
6569
7291
|
/**
|
|
6570
7292
|
* Disconnect provider and cleanup subscriptions
|
|
@@ -7814,19 +8536,33 @@ export class PerpsController extends BaseController {
|
|
|
7814
8536
|
}
|
|
7815
8537
|
}
|
|
7816
8538
|
/**
|
|
7817
|
-
* Report order
|
|
7818
|
-
*
|
|
8539
|
+
* Report an order to notify-perps with retry (non-blocking).
|
|
8540
|
+
* Hyperliquid orders go to the orders endpoint (DataLakeService). Lighter
|
|
8541
|
+
* orders enroll the account for notifications instead and never reach it.
|
|
7819
8542
|
*
|
|
7820
8543
|
* @param params - The operation parameters.
|
|
7821
8544
|
* @param params.action - The order action.
|
|
7822
8545
|
* @param params.symbol - The trading pair symbol.
|
|
8546
|
+
* @param params.providerId - The venue that executed the order, when known.
|
|
7823
8547
|
* @param params.slPrice - The stop loss price.
|
|
7824
8548
|
* @param params.tpPrice - The take profit price.
|
|
7825
8549
|
* @param params.retryCount - Internal retry counter.
|
|
7826
8550
|
* @param params._traceId - Internal trace ID.
|
|
8551
|
+
* @param provider - The provider that executed the order.
|
|
7827
8552
|
* @returns Whether the report was sent successfully, with an optional error message.
|
|
7828
8553
|
*/
|
|
7829
|
-
async reportOrderToDataLake(params) {
|
|
8554
|
+
async reportOrderToDataLake(params, provider) {
|
|
8555
|
+
if (params.providerId === 'lighter') {
|
|
8556
|
+
return this.#lighterNotificationEnrollmentService.enroll({
|
|
8557
|
+
isTestnet: isProviderOnTestnet('lighter', this.state.isTestnet),
|
|
8558
|
+
getAccountIndex: async () => {
|
|
8559
|
+
if (!provider.getNotificationAccountIndex) {
|
|
8560
|
+
throw new Error('Provider does not support notification enrollment');
|
|
8561
|
+
}
|
|
8562
|
+
return provider.getNotificationAccountIndex();
|
|
8563
|
+
},
|
|
8564
|
+
});
|
|
8565
|
+
}
|
|
7830
8566
|
return this.#dataLakeService.reportOrder({
|
|
7831
8567
|
action: params.action,
|
|
7832
8568
|
symbol: params.symbol,
|