@metamask/network-controller 23.0.0 → 23.2.0

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 CHANGED
@@ -7,6 +7,39 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [23.2.0]
11
+
12
+ ### Added
13
+
14
+ - Add optional `additionalDefaultNetworks` option to `NetworkController` constructor ([#5527](https://github.com/MetaMask/core/pull/5527))
15
+ - This can be used to customize which custom networks the default `networkConfigurationsByChainId` includes.
16
+ - Add `getSelectedChainId` method to `NetworkController` ([#5516](https://github.com/MetaMask/core/pull/5516))
17
+ - This is also callable via the messenger.
18
+ - Add `DEPRECATED_NETWORKS` constant ([#5560](https://github.com/MetaMask/core/pull/5560))
19
+
20
+ ### Changed
21
+
22
+ - Remove Goerli and Linea Goerli from set of default networks ([#5560](https://github.com/MetaMask/core/pull/5560))
23
+ - Note that if you do not pass any initial state to NetworkController, this means that `0x5` and `0xe704` will no longer be keys in `networkConfigurationsByChainId`.
24
+ - We are not counting this as a breaking change because we don't make any guarantees about what keys are present in `networkConfigurationsByChainId` at runtime — only that they must be valid chain IDs.
25
+ - If you want more of a guarantee, you are recommended to persist the NetworkController state and then pass it back through as initial state.
26
+ - Update `RpcEndpoint` so that `failoverUrls` is optional ([#5561](https://github.com/MetaMask/core/pull/5561))
27
+ - This property was introduced in 23.0.0 as a breaking change, but this change makes it non-breaking.
28
+ - Update `NetworkClientConfiguration` so that `failoverUrls` is optional ([#5561](https://github.com/MetaMask/core/pull/5561))
29
+ - This property was introduced in 23.0.0 as a breaking change, but this change makes it non-breaking.
30
+ - Bump `@metamask/controller-utils` to `^11.7.0` ([#5583](https://github.com/MetaMask/core/pull/5583))
31
+
32
+ ### Fixed
33
+
34
+ - Upgrade `@metamask/eth-json-rpc-infura` to `^10.1.1` and `@metamask/eth-json-rpc-infura` to `^16.0.1` ([#5573](https://github.com/MetaMask/core/pull/5573))
35
+ - This fixes a bug where non-standard unsuccessful JSON-RPC errors were being ignored/discarded
36
+
37
+ ## [23.1.0]
38
+
39
+ ### Added
40
+
41
+ - The `NetworkController:rpcEndpointDegraded` messenger event now has a new `chainId` property in its data, which is the ID of the chain that the endpoint represents ([#5517](https://github.com/MetaMask/core/pull/5517))
42
+
10
43
  ## [23.0.0]
11
44
 
12
45
  ### Added
@@ -781,7 +814,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
781
814
 
782
815
  All changes listed after this point were applied to this package following the monorepo conversion.
783
816
 
784
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/network-controller@23.0.0...HEAD
817
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/network-controller@23.2.0...HEAD
818
+ [23.2.0]: https://github.com/MetaMask/core/compare/@metamask/network-controller@23.1.0...@metamask/network-controller@23.2.0
819
+ [23.1.0]: https://github.com/MetaMask/core/compare/@metamask/network-controller@23.0.0...@metamask/network-controller@23.1.0
785
820
  [23.0.0]: https://github.com/MetaMask/core/compare/@metamask/network-controller@22.2.1...@metamask/network-controller@23.0.0
786
821
  [22.2.1]: https://github.com/MetaMask/core/compare/@metamask/network-controller@22.2.0...@metamask/network-controller@22.2.1
787
822
  [22.2.0]: https://github.com/MetaMask/core/compare/@metamask/network-controller@22.1.1...@metamask/network-controller@22.2.0
@@ -106,11 +106,33 @@ const controllerName = 'NetworkController';
106
106
  * Constructs a value for the state property `networkConfigurationsByChainId`
107
107
  * which will be used if it has not been provided to the constructor.
108
108
  *
109
+ * @param [additionalDefaultNetworks] - An array of Hex Chain IDs representing the additional networks to be included as default.
109
110
  * @returns The default value for `networkConfigurationsByChainId`.
110
111
  */
111
- function getDefaultNetworkConfigurationsByChainId() {
112
+ function getDefaultNetworkConfigurationsByChainId(additionalDefaultNetworks = []) {
113
+ const infuraNetworks = getDefaultInfuraNetworkConfigurationsByChainId();
114
+ const customNetworks = getDefaultCustomNetworkConfigurationsByChainId();
115
+ return additionalDefaultNetworks.reduce((obj, chainId) => {
116
+ if ((0, utils_1.hasProperty)(customNetworks, chainId)) {
117
+ obj[chainId] = customNetworks[chainId];
118
+ }
119
+ return obj;
120
+ },
121
+ // Always include the infura networks in the default networks
122
+ infuraNetworks);
123
+ }
124
+ /**
125
+ * Constructs a `networkConfigurationsByChainId` object for all default Infura networks.
126
+ *
127
+ * @returns The `networkConfigurationsByChainId` object of all Infura networks.
128
+ */
129
+ function getDefaultInfuraNetworkConfigurationsByChainId() {
112
130
  return Object.values(controller_utils_1.InfuraNetworkType).reduce((obj, infuraNetworkType) => {
113
131
  const chainId = controller_utils_1.ChainId[infuraNetworkType];
132
+ // Skip deprecated network as default network.
133
+ if (constants_1.DEPRECATED_NETWORKS.has(chainId)) {
134
+ return obj;
135
+ }
114
136
  const rpcEndpointUrl =
115
137
  // This ESLint rule mistakenly produces an error.
116
138
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
@@ -133,15 +155,42 @@ function getDefaultNetworkConfigurationsByChainId() {
133
155
  return { ...obj, [chainId]: networkConfiguration };
134
156
  }, {});
135
157
  }
158
+ /**
159
+ * Constructs a `networkConfigurationsByChainId` object for all default custom networks.
160
+ *
161
+ * @returns The `networkConfigurationsByChainId` object of all custom networks.
162
+ */
163
+ function getDefaultCustomNetworkConfigurationsByChainId() {
164
+ const { ticker, rpcPrefs } = controller_utils_1.BUILT_IN_NETWORKS[controller_utils_1.BuiltInNetworkName.MegaETHTestnet];
165
+ return {
166
+ [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.MegaETHTestnet]]: {
167
+ blockExplorerUrls: [rpcPrefs.blockExplorerUrl],
168
+ chainId: controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.MegaETHTestnet],
169
+ defaultRpcEndpointIndex: 0,
170
+ defaultBlockExplorerUrlIndex: 0,
171
+ name: controller_utils_1.NetworkNickname[controller_utils_1.BuiltInNetworkName.MegaETHTestnet],
172
+ nativeCurrency: ticker,
173
+ rpcEndpoints: [
174
+ {
175
+ failoverUrls: [],
176
+ networkClientId: controller_utils_1.BuiltInNetworkName.MegaETHTestnet,
177
+ type: RpcEndpointType.Custom,
178
+ url: controller_utils_1.BUILT_IN_CUSTOM_NETWORKS_RPC.MEGAETH_TESTNET,
179
+ },
180
+ ],
181
+ },
182
+ };
183
+ }
136
184
  /**
137
185
  * Constructs properties for the NetworkController state whose values will be
138
186
  * used if not provided to the constructor.
139
187
  *
188
+ * @param [additionalDefaultNetworks] - An array of Hex Chain IDs representing the additional networks to be included as default.
140
189
  * @returns The default NetworkController state.
141
190
  */
142
- function getDefaultNetworkControllerState() {
191
+ function getDefaultNetworkControllerState(additionalDefaultNetworks) {
143
192
  const networksMetadata = {};
144
- const networkConfigurationsByChainId = getDefaultNetworkConfigurationsByChainId();
193
+ const networkConfigurationsByChainId = getDefaultNetworkConfigurationsByChainId(additionalDefaultNetworks);
145
194
  return {
146
195
  selectedNetworkClientId: controller_utils_1.InfuraNetworkType.mainnet,
147
196
  networksMetadata,
@@ -293,8 +342,11 @@ class NetworkController extends base_controller_1.BaseController {
293
342
  * @param options - The options; see {@link NetworkControllerOptions}.
294
343
  */
295
344
  constructor(options) {
296
- const { messenger, state, infuraProjectId, log, getRpcServiceOptions } = options;
297
- const initialState = { ...getDefaultNetworkControllerState(), ...state };
345
+ const { messenger, state, infuraProjectId, log, getRpcServiceOptions, additionalDefaultNetworks, } = options;
346
+ const initialState = {
347
+ ...getDefaultNetworkControllerState(additionalDefaultNetworks),
348
+ ...state,
349
+ };
298
350
  validateNetworkControllerState(initialState);
299
351
  if (!infuraProjectId || typeof infuraProjectId !== 'string') {
300
352
  throw new Error('Invalid Infura project ID');
@@ -368,10 +420,8 @@ class NetworkController extends base_controller_1.BaseController {
368
420
  // ESLint is mistaken here; `name` is a string.
369
421
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
370
422
  `${this.name}:getNetworkConfigurationByNetworkClientId`, this.getNetworkConfigurationByNetworkClientId.bind(this));
371
- this.messagingSystem.registerActionHandler(
372
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
373
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
374
- `${this.name}:getSelectedNetworkClient`, this.getSelectedNetworkClient.bind(this));
423
+ this.messagingSystem.registerActionHandler(`${this.name}:getSelectedNetworkClient`, this.getSelectedNetworkClient.bind(this));
424
+ this.messagingSystem.registerActionHandler(`${this.name}:getSelectedChainId`, this.getSelectedChainId.bind(this));
375
425
  this.messagingSystem.registerActionHandler(
376
426
  // ESLint is mistaken here; `name` is a string.
377
427
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
@@ -410,6 +460,15 @@ class NetworkController extends base_controller_1.BaseController {
410
460
  }
411
461
  return undefined;
412
462
  }
463
+ /**
464
+ * Accesses the chain ID from the selected network client.
465
+ *
466
+ * @returns The chain ID of the selected network client in hex format or undefined if there is no network client.
467
+ */
468
+ getSelectedChainId() {
469
+ const networkConfiguration = this.getNetworkConfigurationByNetworkClientId(this.state.selectedNetworkClientId);
470
+ return networkConfiguration?.chainId;
471
+ }
413
472
  /**
414
473
  * Internally, the Infura and custom network clients are categorized by type
415
474
  * so that when accessing either kind of network client, TypeScript knows