Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/network-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The default `policyOptions.circuitBreakDuration` is now `30` seconds.
- The default `pollingInterval` for the block tracker is now `20` seconds.
- The default `retryTimeout` for the block tracker is now `20` seconds.
- Add `failoverUrls` constructor argument ([#9140](https://github.com/MetaMask/core/pull/9140))
- These will override `failoverUrls` from state during network client creation.

### Changed

Expand Down
21 changes: 17 additions & 4 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,10 @@ export type NetworkControllerOptions = {
* The API key for Infura, used to make requests to Infura.
*/
infuraProjectId: string;
/**
* An optional map of available failover URLs for each chain ID.
*/
failoverUrls?: Record<Hex, string[]>;
/**
* The desired state with which to initialize this controller.
* Missing properties will be filled in with defaults. For instance, if not
Expand Down Expand Up @@ -1256,6 +1260,8 @@ export class NetworkController extends BaseController<

readonly #infuraProjectId: string;

readonly #failoverUrls?: Record<Hex, string[]>;

#previouslySelectedNetworkClientId: string;

#providerProxy: ProviderProxy | undefined;
Expand Down Expand Up @@ -1291,6 +1297,7 @@ export class NetworkController extends BaseController<
messenger,
state,
infuraProjectId,
failoverUrls,
log,
getRpcServiceOptions,
getBlockTrackerOptions,
Expand Down Expand Up @@ -1333,6 +1340,7 @@ export class NetworkController extends BaseController<
});

this.#infuraProjectId = infuraProjectId;
this.#failoverUrls = failoverUrls;
this.#log = log;
this.#getRpcServiceOptions = getRpcServiceOptions;
this.#getBlockTrackerOptions = getBlockTrackerOptions;
Expand Down Expand Up @@ -2844,6 +2852,7 @@ export class NetworkController extends BaseController<
),
);

const defaultFailoverUrls = this.#failoverUrls?.[networkFields.chainId];
for (const addedRpcEndpoint of addedRpcEndpoints) {
if (addedRpcEndpoint.type === RpcEndpointType.Infura) {
autoManagedNetworkClientRegistry[NetworkClientType.Infura][
Expand All @@ -2854,7 +2863,8 @@ export class NetworkController extends BaseController<
type: NetworkClientType.Infura,
chainId: networkFields.chainId,
network: addedRpcEndpoint.networkClientId,
failoverRpcUrls: addedRpcEndpoint.failoverUrls,
failoverRpcUrls:
defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
infuraProjectId: this.#infuraProjectId,
ticker: networkFields.nativeCurrency,
},
Expand All @@ -2872,7 +2882,8 @@ export class NetworkController extends BaseController<
networkClientConfiguration: {
type: NetworkClientType.Custom,
chainId: networkFields.chainId,
failoverRpcUrls: addedRpcEndpoint.failoverUrls,
failoverRpcUrls:
defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
rpcUrl: addedRpcEndpoint.url,
ticker: networkFields.nativeCurrency,
},
Expand Down Expand Up @@ -3023,6 +3034,7 @@ export class NetworkController extends BaseController<
const networkClientsWithIds = chainIds.flatMap((chainId) => {
const networkConfiguration =
this.state.networkConfigurationsByChainId[chainId];
const defaultFailoverUrls = this.#failoverUrls?.[chainId];
return networkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
if (rpcEndpoint.type === RpcEndpointType.Infura) {
const infuraNetworkName = deriveInfuraNetworkNameFromRpcEndpointUrl(
Expand All @@ -3035,7 +3047,8 @@ export class NetworkController extends BaseController<
networkClientConfiguration: {
type: NetworkClientType.Infura,
network: infuraNetworkName,
failoverRpcUrls: rpcEndpoint.failoverUrls,
failoverRpcUrls:
defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
infuraProjectId: this.#infuraProjectId,
chainId: networkConfiguration.chainId,
ticker: networkConfiguration.nativeCurrency,
Expand All @@ -3055,7 +3068,7 @@ export class NetworkController extends BaseController<
networkClientConfiguration: {
type: NetworkClientType.Custom,
chainId: networkConfiguration.chainId,
failoverRpcUrls: rpcEndpoint.failoverUrls,
failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
rpcUrl: rpcEndpoint.url,
ticker: networkConfiguration.nativeCurrency,
},
Expand Down
Loading
Loading