Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@metamask/network-controller

Package Overview
Dependencies
Maintainers
11
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/network-controller - npm Package Compare versions

Comparing version 20.2.0 to 21.0.0

dist/chunk-BV3ZGWII.mjs

56

CHANGELOG.md

@@ -10,2 +10,55 @@ # Changelog

## [21.0.0]
### Added
- **BREAKING:** Add `networkConfigurationsByChainId` to `NetworkState` (type: `Record<Hex, NetworkConfiguration>`) ([#4268](https://github.com/MetaMask/core/pull/4286))
- This property replaces `networkConfigurations`, and, as its name implies, organizes network configurations by chain ID rather than network client ID.
- If no initial state or this property is not included in initial state, the default value of this property will now include configurations for known Infura networks (Mainnet, Goerli, Sepolia, Linea Goerli, Linea Sepolia, and Linea Mainnet) by default.
- Add `getNetworkConfigurationByChainId` method, `NetworkController:getNetworkConfigurationByChainId` messenger action, and `NetworkControllerGetNetworkConfigurationByNetworkClientId` type ([#4268](https://github.com/MetaMask/core/pull/4286))
- Add `addNetwork`, which replaces one half of `upsertNetworkConfiguration` and can be used to add new network clients for a chain ([#4268](https://github.com/MetaMask/core/pull/4286))
- It's worth noting that this method now publishes a `NetworkController:networkAdded` event instead of calling a `trackMetaMetricsEvent` callback. It is expected that you will subscribe to this event and create a MetaMetrics event yourself.
- Add `updateNetwork`, which replaces one half of `upsertNetworkConfiguration` and can be used to recreate the network clients for an existing chain based on an updated configuration ([#4268](https://github.com/MetaMask/core/pull/4286))
- Note that it is not possible to remove the RPC endpoint from a network configuration that is currently represented by the globally selected network client. To prevent an error, you'll need to detect when such a removal is occurring and pass the `replacementSelectedRpcEndpointIndex` to `updateNetwork`. It will then switch to the designated RPC endpoint's network client on your behalf.
- Add `removeNetwork`, which replaces `removeNetworkConfiguration` and can be used to remove existing network clients for a chain ([#4268](https://github.com/MetaMask/core/pull/4286))
- Add `getDefaultNetworkControllerState` function, which replaces `defaultState` and matches patterns in other controllers ([#4268](https://github.com/MetaMask/core/pull/4286))
- Add `RpcEndpointType`, `AddNetworkFields`, and `UpdateNetworkFields` types ([#4268](https://github.com/MetaMask/core/pull/4286))
- Add `getNetworkConfigurations`, `getAvailableNetworkClientIds` and `selectAvailableNetworkClientIds` selectors ([#4268](https://github.com/MetaMask/core/pull/4286))
- These new selectors can be applied to messenger event subscriptions
### Changed
- **BREAKING:** Replace `NetworkConfiguration` type with a new definition ([#4268](https://github.com/MetaMask/core/pull/4286))
- A network configuration no longer represents a single RPC endpoint but rather a collection of RPC endpoints that can all be used to interface with a single chain.
- The only property that has brought over to this type unchanged is `chainId`.
- `ticker` has been renamed to `nativeCurrency`.
- `nickname` has been renamed to `name`.
- `rpcEndpoints` has been added. This is an an array of objects, where each object has properties `name` (optional), `networkClientId` (optional), `type`, and `url`.
- `defaultRpcEndpointIndex` has been added. This must point to an entry in `rpcEndpoints`.
- The block explorer URL is no longer located in `rpcPrefs` and is no longer restricted to one: `blockExplorerUrls` has been added along with a corresponding property `defaultBlockExplorerUrlIndex`, which must point to an entry in `blockExplorerUrls`.
- `id` has been removed. Previously, this represented the ID of the network client associated with the network configuration. Since network clients are now created from RPC endpoints, the equivalent to this is the `networkClientId` property on an `RpcEndpoint`.
- **BREAKING:** The network controller messenger must now allow the action `NetworkController:getNetworkConfigurationByChainId` ([#4268](https://github.com/MetaMask/core/pull/4286))
- **BREAKING:** The network controller messenger must now allow the event `NetworkController:networkAdded` ([#4268](https://github.com/MetaMask/core/pull/4286))
- **BREAKING:** The `NetworkController` constructor will now throw if the initial state provided is invalid ([#4268](https://github.com/MetaMask/core/pull/4286))
- `networkConfigurationsByChainId` cannot be empty.
- The `chainId` of a network configuration in `networkConfigurationsByChainId` must match the chain ID it is filed under.
- The `defaultRpcEndpointIndex` of a network configuration in `networkConfigurationsByChainId` must point to an entry in its `rpcEndpoints`.
- The `defaultBlockExplorerUrlIndex` of a network configuration in `networkConfigurationsByChainId` must point to an entry in its `blockExplorerUrls`.
- `selectedNetworkClientId` must match the `networkClientId` of an RPC endpoint in `networkConfigurationsByChainId`.
- **BREAKING:** Update `getNetworkConfigurationByNetworkClientId` so that when given an Infura network name (that is, a value from `InfuraNetworkType`), it will return a masked version of the RPC endpoint URL for the associated Infura network ([#4268](https://github.com/MetaMask/core/pull/4286))
- If you want the unmasked version, you'll need the `url` property from the network _client_ configuration, which you can get by calling `getNetworkClientById` and then accessing the `configuration` property off of the network client.
- **BREAKING:** Update `loadBackup` to take and update `networkConfigurationsByChainId` instead of `networkConfigurations` ([#4268](https://github.com/MetaMask/core/pull/4286))
- Bump `@metamask/base-controller` from `^6.0.2` to `^7.0.0` ([#4625](https://github.com/MetaMask/core/pull/4625), [#4643](https://github.com/MetaMask/core/pull/4643))
- Bump `@metamask/controller-utils` from `^11.0.2` to `^11.2.0` ([#4639](https://github.com/MetaMask/core/pull/4639), [#4651](https://github.com/MetaMask/core/pull/4651))
- Bump `@metamask/eth-block-tracker` from `^9.0.3` to `^10.0.0` ([#4424](https://github.com/MetaMask/core/pull/4424))
- Bump `@metamask/eth-json-rpc-middleware` from `^12.1.1` to `^13.0.0` ([#4424](https://github.com/MetaMask/core/pull/4424))
### Removed
- **BREAKING:** Remove `networkConfigurations` from `NetworkState`, which has been replaced with `networkConfigurationsByChainId` ([#4268](https://github.com/MetaMask/core/pull/4286))
- **BREAKING:** Remove `upsertNetworkConfiguration` and `removeNetworkConfiguration`, which have been replaced with `addNetwork`, `updateNetwork`, and `removeNetwork` ([#4268](https://github.com/MetaMask/core/pull/4286))
- **BREAKING:** Remove `defaultState` variable, which has been replaced with a `getDefaultNetworkControllerState` function ([#4268](https://github.com/MetaMask/core/pull/4286))
- **BREAKING:** Remove `trackMetaMetricsEvent` option from the NetworkController constructor ([#4268](https://github.com/MetaMask/core/pull/4286))
- Previously, this was used in `upsertNetworkConfiguration` to create a MetaMetrics event when a new network was added. This can now be achieved by subscribing to the `NetworkController:networkAdded` event and creating the event inside of the event handler.
## [20.2.0]

@@ -556,3 +609,4 @@

[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/network-controller@20.2.0...HEAD
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/network-controller@21.0.0...HEAD
[21.0.0]: https://github.com/MetaMask/core/compare/@metamask/network-controller@20.2.0...@metamask/network-controller@21.0.0
[20.2.0]: https://github.com/MetaMask/core/compare/@metamask/network-controller@20.1.0...@metamask/network-controller@20.2.0

@@ -559,0 +613,0 @@ [20.1.0]: https://github.com/MetaMask/core/compare/@metamask/network-controller@20.0.0...@metamask/network-controller@20.1.0

8

dist/index.js

@@ -5,6 +5,8 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true});

var _chunkBEL2VMHNjs = require('./chunk-BEL2VMHN.js');
var _chunkYOHMQPGMjs = require('./chunk-YOHMQPGM.js');
var _chunkZKNI7MD3js = require('./chunk-ZKNI7MD3.js');

@@ -25,3 +27,5 @@ require('./chunk-LLMZDA4Q.js');

exports.INFURA_BLOCKED_KEY = _chunkZKNI7MD3js.INFURA_BLOCKED_KEY; exports.NetworkClientType = _chunkKHZTS7TFjs.NetworkClientType; exports.NetworkController = _chunkBEL2VMHNjs.NetworkController; exports.NetworkStatus = _chunkZKNI7MD3js.NetworkStatus; exports.defaultState = _chunkBEL2VMHNjs.defaultState; exports.knownKeysOf = _chunkBEL2VMHNjs.knownKeysOf;
exports.INFURA_BLOCKED_KEY = _chunkZKNI7MD3js.INFURA_BLOCKED_KEY; exports.NetworkClientType = _chunkKHZTS7TFjs.NetworkClientType; exports.NetworkController = _chunkYOHMQPGMjs.NetworkController; exports.NetworkStatus = _chunkZKNI7MD3js.NetworkStatus; exports.RpcEndpointType = _chunkYOHMQPGMjs.RpcEndpointType; exports.getDefaultNetworkControllerState = _chunkYOHMQPGMjs.getDefaultNetworkControllerState; exports.knownKeysOf = _chunkYOHMQPGMjs.knownKeysOf; exports.selectAvailableNetworkClientIds = _chunkYOHMQPGMjs.selectAvailableNetworkClientIds;
//# sourceMappingURL=index.js.map

@@ -5,3 +5,7 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true});

var _chunkBEL2VMHNjs = require('./chunk-BEL2VMHN.js');
var _chunkYOHMQPGMjs = require('./chunk-YOHMQPGM.js');
require('./chunk-ZKNI7MD3.js');

@@ -17,3 +21,7 @@ require('./chunk-LLMZDA4Q.js');

exports.NetworkController = _chunkBEL2VMHNjs.NetworkController; exports.defaultState = _chunkBEL2VMHNjs.defaultState; exports.knownKeysOf = _chunkBEL2VMHNjs.knownKeysOf;
exports.NetworkController = _chunkYOHMQPGMjs.NetworkController; exports.RpcEndpointType = _chunkYOHMQPGMjs.RpcEndpointType; exports.getAvailableNetworkClientIds = _chunkYOHMQPGMjs.getAvailableNetworkClientIds; exports.getDefaultNetworkControllerState = _chunkYOHMQPGMjs.getDefaultNetworkControllerState; exports.getNetworkConfigurations = _chunkYOHMQPGMjs.getNetworkConfigurations; exports.knownKeysOf = _chunkYOHMQPGMjs.knownKeysOf; exports.selectAvailableNetworkClientIds = _chunkYOHMQPGMjs.selectAvailableNetworkClientIds;
//# sourceMappingURL=NetworkController.js.map
export type { AutoManagedNetworkClient } from './create-auto-managed-network-client';
export * from './NetworkController';
export type { Block, NetworkMetadata, NetworkConfiguration, BuiltInNetworkClientId, CustomNetworkClientId, NetworkClientId, NetworksMetadata, NetworkState, BlockTrackerProxy, ProviderProxy, AddNetworkFields, UpdateNetworkFields, NetworkControllerStateChangeEvent, NetworkControllerNetworkWillChangeEvent, NetworkControllerNetworkDidChangeEvent, NetworkControllerInfuraIsBlockedEvent, NetworkControllerInfuraIsUnblockedEvent, NetworkControllerEvents, NetworkControllerGetStateAction, NetworkControllerGetEthQueryAction, NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetSelectedNetworkClientAction, NetworkControllerGetEIP1559CompatibilityAction, NetworkControllerFindNetworkClientIdByChainIdAction, NetworkControllerSetProviderTypeAction, NetworkControllerSetActiveNetworkAction, NetworkControllerGetNetworkConfigurationByNetworkClientId, NetworkControllerActions, NetworkControllerMessenger, NetworkControllerOptions, } from './NetworkController';
export { getDefaultNetworkControllerState, selectAvailableNetworkClientIds, knownKeysOf, NetworkController, RpcEndpointType, } from './NetworkController';
export * from './constants';

@@ -4,0 +5,0 @@ export type { BlockTracker, Provider } from './types';

import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedControllerMessenger } from '@metamask/base-controller';
import { BaseController } from '@metamask/base-controller';
import type { Partialize } from '@metamask/controller-utils';
import { InfuraNetworkType } from '@metamask/controller-utils';

@@ -7,5 +8,7 @@ import EthQuery from '@metamask/eth-query';

import type { Hex } from '@metamask/utils';
import type { Draft } from 'immer';
import type { Logger } from 'loglevel';
import { NetworkStatus } from './constants';
import type { AutoManagedNetworkClient, ProxyWithAccessibleTarget } from './create-auto-managed-network-client';
import { NetworkClientType } from './types';
import type { BlockTracker, Provider, CustomNetworkClientConfiguration, InfuraNetworkClientConfiguration } from './types';

@@ -31,26 +34,170 @@ export type Block = {

/**
* Custom RPC network information
* The type of an RPC endpoint.
*
* @property rpcUrl - RPC target URL.
* @property chainId - Network ID as per EIP-155
* @property nickname - Personalized network name.
* @property ticker - Currency ticker.
* @property rpcPrefs - Personalized preferences.
* @see {@link CustomRpcEndpoint}
* @see {@link InfuraRpcEndpoint}
*/
export declare enum RpcEndpointType {
Custom = "custom",
Infura = "infura"
}
/**
* An Infura RPC endpoint is a reference to a specific network that Infura
* supports as well as an Infura account we own that we allow users to make use
* of for free. We need to disambiguate these endpoints from custom RPC
* endpoints, because while the types for these kinds of object both have the
* same interface, the URL for an Infura endpoint contains the Infura project
* ID, and we don't want this to be present in state. We therefore hide it by
* representing it in the URL as `{infuraProjectId}`, which we replace this when
* create network clients. But we need to know somehow that we only need to do
* this replacement for Infura endpoints and not custom endpoints — hence the
* separate type.
*/
export type InfuraRpcEndpoint = {
/**
* The optional user-facing nickname of the endpoint.
*/
name?: string;
/**
* The identifier for the network client that has been created for this RPC
* endpoint. This is also used to uniquely identify the RPC endpoint in a
* set of RPC endpoints as well: once assigned, it is used to determine
* whether the `name`, `type`, or `url` of the RPC endpoint has changed.
*/
networkClientId: BuiltInNetworkClientId;
/**
* The type of this endpoint, always "default".
*/
type: RpcEndpointType.Infura;
/**
* The URL of the endpoint. Expected to be a template with the string
* `{infuraProjectId}`, which will get replaced with the Infura project ID
* when the network client is created.
*/
url: `https://${InfuraNetworkType}.infura.io/v3/{infuraProjectId}`;
};
/**
* A custom RPC endpoint is a reference to a user-defined server which fronts an
* EVM chain. It may refer to an Infura network, but only by coincidence.
*/
export type CustomRpcEndpoint = {
/**
* The optional user-facing nickname of the endpoint.
*/
name?: string;
/**
* The identifier for the network client that has been created for this RPC
* endpoint. This is also used to uniquely identify the RPC endpoint in a
* set of RPC endpoints as well: once assigned, it is used to determine
* whether the `name`, `type`, or `url` of the RPC endpoint has changed.
*/
networkClientId: CustomNetworkClientId;
/**
* The type of this endpoint, always "custom".
*/
type: RpcEndpointType.Custom;
/**
* The URL of the endpoint.
*/
url: string;
};
/**
* An RPC endpoint is a reference to a server which fronts an EVM chain. There
* are two varieties of RPC endpoints: Infura and custom.
*
* @see {@link CustomRpcEndpoint}
* @see {@link InfuraRpcEndpoint}
*/
export type RpcEndpoint = InfuraRpcEndpoint | CustomRpcEndpoint;
/**
* From a user perspective, a network configuration holds information about a
* network that a user can select through the client. A "network" in this sense
* can explicitly refer to an EVM chain that the user explicitly adds or doesn't
* need to add (because it comes shipped with the client). The properties here
* therefore directly map to fields that a user sees and can edit for a network
* within the client.
*
* Internally, a network configuration represents a single conceptual EVM chain,
* which is represented tangibly via multiple RPC endpoints. A "network" is then
* something for which a network client object is created automatically or
* created on demand when it is added to the client.
*/
export type NetworkConfiguration = {
rpcUrl: string;
/**
* A set of URLs that allows the user to view activity that has occurred on
* the chain.
*/
blockExplorerUrls: string[];
/**
* The ID of the chain. Represented in hexadecimal format with a leading "0x"
* instead of decimal format so that when viewed out of context it can be
* unambiguously interpreted.
*/
chainId: Hex;
ticker: string;
nickname?: string;
rpcPrefs?: {
blockExplorerUrl: string;
};
/**
* A reference to a URL that the client will use by default to allow the user
* to view activity that has occurred on the chain. This index must refer to
* an item in `blockExplorerUrls`.
*/
defaultBlockExplorerUrlIndex?: number;
/**
* A reference to an RPC endpoint that all requests will use by default in order to
* interact with the chain. This index must refer to an item in
* `rpcEndpoints`.
*/
defaultRpcEndpointIndex: number;
/**
* The user-facing nickname assigned to the chain.
*/
name: string;
/**
* The name of the currency to use for the chain.
*/
nativeCurrency: string;
/**
* The collection of possible RPC endpoints that the client can use to
* interact with the chain.
*/
rpcEndpoints: RpcEndpoint[];
};
/**
* The collection of network configurations in state.
* A custom RPC endpoint in a new network configuration, meant to be used in
* conjunction with `AddNetworkFields`.
*
* Custom RPC endpoints do not need a `networkClientId` property because it is
* assumed that they have not already been added and therefore network clients
* do not exist for them yet (and hence IDs need to be generated).
*/
type NetworkConfigurations = Record<NetworkConfigurationId, NetworkConfiguration & {
id: NetworkConfigurationId;
}>;
export type AddNetworkCustomRpcEndpointFields = Omit<CustomRpcEndpoint, 'networkClientId'>;
/**
* A new network configuration that `addNetwork` takes.
*
* Custom RPC endpoints do not need a `networkClientId` property because it is
* assumed that they have not already been added and are not represented by
* network clients yet.
*/
export type AddNetworkFields = Omit<NetworkConfiguration, 'rpcEndpoints'> & {
rpcEndpoints: (InfuraRpcEndpoint | AddNetworkCustomRpcEndpointFields)[];
};
/**
* A custom RPC endpoint in an updated representation of a network
* configuration, meant to be used in conjunction with `UpdateNetworkFields`.
*
* Custom RPC endpoints do not need a `networkClientId` property because it is
* assumed that they have not already been added and therefore network clients
* do not exist for them yet (and hence IDs need to be generated).
*/
export type UpdateNetworkCustomRpcEndpointFields = Partialize<CustomRpcEndpoint, 'networkClientId'>;
/**
* An updated representation of an existing network configuration that
* `updateNetwork` takes.
*
* Custom RPC endpoints may or may not have a `networkClientId` property; if
* they do, then it is assumed that they already exist, and if not, then it is
* assumed that they are new and are not represented by network clients yet.
*/
export type UpdateNetworkFields = Omit<NetworkConfiguration, 'rpcEndpoints'> & {
rpcEndpoints: (InfuraRpcEndpoint | UpdateNetworkCustomRpcEndpointFields)[];
};
/**
* `Object.keys()` is intentionally generic: it returns the keys of an object,

@@ -82,20 +229,29 @@ * but it cannot make guarantees about the contents of that object, so the type

/**
* Information about networks not held by any other part of state.
* Extra information about each network, such as whether it is accessible or
* blocked and whether it supports EIP-1559, keyed by network client ID.
*/
export type NetworksMetadata = {
[networkClientId: NetworkClientId]: NetworkMetadata;
};
export type NetworksMetadata = Record<NetworkClientId, NetworkMetadata>;
/**
* @type NetworkState
*
* Network controller state
* @property properties - an additional set of network properties for the currently connected network
* @property networkConfigurations - the full list of configured networks either preloaded or added by the user.
* The state that NetworkController stores.
*/
export type NetworkState = {
/**
* The ID of the network client that the proxies returned by
* `getSelectedNetworkClient` currently point to.
*/
selectedNetworkClientId: NetworkClientId;
networkConfigurations: NetworkConfigurations;
/**
* The registry of networks and corresponding RPC endpoints that the
* controller can use to make requests for various chains.
*
* @see {@link NetworkConfiguration}
*/
networkConfigurationsByChainId: Record<Hex, NetworkConfiguration>;
/**
* Extra information about each network, such as whether it is accessible or
* blocked and whether it supports EIP-1559, keyed by network client ID.
*/
networksMetadata: NetworksMetadata;
};
declare const name = "NetworkController";
declare const controllerName = "NetworkController";
/**

@@ -117,3 +273,3 @@ * Represents the block tracker for the currently selected network. (Note that

export type ProviderProxy = SwappableProxy<ProxyWithAccessibleTarget<Provider>>;
export type NetworkControllerStateChangeEvent = ControllerStateChangeEvent<typeof name, NetworkState>;
export type NetworkControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, NetworkState>;
/**

@@ -154,4 +310,12 @@ * `networkWillChange` is published when the current network is about to be

};
export type NetworkControllerEvents = NetworkControllerStateChangeEvent | NetworkControllerNetworkWillChangeEvent | NetworkControllerNetworkDidChangeEvent | NetworkControllerInfuraIsBlockedEvent | NetworkControllerInfuraIsUnblockedEvent;
export type NetworkControllerGetStateAction = ControllerGetStateAction<typeof name, NetworkState>;
/**
* `networkAdded` is published after a network configuration is added to the
* network configuration registry and network clients are created for it.
*/
export type NetworkControllerNetworkAddedEvent = {
type: 'NetworkController:networkAdded';
payload: [networkConfiguration: NetworkConfiguration];
};
export type NetworkControllerEvents = NetworkControllerStateChangeEvent | NetworkControllerNetworkWillChangeEvent | NetworkControllerNetworkDidChangeEvent | NetworkControllerInfuraIsBlockedEvent | NetworkControllerInfuraIsUnblockedEvent | NetworkControllerNetworkAddedEvent;
export type NetworkControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NetworkState>;
export type NetworkControllerGetEthQueryAction = {

@@ -191,2 +355,6 @@ type: `NetworkController:getEthQuery`;

};
export type NetworkControllerGetNetworkConfigurationByChainId = {
type: `NetworkController:getNetworkConfigurationByChainId`;
handler: NetworkController['getNetworkConfigurationByChainId'];
};
export type NetworkControllerGetNetworkConfigurationByNetworkClientId = {

@@ -196,7 +364,6 @@ type: `NetworkController:getNetworkConfigurationByNetworkClientId`;

};
export type NetworkControllerActions = NetworkControllerGetStateAction | NetworkControllerGetEthQueryAction | NetworkControllerGetNetworkClientByIdAction | NetworkControllerGetSelectedNetworkClientAction | NetworkControllerGetEIP1559CompatibilityAction | NetworkControllerFindNetworkClientIdByChainIdAction | NetworkControllerSetActiveNetworkAction | NetworkControllerSetProviderTypeAction | NetworkControllerGetNetworkConfigurationByNetworkClientId;
export type NetworkControllerMessenger = RestrictedControllerMessenger<typeof name, NetworkControllerActions, NetworkControllerEvents, never, never>;
export type NetworkControllerActions = NetworkControllerGetStateAction | NetworkControllerGetEthQueryAction | NetworkControllerGetNetworkClientByIdAction | NetworkControllerGetSelectedNetworkClientAction | NetworkControllerGetEIP1559CompatibilityAction | NetworkControllerFindNetworkClientIdByChainIdAction | NetworkControllerSetActiveNetworkAction | NetworkControllerSetProviderTypeAction | NetworkControllerGetNetworkConfigurationByChainId | NetworkControllerGetNetworkConfigurationByNetworkClientId;
export type NetworkControllerMessenger = RestrictedControllerMessenger<typeof controllerName, NetworkControllerActions, NetworkControllerEvents, never, never>;
export type NetworkControllerOptions = {
messenger: NetworkControllerMessenger;
trackMetaMetricsEvent: () => void;
infuraProjectId: string;

@@ -206,18 +373,66 @@ state?: Partial<NetworkState>;

};
export declare const defaultState: NetworkState;
type NetworkConfigurationId = string;
/**
* Constructs properties for the NetworkController state whose values will be
* used if not provided to the constructor.
*
* @returns The default NetworkController state.
*/
export declare function getDefaultNetworkControllerState(): NetworkState;
/**
* Get a list of all network configurations.
*
* @param state - NetworkController state
* @returns A list of all available network configurations
*/
export declare function getNetworkConfigurations(state: NetworkState): NetworkConfiguration[];
/**
* Get a list of all available client IDs from a list of
* network configurations
* @param networkConfigurations - The array of network configurations
* @returns A list of all available client IDs
*/
export declare function getAvailableNetworkClientIds(networkConfigurations: NetworkConfiguration[]): string[];
export declare const selectAvailableNetworkClientIds: ((state: NetworkState) => string[]) & {
clearCache: () => void;
resultsCount: () => number;
resetResultsCount: () => void;
} & {
resultFunc: (resultFuncArgs_0: NetworkConfiguration[]) => string[];
memoizedResultFunc: ((resultFuncArgs_0: NetworkConfiguration[]) => string[]) & {
clearCache: () => void;
resultsCount: () => number;
resetResultsCount: () => void;
};
lastResult: () => string[];
dependencies: [typeof getNetworkConfigurations];
recomputations: () => number;
resetRecomputations: () => void;
dependencyRecomputations: () => number;
resetDependencyRecomputations: () => void;
} & {
argsMemoize: typeof import("reselect").weakMapMemoize;
memoize: typeof import("reselect").weakMapMemoize;
};
/**
* The collection of auto-managed network clients that map to Infura networks.
*/
type AutoManagedBuiltInNetworkClientRegistry = Record<BuiltInNetworkClientId, AutoManagedNetworkClient<InfuraNetworkClientConfiguration>>;
export type AutoManagedBuiltInNetworkClientRegistry = Record<BuiltInNetworkClientId, AutoManagedNetworkClient<InfuraNetworkClientConfiguration>>;
/**
* The collection of auto-managed network clients that map to Infura networks.
*/
type AutoManagedCustomNetworkClientRegistry = Record<CustomNetworkClientId, AutoManagedNetworkClient<CustomNetworkClientConfiguration>>;
export type AutoManagedCustomNetworkClientRegistry = Record<CustomNetworkClientId, AutoManagedNetworkClient<CustomNetworkClientConfiguration>>;
/**
* The collection of auto-managed network clients that map to Infura networks
* as well as custom networks that users have added.
*/
export type AutoManagedNetworkClientRegistry = {
[NetworkClientType.Infura]: AutoManagedBuiltInNetworkClientRegistry;
[NetworkClientType.Custom]: AutoManagedCustomNetworkClientRegistry;
};
/**
* Controller that creates and manages an Ethereum network provider.
*/
export declare class NetworkController extends BaseController<typeof name, NetworkState, NetworkControllerMessenger> {
export declare class NetworkController extends BaseController<typeof controllerName, NetworkState, NetworkControllerMessenger> {
#private;
constructor({ messenger, state, infuraProjectId, trackMetaMetricsEvent, log, }: NetworkControllerOptions);
constructor({ messenger, state, infuraProjectId, log, }: NetworkControllerOptions);
/**

@@ -242,8 +457,9 @@ * Accesses the provider and block tracker for the currently selected network.

/**
* Returns all of the network clients that have been created so far, keyed by
* their identifier in the network client registry. This collection represents
* not only built-in networks but also any custom networks that consumers have
* added.
* Internally, the Infura and custom network clients are categorized by type
* so that when accessing either kind of network client, TypeScript knows
* which type to assign to the network client. For some cases it's more useful
* to be able to access network clients by ID instead of by type and then ID,
* so this function makes that possible.
*
* @returns The list of known network clients.
* @returns The network clients registered so far, keyed by ID.
*/

@@ -268,4 +484,5 @@ getNetworkClientRegistry(): AutoManagedBuiltInNetworkClientRegistry & AutoManagedCustomNetworkClientRegistry;

/**
* Creates network clients for built-in and custom networks, then establishes
* the currently selected network client based on state.
* Ensures that network clients for Infura and custom RPC endpoints have been
* created. Then, consulting state, initializes and establishes the currently
* selected network client.
*/

@@ -307,9 +524,12 @@ initializeProvider(): Promise<void>;

*
* @param networkClientId - The ID of a network client that requests will be
* routed through (either the name of an Infura network or the ID of a custom
* network configuration).
* @param networkClientId - The ID of a network client that will be used to
* make requests.
* @param options - Options for this method.
* @param options.updateState - Allows for updating state.
* @throws if no network client is associated with the given
* `networkClientId`.
* network client ID.
*/
setActiveNetwork(networkClientId: string): Promise<void>;
setActiveNetwork(networkClientId: string, options?: {
updateState?: (state: Draft<NetworkState>) => void;
}): Promise<void>;
/**

@@ -332,54 +552,66 @@ * Determines whether the network supports EIP-1559 by checking whether the

/**
* Returns a configuration object for the network identified by the given
* network client ID. If given an Infura network type, constructs one based on
* what we know about the network; otherwise attempts locates a network
* configuration in state that corresponds to the network client ID.
* Returns the network configuration that has been filed under the given chain
* ID.
*
* @param networkClientId - The network client ID.
* @returns The configuration for the referenced network if one exists, or
* undefined otherwise.
* @param chainId - The chain ID to use as a key.
* @returns The network configuration if one exists, or undefined.
*/
getNetworkConfigurationByChainId(chainId: Hex): NetworkConfiguration | undefined;
/**
* Returns the network configuration that contains an RPC endpoint with the
* given network client ID.
*
* @param networkClientId - The network client ID to use as a key.
* @returns The network configuration if one exists, or undefined.
*/
getNetworkConfigurationByNetworkClientId(networkClientId: NetworkClientId): NetworkConfiguration | undefined;
/**
* Adds a new custom network or updates the information for an existing
* network.
* Creates and registers network clients for the collection of Infura and
* custom RPC endpoints that can be used to make requests for a particular
* chain, storing the given configuration object in state for later reference.
*
* This may involve updating the `networkConfigurations` property in
* state as well and/or adding a new network client to the network client
* registry. The `rpcUrl` and `chainId` of the given object are used to
* determine which action to take:
* @param fields - The object that describes the new network/chain and lists
* the RPC endpoints which front that chain.
* @returns The newly added network configuration.
* @throws if any part of `fields` would produce invalid state.
* @see {@link NetworkConfiguration}
*/
addNetwork(fields: AddNetworkFields): NetworkConfiguration;
/**
* Updates the configuration for a previously stored network filed under the
* given chain ID, creating + registering new network clients to represent RPC
* endpoints that have been added and destroying + unregistering existing
* network clients for RPC endpoints that have been removed.
*
* - If the `rpcUrl` corresponds to an existing network configuration
* (case-insensitively), then it is overwritten with the object. Furthermore,
* if the `chainId` is different from the existing network configuration, then
* the existing network client is replaced with a new one.
* - If the `rpcUrl` does not correspond to an existing network configuration
* (case-insensitively), then the object is used to add a new network
* configuration along with a new network client.
* Note that if `chainId` is changed, then all network clients associated with
* that chain will be removed and re-added, even if none of the RPC endpoints
* have changed.
*
* @param networkConfiguration - The network configuration to add or update.
* @param options - Additional configuration options.
* @param options.referrer - Used to create a metrics event; the site from which the call originated, or 'metamask' for internal calls.
* @param options.source - Used to create a metrics event; where the event originated (i.e. from a dapp or from the network form).
* @param options.setActive - If true, switches to the network upon adding or updating it (default: false).
* @returns The ID for the added or updated network configuration.
* @param chainId - The chain ID associated with an existing network.
* @param fields - The object that describes the updates to the network/chain,
* including the new set of RPC endpoints which should front that chain.
* @param options - Options to provide.
* @param options.replacementSelectedRpcEndpointIndex - Usually you cannot
* remove an RPC endpoint that is being represented by the currently selected
* network client. This option allows you to specify another RPC endpoint
* (either an existing one or a new one) that should be used to select a new
* network instead.
* @returns The updated network configuration.
* @throws if `chainId` does not refer to an existing network configuration,
* if any part of `fields` would produce invalid state, etc.
* @see {@link NetworkConfiguration}
*/
upsertNetworkConfiguration(networkConfiguration: NetworkConfiguration & {
id?: NetworkConfigurationId;
}, { referrer, source, setActive, }: {
referrer: string;
source: string;
setActive?: boolean;
}): Promise<string>;
updateNetwork(chainId: Hex, fields: UpdateNetworkFields, { replacementSelectedRpcEndpointIndex, }?: {
replacementSelectedRpcEndpointIndex?: number;
}): Promise<NetworkConfiguration>;
/**
* Removes a custom network from state.
* Destroys and unregisters the network identified by the given chain ID, also
* removing the associated network configuration from state.
*
* This involves updating the `networkConfigurations` property in state as
* well and removing the network client that corresponds to the network from
* the client registry.
*
* @param networkConfigurationId - The ID of an existing network
* configuration.
* @param chainId - The chain ID associated with an existing network.
* @throws if `chainId` does not refer to an existing network configuration,
* or if the currently selected network is being removed.
* @see {@link NetworkConfiguration}
*/
removeNetworkConfiguration(networkConfigurationId: string): void;
removeNetwork(chainId: Hex): void;
/**

@@ -400,10 +632,9 @@ * Assuming that the network has been previously switched, switches to this

/**
* Updates the controller using the given backup data.
* Merges the given backup data into controller state.
*
* @param backup - The data that has been backed up.
* @param backup.networkConfigurations - Network configurations in the backup.
* @param backup.networkConfigurationsByChainId - Network configurations,
* keyed by chain ID.
*/
loadBackup({ networkConfigurations, }: {
networkConfigurations: NetworkState['networkConfigurations'];
}): void;
loadBackup({ networkConfigurationsByChainId, }: Pick<NetworkState, 'networkConfigurationsByChainId'>): void;
/**

@@ -410,0 +641,0 @@ * Searches for a network configuration ID with the given ChainID and returns it.

{
"name": "@metamask/network-controller",
"version": "20.2.0",
"version": "21.0.0",
"description": "Provides an interface to the currently selected network via a MetaMask-compatible provider object",

@@ -38,2 +38,3 @@ "keywords": [

"publish:preview": "yarn npm publish --tag preview",
"since-latest-release": "../../scripts/since-latest-release.sh",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",

@@ -45,7 +46,7 @@ "test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",

"dependencies": {
"@metamask/base-controller": "^6.0.2",
"@metamask/controller-utils": "^11.0.2",
"@metamask/eth-block-tracker": "^9.0.3",
"@metamask/base-controller": "^7.0.0",
"@metamask/controller-utils": "^11.2.0",
"@metamask/eth-block-tracker": "^10.0.0",
"@metamask/eth-json-rpc-infura": "^9.1.0",
"@metamask/eth-json-rpc-middleware": "^12.1.1",
"@metamask/eth-json-rpc-middleware": "^13.0.0",
"@metamask/eth-json-rpc-provider": "^4.1.3",

@@ -60,2 +61,4 @@ "@metamask/eth-query": "^4.0.0",

"loglevel": "^1.8.1",
"reselect": "^5.1.1",
"uri-js": "^4.4.1",
"uuid": "^8.3.2"

@@ -62,0 +65,0 @@ },

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc