@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
package/dist/PerpsController.js
CHANGED
|
@@ -40,7 +40,7 @@ import { PERPS_ERROR_ACTION, PERPS_ERROR_COMPONENT, PERPS_ERROR_OPERATION, creat
|
|
|
40
40
|
import { ensureError } from './utils/errorUtils.js';
|
|
41
41
|
import { parseAssetName } from './utils/hyperLiquidAdapter.js';
|
|
42
42
|
import { captureScaleOrderParams } from './utils/lighterScaleOrders.js';
|
|
43
|
-
import { clonePerpsMarketData, compileMarketPattern, shouldIncludeMarket, } from './utils/marketUtils.js';
|
|
43
|
+
import { clonePerpsMarketData, compileMarketPattern, getPerpsWatchlistReferences, shouldIncludeMarket, } from './utils/marketUtils.js';
|
|
44
44
|
import { isStrategyOrderType } from './utils/orderTypes.js';
|
|
45
45
|
import { hydrateFromDiskSync, persistMarketEntriesToDisk, persistUserEntriesToDisk, } from './utils/perpsDiskPersistence.js';
|
|
46
46
|
import { getProviderHealthOf, subscribeToProviderHealthOf, } from './utils/providerHealth.js';
|
|
@@ -744,11 +744,15 @@ export class PerpsController extends BaseController {
|
|
|
744
744
|
// Live chase lists waiting to hear which chase cancels succeeded.
|
|
745
745
|
#chaseCancellationListeners = new Set();
|
|
746
746
|
/**
|
|
747
|
-
* Latest toggle
|
|
748
|
-
*
|
|
749
|
-
* has superseded it.
|
|
747
|
+
* Latest toggle per network and saved reference whose write is pending.
|
|
748
|
+
* Grouped and symbol-only calls can overlap on a legacy reference.
|
|
750
749
|
*/
|
|
751
750
|
#pendingWatchlistToggles = new Map();
|
|
751
|
+
/**
|
|
752
|
+
* Removals a failed toggle could not undo because a newer pending toggle
|
|
753
|
+
* owned one of its references. The newer toggle undoes them if it fails too.
|
|
754
|
+
*/
|
|
755
|
+
#deferredWatchlistRestores = new Map();
|
|
752
756
|
#userDiskWrite = Promise.resolve();
|
|
753
757
|
#providerSubscriptions = new Set();
|
|
754
758
|
// A failed automatic rollback does not end the consumers' subscription context.
|
|
@@ -953,23 +957,8 @@ export class PerpsController extends BaseController {
|
|
|
953
957
|
const { activeProvider } = this.state;
|
|
954
958
|
const cache = this.state.cachedMarketDataByProvider;
|
|
955
959
|
if (activeProvider === 'aggregated') {
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
for (const providerId of this.#getAggregatedCacheProviderIds(Object.keys(cache))) {
|
|
959
|
-
const key = buildProviderCacheKey(providerId, this.state.isTestnet);
|
|
960
|
-
const entry = cache[key];
|
|
961
|
-
if (!entry || entry.data.length === 0) {
|
|
962
|
-
continue;
|
|
963
|
-
}
|
|
964
|
-
if (!this.#isMarketCacheEntryCurrent(providerId, entry, options)) {
|
|
965
|
-
continue;
|
|
966
|
-
}
|
|
967
|
-
assembled.push(...clonePerpsMarketData(entry.data));
|
|
968
|
-
}
|
|
969
|
-
if (assembled.length === 0) {
|
|
970
|
-
return null;
|
|
971
|
-
}
|
|
972
|
-
return assembled;
|
|
960
|
+
const rows = this.#getCachedAggregateMarketRows(options);
|
|
961
|
+
return rows ? clonePerpsMarketData(rows) : null;
|
|
973
962
|
}
|
|
974
963
|
// Single provider mode
|
|
975
964
|
const key = buildProviderCacheKey(activeProvider, this.state.isTestnet);
|
|
@@ -982,6 +971,34 @@ export class PerpsController extends BaseController {
|
|
|
982
971
|
}
|
|
983
972
|
return clonePerpsMarketData(entry.data);
|
|
984
973
|
}
|
|
974
|
+
#getCachedAggregateMarketRows(options) {
|
|
975
|
+
const { cachedMarketDataByProvider: cache, isTestnet } = this.state;
|
|
976
|
+
const snapshotConfigured = this.#isGlobalSnapshotConfigured();
|
|
977
|
+
const providerIds = new Set(this.#getAggregatedCacheProviderIds(Object.keys(cache)));
|
|
978
|
+
if (snapshotConfigured) {
|
|
979
|
+
providerIds.add('hyperliquid');
|
|
980
|
+
// Before initialization Lighter may still register; afterwards require
|
|
981
|
+
// its slice only when it did, as snapshot reads only include it then.
|
|
982
|
+
if (this.isInitialized
|
|
983
|
+
? this.providers.has('lighter')
|
|
984
|
+
: this.#isLighterProviderEnabled()) {
|
|
985
|
+
providerIds.add('lighter');
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
const assembled = [];
|
|
989
|
+
for (const providerId of providerIds) {
|
|
990
|
+
const entry = cache[buildProviderCacheKey(providerId, isTestnet)];
|
|
991
|
+
if (entry?.data.length &&
|
|
992
|
+
this.#isMarketCacheEntryCurrent(providerId, entry, options)) {
|
|
993
|
+
assembled.push(...entry.data);
|
|
994
|
+
}
|
|
995
|
+
else if (snapshotConfigured || entry?.sourceExpiresAt !== undefined) {
|
|
996
|
+
// Clients treat a nonempty aggregate preload as fresh for every venue.
|
|
997
|
+
return null;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
return assembled.length > 0 ? assembled : null;
|
|
1001
|
+
}
|
|
985
1002
|
#isMarketCacheEntryCurrent(providerId, entry, options) {
|
|
986
1003
|
if (entry.sourceExpiresAt !== undefined) {
|
|
987
1004
|
const expectedDexes = this.#getStaticSnapshotDexes();
|
|
@@ -4140,9 +4157,9 @@ export class PerpsController extends BaseController {
|
|
|
4140
4157
|
* @returns Service context for `MarketDataService.getMarkets`.
|
|
4141
4158
|
*/
|
|
4142
4159
|
#createGetMarketsContext() {
|
|
4143
|
-
const
|
|
4160
|
+
const globalSnapshots = this.#buildGlobalSnapshotContexts();
|
|
4144
4161
|
return this.#createServiceContext('getMarkets', {
|
|
4145
|
-
...(
|
|
4162
|
+
...(globalSnapshots && { globalSnapshots }),
|
|
4146
4163
|
});
|
|
4147
4164
|
}
|
|
4148
4165
|
/**
|
|
@@ -4161,9 +4178,9 @@ export class PerpsController extends BaseController {
|
|
|
4161
4178
|
* @returns A promise that resolves to the market data.
|
|
4162
4179
|
*/
|
|
4163
4180
|
async getMarketDataWithPrices(params) {
|
|
4164
|
-
const
|
|
4181
|
+
const globalSnapshots = this.#buildGlobalSnapshotContexts();
|
|
4165
4182
|
const context = this.#createServiceContext('getMarketDataWithPrices', {
|
|
4166
|
-
...(
|
|
4183
|
+
...(globalSnapshots && { globalSnapshots }),
|
|
4167
4184
|
});
|
|
4168
4185
|
if (params?.standalone) {
|
|
4169
4186
|
const provider = this.activeProviderInstance ?? this.#getOrCreateStandaloneProvider();
|
|
@@ -4184,35 +4201,60 @@ export class PerpsController extends BaseController {
|
|
|
4184
4201
|
});
|
|
4185
4202
|
}
|
|
4186
4203
|
/**
|
|
4187
|
-
* Capture the exact static identity
|
|
4188
|
-
*
|
|
4189
|
-
*
|
|
4204
|
+
* Capture the exact static identity each active provider needs to adopt its
|
|
4205
|
+
* atomic snapshot: the direct provider, or every registered venue in
|
|
4206
|
+
* aggregated mode. HyperLiquid with dynamic DEX discovery opts out and
|
|
4207
|
+
* keeps the provider path.
|
|
4190
4208
|
*
|
|
4191
|
-
* @returns
|
|
4209
|
+
* @returns One snapshot identity with its race guard per provider, or
|
|
4210
|
+
* undefined when none applies.
|
|
4192
4211
|
*/
|
|
4193
|
-
#
|
|
4194
|
-
|
|
4195
|
-
typeof this.#options.infrastructure.terminalMarketService
|
|
4196
|
-
?.fetchGlobalSnapshot === 'function';
|
|
4197
|
-
if (!snapshotConfigured || this.state.activeProvider !== 'hyperliquid') {
|
|
4212
|
+
#buildGlobalSnapshotContexts() {
|
|
4213
|
+
if (!this.#isGlobalSnapshotConfigured()) {
|
|
4198
4214
|
return undefined;
|
|
4199
4215
|
}
|
|
4200
|
-
const
|
|
4201
|
-
|
|
4202
|
-
|
|
4216
|
+
const { activeProvider, isTestnet, hip3ConfigVersion } = this.state;
|
|
4217
|
+
const isActive = (providerId) => activeProvider === providerId ||
|
|
4218
|
+
(activeProvider === 'aggregated' && this.providers.has(providerId));
|
|
4219
|
+
const contexts = {};
|
|
4220
|
+
const hyperLiquidDexes = isActive('hyperliquid')
|
|
4221
|
+
? this.#getStaticSnapshotDexes()
|
|
4222
|
+
: undefined;
|
|
4223
|
+
if (hyperLiquidDexes) {
|
|
4224
|
+
contexts.hyperliquid = {
|
|
4225
|
+
request: {
|
|
4226
|
+
provider: 'hyperliquid',
|
|
4227
|
+
network: isTestnet ? 'testnet' : 'mainnet',
|
|
4228
|
+
enabledDexes: hyperLiquidDexes,
|
|
4229
|
+
},
|
|
4230
|
+
isCurrent: () => this.state.activeProvider === activeProvider &&
|
|
4231
|
+
this.state.isTestnet === isTestnet &&
|
|
4232
|
+
this.state.hip3ConfigVersion === hip3ConfigVersion,
|
|
4233
|
+
isMarketAllowed: this.#buildMarketAllowedFilter(),
|
|
4234
|
+
};
|
|
4203
4235
|
}
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
this.state.
|
|
4214
|
-
|
|
4215
|
-
|
|
4236
|
+
if (isActive('lighter')) {
|
|
4237
|
+
contexts.lighter = {
|
|
4238
|
+
request: {
|
|
4239
|
+
provider: 'lighter',
|
|
4240
|
+
network: isProviderOnTestnet('lighter', isTestnet)
|
|
4241
|
+
? 'testnet'
|
|
4242
|
+
: 'mainnet',
|
|
4243
|
+
enabledDexes: ['main'],
|
|
4244
|
+
},
|
|
4245
|
+
isCurrent: () => this.state.activeProvider === activeProvider &&
|
|
4246
|
+
this.state.isTestnet === isTestnet,
|
|
4247
|
+
// Lighter keeps its provider's market list; the snapshot only adds
|
|
4248
|
+
// metadata to it, so there is no snapshot row to filter.
|
|
4249
|
+
isMarketAllowed: () => true,
|
|
4250
|
+
};
|
|
4251
|
+
}
|
|
4252
|
+
return contexts.hyperliquid || contexts.lighter ? contexts : undefined;
|
|
4253
|
+
}
|
|
4254
|
+
#isGlobalSnapshotConfigured() {
|
|
4255
|
+
const { terminalApi, terminalMarketService } = this.#options.infrastructure;
|
|
4256
|
+
return (Boolean(terminalApi?.globalSnapshotUrl) ||
|
|
4257
|
+
typeof terminalMarketService?.fetchGlobalSnapshot === 'function');
|
|
4216
4258
|
}
|
|
4217
4259
|
#getStaticSnapshotDexes() {
|
|
4218
4260
|
if (!this.#hip3Enabled) {
|
|
@@ -4485,7 +4527,9 @@ export class PerpsController extends BaseController {
|
|
|
4485
4527
|
const existingEntry = this.state.cachedMarketDataByProvider[cacheKey];
|
|
4486
4528
|
if (existingEntry &&
|
|
4487
4529
|
this.#isMarketCacheEntryCurrent(actualProviderId, existingEntry) &&
|
|
4488
|
-
now - existingEntry.timestamp < _a.#preloadGuardMs
|
|
4530
|
+
now - existingEntry.timestamp < _a.#preloadGuardMs &&
|
|
4531
|
+
(actualProviderId !== 'aggregated' ||
|
|
4532
|
+
this.#getCachedAggregateMarketRows() !== null)) {
|
|
4489
4533
|
return;
|
|
4490
4534
|
}
|
|
4491
4535
|
this.#isPreloading = true;
|
|
@@ -4519,20 +4563,23 @@ export class PerpsController extends BaseController {
|
|
|
4519
4563
|
}
|
|
4520
4564
|
// Store under per-provider key(s)
|
|
4521
4565
|
const ts = Date.now();
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4566
|
+
// A slice made only of snapshot rows expires with its snapshot.
|
|
4567
|
+
const snapshotCacheIdentity = (rows) => {
|
|
4568
|
+
const sourceExpiries = rows.flatMap((market) => market.dataSource === 'terminal-global-snapshot-mark' &&
|
|
4569
|
+
typeof market.sourceExpiresAt === 'number'
|
|
4570
|
+
? [market.sourceExpiresAt]
|
|
4571
|
+
: []);
|
|
4572
|
+
if (rows.length === 0 ||
|
|
4573
|
+
sourceExpiries.length !== rows.length ||
|
|
4574
|
+
!staticSnapshotDexes) {
|
|
4575
|
+
return {};
|
|
4576
|
+
}
|
|
4577
|
+
return {
|
|
4578
|
+
sourceExpiresAt: Math.min(...sourceExpiries),
|
|
4532
4579
|
hip3ConfigVersion: preloadContext.hip3ConfigVersion,
|
|
4533
4580
|
dexes: staticSnapshotDexes,
|
|
4534
|
-
}
|
|
4535
|
-
|
|
4581
|
+
};
|
|
4582
|
+
};
|
|
4536
4583
|
const marketDiskEntries = [];
|
|
4537
4584
|
if (!isCurrent()) {
|
|
4538
4585
|
traceData = {
|
|
@@ -4567,6 +4614,7 @@ export class PerpsController extends BaseController {
|
|
|
4567
4614
|
state.cachedMarketDataByProvider[key] = {
|
|
4568
4615
|
data: slice,
|
|
4569
4616
|
timestamp: ts,
|
|
4617
|
+
...snapshotCacheIdentity(slice),
|
|
4570
4618
|
};
|
|
4571
4619
|
}
|
|
4572
4620
|
// Write aggregated sentinel so the staleness guard sees it
|
|
@@ -4586,7 +4634,7 @@ export class PerpsController extends BaseController {
|
|
|
4586
4634
|
state.cachedMarketDataByProvider[cacheKey] = {
|
|
4587
4635
|
data,
|
|
4588
4636
|
timestamp: ts,
|
|
4589
|
-
...snapshotCacheIdentity,
|
|
4637
|
+
...snapshotCacheIdentity(data),
|
|
4590
4638
|
};
|
|
4591
4639
|
});
|
|
4592
4640
|
}
|
|
@@ -7332,11 +7380,16 @@ export class PerpsController extends BaseController {
|
|
|
7332
7380
|
* Watchlist markets are stored per network (testnet/mainnet).
|
|
7333
7381
|
*
|
|
7334
7382
|
* @param symbol - The trading pair symbol.
|
|
7383
|
+
* @param marketKey - Optional cross-venue key for a grouped market.
|
|
7384
|
+
* @param venues - Original rows from the group, used to match a legacy favorite.
|
|
7385
|
+
* @param legacyProvider - Venue that owned saved symbols before grouping.
|
|
7335
7386
|
*/
|
|
7336
|
-
async toggleWatchlistMarket(symbol) {
|
|
7387
|
+
async toggleWatchlistMarket(symbol, marketKey, venues, legacyProvider) {
|
|
7388
|
+
const references = getPerpsWatchlistReferences(symbol, marketKey, venues, legacyProvider);
|
|
7389
|
+
const [reference] = references;
|
|
7337
7390
|
const currentNetwork = this.state.isTestnet ? 'testnet' : 'mainnet';
|
|
7338
7391
|
const currentWatchlist = this.state.watchlistMarkets[currentNetwork];
|
|
7339
|
-
const isWatchlisted = currentWatchlist.includes(
|
|
7392
|
+
const isWatchlisted = references.some((entry) => currentWatchlist.includes(entry));
|
|
7340
7393
|
this.#debugLog('PerpsController: Toggling watchlist market', {
|
|
7341
7394
|
timestamp: new Date().toISOString(),
|
|
7342
7395
|
network: currentNetwork,
|
|
@@ -7346,19 +7399,32 @@ export class PerpsController extends BaseController {
|
|
|
7346
7399
|
// Step 1: Optimistic local state update — UI reflects change immediately.
|
|
7347
7400
|
this.update((state) => {
|
|
7348
7401
|
if (isWatchlisted) {
|
|
7349
|
-
state.watchlistMarkets[currentNetwork] = currentWatchlist.filter((marketSymbol) => marketSymbol
|
|
7402
|
+
state.watchlistMarkets[currentNetwork] = currentWatchlist.filter((marketSymbol) => !references.includes(marketSymbol));
|
|
7350
7403
|
}
|
|
7351
7404
|
else {
|
|
7352
|
-
state.watchlistMarkets[currentNetwork] = [
|
|
7405
|
+
state.watchlistMarkets[currentNetwork] = [
|
|
7406
|
+
...currentWatchlist,
|
|
7407
|
+
reference,
|
|
7408
|
+
];
|
|
7353
7409
|
}
|
|
7354
7410
|
});
|
|
7355
7411
|
for (const edits of this.#watchlistEditsDuringHydration) {
|
|
7356
|
-
|
|
7412
|
+
for (const entry of references) {
|
|
7413
|
+
edits.push({
|
|
7414
|
+
network: currentNetwork,
|
|
7415
|
+
symbol: entry,
|
|
7416
|
+
add: !isWatchlisted && entry === reference,
|
|
7417
|
+
});
|
|
7418
|
+
}
|
|
7357
7419
|
}
|
|
7358
|
-
const
|
|
7359
|
-
const
|
|
7360
|
-
|
|
7361
|
-
const
|
|
7420
|
+
const toggle = Symbol(reference);
|
|
7421
|
+
const toggleKey = (entry) => `${currentNetwork}:${entry}`;
|
|
7422
|
+
// References with no pending write reflect remote storage.
|
|
7423
|
+
const settledReferences = references.filter((entry) => !this.#pendingWatchlistToggles.has(toggleKey(entry)));
|
|
7424
|
+
for (const entry of references) {
|
|
7425
|
+
this.#pendingWatchlistToggles.set(toggleKey(entry), toggle);
|
|
7426
|
+
}
|
|
7427
|
+
const isLatestToggle = (entry) => this.#pendingWatchlistToggles.get(toggleKey(entry)) === toggle;
|
|
7362
7428
|
this.#getMetrics().trackPerpsEvent(PerpsAnalyticsEvent.UiInteraction, {
|
|
7363
7429
|
[PERPS_EVENT_PROPERTY.INTERACTION_TYPE]: PERPS_EVENT_VALUE.INTERACTION_TYPE.FAVORITE_TOGGLED,
|
|
7364
7430
|
[PERPS_EVENT_PROPERTY.ASSET]: symbol,
|
|
@@ -7386,31 +7452,53 @@ export class PerpsController extends BaseController {
|
|
|
7386
7452
|
network: currentNetwork,
|
|
7387
7453
|
action: isWatchlisted ? 'remove' : 'add',
|
|
7388
7454
|
}));
|
|
7389
|
-
// A later toggle of the same market decides its state; its own write
|
|
7390
|
-
// persists it.
|
|
7391
|
-
if (!isLatestToggle()) {
|
|
7392
|
-
return;
|
|
7393
|
-
}
|
|
7394
7455
|
// Undo only this toggle. The list may have changed since it was made
|
|
7395
7456
|
// (hydration, other toggles), so restoring the earlier snapshot would
|
|
7396
7457
|
// discard those changes.
|
|
7458
|
+
const deferred = this.#deferredWatchlistRestores.get(toggle) ?? [];
|
|
7397
7459
|
this.update((state) => {
|
|
7460
|
+
if (!isWatchlisted && isLatestToggle(reference)) {
|
|
7461
|
+
state.watchlistMarkets[currentNetwork] = state.watchlistMarkets[currentNetwork].filter((marketSymbol) => marketSymbol !== reference);
|
|
7462
|
+
}
|
|
7463
|
+
// A newer toggle of any alias owns the market's state now; restoring
|
|
7464
|
+
// the other aliases would save the market twice. It restores them
|
|
7465
|
+
// only if its own write fails too.
|
|
7466
|
+
const newerToggle = [
|
|
7467
|
+
...references,
|
|
7468
|
+
...deferred.flatMap(({ entries }) => entries),
|
|
7469
|
+
]
|
|
7470
|
+
.map((entry) => this.#pendingWatchlistToggles.get(toggleKey(entry)))
|
|
7471
|
+
.find((pending) => pending !== undefined && pending !== toggle);
|
|
7472
|
+
if (newerToggle) {
|
|
7473
|
+
// Hand over only what remote storage still holds.
|
|
7474
|
+
this.#deferredWatchlistRestores.set(newerToggle, [
|
|
7475
|
+
...(isWatchlisted
|
|
7476
|
+
? [{ entries: settledReferences, snapshot: currentWatchlist }]
|
|
7477
|
+
: []),
|
|
7478
|
+
...deferred,
|
|
7479
|
+
...(this.#deferredWatchlistRestores.get(newerToggle) ?? []),
|
|
7480
|
+
]);
|
|
7481
|
+
return;
|
|
7482
|
+
}
|
|
7483
|
+
const restores = isWatchlisted
|
|
7484
|
+
? [{ entries: references, snapshot: currentWatchlist }, ...deferred]
|
|
7485
|
+
: deferred;
|
|
7398
7486
|
const watchlist = state.watchlistMarkets[currentNetwork];
|
|
7399
|
-
|
|
7400
|
-
|
|
7401
|
-
|
|
7402
|
-
|
|
7403
|
-
|
|
7487
|
+
for (const { entries, snapshot } of restores) {
|
|
7488
|
+
for (const [index, entry] of snapshot.entries()) {
|
|
7489
|
+
if (entries.includes(entry) && !watchlist.includes(entry)) {
|
|
7490
|
+
watchlist.splice(Math.min(index, watchlist.length), 0, entry);
|
|
7491
|
+
}
|
|
7404
7492
|
}
|
|
7405
7493
|
}
|
|
7406
|
-
else {
|
|
7407
|
-
state.watchlistMarkets[currentNetwork] = watchlist.filter((marketSymbol) => marketSymbol !== symbol);
|
|
7408
|
-
}
|
|
7409
7494
|
});
|
|
7410
7495
|
}
|
|
7411
7496
|
finally {
|
|
7412
|
-
|
|
7413
|
-
|
|
7497
|
+
this.#deferredWatchlistRestores.delete(toggle);
|
|
7498
|
+
for (const entry of references) {
|
|
7499
|
+
if (isLatestToggle(entry)) {
|
|
7500
|
+
this.#pendingWatchlistToggles.delete(toggleKey(entry));
|
|
7501
|
+
}
|
|
7414
7502
|
}
|
|
7415
7503
|
}
|
|
7416
7504
|
}
|
|
@@ -7418,16 +7506,19 @@ export class PerpsController extends BaseController {
|
|
|
7418
7506
|
* Check if a market is in the watchlist on the current network
|
|
7419
7507
|
*
|
|
7420
7508
|
* @param symbol - The trading pair symbol.
|
|
7421
|
-
* @
|
|
7509
|
+
* @param marketKey - Optional cross-venue key for a grouped market.
|
|
7510
|
+
* @param venues - Original rows from the group, used to match a legacy favorite.
|
|
7511
|
+
* @param legacyProvider - Venue that owned saved symbols before grouping.
|
|
7512
|
+
* @returns Whether the reference is saved.
|
|
7422
7513
|
*/
|
|
7423
|
-
isWatchlistMarket(symbol) {
|
|
7514
|
+
isWatchlistMarket(symbol, marketKey, venues, legacyProvider) {
|
|
7424
7515
|
const currentNetwork = this.state.isTestnet ? 'testnet' : 'mainnet';
|
|
7425
|
-
return this.state.watchlistMarkets[currentNetwork].includes(
|
|
7516
|
+
return getPerpsWatchlistReferences(symbol, marketKey, venues, legacyProvider).some((reference) => this.state.watchlistMarkets[currentNetwork].includes(reference));
|
|
7426
7517
|
}
|
|
7427
7518
|
/**
|
|
7428
7519
|
* Get all watchlist markets for the current network
|
|
7429
7520
|
*
|
|
7430
|
-
* @returns
|
|
7521
|
+
* @returns Saved market references for the current network.
|
|
7431
7522
|
*/
|
|
7432
7523
|
getWatchlistMarkets() {
|
|
7433
7524
|
const currentNetwork = this.state.isTestnet ? 'testnet' : 'mainnet';
|
|
@@ -7436,19 +7527,25 @@ export class PerpsController extends BaseController {
|
|
|
7436
7527
|
/**
|
|
7437
7528
|
* Record that the user viewed a market.
|
|
7438
7529
|
*
|
|
7439
|
-
* The
|
|
7440
|
-
* Any existing entry for the same
|
|
7441
|
-
* duplicates. The list is then capped at
|
|
7530
|
+
* The market reference is prepended to the per-network recently-viewed list (newest-first).
|
|
7531
|
+
* Any existing entry for the same reference, or for its legacy alias, is
|
|
7532
|
+
* removed first so there are no duplicates. The list is then capped at
|
|
7533
|
+
* PERPS_CONSTANTS.RecentlyViewedMarketsLimit.
|
|
7442
7534
|
*
|
|
7443
7535
|
* @param symbol - The trading pair symbol (e.g. 'BTC', 'ETH', 'xyz:TSLA').
|
|
7536
|
+
* @param marketKey - Optional cross-venue key for a grouped market.
|
|
7537
|
+
* @param venues - Original rows from the group, used to match a legacy entry.
|
|
7538
|
+
* @param legacyProvider - Venue that owned saved symbols before grouping.
|
|
7444
7539
|
*/
|
|
7445
|
-
recordMarketViewed(symbol) {
|
|
7540
|
+
recordMarketViewed(symbol, marketKey, venues, legacyProvider) {
|
|
7541
|
+
const references = getPerpsWatchlistReferences(symbol, marketKey, venues, legacyProvider);
|
|
7542
|
+
const [reference] = references;
|
|
7446
7543
|
const currentNetwork = this.state.isTestnet ? 'testnet' : 'mainnet';
|
|
7447
7544
|
const now = Date.now();
|
|
7448
7545
|
this.update((state) => {
|
|
7449
|
-
const current = state.recentlyViewedMarkets[currentNetwork].filter((entry) => entry.symbol
|
|
7546
|
+
const current = state.recentlyViewedMarkets[currentNetwork].filter((entry) => !references.includes(entry.symbol));
|
|
7450
7547
|
state.recentlyViewedMarkets[currentNetwork] = [
|
|
7451
|
-
{ symbol, viewedAt: now },
|
|
7548
|
+
{ symbol: reference, viewedAt: now },
|
|
7452
7549
|
...current,
|
|
7453
7550
|
].slice(0, PERPS_CONSTANTS.RecentlyViewedMarketsLimit);
|
|
7454
7551
|
});
|
|
@@ -7456,12 +7553,12 @@ export class PerpsController extends BaseController {
|
|
|
7456
7553
|
/**
|
|
7457
7554
|
* Get recently viewed markets for the current network.
|
|
7458
7555
|
*
|
|
7459
|
-
* Returns up to PERPS_CONSTANTS.RecentlyViewedMarketsLimit
|
|
7556
|
+
* Returns up to PERPS_CONSTANTS.RecentlyViewedMarketsLimit references, ordered
|
|
7460
7557
|
* newest-first, filtered to entries within the last
|
|
7461
7558
|
* PERPS_CONSTANTS.RecentlyViewedMarketsTtlMs (24 hours). Returns an empty
|
|
7462
7559
|
* array when no qualifying entries exist.
|
|
7463
7560
|
*
|
|
7464
|
-
* @returns Ordered array of market
|
|
7561
|
+
* @returns Ordered array of saved market references.
|
|
7465
7562
|
*/
|
|
7466
7563
|
getRecentlyViewedMarkets() {
|
|
7467
7564
|
const currentNetwork = this.state.isTestnet ? 'testnet' : 'mainnet';
|