@metamask-previews/network-controller 23.1.0-preview-6a548b7c → 23.1.0-preview-9f91d514

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.
@@ -106,9 +106,27 @@ 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];
114
132
  const rpcEndpointUrl =
@@ -133,15 +151,42 @@ function getDefaultNetworkConfigurationsByChainId() {
133
151
  return { ...obj, [chainId]: networkConfiguration };
134
152
  }, {});
135
153
  }
154
+ /**
155
+ * Constructs a `networkConfigurationsByChainId` object for all default custom networks.
156
+ *
157
+ * @returns The `networkConfigurationsByChainId` object of all custom networks.
158
+ */
159
+ function getDefaultCustomNetworkConfigurationsByChainId() {
160
+ const { ticker, rpcPrefs } = controller_utils_1.BUILT_IN_NETWORKS[controller_utils_1.BuiltInNetworkName.MegaETHTestnet];
161
+ return {
162
+ [controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.MegaETHTestnet]]: {
163
+ blockExplorerUrls: [rpcPrefs.blockExplorerUrl],
164
+ chainId: controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.MegaETHTestnet],
165
+ defaultRpcEndpointIndex: 0,
166
+ defaultBlockExplorerUrlIndex: 0,
167
+ name: controller_utils_1.NetworkNickname[controller_utils_1.BuiltInNetworkName.MegaETHTestnet],
168
+ nativeCurrency: ticker,
169
+ rpcEndpoints: [
170
+ {
171
+ failoverUrls: [],
172
+ networkClientId: controller_utils_1.BuiltInNetworkName.MegaETHTestnet,
173
+ type: RpcEndpointType.Custom,
174
+ url: controller_utils_1.BUILT_IN_CUSTOM_NETWORKS_RPC.MEGAETH_TESTNET,
175
+ },
176
+ ],
177
+ },
178
+ };
179
+ }
136
180
  /**
137
181
  * Constructs properties for the NetworkController state whose values will be
138
182
  * used if not provided to the constructor.
139
183
  *
184
+ * @param [additionalDefaultNetworks] - An array of Hex Chain IDs representing the additional networks to be included as default.
140
185
  * @returns The default NetworkController state.
141
186
  */
142
- function getDefaultNetworkControllerState() {
187
+ function getDefaultNetworkControllerState(additionalDefaultNetworks) {
143
188
  const networksMetadata = {};
144
- const networkConfigurationsByChainId = getDefaultNetworkConfigurationsByChainId();
189
+ const networkConfigurationsByChainId = getDefaultNetworkConfigurationsByChainId(additionalDefaultNetworks);
145
190
  return {
146
191
  selectedNetworkClientId: controller_utils_1.InfuraNetworkType.mainnet,
147
192
  networksMetadata,
@@ -293,8 +338,11 @@ class NetworkController extends base_controller_1.BaseController {
293
338
  * @param options - The options; see {@link NetworkControllerOptions}.
294
339
  */
295
340
  constructor(options) {
296
- const { messenger, state, infuraProjectId, log, getRpcServiceOptions } = options;
297
- const initialState = { ...getDefaultNetworkControllerState(), ...state };
341
+ const { messenger, state, infuraProjectId, log, getRpcServiceOptions, additionalDefaultNetworks, } = options;
342
+ const initialState = {
343
+ ...getDefaultNetworkControllerState(additionalDefaultNetworks),
344
+ ...state,
345
+ };
298
346
  validateNetworkControllerState(initialState);
299
347
  if (!infuraProjectId || typeof infuraProjectId !== 'string') {
300
348
  throw new Error('Invalid Infura project ID');
@@ -368,10 +416,8 @@ class NetworkController extends base_controller_1.BaseController {
368
416
  // ESLint is mistaken here; `name` is a string.
369
417
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
370
418
  `${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));
419
+ this.messagingSystem.registerActionHandler(`${this.name}:getSelectedNetworkClient`, this.getSelectedNetworkClient.bind(this));
420
+ this.messagingSystem.registerActionHandler(`${this.name}:getSelectedChainId`, this.getSelectedChainId.bind(this));
375
421
  this.messagingSystem.registerActionHandler(
376
422
  // ESLint is mistaken here; `name` is a string.
377
423
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
@@ -410,6 +456,15 @@ class NetworkController extends base_controller_1.BaseController {
410
456
  }
411
457
  return undefined;
412
458
  }
459
+ /**
460
+ * Accesses the chain ID from the selected network client.
461
+ *
462
+ * @returns The chain ID of the selected network client in hex format or undefined if there is no network client.
463
+ */
464
+ getSelectedChainId() {
465
+ const networkConfiguration = this.getNetworkConfigurationByNetworkClientId(this.state.selectedNetworkClientId);
466
+ return networkConfiguration?.chainId;
467
+ }
413
468
  /**
414
469
  * Internally, the Infura and custom network clients are categorized by type
415
470
  * so that when accessing either kind of network client, TypeScript knows