@metamask-previews/network-controller 37.1.1-preview-677dff22f → 38.0.0-preview-a2a3d82

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,10 +16,14 @@ 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 { getRpcFailoverMode } from './selectors.js';
19
+ import { selectRpcFailoverModeFeatureFlag } 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';
23
27
  /**
24
28
  * The type of an RPC endpoint.
25
29
  *
@@ -36,10 +40,6 @@ const BuiltInNetworkClientIdStruct = NetworkClientIdStruct;
36
40
  const CustomNetworkClientIdStruct = NetworkClientIdStruct;
37
41
  const InfuraRpcEndpointUrlStruct = define('infuraRpcEndpointUrl', string().validator);
38
42
  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,10 +63,6 @@ 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())),
70
66
  /**
71
67
  * The optional user-facing nickname of the endpoint.
72
68
  */
@@ -259,7 +255,6 @@ function getDefaultInfuraNetworkConfigurationsByChainId() {
259
255
  nativeCurrency: NetworksTicker[infuraNetworkType],
260
256
  rpcEndpoints: [
261
257
  {
262
- failoverUrls: [],
263
258
  networkClientId: infuraNetworkType,
264
259
  type: RpcEndpointType.Infura,
265
260
  url: rpcEndpointUrl,
@@ -301,7 +296,6 @@ function getCustomNetworkConfiguration(customNetworkType) {
301
296
  nativeCurrency: ticker,
302
297
  rpcEndpoints: [
303
298
  {
304
- failoverUrls: [],
305
299
  networkClientId: customNetworkType,
306
300
  type: RpcEndpointType.Custom,
307
301
  url: rpcEndpointUrl,
@@ -492,14 +486,14 @@ export class NetworkController extends BaseController {
492
486
  #getBlockTrackerOptions;
493
487
  #analyticsOptions;
494
488
  #networkConfigurationsByNetworkClientId;
495
- #rpcFailoverMode = 'disabled';
489
+ #rpcFailoverMode = DEFAULT_RPC_FAILOVER_MODE;
496
490
  /**
497
491
  * Constructs a NetworkController.
498
492
  *
499
493
  * @param options - The options; see {@link NetworkControllerOptions}.
500
494
  */
501
495
  constructor(options) {
502
- const { messenger, state, infuraProjectId, failoverUrls, log, getRpcServiceOptions, getBlockTrackerOptions, analyticsOptions, } = options;
496
+ const { messenger, infuraProjectId, failoverUrls, state, log, getRpcServiceOptions, getBlockTrackerOptions, analyticsOptions, } = options;
503
497
  const initialState = {
504
498
  ...getDefaultNetworkControllerState(),
505
499
  ...state,
@@ -567,8 +561,10 @@ export class NetworkController extends BaseController {
567
561
  this.messenger.subscribe(`${this.name}:rpcEndpointUnavailable`, (payload) => trackRpcServiceUnavailable(this.messenger, this.#analyticsOptions, payload));
568
562
  this.messenger.subscribe(`${this.name}:rpcEndpointDegraded`, (payload) => trackRpcServiceDegraded(this.messenger, this.#analyticsOptions, payload));
569
563
  this.messenger.subscribe('RemoteFeatureFlagController:stateChange', (rpcFailoverMode) => {
570
- this.#updateRpcFailover(rpcFailoverMode);
571
- }, getRpcFailoverMode);
564
+ if (rpcFailoverMode) {
565
+ this.#updateRpcFailover(rpcFailoverMode);
566
+ }
567
+ }, selectRpcFailoverModeFeatureFlag);
572
568
  this.messenger.subscribe('ConfigRegistryController:stateChanged', (caipChainIds) => this.#autoAddNetworksFromConfigRegistry(caipChainIds), selectEvmAutoEnabledNetworksChainIds);
573
569
  }
574
570
  /**
@@ -692,14 +688,20 @@ export class NetworkController extends BaseController {
692
688
  await this.lookupNetwork();
693
689
  }
694
690
  /**
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.
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.
699
698
  */
700
699
  init() {
701
700
  const state = this.messenger.call('RemoteFeatureFlagController:getState');
702
- this.#updateRpcFailover(getRpcFailoverMode(state));
701
+ const rpcFailoverMode = selectRpcFailoverModeFeatureFlag(state);
702
+ if (rpcFailoverMode) {
703
+ this.#updateRpcFailover(rpcFailoverMode);
704
+ }
703
705
  this.#applyNetworkSelection(this.state.selectedNetworkClientId);
704
706
  this.#autoAddNetworksFromConfigRegistry(selectEvmAutoEnabledNetworksChainIds(this.messenger.call('ConfigRegistryController:getState')));
705
707
  }
@@ -1551,7 +1553,7 @@ export class NetworkController extends BaseController {
1551
1553
  return networkClientOperation.type === 'replace';
1552
1554
  })
1553
1555
  .map((networkClientOperation) => networkClientOperation.newRpcEndpoint));
1554
- const defaultFailoverUrls = this.#failoverUrls?.[networkFields.chainId];
1556
+ const failoverUrls = this.#failoverUrls?.[networkFields.chainId] ?? [];
1555
1557
  for (const addedRpcEndpoint of addedRpcEndpoints) {
1556
1558
  if (addedRpcEndpoint.type === RpcEndpointType.Infura) {
1557
1559
  autoManagedNetworkClientRegistry[NetworkClientType.Infura][addedRpcEndpoint.networkClientId] = createAutoManagedNetworkClient({
@@ -1560,7 +1562,7 @@ export class NetworkController extends BaseController {
1560
1562
  type: NetworkClientType.Infura,
1561
1563
  chainId: networkFields.chainId,
1562
1564
  network: addedRpcEndpoint.networkClientId,
1563
- failoverRpcUrls: defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
1565
+ failoverRpcUrls: failoverUrls,
1564
1566
  infuraProjectId: this.#infuraProjectId,
1565
1567
  ticker: networkFields.nativeCurrency,
1566
1568
  },
@@ -1577,7 +1579,7 @@ export class NetworkController extends BaseController {
1577
1579
  networkClientConfiguration: {
1578
1580
  type: NetworkClientType.Custom,
1579
1581
  chainId: networkFields.chainId,
1580
- failoverRpcUrls: defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
1582
+ failoverRpcUrls: failoverUrls,
1581
1583
  rpcUrl: addedRpcEndpoint.url,
1582
1584
  ticker: networkFields.nativeCurrency,
1583
1585
  },
@@ -1668,7 +1670,7 @@ export class NetworkController extends BaseController {
1668
1670
  const chainIds = knownKeysOf(this.state.networkConfigurationsByChainId);
1669
1671
  const networkClientsWithIds = chainIds.flatMap((chainId) => {
1670
1672
  const networkConfiguration = this.state.networkConfigurationsByChainId[chainId];
1671
- const defaultFailoverUrls = this.#failoverUrls?.[chainId];
1673
+ const failoverUrls = this.#failoverUrls?.[chainId] ?? [];
1672
1674
  return networkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
1673
1675
  if (rpcEndpoint.type === RpcEndpointType.Infura) {
1674
1676
  const infuraNetworkName = deriveInfuraNetworkNameFromRpcEndpointUrl(rpcEndpoint.url);
@@ -1679,7 +1681,7 @@ export class NetworkController extends BaseController {
1679
1681
  networkClientConfiguration: {
1680
1682
  type: NetworkClientType.Infura,
1681
1683
  network: infuraNetworkName,
1682
- failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
1684
+ failoverRpcUrls: failoverUrls,
1683
1685
  infuraProjectId: this.#infuraProjectId,
1684
1686
  chainId: networkConfiguration.chainId,
1685
1687
  ticker: networkConfiguration.nativeCurrency,
@@ -1699,7 +1701,7 @@ export class NetworkController extends BaseController {
1699
1701
  networkClientConfiguration: {
1700
1702
  type: NetworkClientType.Custom,
1701
1703
  chainId: networkConfiguration.chainId,
1702
- failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
1704
+ failoverRpcUrls: failoverUrls,
1703
1705
  rpcUrl: rpcEndpoint.url,
1704
1706
  ticker: networkConfiguration.nativeCurrency,
1705
1707
  },