@metamask-previews/perps-controller 25.0.0-preview-a7bdec3 → 25.0.0-preview-2be370e
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 +40 -15
- package/dist/PerpsController-method-action-types.d.ts +17 -7
- 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 +20 -10
- package/dist/PerpsController.d.ts.map +1 -1
- package/dist/PerpsController.js +196 -99
- package/dist/PerpsController.js.map +1 -1
- package/dist/constants/lighterConfig.js +2 -2
- package/dist/constants/lighterConfig.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.d.ts +14 -1
- package/dist/providers/AggregatedPerpsProvider.d.ts.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.js +46 -6
- package/dist/providers/AggregatedPerpsProvider.js.map +1 -1
- package/dist/providers/HyperLiquidProvider.d.ts.map +1 -1
- package/dist/providers/HyperLiquidProvider.js +60 -35
- package/dist/providers/HyperLiquidProvider.js.map +1 -1
- package/dist/services/HyperLiquidClientService.d.ts.map +1 -1
- package/dist/services/HyperLiquidClientService.js +5 -4
- package/dist/services/HyperLiquidClientService.js.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.d.ts +13 -1
- package/dist/services/HyperLiquidSubscriptionService.d.ts.map +1 -1
- package/dist/services/HyperLiquidSubscriptionService.js +296 -50
- package/dist/services/HyperLiquidSubscriptionService.js.map +1 -1
- package/dist/services/MarketDataService.d.ts +5 -2
- package/dist/services/MarketDataService.d.ts.map +1 -1
- package/dist/services/MarketDataService.js +211 -55
- package/dist/services/MarketDataService.js.map +1 -1
- package/dist/services/ServiceContext.d.ts +12 -7
- package/dist/services/ServiceContext.d.ts.map +1 -1
- package/dist/services/ServiceContext.js.map +1 -1
- package/dist/services/TerminalMarketService.d.ts.map +1 -1
- package/dist/services/TerminalMarketService.js +64 -22
- package/dist/services/TerminalMarketService.js.map +1 -1
- package/dist/types/index.d.ts +36 -6
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/utils/marketUtils.d.ts +60 -1
- package/dist/utils/marketUtils.d.ts.map +1 -1
- package/dist/utils/marketUtils.js +151 -3
- package/dist/utils/marketUtils.js.map +1 -1
- package/docs/architecture.md +9 -1
- package/docs/contracts/feature-flags.md +5 -5
- package/docs/venues/hyperliquid/init-flow.md +25 -8
- package/docs/venues/lighter/capabilities.md +4 -0
- package/docs/venues/lighter/signer-provenance.md +52 -0
- package/package.json +1 -1
|
@@ -172,8 +172,11 @@ export declare class MarketDataService {
|
|
|
172
172
|
/**
|
|
173
173
|
* Get market data with prices (includes price, volume, 24h change).
|
|
174
174
|
* Applies optional category filtering, sorting, and limit after fetching.
|
|
175
|
-
*
|
|
176
|
-
*
|
|
175
|
+
* Each venue reads its own global snapshot when one is configured: it is
|
|
176
|
+
* HyperLiquid's complete source and adds metadata to Lighter's rows. In
|
|
177
|
+
* aggregated mode every venue does this inside the fan-out, and each falls
|
|
178
|
+
* back to its provider independently. `useTerminalApi` controls only legacy
|
|
179
|
+
* metadata enrichment of direct HyperLiquid provider data.
|
|
177
180
|
*
|
|
178
181
|
* @param options - The configuration options.
|
|
179
182
|
* @param options.provider - The perps provider instance.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarketDataService.d.ts","sourceRoot":"","sources":["../../src/services/MarketDataService.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"MarketDataService.d.ts","sourceRoot":"","sources":["../../src/services/MarketDataService.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAOhE,OAAO,KAAK,EACV,aAA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{ v4 as uuidv4 } from 'uuid';
|
|
|
2
2
|
import { PerpsMeasurementName } from '../constants/performanceMetrics.js';
|
|
3
3
|
import { PERPS_CONSTANTS } from '../constants/perpsConfig.js';
|
|
4
4
|
import { PERPS_ERROR_CODES } from '../perpsErrorCodes.js';
|
|
5
|
+
import { AggregatedPerpsProvider } from '../providers/AggregatedPerpsProvider.js';
|
|
5
6
|
import { PerpsTraceNames, PerpsTraceOperations } from '../types/index.js';
|
|
6
7
|
import { coalescePerpsRestRequest } from '../utils/coalescePerpsRestRequest.js';
|
|
7
8
|
import { ensureError, isAbortError } from '../utils/errorUtils.js';
|
|
8
|
-
import { applyMarketFilters } from '../utils/marketUtils.js';
|
|
9
|
+
import { applyMarketFilters, getSnapshotMarketIdentity, } from '../utils/marketUtils.js';
|
|
9
10
|
import { applyFeeResolution } from '../utils/subscriptionFeeWaiver.js';
|
|
10
11
|
/**
|
|
11
12
|
* MarketDataService
|
|
@@ -593,27 +594,195 @@ export class MarketDataService {
|
|
|
593
594
|
if (!terminalMarketService?.fetchGlobalSnapshot) {
|
|
594
595
|
return undefined;
|
|
595
596
|
}
|
|
596
|
-
|
|
597
|
-
throw new Error('Terminal global snapshot context changed');
|
|
598
|
-
}
|
|
597
|
+
this.#assertSnapshotCurrent(globalSnapshot);
|
|
599
598
|
try {
|
|
600
599
|
const snapshot = await terminalMarketService.fetchGlobalSnapshot(globalSnapshot.request);
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
}
|
|
604
|
-
if (Date.now() >= snapshot.expiresAt) {
|
|
600
|
+
this.#assertSnapshotCurrent(globalSnapshot);
|
|
601
|
+
if (!this.#isGlobalSnapshotFresh(snapshot)) {
|
|
605
602
|
throw new Error('Terminal global snapshot expired');
|
|
606
603
|
}
|
|
607
604
|
return snapshot;
|
|
608
605
|
}
|
|
609
606
|
catch (snapshotError) {
|
|
610
|
-
|
|
611
|
-
throw new Error('Terminal global snapshot context changed');
|
|
612
|
-
}
|
|
607
|
+
this.#assertSnapshotCurrent(globalSnapshot);
|
|
613
608
|
terminalMarketService.logError(snapshotError, logMethod);
|
|
614
609
|
return undefined;
|
|
615
610
|
}
|
|
616
611
|
}
|
|
612
|
+
#isGlobalSnapshotFresh(snapshot) {
|
|
613
|
+
return snapshot !== undefined && Date.now() < snapshot.expiresAt;
|
|
614
|
+
}
|
|
615
|
+
#assertSnapshotCurrent(globalSnapshot) {
|
|
616
|
+
if (!globalSnapshot.isCurrent()) {
|
|
617
|
+
if (globalSnapshot.request.provider === 'hyperliquid') {
|
|
618
|
+
throw Object.assign(new Error('Terminal global snapshot context changed'), {
|
|
619
|
+
code: PERPS_ERROR_CODES.PROVIDER_LIFECYCLE_STALE,
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
throw new Error(PERPS_ERROR_CODES.PROVIDER_LIFECYCLE_STALE);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Pick the snapshot context for one venue.
|
|
627
|
+
*
|
|
628
|
+
* @param context - Snapshot contexts keyed by provider.
|
|
629
|
+
* @param protocolId - The venue's provider id.
|
|
630
|
+
* @returns That venue's context, or undefined when it has none.
|
|
631
|
+
*/
|
|
632
|
+
#snapshotContextFor(context, protocolId) {
|
|
633
|
+
if (protocolId !== 'hyperliquid' && protocolId !== 'lighter') {
|
|
634
|
+
return undefined;
|
|
635
|
+
}
|
|
636
|
+
return (context.globalSnapshots?.[protocolId] ??
|
|
637
|
+
(context.globalSnapshot?.request.provider === protocolId
|
|
638
|
+
? context.globalSnapshot
|
|
639
|
+
: undefined));
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Read one venue's market data. An accepted HyperLiquid snapshot replaces
|
|
643
|
+
* the venue rows. A Lighter snapshot only adds display and discovery
|
|
644
|
+
* fields to the venue rows, which keep Lighter's prices and trading rules.
|
|
645
|
+
* A rejected snapshot leaves the venue rows; without a snapshot context,
|
|
646
|
+
* `useTerminalApi` adds legacy Terminal metadata to them.
|
|
647
|
+
*
|
|
648
|
+
* @param provider - The venue's provider.
|
|
649
|
+
* @param globalSnapshot - The venue's snapshot context, if any.
|
|
650
|
+
* @param useTerminalApi - Whether to add legacy Terminal metadata.
|
|
651
|
+
* @returns Venue rows with their expiry and fallback when a snapshot was applied.
|
|
652
|
+
*/
|
|
653
|
+
async #readVenueMarketData(provider, globalSnapshot, useTerminalApi = false) {
|
|
654
|
+
const { terminalMarketService } = this.#deps;
|
|
655
|
+
if (!globalSnapshot || !terminalMarketService?.fetchGlobalSnapshot) {
|
|
656
|
+
// Fetch Terminal metadata before provider data. It enriches the
|
|
657
|
+
// provider rows (name, keywords, tags, categories) but never replaces
|
|
658
|
+
// live pricing or funding.
|
|
659
|
+
let terminalMetadata;
|
|
660
|
+
if (useTerminalApi && terminalMarketService) {
|
|
661
|
+
try {
|
|
662
|
+
const result = await terminalMarketService.fetchMarkets();
|
|
663
|
+
if (result.metadata.size > 0) {
|
|
664
|
+
terminalMetadata = result.metadata;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
catch (terminalError) {
|
|
668
|
+
terminalMarketService.logError(terminalError, 'getMarketDataWithPrices');
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
const markets = await provider.getMarketDataWithPrices();
|
|
672
|
+
return terminalMetadata
|
|
673
|
+
? this.#enrichWithTerminalMetadata(markets, terminalMetadata)
|
|
674
|
+
: markets;
|
|
675
|
+
}
|
|
676
|
+
const logMethod = 'getMarketDataWithPrices.globalSnapshot';
|
|
677
|
+
if (globalSnapshot.request.provider === 'lighter') {
|
|
678
|
+
const [markets, snapshot] = await Promise.all([
|
|
679
|
+
provider.getMarketDataWithPrices(),
|
|
680
|
+
this.#readCurrentGlobalSnapshot(globalSnapshot, logMethod),
|
|
681
|
+
]);
|
|
682
|
+
this.#assertSnapshotCurrent(globalSnapshot);
|
|
683
|
+
if (!this.#isGlobalSnapshotFresh(snapshot)) {
|
|
684
|
+
return markets;
|
|
685
|
+
}
|
|
686
|
+
return {
|
|
687
|
+
markets: this.#mergeSnapshotMetadata(markets, snapshot.markets),
|
|
688
|
+
expiresAt: snapshot.expiresAt,
|
|
689
|
+
getFallback: async () => markets,
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
// A rejected HyperLiquid snapshot has one fallback to the provider and
|
|
693
|
+
// is not followed by a second legacy Terminal request.
|
|
694
|
+
const snapshot = await this.#readCurrentGlobalSnapshot(globalSnapshot, logMethod);
|
|
695
|
+
if (snapshot && snapshot.markets.length > 0) {
|
|
696
|
+
return {
|
|
697
|
+
markets: snapshot.markets.filter((market) => globalSnapshot.isMarketAllowed(market.symbol)),
|
|
698
|
+
expiresAt: snapshot.expiresAt,
|
|
699
|
+
getFallback: async () => {
|
|
700
|
+
this.#assertSnapshotCurrent(globalSnapshot);
|
|
701
|
+
const markets = await provider.getMarketDataWithPrices();
|
|
702
|
+
this.#assertSnapshotCurrent(globalSnapshot);
|
|
703
|
+
return markets;
|
|
704
|
+
},
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
const markets = await provider.getMarketDataWithPrices();
|
|
708
|
+
this.#assertSnapshotCurrent(globalSnapshot);
|
|
709
|
+
return markets;
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Add a snapshot's display and discovery fields to venue rows of the same
|
|
713
|
+
* symbol. Price, change, funding and leverage stay with the venue; a row
|
|
714
|
+
* the snapshot does not list is returned unchanged.
|
|
715
|
+
*
|
|
716
|
+
* @param markets - Rows from the venue's provider.
|
|
717
|
+
* @param snapshotMarkets - Rows from the venue's snapshot.
|
|
718
|
+
* @returns The venue rows with snapshot metadata.
|
|
719
|
+
*/
|
|
720
|
+
#mergeSnapshotMetadata(markets, snapshotMarkets) {
|
|
721
|
+
const snapshotBySymbol = new Map(snapshotMarkets.map((market) => [market.symbol, market]));
|
|
722
|
+
return markets.map((market) => {
|
|
723
|
+
const snapshot = snapshotBySymbol.get(market.symbol);
|
|
724
|
+
if (!snapshot) {
|
|
725
|
+
return market;
|
|
726
|
+
}
|
|
727
|
+
const { name, description, iconUrl, marketType, categories, keywords, tags, listedAt, volume, openInterest, } = snapshot;
|
|
728
|
+
return {
|
|
729
|
+
...market,
|
|
730
|
+
name,
|
|
731
|
+
volume,
|
|
732
|
+
...(openInterest !== undefined && { openInterest }),
|
|
733
|
+
...(description !== undefined && { description }),
|
|
734
|
+
...(iconUrl !== undefined && { iconUrl }),
|
|
735
|
+
...(marketType !== undefined && { marketType }),
|
|
736
|
+
...(categories !== undefined && { categories }),
|
|
737
|
+
...(keywords !== undefined && { keywords }),
|
|
738
|
+
...(tags !== undefined && { tags }),
|
|
739
|
+
...(listedAt !== undefined && { listedAt }),
|
|
740
|
+
...getSnapshotMarketIdentity(snapshot),
|
|
741
|
+
};
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Read trading rules from the venue and join only snapshot market identity.
|
|
746
|
+
*
|
|
747
|
+
* @param provider - The direct or aggregated provider.
|
|
748
|
+
* @param params - Trading-list query parameters.
|
|
749
|
+
* @param context - Per-venue snapshot identities for this read.
|
|
750
|
+
* @returns The venue trading rules with any available market keys and scales.
|
|
751
|
+
*/
|
|
752
|
+
async #readProviderMarketInfos(provider, params, context) {
|
|
753
|
+
const lighter = provider.protocolId === 'lighter'
|
|
754
|
+
? this.#snapshotContextFor(context, 'lighter')
|
|
755
|
+
: undefined;
|
|
756
|
+
const venueContexts = provider instanceof AggregatedPerpsProvider
|
|
757
|
+
? Object.values(context.globalSnapshots ?? {})
|
|
758
|
+
: [lighter].filter((snapshot) => snapshot !== undefined);
|
|
759
|
+
if (params?.skipFilters || venueContexts.length === 0) {
|
|
760
|
+
return provider.getMarkets(params);
|
|
761
|
+
}
|
|
762
|
+
const [markets, snapshots] = await Promise.all([
|
|
763
|
+
provider.getMarkets(params),
|
|
764
|
+
Promise.all(venueContexts.map(async (snapshotContext) => {
|
|
765
|
+
const snapshot = await this.#readCurrentGlobalSnapshot(snapshotContext, 'getMarkets.globalSnapshot');
|
|
766
|
+
return [snapshotContext.request.provider, snapshot];
|
|
767
|
+
})),
|
|
768
|
+
]);
|
|
769
|
+
venueContexts.forEach((snapshotContext) => this.#assertSnapshotCurrent(snapshotContext));
|
|
770
|
+
const byVenue = new Map();
|
|
771
|
+
for (const [venue, snapshot] of snapshots) {
|
|
772
|
+
if (this.#isGlobalSnapshotFresh(snapshot)) {
|
|
773
|
+
byVenue.set(venue, new Map(snapshot.marketInfos?.map((market) => [market.name, market])));
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return markets.map((market) => {
|
|
777
|
+
const venue = market.providerId ?? provider.protocolId;
|
|
778
|
+
const snapshot = venue === 'hyperliquid' || venue === 'lighter'
|
|
779
|
+
? byVenue.get(venue)?.get(market.name)
|
|
780
|
+
: undefined;
|
|
781
|
+
return snapshot
|
|
782
|
+
? { ...market, ...getSnapshotMarketIdentity(snapshot) }
|
|
783
|
+
: market;
|
|
784
|
+
});
|
|
785
|
+
}
|
|
617
786
|
/**
|
|
618
787
|
* Get available markets
|
|
619
788
|
* Handles full orchestration: tracing, error logging, state management, and provider delegation.
|
|
@@ -672,8 +841,11 @@ export class MarketDataService {
|
|
|
672
841
|
};
|
|
673
842
|
// A configured snapshot URL is the list. Do not follow a rejected
|
|
674
843
|
// snapshot with the legacy /v1 market request. `useTerminalApi` does
|
|
675
|
-
// not choose this path.
|
|
676
|
-
|
|
844
|
+
// not choose this path. Lighter trading rules stay with its provider,
|
|
845
|
+
// so only a direct HyperLiquid read takes its list from the snapshot.
|
|
846
|
+
const globalSnapshot = provider.protocolId === 'hyperliquid'
|
|
847
|
+
? this.#snapshotContextFor(context, 'hyperliquid')
|
|
848
|
+
: undefined;
|
|
677
849
|
const snapshotUrlConfigured = Boolean(this.#deps.terminalApi?.globalSnapshotUrl);
|
|
678
850
|
let snapshotAttempted = false;
|
|
679
851
|
if (snapshotUrlConfigured &&
|
|
@@ -709,9 +881,9 @@ export class MarketDataService {
|
|
|
709
881
|
this.#deps.terminalMarketService.logError(terminalError, 'getMarkets');
|
|
710
882
|
}
|
|
711
883
|
}
|
|
712
|
-
const markets = await provider
|
|
713
|
-
if (snapshotAttempted && globalSnapshot
|
|
714
|
-
|
|
884
|
+
const markets = await this.#readProviderMarketInfos(provider, params, context);
|
|
885
|
+
if (snapshotAttempted && globalSnapshot) {
|
|
886
|
+
this.#assertSnapshotCurrent(globalSnapshot);
|
|
715
887
|
}
|
|
716
888
|
if (context.stateManager) {
|
|
717
889
|
context.stateManager.update((state) => {
|
|
@@ -763,8 +935,11 @@ export class MarketDataService {
|
|
|
763
935
|
/**
|
|
764
936
|
* Get market data with prices (includes price, volume, 24h change).
|
|
765
937
|
* Applies optional category filtering, sorting, and limit after fetching.
|
|
766
|
-
*
|
|
767
|
-
*
|
|
938
|
+
* Each venue reads its own global snapshot when one is configured: it is
|
|
939
|
+
* HyperLiquid's complete source and adds metadata to Lighter's rows. In
|
|
940
|
+
* aggregated mode every venue does this inside the fan-out, and each falls
|
|
941
|
+
* back to its provider independently. `useTerminalApi` controls only legacy
|
|
942
|
+
* metadata enrichment of direct HyperLiquid provider data.
|
|
768
943
|
*
|
|
769
944
|
* @param options - The configuration options.
|
|
770
945
|
* @param options.provider - The perps provider instance.
|
|
@@ -774,7 +949,7 @@ export class MarketDataService {
|
|
|
774
949
|
*/
|
|
775
950
|
async getMarketDataWithPrices(options) {
|
|
776
951
|
const { provider, params, context } = options;
|
|
777
|
-
const {
|
|
952
|
+
const { globalSnapshots } = context;
|
|
778
953
|
// Legacy Terminal metadata is HyperLiquid-specific. Aggregated and
|
|
779
954
|
// direct non-HyperLiquid results must retain their own venue metadata.
|
|
780
955
|
const useTerminalApi = params?.useTerminalApi && provider.protocolId === 'hyperliquid';
|
|
@@ -796,47 +971,28 @@ export class MarketDataService {
|
|
|
796
971
|
}),
|
|
797
972
|
},
|
|
798
973
|
});
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
snapshotAttempted = true;
|
|
807
|
-
const snapshot = await this.#readCurrentGlobalSnapshot(globalSnapshot, 'getMarketDataWithPrices.globalSnapshot');
|
|
808
|
-
if (snapshot && snapshot.markets.length > 0) {
|
|
809
|
-
traceData = { success: true };
|
|
810
|
-
const allowedMarkets = snapshot.markets.filter((market) => globalSnapshot.isMarketAllowed(market.symbol));
|
|
811
|
-
return applyMarketFilters(allowedMarkets, params);
|
|
974
|
+
let markets;
|
|
975
|
+
if (provider instanceof AggregatedPerpsProvider) {
|
|
976
|
+
markets = await provider.getMarketDataWithPrices(async (providerId, venue) => this.#readVenueMarketData(venue, this.#snapshotContextFor(context, providerId)));
|
|
977
|
+
// The fan-out drops a venue whose read saw a context change, so the
|
|
978
|
+
// remaining rows could describe a context that is no longer current.
|
|
979
|
+
if (Object.values(globalSnapshots ?? {}).some((snapshot) => !snapshot.isCurrent())) {
|
|
980
|
+
throw new Error(PERPS_ERROR_CODES.PROVIDER_LIFECYCLE_STALE);
|
|
812
981
|
}
|
|
813
982
|
}
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
if (!snapshotAttempted &&
|
|
819
|
-
useTerminalApi &&
|
|
820
|
-
this.#deps.terminalMarketService) {
|
|
821
|
-
try {
|
|
822
|
-
const result = await this.#deps.terminalMarketService.fetchMarkets();
|
|
823
|
-
if (result.metadata.size > 0) {
|
|
824
|
-
terminalMetadata = result.metadata;
|
|
825
|
-
}
|
|
983
|
+
else {
|
|
984
|
+
const read = await this.#readVenueMarketData(provider, this.#snapshotContextFor(context, provider.protocolId), useTerminalApi);
|
|
985
|
+
if (Array.isArray(read)) {
|
|
986
|
+
markets = read;
|
|
826
987
|
}
|
|
827
|
-
|
|
828
|
-
|
|
988
|
+
else {
|
|
989
|
+
markets =
|
|
990
|
+
Date.now() < read.expiresAt
|
|
991
|
+
? read.markets
|
|
992
|
+
: await read.getFallback();
|
|
829
993
|
}
|
|
830
994
|
}
|
|
831
|
-
const
|
|
832
|
-
if (snapshotAttempted && globalSnapshot && !globalSnapshot.isCurrent()) {
|
|
833
|
-
throw new Error('Terminal global snapshot context changed');
|
|
834
|
-
}
|
|
835
|
-
// Enrich with terminal metadata when available
|
|
836
|
-
const enriched = terminalMetadata
|
|
837
|
-
? this.#enrichWithTerminalMetadata(markets, terminalMetadata)
|
|
838
|
-
: markets;
|
|
839
|
-
const filtered = applyMarketFilters(enriched, params);
|
|
995
|
+
const filtered = applyMarketFilters(markets, params);
|
|
840
996
|
traceData = { success: true };
|
|
841
997
|
return filtered;
|
|
842
998
|
}
|