@metamask/network-controller 19.0.0 → 20.1.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +38 -1
  2. package/dist/NetworkController.js +4 -4
  3. package/dist/NetworkController.mjs +3 -3
  4. package/dist/{chunk-UG2NYGJD.mjs → chunk-6PT34M4U.mjs} +162 -230
  5. package/dist/chunk-6PT34M4U.mjs.map +1 -0
  6. package/dist/{chunk-LDNWNT2P.js → chunk-E4V6XEBR.js} +3 -1
  7. package/dist/chunk-E4V6XEBR.js.map +1 -0
  8. package/dist/{chunk-ZV34XXLT.js → chunk-LLMZDA4Q.js} +6 -6
  9. package/dist/chunk-LLMZDA4Q.js.map +1 -0
  10. package/dist/{chunk-G5HYTGGH.mjs → chunk-TZA3CBEI.mjs} +2 -2
  11. package/dist/chunk-TZA3CBEI.mjs.map +1 -0
  12. package/dist/{chunk-TPYFTFZ2.mjs → chunk-U43RY4MY.mjs} +3 -1
  13. package/dist/chunk-U43RY4MY.mjs.map +1 -0
  14. package/dist/{chunk-4ZD3DTQ7.js → chunk-WT5ZBF4X.js} +164 -232
  15. package/dist/chunk-WT5ZBF4X.js.map +1 -0
  16. package/dist/create-auto-managed-network-client.js +3 -3
  17. package/dist/create-auto-managed-network-client.mjs +2 -2
  18. package/dist/create-network-client.js +2 -2
  19. package/dist/create-network-client.mjs +1 -1
  20. package/dist/index.js +4 -4
  21. package/dist/index.mjs +3 -3
  22. package/dist/tsconfig.build.tsbuildinfo +1 -1
  23. package/dist/types/NetworkController.d.ts +32 -50
  24. package/dist/types/NetworkController.d.ts.map +1 -1
  25. package/dist/types/create-auto-managed-network-client.d.ts.map +1 -1
  26. package/dist/types/create-network-client.d.ts.map +1 -1
  27. package/dist/types/index.d.ts +2 -1
  28. package/dist/types/index.d.ts.map +1 -1
  29. package/package.json +14 -13
  30. package/dist/chunk-4ZD3DTQ7.js.map +0 -1
  31. package/dist/chunk-G5HYTGGH.mjs.map +0 -1
  32. package/dist/chunk-LDNWNT2P.js.map +0 -1
  33. package/dist/chunk-TPYFTFZ2.mjs.map +0 -1
  34. package/dist/chunk-UG2NYGJD.mjs.map +0 -1
  35. package/dist/chunk-ZV34XXLT.js.map +0 -1
@@ -3,7 +3,7 @@
3
3
  var _chunkZKNI7MD3js = require('./chunk-ZKNI7MD3.js');
4
4
 
5
5
 
6
- var _chunkZV34XXLTjs = require('./chunk-ZV34XXLT.js');
6
+ var _chunkLLMZDA4Qjs = require('./chunk-LLMZDA4Q.js');
7
7
 
8
8
 
9
9
 
@@ -23,8 +23,6 @@ var _basecontroller = require('@metamask/base-controller');
23
23
 
24
24
 
25
25
 
26
-
27
-
28
26
  var _controllerutils = require('@metamask/controller-utils');
29
27
  var _ethquery = require('@metamask/eth-query'); var _ethquery2 = _interopRequireDefault(_ethquery);
30
28
  var _rpcerrors = require('@metamask/rpc-errors');
@@ -36,7 +34,7 @@ var _swappableobjproxy = require('@metamask/swappable-obj-proxy');
36
34
  var _utils = require('@metamask/utils');
37
35
  var _assert = require('assert');
38
36
  var _uuid = require('uuid');
39
- var log = _chunkVGYLDDJBjs.createModuleLogger.call(void 0, _chunkVGYLDDJBjs.projectLogger, "NetworkController");
37
+ var debugLog = _chunkVGYLDDJBjs.createModuleLogger.call(void 0, _chunkVGYLDDJBjs.projectLogger, "NetworkController");
40
38
  function knownKeysOf(object) {
41
39
  return Object.keys(object);
42
40
  }
@@ -60,62 +58,20 @@ function pick(object, keys) {
60
58
  function isErrorWithCode(error) {
61
59
  return typeof error === "object" && error !== null && "code" in error;
62
60
  }
63
- function buildInfuraNetworkClientId(infuraNetworkOrProviderConfig) {
64
- if (typeof infuraNetworkOrProviderConfig === "string") {
65
- return infuraNetworkOrProviderConfig;
66
- }
67
- return infuraNetworkOrProviderConfig.type;
68
- }
69
- function buildCustomNetworkClientId(...args) {
70
- if (args.length === 1) {
71
- return args[0];
72
- }
73
- const [{ id, rpcUrl }, networkConfigurations] = args;
74
- if (id === void 0) {
75
- const matchingNetworkConfiguration = Object.values(
76
- networkConfigurations
77
- ).find((networkConfiguration) => {
78
- return networkConfiguration.rpcUrl === rpcUrl.toLowerCase();
79
- });
80
- if (matchingNetworkConfiguration) {
81
- return matchingNetworkConfiguration.id;
82
- }
83
- return rpcUrl.toLowerCase();
84
- }
85
- return id;
86
- }
87
- function isInfuraProviderConfig(providerConfig) {
88
- return _controllerutils.isInfuraNetworkType.call(void 0, providerConfig.type);
89
- }
90
- function isCustomProviderConfig(providerConfig) {
91
- return providerConfig.type === _controllerutils.NetworkType.rpc;
92
- }
93
- function validateCustomProviderConfig(providerConfig) {
94
- if (providerConfig.chainId === void 0) {
95
- throw new Error("chainId must be provided for custom RPC endpoints");
96
- }
97
- if (providerConfig.rpcUrl === void 0) {
98
- throw new Error("rpcUrl must be provided for custom RPC endpoints");
99
- }
100
- }
101
61
  var name = "NetworkController";
102
62
  var defaultState = {
103
63
  selectedNetworkClientId: _controllerutils.NetworkType.mainnet,
104
- providerConfig: {
105
- type: _controllerutils.NetworkType.mainnet,
106
- chainId: _controllerutils.ChainId.mainnet,
107
- ticker: _controllerutils.NetworksTicker.mainnet
108
- },
109
64
  networksMetadata: {},
110
65
  networkConfigurations: {}
111
66
  };
112
- var _ethQuery, _infuraProjectId, _trackMetaMetricsEvent, _previousProviderConfig, _providerProxy, _blockTrackerProxy, _autoManagedNetworkClientRegistry, _refreshNetwork, refreshNetwork_fn, _getLatestBlock, getLatestBlock_fn, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn, _createAutoManagedNetworkClientRegistry, createAutoManagedNetworkClientRegistry_fn, _buildIdentifiedInfuraNetworkClientConfigurations, buildIdentifiedInfuraNetworkClientConfigurations_fn, _buildIdentifiedCustomNetworkClientConfigurations, buildIdentifiedCustomNetworkClientConfigurations_fn, _buildIdentifiedNetworkClientConfigurationsFromProviderConfig, buildIdentifiedNetworkClientConfigurationsFromProviderConfig_fn, _applyNetworkSelection, applyNetworkSelection_fn;
67
+ var _ethQuery, _infuraProjectId, _trackMetaMetricsEvent, _previouslySelectedNetworkClientId, _providerProxy, _blockTrackerProxy, _autoManagedNetworkClientRegistry, _autoManagedNetworkClient, _log, _refreshNetwork, refreshNetwork_fn, _getLatestBlock, getLatestBlock_fn, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn, _createAutoManagedNetworkClientRegistry, createAutoManagedNetworkClientRegistry_fn, _buildIdentifiedInfuraNetworkClientConfigurations, buildIdentifiedInfuraNetworkClientConfigurations_fn, _buildIdentifiedCustomNetworkClientConfigurations, buildIdentifiedCustomNetworkClientConfigurations_fn, _applyNetworkSelection, applyNetworkSelection_fn;
113
68
  var NetworkController = class extends _basecontroller.BaseController {
114
69
  constructor({
115
70
  messenger,
116
71
  state,
117
72
  infuraProjectId,
118
- trackMetaMetricsEvent
73
+ trackMetaMetricsEvent,
74
+ log
119
75
  }) {
120
76
  super({
121
77
  name,
@@ -128,10 +84,6 @@ var NetworkController = class extends _basecontroller.BaseController {
128
84
  persist: true,
129
85
  anonymous: false
130
86
  },
131
- providerConfig: {
132
- persist: true,
133
- anonymous: false
134
- },
135
87
  networkConfigurations: {
136
88
  persist: true,
137
89
  anonymous: false
@@ -141,12 +93,20 @@ var NetworkController = class extends _basecontroller.BaseController {
141
93
  state: { ...defaultState, ...state }
142
94
  });
143
95
  /**
144
- * Executes a series of steps to apply the changes to the provider config:
96
+ * Executes a series of steps to switch the network:
97
+ *
98
+ * 1. Notifies subscribers via the messenger that the network is about to be
99
+ * switched (and, really, that the global provider and block tracker proxies
100
+ * will be re-pointed to a new network).
101
+ * 2. Looks up a known and preinitialized network client matching the given
102
+ * ID and uses it to re-point the aforementioned provider and block tracker
103
+ * proxies.
104
+ * 3. Notifies subscribers via the messenger that the network has switched.
105
+ * 4. Captures metadata for the newly switched network in state.
145
106
  *
146
- * 1. Notifies subscribers that the network is about to change.
147
- * 2. Looks up a known and preinitialized network client matching the provider
148
- * config and re-points the provider and block tracker proxy to it.
149
- * 3. Notifies subscribers that the network has changed.
107
+ * @param networkClientId - The ID of a network client that requests will be
108
+ * routed through (either the name of an Infura network or the ID of a custom
109
+ * network configuration).
150
110
  */
151
111
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _refreshNetwork);
152
112
  /**
@@ -199,77 +159,88 @@ var NetworkController = class extends _basecontroller.BaseController {
199
159
  */
200
160
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _buildIdentifiedCustomNetworkClientConfigurations);
201
161
  /**
202
- * Converts the provider config object in state to a network client
203
- * configuration object.
162
+ * Updates the global provider and block tracker proxies (accessible via
163
+ * {@link getSelectedNetworkClient}) to point to the same ones within the
164
+ * given network client, thereby magically switching any consumers using these
165
+ * proxies to use the new network.
204
166
  *
205
- * @returns The network client config.
206
- * @throws If the provider config is of type "rpc" and lacks either a
207
- * `chainId` or an `rpcUrl`.
208
- */
209
- _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _buildIdentifiedNetworkClientConfigurationsFromProviderConfig);
210
- /**
211
- * Uses the information in the provider config object to look up a known and
212
- * preinitialized network client. Once a network client is found, updates the
213
- * provider and block tracker proxy to point to those from the network client,
214
- * then finally creates an EthQuery that points to the provider proxy.
167
+ * Also refreshes the EthQuery instance accessible via the `getEthQuery`
168
+ * action to wrap the provider from the new network client. Note that this is
169
+ * not a proxy, so consumers will need to call `getEthQuery` again after the
170
+ * network switch.
215
171
  *
216
- * @throws If no network client could be found matching the current provider
217
- * config.
172
+ * @param networkClientId - The ID of a network client that requests will be
173
+ * routed through (either the name of an Infura network or the ID of a custom
174
+ * network configuration).
175
+ * @throws if no network client could be found matching the given ID.
218
176
  */
219
177
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _applyNetworkSelection);
220
178
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _ethQuery, void 0);
221
179
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _infuraProjectId, void 0);
222
180
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _trackMetaMetricsEvent, void 0);
223
- _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _previousProviderConfig, void 0);
181
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _previouslySelectedNetworkClientId, void 0);
224
182
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _providerProxy, void 0);
225
183
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _blockTrackerProxy, void 0);
226
184
  _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _autoManagedNetworkClientRegistry, void 0);
185
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _autoManagedNetworkClient, void 0);
186
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _log, void 0);
227
187
  if (!infuraProjectId || typeof infuraProjectId !== "string") {
228
188
  throw new Error("Invalid Infura project ID");
229
189
  }
230
190
  _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _infuraProjectId, infuraProjectId);
231
191
  _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _trackMetaMetricsEvent, trackMetaMetricsEvent);
232
192
  this.messagingSystem.registerActionHandler(
233
- `${this.name}:getProviderConfig`,
234
- () => {
235
- return this.state.providerConfig;
236
- }
237
- );
238
- this.messagingSystem.registerActionHandler(
193
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
194
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
239
195
  `${this.name}:getEthQuery`,
240
196
  () => {
241
197
  return _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _ethQuery);
242
198
  }
243
199
  );
244
200
  this.messagingSystem.registerActionHandler(
201
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
202
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
245
203
  `${this.name}:getNetworkClientById`,
246
204
  this.getNetworkClientById.bind(this)
247
205
  );
248
206
  this.messagingSystem.registerActionHandler(
207
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
208
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
249
209
  `${this.name}:getEIP1559Compatibility`,
250
210
  this.getEIP1559Compatibility.bind(this)
251
211
  );
252
212
  this.messagingSystem.registerActionHandler(
213
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
214
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
253
215
  `${this.name}:setActiveNetwork`,
254
216
  this.setActiveNetwork.bind(this)
255
217
  );
256
218
  this.messagingSystem.registerActionHandler(
219
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
220
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
257
221
  `${this.name}:setProviderType`,
258
222
  this.setProviderType.bind(this)
259
223
  );
260
224
  this.messagingSystem.registerActionHandler(
225
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
226
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
261
227
  `${this.name}:findNetworkClientIdByChainId`,
262
228
  this.findNetworkClientIdByChainId.bind(this)
263
229
  );
264
230
  this.messagingSystem.registerActionHandler(
231
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
232
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
265
233
  `${this.name}:getNetworkConfigurationByNetworkClientId`,
266
234
  this.getNetworkConfigurationByNetworkClientId.bind(this)
267
235
  );
268
236
  this.messagingSystem.registerActionHandler(
237
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
238
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
269
239
  `${this.name}:getSelectedNetworkClient`,
270
240
  this.getSelectedNetworkClient.bind(this)
271
241
  );
272
- _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _previousProviderConfig, this.state.providerConfig);
242
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _previouslySelectedNetworkClientId, this.state.selectedNetworkClientId);
243
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _log, log);
273
244
  }
274
245
  /**
275
246
  * Accesses the provider and block tracker for the currently selected network.
@@ -321,6 +292,8 @@ var NetworkController = class extends _basecontroller.BaseController {
321
292
  const infuraNetworkClient = autoManagedNetworkClientRegistry["infura" /* Infura */][networkClientId];
322
293
  if (!infuraNetworkClient) {
323
294
  throw new Error(
295
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
296
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
324
297
  `No Infura network client was found with the ID "${networkClientId}".`
325
298
  );
326
299
  }
@@ -329,18 +302,19 @@ var NetworkController = class extends _basecontroller.BaseController {
329
302
  const customNetworkClient = autoManagedNetworkClientRegistry["custom" /* Custom */][networkClientId];
330
303
  if (!customNetworkClient) {
331
304
  throw new Error(
305
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
306
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
332
307
  `No custom network client was found with the ID "${networkClientId}".`
333
308
  );
334
309
  }
335
310
  return customNetworkClient;
336
311
  }
337
312
  /**
338
- * Populates the network clients and establishes the initial network based on
339
- * the provider configuration in state.
313
+ * Creates network clients for built-in and custom networks, then establishes
314
+ * the currently selected network client based on state.
340
315
  */
341
316
  async initializeProvider() {
342
- _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
343
- _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _applyNetworkSelection, applyNetworkSelection_fn).call(this);
317
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _applyNetworkSelection, applyNetworkSelection_fn).call(this, this.state.selectedNetworkClientId);
344
318
  await this.lookupNetwork();
345
319
  }
346
320
  /**
@@ -357,28 +331,45 @@ var NetworkController = class extends _basecontroller.BaseController {
357
331
  updatedIsEIP1559Compatible = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn).call(this, networkClientId);
358
332
  updatedNetworkStatus = "available" /* Available */;
359
333
  } catch (error) {
334
+ debugLog("NetworkController: lookupNetworkByClientId: ", error);
360
335
  if (isErrorWithCode(error)) {
361
336
  let responseBody;
362
337
  if (isInfura && _utils.hasProperty.call(void 0, error, "message") && typeof error.message === "string") {
363
338
  try {
364
339
  responseBody = JSON.parse(error.message);
365
340
  } catch {
341
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _log)?.warn(
342
+ "NetworkController: lookupNetworkByClientId: json parse error: ",
343
+ error
344
+ );
366
345
  }
367
346
  }
368
347
  if (_utils.isPlainObject.call(void 0, responseBody) && responseBody.error === _chunkZKNI7MD3js.INFURA_BLOCKED_KEY) {
369
348
  updatedNetworkStatus = "blocked" /* Blocked */;
370
349
  } else if (error.code === _rpcerrors.errorCodes.rpc.internal) {
371
350
  updatedNetworkStatus = "unknown" /* Unknown */;
351
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _log)?.warn(
352
+ "NetworkController: lookupNetworkByClientId: rpc internal error: ",
353
+ error
354
+ );
372
355
  } else {
373
356
  updatedNetworkStatus = "unavailable" /* Unavailable */;
357
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _log)?.warn(
358
+ "NetworkController: lookupNetworkByClientId: ",
359
+ error
360
+ );
374
361
  }
375
362
  } else if (typeof Error !== "undefined" && _utils.hasProperty.call(void 0, error, "message") && typeof error.message === "string" && error.message.includes(
376
363
  "No custom network client was found with the ID"
377
364
  )) {
378
365
  throw error;
379
366
  } else {
380
- log("NetworkController - could not determine network status", error);
367
+ debugLog(
368
+ "NetworkController - could not determine network status",
369
+ error
370
+ );
381
371
  updatedNetworkStatus = "unknown" /* Unknown */;
372
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _log)?.warn("NetworkController: lookupNetworkByClientId: ", error);
382
373
  }
383
374
  }
384
375
  this.update((state) => {
@@ -398,17 +389,19 @@ var NetworkController = class extends _basecontroller.BaseController {
398
389
  });
399
390
  }
400
391
  /**
401
- * Performs side effects after switching to a network. If the network is
402
- * available, updates the network state with the network ID of the network and
403
- * stores whether the network supports EIP-1559; otherwise clears said
404
- * information about the network that may have been previously stored.
392
+ * Persists the following metadata about the given or selected network to
393
+ * state:
394
+ *
395
+ * - The status of the network, namely, whether it is available, geo-blocked
396
+ * (Infura only), or unavailable, or whether the status is unknown
397
+ * - Whether the network supports EIP-1559, or whether it is unknown
405
398
  *
406
- * @param networkClientId - (Optional) The ID of the network client to update.
399
+ * Note that it is possible for the network to be switched while this data is
400
+ * being collected. If that is the case, no metadata for the (now previously)
401
+ * selected network will be updated.
402
+ *
403
+ * @param networkClientId - The ID of the network client to update.
407
404
  * If no ID is provided, uses the currently selected network.
408
- * @fires infuraIsBlocked if the network is Infura-supported and is blocking
409
- * requests.
410
- * @fires infuraIsUnblocked if the network is Infura-supported and is not
411
- * blocking requests, or if the network is not Infura-supported.
412
405
  */
413
406
  async lookupNetwork(networkClientId) {
414
407
  if (networkClientId) {
@@ -418,7 +411,7 @@ var NetworkController = class extends _basecontroller.BaseController {
418
411
  if (!_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _ethQuery)) {
419
412
  return;
420
413
  }
421
- const isInfura = isInfuraProviderConfig(this.state.providerConfig);
414
+ const isInfura = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClient)?.configuration.type === "infura" /* Infura */;
422
415
  let networkChanged = false;
423
416
  const listener = () => {
424
417
  networkChanged = true;
@@ -443,19 +436,32 @@ var NetworkController = class extends _basecontroller.BaseController {
443
436
  if (isInfura && _utils.hasProperty.call(void 0, error, "message") && typeof error.message === "string") {
444
437
  try {
445
438
  responseBody = JSON.parse(error.message);
446
- } catch {
439
+ } catch (parseError) {
440
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _log)?.warn(
441
+ "NetworkController: lookupNetwork: json parse error",
442
+ parseError
443
+ );
447
444
  }
448
445
  }
449
446
  if (_utils.isPlainObject.call(void 0, responseBody) && responseBody.error === _chunkZKNI7MD3js.INFURA_BLOCKED_KEY) {
450
447
  updatedNetworkStatus = "blocked" /* Blocked */;
451
448
  } else if (error.code === _rpcerrors.errorCodes.rpc.internal) {
452
449
  updatedNetworkStatus = "unknown" /* Unknown */;
450
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _log)?.warn(
451
+ "NetworkController: lookupNetwork: rpc internal error",
452
+ error
453
+ );
453
454
  } else {
454
455
  updatedNetworkStatus = "unavailable" /* Unavailable */;
456
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _log)?.warn("NetworkController: lookupNetwork: ", error);
455
457
  }
456
458
  } else {
457
- log("NetworkController - could not determine network status", error);
459
+ debugLog(
460
+ "NetworkController - could not determine network status",
461
+ error
462
+ );
458
463
  updatedNetworkStatus = "unknown" /* Unknown */;
464
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _log)?.warn("NetworkController: lookupNetwork: ", error);
459
465
  }
460
466
  }
461
467
  if (networkChanged) {
@@ -495,52 +501,30 @@ var NetworkController = class extends _basecontroller.BaseController {
495
501
  _assert.strict.notStrictEqual(
496
502
  type,
497
503
  _controllerutils.NetworkType.rpc,
504
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
505
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
498
506
  `NetworkController - cannot call "setProviderType" with type "${_controllerutils.NetworkType.rpc}". Use "setActiveNetwork"`
499
507
  );
500
508
  _assert.strict.ok(
501
509
  _controllerutils.isInfuraNetworkType.call(void 0, type),
510
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
511
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
502
512
  `Unknown Infura provider type "${type}".`
503
513
  );
504
514
  await this.setActiveNetwork(type);
505
515
  }
506
516
  /**
507
- * Convenience method to update provider RPC settings.
517
+ * Changes the selected network.
508
518
  *
509
- * @param networkConfigurationIdOrType - The unique id for the network configuration to set as the active provider,
510
- * or the type of a built-in network.
519
+ * @param networkClientId - The ID of a network client that requests will be
520
+ * routed through (either the name of an Infura network or the ID of a custom
521
+ * network configuration).
522
+ * @throws if no network client is associated with the given
523
+ * `networkClientId`.
511
524
  */
512
- async setActiveNetwork(networkConfigurationIdOrType) {
513
- _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _previousProviderConfig, this.state.providerConfig);
514
- let targetNetwork;
515
- if (_controllerutils.isInfuraNetworkType.call(void 0, networkConfigurationIdOrType)) {
516
- const ticker = _controllerutils.NetworksTicker[networkConfigurationIdOrType];
517
- targetNetwork = {
518
- chainId: _controllerutils.ChainId[networkConfigurationIdOrType],
519
- id: void 0,
520
- rpcPrefs: _controllerutils.BUILT_IN_NETWORKS[networkConfigurationIdOrType].rpcPrefs,
521
- rpcUrl: void 0,
522
- nickname: void 0,
523
- ticker,
524
- type: networkConfigurationIdOrType
525
- };
526
- } else {
527
- if (!Object.keys(this.state.networkConfigurations).includes(
528
- networkConfigurationIdOrType
529
- )) {
530
- throw new Error(
531
- `networkConfigurationId ${networkConfigurationIdOrType} does not match a configured networkConfiguration or built-in network type`
532
- );
533
- }
534
- targetNetwork = {
535
- ...this.state.networkConfigurations[networkConfigurationIdOrType],
536
- type: _controllerutils.NetworkType.rpc
537
- };
538
- }
539
- _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
540
- this.update((state) => {
541
- state.providerConfig = targetNetwork;
542
- });
543
- await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _refreshNetwork, refreshNetwork_fn).call(this);
525
+ async setActiveNetwork(networkClientId) {
526
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _previouslySelectedNetworkClientId, this.state.selectedNetworkClientId);
527
+ await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _refreshNetwork, refreshNetwork_fn).call(this, networkClientId);
544
528
  }
545
529
  /**
546
530
  * Determines whether the network supports EIP-1559 by checking whether the
@@ -580,11 +564,11 @@ var NetworkController = class extends _basecontroller.BaseController {
580
564
  return EIPS[1559];
581
565
  }
582
566
  /**
583
- * Re-initializes the provider and block tracker for the current network.
567
+ * Ensures that the provider and block tracker proxies are pointed to the
568
+ * currently selected network and refreshes the metadata for the
584
569
  */
585
570
  async resetConnection() {
586
- _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
587
- await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _refreshNetwork, refreshNetwork_fn).call(this);
571
+ await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _refreshNetwork, refreshNetwork_fn).call(this, this.state.selectedNetworkClientId);
588
572
  }
589
573
  /**
590
574
  * Returns a configuration object for the network identified by the given
@@ -675,9 +659,7 @@ var NetworkController = class extends _basecontroller.BaseController {
675
659
  (networkConfig) => networkConfig.rpcUrl.toLowerCase() === rpcUrl.toLowerCase()
676
660
  );
677
661
  const upsertedNetworkConfigurationId = existingNetworkConfiguration ? existingNetworkConfiguration.id : _uuid.v4.call(void 0, );
678
- const networkClientId = buildCustomNetworkClientId(
679
- upsertedNetworkConfigurationId
680
- );
662
+ const networkClientId = upsertedNetworkConfigurationId;
681
663
  const customNetworkClientRegistry = autoManagedNetworkClientRegistry["custom" /* Custom */];
682
664
  const existingAutoManagedNetworkClient = customNetworkClientRegistry[networkClientId];
683
665
  const shouldDestroyExistingNetworkClient = existingAutoManagedNetworkClient && existingAutoManagedNetworkClient.configuration.chainId !== chainId;
@@ -685,7 +667,7 @@ var NetworkController = class extends _basecontroller.BaseController {
685
667
  existingAutoManagedNetworkClient.destroy();
686
668
  }
687
669
  if (!existingAutoManagedNetworkClient || shouldDestroyExistingNetworkClient) {
688
- customNetworkClientRegistry[networkClientId] = _chunkZV34XXLTjs.createAutoManagedNetworkClient.call(void 0, {
670
+ customNetworkClientRegistry[networkClientId] = _chunkLLMZDA4Qjs.createAutoManagedNetworkClient.call(void 0, {
689
671
  type: "custom" /* Custom */,
690
672
  chainId,
691
673
  rpcUrl,
@@ -706,6 +688,8 @@ var NetworkController = class extends _basecontroller.BaseController {
706
688
  url: referrer
707
689
  },
708
690
  properties: {
691
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
692
+ // eslint-disable-next-line @typescript-eslint/naming-convention
709
693
  chain_id: chainId,
710
694
  symbol: ticker,
711
695
  source
@@ -734,7 +718,7 @@ var NetworkController = class extends _basecontroller.BaseController {
734
718
  );
735
719
  }
736
720
  const autoManagedNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
737
- const networkClientId = buildCustomNetworkClientId(networkConfigurationId);
721
+ const networkClientId = networkConfigurationId;
738
722
  this.update((state) => {
739
723
  delete state.networkConfigurations[networkConfigurationId];
740
724
  });
@@ -744,16 +728,14 @@ var NetworkController = class extends _basecontroller.BaseController {
744
728
  delete customNetworkClientRegistry[networkClientId];
745
729
  }
746
730
  /**
747
- * Switches to the previously selected network, assuming that there is one
748
- * (if not and `initializeProvider` has not been previously called, then this
749
- * method is equivalent to calling `resetConnection`).
731
+ * Assuming that the network has been previously switched, switches to this
732
+ * new network.
733
+ *
734
+ * If the network has not been previously switched, this method is equivalent
735
+ * to {@link resetConnection}.
750
736
  */
751
737
  async rollbackToPreviousProvider() {
752
- _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
753
- this.update((state) => {
754
- state.providerConfig = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _previousProviderConfig);
755
- });
756
- await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _refreshNetwork, refreshNetwork_fn).call(this);
738
+ await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _refreshNetwork, refreshNetwork_fn).call(this, _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _previouslySelectedNetworkClientId));
757
739
  }
758
740
  /**
759
741
  * Deactivates the controller, stopping any ongoing polling.
@@ -799,17 +781,19 @@ var NetworkController = class extends _basecontroller.BaseController {
799
781
  _ethQuery = new WeakMap();
800
782
  _infuraProjectId = new WeakMap();
801
783
  _trackMetaMetricsEvent = new WeakMap();
802
- _previousProviderConfig = new WeakMap();
784
+ _previouslySelectedNetworkClientId = new WeakMap();
803
785
  _providerProxy = new WeakMap();
804
786
  _blockTrackerProxy = new WeakMap();
805
787
  _autoManagedNetworkClientRegistry = new WeakMap();
788
+ _autoManagedNetworkClient = new WeakMap();
789
+ _log = new WeakMap();
806
790
  _refreshNetwork = new WeakSet();
807
- refreshNetwork_fn = async function() {
791
+ refreshNetwork_fn = async function(networkClientId) {
808
792
  this.messagingSystem.publish(
809
793
  "NetworkController:networkWillChange",
810
794
  this.state
811
795
  );
812
- _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _applyNetworkSelection, applyNetworkSelection_fn).call(this);
796
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _applyNetworkSelection, applyNetworkSelection_fn).call(this, networkClientId);
813
797
  this.messagingSystem.publish(
814
798
  "NetworkController:networkDidChange",
815
799
  this.state
@@ -854,16 +838,12 @@ _createAutoManagedNetworkClientRegistry = new WeakSet();
854
838
  createAutoManagedNetworkClientRegistry_fn = function() {
855
839
  return [
856
840
  ..._chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _buildIdentifiedInfuraNetworkClientConfigurations, buildIdentifiedInfuraNetworkClientConfigurations_fn).call(this),
857
- ..._chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _buildIdentifiedCustomNetworkClientConfigurations, buildIdentifiedCustomNetworkClientConfigurations_fn).call(this),
858
- ..._chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _buildIdentifiedNetworkClientConfigurationsFromProviderConfig, buildIdentifiedNetworkClientConfigurationsFromProviderConfig_fn).call(this)
841
+ ..._chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _buildIdentifiedCustomNetworkClientConfigurations, buildIdentifiedCustomNetworkClientConfigurations_fn).call(this)
859
842
  ].reduce(
860
843
  (registry, [networkClientType, networkClientId, networkClientConfiguration]) => {
861
- const autoManagedNetworkClient = _chunkZV34XXLTjs.createAutoManagedNetworkClient.call(void 0,
844
+ const autoManagedNetworkClient = _chunkLLMZDA4Qjs.createAutoManagedNetworkClient.call(void 0,
862
845
  networkClientConfiguration
863
846
  );
864
- if (networkClientId in registry[networkClientType]) {
865
- return registry;
866
- }
867
847
  return {
868
848
  ...registry,
869
849
  [networkClientType]: {
@@ -881,7 +861,6 @@ createAutoManagedNetworkClientRegistry_fn = function() {
881
861
  _buildIdentifiedInfuraNetworkClientConfigurations = new WeakSet();
882
862
  buildIdentifiedInfuraNetworkClientConfigurations_fn = function() {
883
863
  return knownKeysOf(_controllerutils.InfuraNetworkType).map((network) => {
884
- const networkClientId = buildInfuraNetworkClientId(network);
885
864
  const networkClientConfiguration = {
886
865
  type: "infura" /* Infura */,
887
866
  network,
@@ -889,26 +868,14 @@ buildIdentifiedInfuraNetworkClientConfigurations_fn = function() {
889
868
  chainId: _controllerutils.BUILT_IN_NETWORKS[network].chainId,
890
869
  ticker: _controllerutils.BUILT_IN_NETWORKS[network].ticker
891
870
  };
892
- return [
893
- "infura" /* Infura */,
894
- networkClientId,
895
- networkClientConfiguration
896
- ];
871
+ return ["infura" /* Infura */, network, networkClientConfiguration];
897
872
  });
898
873
  };
899
874
  _buildIdentifiedCustomNetworkClientConfigurations = new WeakSet();
900
875
  buildIdentifiedCustomNetworkClientConfigurations_fn = function() {
901
876
  return Object.entries(this.state.networkConfigurations).map(
902
877
  ([networkConfigurationId, networkConfiguration]) => {
903
- if (networkConfiguration.chainId === void 0) {
904
- throw new Error("chainId must be provided for custom RPC endpoints");
905
- }
906
- if (networkConfiguration.rpcUrl === void 0) {
907
- throw new Error("rpcUrl must be provided for custom RPC endpoints");
908
- }
909
- const networkClientId = buildCustomNetworkClientId(
910
- networkConfigurationId
911
- );
878
+ const networkClientId = networkConfigurationId;
912
879
  const networkClientConfiguration = {
913
880
  type: "custom" /* Custom */,
914
881
  chainId: networkConfiguration.chainId,
@@ -923,67 +890,28 @@ buildIdentifiedCustomNetworkClientConfigurations_fn = function() {
923
890
  }
924
891
  );
925
892
  };
926
- _buildIdentifiedNetworkClientConfigurationsFromProviderConfig = new WeakSet();
927
- buildIdentifiedNetworkClientConfigurationsFromProviderConfig_fn = function() {
928
- const { providerConfig } = this.state;
929
- if (isCustomProviderConfig(providerConfig)) {
930
- validateCustomProviderConfig(providerConfig);
931
- const networkClientId = buildCustomNetworkClientId(
932
- providerConfig,
933
- this.state.networkConfigurations
934
- );
935
- const networkClientConfiguration = {
936
- chainId: providerConfig.chainId,
937
- rpcUrl: providerConfig.rpcUrl,
938
- type: "custom" /* Custom */,
939
- ticker: providerConfig.ticker
940
- };
941
- return [
942
- ["custom" /* Custom */, networkClientId, networkClientConfiguration]
943
- ];
944
- }
945
- if (isInfuraProviderConfig(providerConfig)) {
946
- return [];
947
- }
948
- throw new Error(`Unrecognized network type: '${providerConfig.type}'`);
949
- };
950
893
  _applyNetworkSelection = new WeakSet();
951
- applyNetworkSelection_fn = function() {
952
- if (!_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClientRegistry)) {
953
- throw new Error(
954
- "initializeProvider must be called first in order to switch the network"
955
- );
956
- }
957
- const { providerConfig } = this.state;
894
+ applyNetworkSelection_fn = function(networkClientId) {
895
+ const autoManagedNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
958
896
  let autoManagedNetworkClient;
959
- let networkClientId;
960
- if (isInfuraProviderConfig(providerConfig)) {
961
- const networkClientType = "infura" /* Infura */;
962
- networkClientId = buildInfuraNetworkClientId(providerConfig);
963
- const builtInNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClientRegistry)[networkClientType];
964
- autoManagedNetworkClient = builtInNetworkClientRegistry[networkClientId];
965
- if (!autoManagedNetworkClient) {
897
+ if (_controllerutils.isInfuraNetworkType.call(void 0, networkClientId)) {
898
+ const possibleAutoManagedNetworkClient = autoManagedNetworkClientRegistry["infura" /* Infura */][networkClientId];
899
+ if (!possibleAutoManagedNetworkClient) {
966
900
  throw new Error(
967
- `Could not find custom network matching ${networkClientId}`
901
+ `Infura network client not found with ID '${networkClientId}'`
968
902
  );
969
903
  }
970
- } else if (isCustomProviderConfig(providerConfig)) {
971
- validateCustomProviderConfig(providerConfig);
972
- const networkClientType = "custom" /* Custom */;
973
- networkClientId = buildCustomNetworkClientId(
974
- providerConfig,
975
- this.state.networkConfigurations
976
- );
977
- const customNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClientRegistry)[networkClientType];
978
- autoManagedNetworkClient = customNetworkClientRegistry[networkClientId];
979
- if (!autoManagedNetworkClient) {
904
+ autoManagedNetworkClient = possibleAutoManagedNetworkClient;
905
+ } else {
906
+ const possibleAutoManagedNetworkClient = autoManagedNetworkClientRegistry["custom" /* Custom */][networkClientId];
907
+ if (!possibleAutoManagedNetworkClient) {
980
908
  throw new Error(
981
- `Could not find built-in network matching ${networkClientId}`
909
+ `Custom network client not found with ID '${networkClientId}'`
982
910
  );
983
911
  }
984
- } else {
985
- throw new Error("Could not determine type of provider config");
912
+ autoManagedNetworkClient = possibleAutoManagedNetworkClient;
986
913
  }
914
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _autoManagedNetworkClient, autoManagedNetworkClient);
987
915
  this.update((state) => {
988
916
  state.selectedNetworkClientId = networkClientId;
989
917
  if (state.networksMetadata[networkClientId] === void 0) {
@@ -993,18 +921,22 @@ applyNetworkSelection_fn = function() {
993
921
  };
994
922
  }
995
923
  });
996
- const { provider, blockTracker } = autoManagedNetworkClient;
997
924
  if (_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _providerProxy)) {
998
- _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _providerProxy).setTarget(provider);
925
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _providerProxy).setTarget(_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClient).provider);
999
926
  } else {
1000
- _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _providerProxy, _swappableobjproxy.createEventEmitterProxy.call(void 0, provider));
927
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _providerProxy, _swappableobjproxy.createEventEmitterProxy.call(void 0,
928
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClient).provider
929
+ ));
1001
930
  }
1002
931
  if (_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _blockTrackerProxy)) {
1003
- _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _blockTrackerProxy).setTarget(blockTracker);
932
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _blockTrackerProxy).setTarget(
933
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClient).blockTracker
934
+ );
1004
935
  } else {
1005
- _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _blockTrackerProxy, _swappableobjproxy.createEventEmitterProxy.call(void 0, blockTracker, {
1006
- eventFilter: "skipInternal"
1007
- }));
936
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _blockTrackerProxy, _swappableobjproxy.createEventEmitterProxy.call(void 0,
937
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClient).blockTracker,
938
+ { eventFilter: "skipInternal" }
939
+ ));
1008
940
  }
1009
941
  _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _ethQuery, new (0, _ethquery2.default)(_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _providerProxy)));
1010
942
  };
@@ -1014,4 +946,4 @@ applyNetworkSelection_fn = function() {
1014
946
 
1015
947
 
1016
948
  exports.knownKeysOf = knownKeysOf; exports.defaultState = defaultState; exports.NetworkController = NetworkController;
1017
- //# sourceMappingURL=chunk-4ZD3DTQ7.js.map
949
+ //# sourceMappingURL=chunk-WT5ZBF4X.js.map