@metamask-previews/perps-controller 23.1.0-preview-d8f16be → 23.1.0-preview-682f78a99
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 +45 -0
- package/README.md +35 -0
- package/dist/PerpsController-method-action-types.d.ts +46 -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 +87 -52
- package/dist/PerpsController.d.ts.map +1 -1
- package/dist/PerpsController.js +738 -54
- package/dist/PerpsController.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/perpsErrorCodes.d.ts +1 -0
- package/dist/perpsErrorCodes.d.ts.map +1 -1
- package/dist/perpsErrorCodes.js +2 -0
- package/dist/perpsErrorCodes.js.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.d.ts +10 -0
- package/dist/providers/AggregatedPerpsProvider.d.ts.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.js +203 -38
- package/dist/providers/AggregatedPerpsProvider.js.map +1 -1
- package/dist/routing/isPerpsProviderType.d.ts +10 -0
- package/dist/routing/isPerpsProviderType.d.ts.map +1 -0
- package/dist/routing/isPerpsProviderType.js +11 -0
- package/dist/routing/isPerpsProviderType.js.map +1 -0
- package/dist/routing/resolveTradeConfigProviderKey.d.ts +22 -0
- package/dist/routing/resolveTradeConfigProviderKey.d.ts.map +1 -0
- package/dist/routing/resolveTradeConfigProviderKey.js +25 -0
- package/dist/routing/resolveTradeConfigProviderKey.js.map +1 -0
- package/dist/selectors.d.ts +35 -233
- package/dist/selectors.d.ts.map +1 -1
- package/dist/selectors.js +36 -13
- package/dist/selectors.js.map +1 -1
- package/dist/services/MarketDataService.d.ts.map +1 -1
- package/dist/services/MarketDataService.js +12 -6
- package/dist/services/MarketDataService.js.map +1 -1
- package/dist/services/ServiceContext.d.ts +15 -3
- package/dist/services/ServiceContext.d.ts.map +1 -1
- package/dist/services/ServiceContext.js.map +1 -1
- package/dist/services/TradingService.d.ts +3 -1
- package/dist/services/TradingService.d.ts.map +1 -1
- package/dist/services/TradingService.js +9 -4
- package/dist/services/TradingService.js.map +1 -1
- package/dist/types/index.d.ts +59 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -67,12 +67,15 @@ export class AggregatedPerpsProvider {
|
|
|
67
67
|
protocolId = 'aggregated';
|
|
68
68
|
routesOrdersByProviderId = true;
|
|
69
69
|
#providers;
|
|
70
|
+
/** Default provider used when an operation does not name a venue. */
|
|
70
71
|
#defaultProvider;
|
|
71
72
|
#aggregationMode;
|
|
72
73
|
#deps;
|
|
73
74
|
#isTestnet;
|
|
74
75
|
#router;
|
|
75
76
|
#subscriptionMux;
|
|
77
|
+
/** Streams that follow the routing default and must move with it. */
|
|
78
|
+
#defaultRoutedSubscriptions = new Set();
|
|
76
79
|
constructor(config) {
|
|
77
80
|
this.#providers = config.providers;
|
|
78
81
|
this.#defaultProvider = config.defaultProvider;
|
|
@@ -132,6 +135,135 @@ export class AggregatedPerpsProvider {
|
|
|
132
135
|
getWriteProviderId(providerId) {
|
|
133
136
|
return providerId ?? this.#defaultProvider;
|
|
134
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Change the default provider used when an operation does not name a venue.
|
|
140
|
+
*
|
|
141
|
+
* Only the routing default changes: no provider is re-initialized and no
|
|
142
|
+
* stream is reconnected, so the call is synchronous.
|
|
143
|
+
*
|
|
144
|
+
* @param providerId - New default provider. Must be registered.
|
|
145
|
+
* @throws If the provider is not registered.
|
|
146
|
+
*/
|
|
147
|
+
setDefaultProvider(providerId) {
|
|
148
|
+
if (!this.#providers.has(providerId)) {
|
|
149
|
+
throw new Error(`[AggregatedPerpsProvider] Default provider '${providerId}' not available`);
|
|
150
|
+
}
|
|
151
|
+
const changed = providerId !== this.#defaultProvider;
|
|
152
|
+
if (changed) {
|
|
153
|
+
this.#defaultProvider = providerId;
|
|
154
|
+
this.#router.setDefaultProvider(providerId);
|
|
155
|
+
}
|
|
156
|
+
// Runs even when the default is unchanged so a stream that failed to
|
|
157
|
+
// re-open earlier is retried instead of staying detached.
|
|
158
|
+
this.#rerouteDefaultSubscriptions();
|
|
159
|
+
if (changed) {
|
|
160
|
+
this.#deps.debugLogger.log('[AggregatedPerpsProvider] Default provider changed', { defaultProvider: providerId });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Register a stream that follows the routing default.
|
|
165
|
+
*
|
|
166
|
+
* The returned cleanup detaches the stream from whichever venue it is
|
|
167
|
+
* currently open on, so it stays valid across a default change.
|
|
168
|
+
*
|
|
169
|
+
* @param options - Stream options.
|
|
170
|
+
* @param options.open - Opens the stream on a given provider.
|
|
171
|
+
* @param options.onInvalidate - Invalidates a consumer's cached value before
|
|
172
|
+
* the stream moves (for example, emits an unavailable account snapshot).
|
|
173
|
+
* @returns A cleanup function that closes the stream.
|
|
174
|
+
*/
|
|
175
|
+
#trackDefaultRoutedSubscription(options) {
|
|
176
|
+
const subscription = {
|
|
177
|
+
open: options.open,
|
|
178
|
+
onInvalidate: options.onInvalidate,
|
|
179
|
+
unsubscribe: () => undefined,
|
|
180
|
+
providerId: this.#defaultProvider,
|
|
181
|
+
failed: false,
|
|
182
|
+
};
|
|
183
|
+
// Initial open failures propagate to the caller, matching the previous
|
|
184
|
+
// contract for these subscribe methods.
|
|
185
|
+
subscription.unsubscribe = options.open(this.#getDefaultProvider());
|
|
186
|
+
this.#defaultRoutedSubscriptions.add(subscription);
|
|
187
|
+
return () => {
|
|
188
|
+
if (!this.#defaultRoutedSubscriptions.delete(subscription)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
subscription.unsubscribe();
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Move every default-routed stream to the current default venue.
|
|
196
|
+
*
|
|
197
|
+
* Only the stream's subscription is torn down and re-opened; the venue
|
|
198
|
+
* transports stay connected. The caller's cleanup function keeps working.
|
|
199
|
+
*/
|
|
200
|
+
#rerouteDefaultSubscriptions() {
|
|
201
|
+
if (this.#defaultRoutedSubscriptions.size === 0) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const target = this.#defaultProvider;
|
|
205
|
+
for (const subscription of this.#defaultRoutedSubscriptions) {
|
|
206
|
+
if (!subscription.failed && subscription.providerId === target) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
this.#rerouteSubscription(subscription);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Re-open one stream on the current default, isolating its failure from the
|
|
214
|
+
* other subscriptions. A stream that throws is marked failed and retried on
|
|
215
|
+
* the next `setDefaultProvider` call.
|
|
216
|
+
*
|
|
217
|
+
* @param subscription - The stream to move.
|
|
218
|
+
*/
|
|
219
|
+
#rerouteSubscription(subscription) {
|
|
220
|
+
const providerId = this.#defaultProvider;
|
|
221
|
+
const { unsubscribe } = subscription;
|
|
222
|
+
subscription.unsubscribe = () => undefined;
|
|
223
|
+
try {
|
|
224
|
+
unsubscribe();
|
|
225
|
+
}
|
|
226
|
+
catch (caughtError) {
|
|
227
|
+
this.#logSubscriptionRerouteError(caughtError, 'unsubscribe', providerId);
|
|
228
|
+
}
|
|
229
|
+
subscription.providerId = undefined;
|
|
230
|
+
if (subscription.onInvalidate) {
|
|
231
|
+
try {
|
|
232
|
+
subscription.onInvalidate();
|
|
233
|
+
}
|
|
234
|
+
catch (caughtError) {
|
|
235
|
+
this.#logSubscriptionRerouteError(caughtError, 'onInvalidate', providerId);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
subscription.unsubscribe = subscription.open(this.#getDefaultProvider());
|
|
240
|
+
subscription.providerId = providerId;
|
|
241
|
+
subscription.failed = false;
|
|
242
|
+
}
|
|
243
|
+
catch (caughtError) {
|
|
244
|
+
subscription.failed = true;
|
|
245
|
+
this.#logSubscriptionRerouteError(caughtError, 'open', providerId);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Report a default-routed subscription that could not be moved.
|
|
250
|
+
*
|
|
251
|
+
* @param caughtError - The thrown error.
|
|
252
|
+
* @param method - Which step failed.
|
|
253
|
+
* @param providerId - The venue the stream was moving to.
|
|
254
|
+
*/
|
|
255
|
+
#logSubscriptionRerouteError(caughtError, method, providerId) {
|
|
256
|
+
this.#deps.logger.error(ensureError(caughtError, 'AggregatedPerpsProvider.rerouteSubscription'), {
|
|
257
|
+
tags: {
|
|
258
|
+
feature: PERPS_CONSTANTS.FeatureName,
|
|
259
|
+
provider: providerId,
|
|
260
|
+
},
|
|
261
|
+
context: {
|
|
262
|
+
name: 'AggregatedPerpsProvider',
|
|
263
|
+
data: { method: `subscription.${method}`, providerId },
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
}
|
|
135
267
|
/**
|
|
136
268
|
* Get the explicit provider, or the default when no route was supplied.
|
|
137
269
|
*
|
|
@@ -326,10 +458,13 @@ export class AggregatedPerpsProvider {
|
|
|
326
458
|
return this.#extractSuccessfulResults(results, 'getPositions').flat();
|
|
327
459
|
}
|
|
328
460
|
async getAccountState(params) {
|
|
329
|
-
//
|
|
461
|
+
// Capture the route before awaiting: `setDefaultProvider` can run while
|
|
462
|
+
// the venue request is in flight, and the response must stay attributed to
|
|
463
|
+
// the provider that served it (the user-data preloader buckets by this id).
|
|
464
|
+
const providerId = this.#defaultProvider;
|
|
330
465
|
const provider = this.#getDefaultProvider();
|
|
331
466
|
const state = await provider.getAccountState(params);
|
|
332
|
-
return { ...state, providerId
|
|
467
|
+
return { ...state, providerId };
|
|
333
468
|
}
|
|
334
469
|
async getMarkets(params) {
|
|
335
470
|
const results = await Promise.allSettled(this.#getActiveProviders().map(async ([id, provider]) => {
|
|
@@ -882,42 +1017,56 @@ export class AggregatedPerpsProvider {
|
|
|
882
1017
|
* native TWAP push channel.
|
|
883
1018
|
*/
|
|
884
1019
|
subscribeToTwapOrders(params) {
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
1020
|
+
return this.#trackDefaultRoutedSubscription({
|
|
1021
|
+
// A full empty snapshot replaces the previous venue's cached schedules,
|
|
1022
|
+
// including when the new venue exposes no native TWAP stream.
|
|
1023
|
+
onInvalidate: () => params.callback([], true),
|
|
1024
|
+
open: (provider) => {
|
|
1025
|
+
if (!provider.subscribeToTwapOrders) {
|
|
1026
|
+
return () => {
|
|
1027
|
+
// No-op: default provider exposes no native TWAP push channel
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
// Capture each stream's route so late batches from the old venue keep
|
|
1031
|
+
// their source identity during teardown or after a default change.
|
|
1032
|
+
const providerId = this.#defaultProvider;
|
|
1033
|
+
return provider.subscribeToTwapOrders({
|
|
1034
|
+
...params,
|
|
1035
|
+
// Stamp the same way getTwapOrders does, so swapping a poll for this
|
|
1036
|
+
// subscription does not silently drop providerId from every schedule.
|
|
1037
|
+
callback: (twapOrders, isSnapshot) => params.callback(twapOrders.map((order) => ({
|
|
1038
|
+
...order,
|
|
1039
|
+
providerId,
|
|
1040
|
+
})), isSnapshot),
|
|
1041
|
+
});
|
|
1042
|
+
},
|
|
902
1043
|
});
|
|
903
1044
|
}
|
|
904
1045
|
subscribeToAccount(params) {
|
|
905
|
-
//
|
|
906
|
-
//
|
|
907
|
-
|
|
908
|
-
|
|
1046
|
+
// Account, candles, order book and OI caps have no per-venue route of
|
|
1047
|
+
// their own, so they follow the routing default and move with it.
|
|
1048
|
+
return this.#trackDefaultRoutedSubscription({
|
|
1049
|
+
open: (provider) => provider.subscribeToAccount(params),
|
|
1050
|
+
// Emit an unavailable snapshot so a consumer that caches the balance
|
|
1051
|
+
// (for example Mobile's account stream channel) does not keep showing
|
|
1052
|
+
// the previous venue's value while the new venue's first update pends.
|
|
1053
|
+
onInvalidate: () => params.callback(null),
|
|
1054
|
+
});
|
|
909
1055
|
}
|
|
910
1056
|
subscribeToOICaps(params) {
|
|
911
|
-
|
|
912
|
-
|
|
1057
|
+
return this.#trackDefaultRoutedSubscription({
|
|
1058
|
+
open: (provider) => provider.subscribeToOICaps(params),
|
|
1059
|
+
});
|
|
913
1060
|
}
|
|
914
1061
|
subscribeToCandles(params) {
|
|
915
|
-
|
|
916
|
-
|
|
1062
|
+
return this.#trackDefaultRoutedSubscription({
|
|
1063
|
+
open: (provider) => provider.subscribeToCandles(params),
|
|
1064
|
+
});
|
|
917
1065
|
}
|
|
918
1066
|
subscribeToOrderBook(params) {
|
|
919
|
-
|
|
920
|
-
|
|
1067
|
+
return this.#trackDefaultRoutedSubscription({
|
|
1068
|
+
open: (provider) => provider.subscribeToOrderBook(params),
|
|
1069
|
+
});
|
|
921
1070
|
}
|
|
922
1071
|
/**
|
|
923
1072
|
* Stream available to trade from the explicit route, else the default
|
|
@@ -928,7 +1077,18 @@ export class AggregatedPerpsProvider {
|
|
|
928
1077
|
* @returns A cleanup function to unsubscribe.
|
|
929
1078
|
*/
|
|
930
1079
|
subscribeToAvailableToTrade(params) {
|
|
931
|
-
|
|
1080
|
+
if (params.providerId === undefined) {
|
|
1081
|
+
return this.#trackDefaultRoutedSubscription({
|
|
1082
|
+
open: (provider) => provider.subscribeToAvailableToTrade?.({
|
|
1083
|
+
...params,
|
|
1084
|
+
providerId: this.#defaultProvider,
|
|
1085
|
+
}) ?? (() => undefined),
|
|
1086
|
+
// As with a stopped stream, the old venue's value is now unknown.
|
|
1087
|
+
// Notify before opening the new stream, which may emit immediately.
|
|
1088
|
+
onInvalidate: () => params.onError?.(new Error('Available-to-trade provider changed; value is unknown')),
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
const { providerId } = params;
|
|
932
1092
|
// Read the map directly: #getProviderOrDefault throws when the route is
|
|
933
1093
|
// unavailable, and this method's contract is a no-op cleanup.
|
|
934
1094
|
const provider = this.#providers.get(providerId);
|
|
@@ -1058,14 +1218,19 @@ export class AggregatedPerpsProvider {
|
|
|
1058
1218
|
return WebSocketConnectionState.Disconnected;
|
|
1059
1219
|
}
|
|
1060
1220
|
subscribeToConnectionState(listener) {
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1221
|
+
return this.#trackDefaultRoutedSubscription({
|
|
1222
|
+
open: (provider) => {
|
|
1223
|
+
if (provider.subscribeToConnectionState) {
|
|
1224
|
+
// Providers deliver their current state immediately on subscribe, so
|
|
1225
|
+
// a re-route notifies the listener of the new venue's state.
|
|
1226
|
+
return provider.subscribeToConnectionState(listener);
|
|
1227
|
+
}
|
|
1228
|
+
listener(WebSocketConnectionState.Disconnected, 0);
|
|
1229
|
+
return () => {
|
|
1230
|
+
/* noop */
|
|
1231
|
+
};
|
|
1232
|
+
},
|
|
1233
|
+
});
|
|
1069
1234
|
}
|
|
1070
1235
|
async reconnect() {
|
|
1071
1236
|
const provider = this.#getDefaultProvider();
|