@metamask-previews/network-controller 37.1.1-preview-9dfb538be → 37.1.1-preview-677dff22f

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.
@@ -16,14 +16,10 @@ import { DEPRECATED_NETWORKS, INFURA_BLOCKED_KEY, NetworkStatus, } from './const
16
16
  import { createAutoManagedNetworkClient } from './create-auto-managed-network-client.js';
17
17
  import { projectLogger, createModuleLogger } from './logger.js';
18
18
  import { trackRpcServiceDegraded, trackRpcServiceUnavailable, } from './rpc-service-analytics.js';
19
- import { selectRpcFailoverModeFeatureFlag } from './selectors.js';
19
+ import { getRpcFailoverMode } from './selectors.js';
20
20
  import { NetworkClientType } from './types.js';
21
21
  const debugLog = createModuleLogger(projectLogger, 'NetworkController');
22
22
  const INFURA_URL_REGEX = /^https:\/\/(?<networkName>[^.]+)\.infura\.io\/v\d+\/(?<apiKey>.+)$/u;
23
- /**
24
- * The failover mode that network clients will be initialized with.
25
- */
26
- export const DEFAULT_RPC_FAILOVER_MODE = 'enabled';
27
23
  /**
28
24
  * The type of an RPC endpoint.
29
25
  *
@@ -40,6 +36,10 @@ const BuiltInNetworkClientIdStruct = NetworkClientIdStruct;
40
36
  const CustomNetworkClientIdStruct = NetworkClientIdStruct;
41
37
  const InfuraRpcEndpointUrlStruct = define('infuraRpcEndpointUrl', string().validator);
42
38
  const InfuraRpcEndpointStruct = superstructObject({
39
+ /**
40
+ * Alternate RPC endpoints to use when this endpoint is down.
41
+ */
42
+ failoverUrls: optional(array(string())),
43
43
  /**
44
44
  * The optional user-facing nickname of the endpoint.
45
45
  */
@@ -63,6 +63,10 @@ const InfuraRpcEndpointStruct = superstructObject({
63
63
  url: InfuraRpcEndpointUrlStruct,
64
64
  });
65
65
  const CustomRpcEndpointStruct = superstructObject({
66
+ /**
67
+ * Alternate RPC endpoints to use when this endpoint is down.
68
+ */
69
+ failoverUrls: optional(array(string())),
66
70
  /**
67
71
  * The optional user-facing nickname of the endpoint.
68
72
  */
@@ -255,6 +259,7 @@ function getDefaultInfuraNetworkConfigurationsByChainId() {
255
259
  nativeCurrency: NetworksTicker[infuraNetworkType],
256
260
  rpcEndpoints: [
257
261
  {
262
+ failoverUrls: [],
258
263
  networkClientId: infuraNetworkType,
259
264
  type: RpcEndpointType.Infura,
260
265
  url: rpcEndpointUrl,
@@ -296,6 +301,7 @@ function getCustomNetworkConfiguration(customNetworkType) {
296
301
  nativeCurrency: ticker,
297
302
  rpcEndpoints: [
298
303
  {
304
+ failoverUrls: [],
299
305
  networkClientId: customNetworkType,
300
306
  type: RpcEndpointType.Custom,
301
307
  url: rpcEndpointUrl,
@@ -486,14 +492,14 @@ export class NetworkController extends BaseController {
486
492
  #getBlockTrackerOptions;
487
493
  #analyticsOptions;
488
494
  #networkConfigurationsByNetworkClientId;
489
- #rpcFailoverMode = DEFAULT_RPC_FAILOVER_MODE;
495
+ #rpcFailoverMode = 'disabled';
490
496
  /**
491
497
  * Constructs a NetworkController.
492
498
  *
493
499
  * @param options - The options; see {@link NetworkControllerOptions}.
494
500
  */
495
501
  constructor(options) {
496
- const { messenger, infuraProjectId, failoverUrls, state, log, getRpcServiceOptions, getBlockTrackerOptions, analyticsOptions, } = options;
502
+ const { messenger, state, infuraProjectId, failoverUrls, log, getRpcServiceOptions, getBlockTrackerOptions, analyticsOptions, } = options;
497
503
  const initialState = {
498
504
  ...getDefaultNetworkControllerState(),
499
505
  ...state,
@@ -561,10 +567,8 @@ export class NetworkController extends BaseController {
561
567
  this.messenger.subscribe(`${this.name}:rpcEndpointUnavailable`, (payload) => trackRpcServiceUnavailable(this.messenger, this.#analyticsOptions, payload));
562
568
  this.messenger.subscribe(`${this.name}:rpcEndpointDegraded`, (payload) => trackRpcServiceDegraded(this.messenger, this.#analyticsOptions, payload));
563
569
  this.messenger.subscribe('RemoteFeatureFlagController:stateChange', (rpcFailoverMode) => {
564
- if (rpcFailoverMode) {
565
- this.#updateRpcFailover(rpcFailoverMode);
566
- }
567
- }, selectRpcFailoverModeFeatureFlag);
570
+ this.#updateRpcFailover(rpcFailoverMode);
571
+ }, getRpcFailoverMode);
568
572
  this.messenger.subscribe('ConfigRegistryController:stateChanged', (caipChainIds) => this.#autoAddNetworksFromConfigRegistry(caipChainIds), selectEvmAutoEnabledNetworksChainIds);
569
573
  }
570
574
  /**
@@ -688,20 +692,14 @@ export class NetworkController extends BaseController {
688
692
  await this.lookupNetwork();
689
693
  }
690
694
  /**
691
- * Initialize the NetworkController by:
692
- *
693
- * - applying the RPC failover mode from the `corePlatformRpcFailoverMode`
694
- * feature flag, if present;
695
- * - setting proxies for the globally selected network; and
696
- * - auto-adding networks for any chains that are configured to be
697
- * auto-enabled in ConfigRegistryController.
695
+ * Initialize the NetworkController:
696
+ * - Apply the RPC failover mode from the `corePlatformRpcFailoverMode` remote feature flag;
697
+ * - Apply the network selection.
698
+ * - Auto-add networks for any chains that are configured to be auto-enabled in ConfigRegistryController.
698
699
  */
699
700
  init() {
700
701
  const state = this.messenger.call('RemoteFeatureFlagController:getState');
701
- const rpcFailoverMode = selectRpcFailoverModeFeatureFlag(state);
702
- if (rpcFailoverMode) {
703
- this.#updateRpcFailover(rpcFailoverMode);
704
- }
702
+ this.#updateRpcFailover(getRpcFailoverMode(state));
705
703
  this.#applyNetworkSelection(this.state.selectedNetworkClientId);
706
704
  this.#autoAddNetworksFromConfigRegistry(selectEvmAutoEnabledNetworksChainIds(this.messenger.call('ConfigRegistryController:getState')));
707
705
  }
@@ -1553,7 +1551,7 @@ export class NetworkController extends BaseController {
1553
1551
  return networkClientOperation.type === 'replace';
1554
1552
  })
1555
1553
  .map((networkClientOperation) => networkClientOperation.newRpcEndpoint));
1556
- const failoverUrls = this.#failoverUrls?.[networkFields.chainId] ?? [];
1554
+ const defaultFailoverUrls = this.#failoverUrls?.[networkFields.chainId];
1557
1555
  for (const addedRpcEndpoint of addedRpcEndpoints) {
1558
1556
  if (addedRpcEndpoint.type === RpcEndpointType.Infura) {
1559
1557
  autoManagedNetworkClientRegistry[NetworkClientType.Infura][addedRpcEndpoint.networkClientId] = createAutoManagedNetworkClient({
@@ -1562,7 +1560,7 @@ export class NetworkController extends BaseController {
1562
1560
  type: NetworkClientType.Infura,
1563
1561
  chainId: networkFields.chainId,
1564
1562
  network: addedRpcEndpoint.networkClientId,
1565
- failoverRpcUrls: failoverUrls,
1563
+ failoverRpcUrls: defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
1566
1564
  infuraProjectId: this.#infuraProjectId,
1567
1565
  ticker: networkFields.nativeCurrency,
1568
1566
  },
@@ -1579,7 +1577,7 @@ export class NetworkController extends BaseController {
1579
1577
  networkClientConfiguration: {
1580
1578
  type: NetworkClientType.Custom,
1581
1579
  chainId: networkFields.chainId,
1582
- failoverRpcUrls: failoverUrls,
1580
+ failoverRpcUrls: defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
1583
1581
  rpcUrl: addedRpcEndpoint.url,
1584
1582
  ticker: networkFields.nativeCurrency,
1585
1583
  },
@@ -1670,7 +1668,7 @@ export class NetworkController extends BaseController {
1670
1668
  const chainIds = knownKeysOf(this.state.networkConfigurationsByChainId);
1671
1669
  const networkClientsWithIds = chainIds.flatMap((chainId) => {
1672
1670
  const networkConfiguration = this.state.networkConfigurationsByChainId[chainId];
1673
- const failoverUrls = this.#failoverUrls?.[chainId] ?? [];
1671
+ const defaultFailoverUrls = this.#failoverUrls?.[chainId];
1674
1672
  return networkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
1675
1673
  if (rpcEndpoint.type === RpcEndpointType.Infura) {
1676
1674
  const infuraNetworkName = deriveInfuraNetworkNameFromRpcEndpointUrl(rpcEndpoint.url);
@@ -1681,7 +1679,7 @@ export class NetworkController extends BaseController {
1681
1679
  networkClientConfiguration: {
1682
1680
  type: NetworkClientType.Infura,
1683
1681
  network: infuraNetworkName,
1684
- failoverRpcUrls: failoverUrls,
1682
+ failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
1685
1683
  infuraProjectId: this.#infuraProjectId,
1686
1684
  chainId: networkConfiguration.chainId,
1687
1685
  ticker: networkConfiguration.nativeCurrency,
@@ -1701,7 +1699,7 @@ export class NetworkController extends BaseController {
1701
1699
  networkClientConfiguration: {
1702
1700
  type: NetworkClientType.Custom,
1703
1701
  chainId: networkConfiguration.chainId,
1704
- failoverRpcUrls: failoverUrls,
1702
+ failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
1705
1703
  rpcUrl: rpcEndpoint.url,
1706
1704
  ticker: networkConfiguration.nativeCurrency,
1707
1705
  },