@metamask-previews/network-controller 36.0.0-preview-3866c0ff1 → 36.0.0-preview-acb2929f3
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 +0 -1
- package/dist/NetworkController-method-action-types.cjs +7 -0
- package/dist/NetworkController-method-action-types.cjs.map +1 -0
- package/dist/{NetworkController-method-action-types.d.ts → NetworkController-method-action-types.d.cts} +2 -2
- package/dist/NetworkController-method-action-types.d.cts.map +1 -0
- package/dist/NetworkController-method-action-types.d.mts +268 -0
- package/dist/NetworkController-method-action-types.d.mts.map +1 -0
- package/dist/{NetworkController-method-action-types.js → NetworkController-method-action-types.mjs} +1 -1
- package/dist/NetworkController-method-action-types.mjs.map +1 -0
- package/dist/NetworkController.cjs +1599 -0
- package/dist/NetworkController.cjs.map +1 -0
- package/dist/{NetworkController.d.ts → NetworkController.d.cts} +29 -37
- package/dist/NetworkController.d.cts.map +1 -0
- package/dist/NetworkController.d.mts +956 -0
- package/dist/NetworkController.d.mts.map +1 -0
- package/dist/{NetworkController.js → NetworkController.mjs} +612 -716
- package/dist/NetworkController.mjs.map +1 -0
- package/dist/constants.cjs +46 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/{constants.d.ts → constants.d.cts} +1 -1
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +42 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/{constants.js → constants.mjs} +1 -1
- package/dist/constants.mjs.map +1 -0
- package/dist/create-auto-managed-network-client.cjs +149 -0
- package/dist/create-auto-managed-network-client.cjs.map +1 -0
- package/dist/{create-auto-managed-network-client.d.ts → create-auto-managed-network-client.d.cts} +7 -7
- package/dist/create-auto-managed-network-client.d.cts.map +1 -0
- package/dist/create-auto-managed-network-client.d.mts +74 -0
- package/dist/create-auto-managed-network-client.d.mts.map +1 -0
- package/dist/{create-auto-managed-network-client.js → create-auto-managed-network-client.mjs} +4 -4
- package/dist/create-auto-managed-network-client.mjs.map +1 -0
- package/dist/create-network-client.cjs +439 -0
- package/dist/create-network-client.cjs.map +1 -0
- package/dist/{create-network-client.d.ts → create-network-client.d.cts} +7 -7
- package/dist/create-network-client.d.cts.map +1 -0
- package/dist/create-network-client.d.mts +60 -0
- package/dist/create-network-client.d.mts.map +1 -0
- package/dist/{create-network-client.js → create-network-client.mjs} +14 -15
- package/dist/create-network-client.mjs.map +1 -0
- package/dist/index.cjs +31 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.ts → index.d.cts} +16 -16
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +6 -0
- package/dist/index.mjs.map +1 -0
- package/dist/logger.cjs +7 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +5 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.mts +5 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs +4 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/rpc-service/abstract-rpc-service.cjs +3 -0
- package/dist/rpc-service/abstract-rpc-service.cjs.map +1 -0
- package/dist/rpc-service/{abstract-rpc-service.d.ts → abstract-rpc-service.d.cts} +2 -2
- package/dist/rpc-service/abstract-rpc-service.d.cts.map +1 -0
- package/dist/rpc-service/abstract-rpc-service.d.mts +17 -0
- package/dist/rpc-service/abstract-rpc-service.d.mts.map +1 -0
- package/dist/rpc-service/abstract-rpc-service.mjs +2 -0
- package/dist/rpc-service/abstract-rpc-service.mjs.map +1 -0
- package/dist/rpc-service/rpc-service-chain.cjs +304 -0
- package/dist/rpc-service/rpc-service-chain.cjs.map +1 -0
- package/dist/rpc-service/{rpc-service-chain.d.ts → rpc-service-chain.d.cts} +6 -6
- package/dist/rpc-service/rpc-service-chain.d.cts.map +1 -0
- package/dist/rpc-service/rpc-service-chain.d.mts +169 -0
- package/dist/rpc-service/rpc-service-chain.d.mts.map +1 -0
- package/dist/rpc-service/{rpc-service-chain.js → rpc-service-chain.mjs} +70 -57
- package/dist/rpc-service/rpc-service-chain.mjs.map +1 -0
- package/dist/rpc-service/rpc-service-requestable.cjs +3 -0
- package/dist/rpc-service/rpc-service-requestable.cjs.map +1 -0
- package/dist/rpc-service/{rpc-service-requestable.d.ts → rpc-service-requestable.d.cts} +4 -4
- package/dist/rpc-service/rpc-service-requestable.d.cts.map +1 -0
- package/dist/rpc-service/rpc-service-requestable.d.mts +63 -0
- package/dist/rpc-service/rpc-service-requestable.d.mts.map +1 -0
- package/dist/rpc-service/rpc-service-requestable.mjs +2 -0
- package/dist/rpc-service/rpc-service-requestable.mjs.map +1 -0
- package/dist/rpc-service/rpc-service.cjs +540 -0
- package/dist/rpc-service/rpc-service.cjs.map +1 -0
- package/dist/rpc-service/{rpc-service.d.ts → rpc-service.d.cts} +6 -6
- package/dist/rpc-service/rpc-service.d.cts.map +1 -0
- package/dist/rpc-service/rpc-service.d.mts +247 -0
- package/dist/rpc-service/rpc-service.d.mts.map +1 -0
- package/dist/rpc-service/{rpc-service.js → rpc-service.mjs} +197 -206
- package/dist/rpc-service/rpc-service.mjs.map +1 -0
- package/dist/rpc-service/shared.cjs +3 -0
- package/dist/rpc-service/shared.cjs.map +1 -0
- package/dist/rpc-service/{shared.d.ts → shared.d.cts} +2 -2
- package/dist/rpc-service/shared.d.cts.map +1 -0
- package/dist/rpc-service/shared.d.mts +32 -0
- package/dist/rpc-service/shared.d.mts.map +1 -0
- package/dist/rpc-service/shared.mjs +2 -0
- package/dist/rpc-service/shared.mjs.map +1 -0
- package/dist/rpc-service-analytics.cjs +180 -0
- package/dist/rpc-service-analytics.cjs.map +1 -0
- package/dist/{rpc-service-analytics.d.ts → rpc-service-analytics.d.cts} +3 -3
- package/dist/rpc-service-analytics.d.cts.map +1 -0
- package/dist/rpc-service-analytics.d.mts +85 -0
- package/dist/rpc-service-analytics.d.mts.map +1 -0
- package/dist/{rpc-service-analytics.js → rpc-service-analytics.mjs} +4 -4
- package/dist/rpc-service-analytics.mjs.map +1 -0
- package/dist/selectors.cjs +16 -0
- package/dist/selectors.cjs.map +1 -0
- package/dist/{selectors.d.ts → selectors.d.cts} +2 -2
- package/dist/selectors.d.cts.map +1 -0
- package/dist/selectors.d.mts +22 -0
- package/dist/selectors.d.mts.map +1 -0
- package/dist/{selectors.js → selectors.mjs} +1 -1
- package/dist/selectors.mjs.map +1 -0
- package/dist/types.cjs +12 -0
- package/dist/types.cjs.map +1 -0
- package/dist/{types.d.ts → types.d.cts} +5 -5
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +49 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/{types.js → types.mjs} +1 -1
- package/dist/types.mjs.map +1 -0
- package/package.json +18 -14
- package/dist/NetworkController-method-action-types.d.ts.map +0 -1
- package/dist/NetworkController-method-action-types.js.map +0 -1
- package/dist/NetworkController.d.ts.map +0 -1
- package/dist/NetworkController.js.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/create-auto-managed-network-client.d.ts.map +0 -1
- package/dist/create-auto-managed-network-client.js.map +0 -1
- package/dist/create-network-client.d.ts.map +0 -1
- package/dist/create-network-client.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -6
- package/dist/index.js.map +0 -1
- package/dist/logger.d.ts +0 -4
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -4
- package/dist/logger.js.map +0 -1
- package/dist/rpc-service/abstract-rpc-service.d.ts.map +0 -1
- package/dist/rpc-service/abstract-rpc-service.js +0 -2
- package/dist/rpc-service/abstract-rpc-service.js.map +0 -1
- package/dist/rpc-service/rpc-service-chain.d.ts.map +0 -1
- package/dist/rpc-service/rpc-service-chain.js.map +0 -1
- package/dist/rpc-service/rpc-service-requestable.d.ts.map +0 -1
- package/dist/rpc-service/rpc-service-requestable.js +0 -2
- package/dist/rpc-service/rpc-service-requestable.js.map +0 -1
- package/dist/rpc-service/rpc-service.d.ts.map +0 -1
- package/dist/rpc-service/rpc-service.js.map +0 -1
- package/dist/rpc-service/shared.d.ts.map +0 -1
- package/dist/rpc-service/shared.js +0 -2
- package/dist/rpc-service/shared.js.map +0 -1
- package/dist/rpc-service-analytics.d.ts.map +0 -1
- package/dist/rpc-service-analytics.js.map +0 -1
- package/dist/selectors.d.ts.map +0 -1
- package/dist/selectors.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -1,22 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
import {
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _NetworkController_instances, _NetworkController_ethQuery, _NetworkController_infuraProjectId, _NetworkController_failoverUrls, _NetworkController_previouslySelectedNetworkClientId, _NetworkController_providerProxy, _NetworkController_blockTrackerProxy, _NetworkController_autoManagedNetworkClientRegistry, _NetworkController_autoManagedNetworkClient, _NetworkController_log, _NetworkController_getRpcServiceOptions, _NetworkController_getBlockTrackerOptions, _NetworkController_analyticsOptions, _NetworkController_networkConfigurationsByNetworkClientId, _NetworkController_rpcFailoverMode, _NetworkController_updateRpcFailover, _NetworkController_refreshNetwork, _NetworkController_determineNetworkMetadata, _NetworkController_lookupGivenNetwork, _NetworkController_lookupSelectedNetwork, _NetworkController_updateMetadataForNetwork, _NetworkController_getLatestBlock, _NetworkController_determineEIP1559Compatibility, _NetworkController_validateNetworkFields, _NetworkController_determineNetworkConfigurationToPersist, _NetworkController_registerNetworkClientsAsNeeded, _NetworkController_unregisterNetworkClientsAsNeeded, _NetworkController_updateNetworkConfigurations, _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated, _NetworkController_createAutoManagedNetworkClientRegistry, _NetworkController_applyNetworkSelection, _NetworkController_autoAddNetworksFromConfigRegistry;
|
|
13
|
+
function $importDefault(module) {
|
|
14
|
+
if (module?.__esModule) {
|
|
15
|
+
return module.default;
|
|
16
|
+
}
|
|
17
|
+
return module;
|
|
18
|
+
}
|
|
19
|
+
import { BaseController } from "@metamask/base-controller";
|
|
20
|
+
import { selectEvmAutoEnabledNetworksChainIds } from "@metamask/config-registry-controller";
|
|
21
|
+
import { InfuraNetworkType, CustomNetworkType, NetworkType, isSafeChainId, isInfuraNetworkType, ChainId, NetworksTicker, NetworkNickname, BUILT_IN_CUSTOM_NETWORKS_RPC, BUILT_IN_NETWORKS, DEFAULT_INFURA_NETWORKS } from "@metamask/controller-utils";
|
|
22
|
+
import $EthQuery from "@metamask/eth-query";
|
|
23
|
+
const EthQuery = $importDefault($EthQuery);
|
|
24
|
+
import { errorCodes } from "@metamask/rpc-errors";
|
|
25
|
+
import { createEventEmitterProxy, createSwappableProxy } from "@metamask/swappable-obj-proxy";
|
|
26
|
+
import { hasProperty, isPlainObject, isStrictHexString, numberToHex, parseCaipChainId } from "@metamask/utils";
|
|
27
|
+
import $deepEqual from "fast-deep-equal";
|
|
28
|
+
const deepEqual = $importDefault($deepEqual);
|
|
29
|
+
import { produce } from "immer";
|
|
30
|
+
import $lodash from "lodash";
|
|
31
|
+
const { cloneDeep } = $lodash;
|
|
32
|
+
import { createSelector } from "reselect";
|
|
33
|
+
import * as URI from "uri-js";
|
|
34
|
+
import { v4 as uuidV4 } from "uuid";
|
|
35
|
+
import { DEPRECATED_NETWORKS, INFURA_BLOCKED_KEY, NetworkStatus } from "./constants.mjs";
|
|
36
|
+
import { createAutoManagedNetworkClient } from "./create-auto-managed-network-client.mjs";
|
|
37
|
+
import { projectLogger, createModuleLogger } from "./logger.mjs";
|
|
38
|
+
import { trackRpcServiceDegraded, trackRpcServiceUnavailable } from "./rpc-service-analytics.mjs";
|
|
39
|
+
import { getRpcFailoverMode } from "./selectors.mjs";
|
|
40
|
+
import { NetworkClientType } from "./types.mjs";
|
|
20
41
|
const debugLog = createModuleLogger(projectLogger, 'NetworkController');
|
|
21
42
|
const INFURA_URL_REGEX = /^https:\/\/(?<networkName>[^.]+)\.infura\.io\/v\d+\/(?<apiKey>.+)$/u;
|
|
22
43
|
/**
|
|
@@ -338,20 +359,6 @@ function buildNetworkConfigurationsByNetworkClientId(networkConfigurationsByChai
|
|
|
338
359
|
* Controller that creates and manages an Ethereum network provider.
|
|
339
360
|
*/
|
|
340
361
|
export class NetworkController extends BaseController {
|
|
341
|
-
#ethQuery;
|
|
342
|
-
#infuraProjectId;
|
|
343
|
-
#failoverUrls;
|
|
344
|
-
#previouslySelectedNetworkClientId;
|
|
345
|
-
#providerProxy;
|
|
346
|
-
#blockTrackerProxy;
|
|
347
|
-
#autoManagedNetworkClientRegistry;
|
|
348
|
-
#autoManagedNetworkClient;
|
|
349
|
-
#log;
|
|
350
|
-
#getRpcServiceOptions;
|
|
351
|
-
#getBlockTrackerOptions;
|
|
352
|
-
#analyticsOptions;
|
|
353
|
-
#networkConfigurationsByNetworkClientId;
|
|
354
|
-
#rpcFailoverMode = 'disabled';
|
|
355
362
|
/**
|
|
356
363
|
* Constructs a NetworkController.
|
|
357
364
|
*
|
|
@@ -393,44 +400,57 @@ export class NetworkController extends BaseController {
|
|
|
393
400
|
messenger,
|
|
394
401
|
state: correctedInitialState,
|
|
395
402
|
});
|
|
396
|
-
this
|
|
397
|
-
this
|
|
398
|
-
this
|
|
399
|
-
this
|
|
400
|
-
this
|
|
401
|
-
this
|
|
403
|
+
_NetworkController_instances.add(this);
|
|
404
|
+
_NetworkController_ethQuery.set(this, void 0);
|
|
405
|
+
_NetworkController_infuraProjectId.set(this, void 0);
|
|
406
|
+
_NetworkController_failoverUrls.set(this, void 0);
|
|
407
|
+
_NetworkController_previouslySelectedNetworkClientId.set(this, void 0);
|
|
408
|
+
_NetworkController_providerProxy.set(this, void 0);
|
|
409
|
+
_NetworkController_blockTrackerProxy.set(this, void 0);
|
|
410
|
+
_NetworkController_autoManagedNetworkClientRegistry.set(this, void 0);
|
|
411
|
+
_NetworkController_autoManagedNetworkClient.set(this, void 0);
|
|
412
|
+
_NetworkController_log.set(this, void 0);
|
|
413
|
+
_NetworkController_getRpcServiceOptions.set(this, void 0);
|
|
414
|
+
_NetworkController_getBlockTrackerOptions.set(this, void 0);
|
|
415
|
+
_NetworkController_analyticsOptions.set(this, void 0);
|
|
416
|
+
_NetworkController_networkConfigurationsByNetworkClientId.set(this, void 0);
|
|
417
|
+
_NetworkController_rpcFailoverMode.set(this, 'disabled');
|
|
418
|
+
__classPrivateFieldSet(this, _NetworkController_infuraProjectId, infuraProjectId, "f");
|
|
419
|
+
__classPrivateFieldSet(this, _NetworkController_failoverUrls, failoverUrls, "f");
|
|
420
|
+
__classPrivateFieldSet(this, _NetworkController_log, log, "f");
|
|
421
|
+
__classPrivateFieldSet(this, _NetworkController_getRpcServiceOptions, getRpcServiceOptions, "f");
|
|
422
|
+
__classPrivateFieldSet(this, _NetworkController_getBlockTrackerOptions, getBlockTrackerOptions, "f");
|
|
423
|
+
__classPrivateFieldSet(this, _NetworkController_analyticsOptions, {
|
|
402
424
|
isRpcEndpointUrlPublic: () => false,
|
|
403
425
|
rpcServiceEventsSampleRate: 0,
|
|
404
426
|
...analyticsOptions,
|
|
405
|
-
};
|
|
406
|
-
this
|
|
407
|
-
|
|
408
|
-
this.#networkConfigurationsByNetworkClientId =
|
|
409
|
-
buildNetworkConfigurationsByNetworkClientId(this.state.networkConfigurationsByChainId);
|
|
427
|
+
}, "f");
|
|
428
|
+
__classPrivateFieldSet(this, _NetworkController_previouslySelectedNetworkClientId, this.state.selectedNetworkClientId, "f");
|
|
429
|
+
__classPrivateFieldSet(this, _NetworkController_networkConfigurationsByNetworkClientId, buildNetworkConfigurationsByNetworkClientId(this.state.networkConfigurationsByChainId), "f");
|
|
410
430
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
411
431
|
this.messenger.subscribe(`${this.name}:rpcEndpointChainUnavailable`, ({ networkClientId }) => {
|
|
412
|
-
this
|
|
432
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateMetadataForNetwork).call(this, networkClientId, {
|
|
413
433
|
networkStatus: NetworkStatus.Unavailable,
|
|
414
434
|
});
|
|
415
435
|
});
|
|
416
436
|
this.messenger.subscribe(`${this.name}:rpcEndpointChainDegraded`, ({ networkClientId }) => {
|
|
417
|
-
this
|
|
437
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateMetadataForNetwork).call(this, networkClientId, {
|
|
418
438
|
networkStatus: NetworkStatus.Degraded,
|
|
419
439
|
});
|
|
420
440
|
});
|
|
421
441
|
this.messenger.subscribe(`${this.name}:rpcEndpointChainAvailable`, ({ networkClientId }) => {
|
|
422
|
-
this
|
|
442
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateMetadataForNetwork).call(this, networkClientId, {
|
|
423
443
|
networkStatus: NetworkStatus.Available,
|
|
424
444
|
});
|
|
425
445
|
});
|
|
426
|
-
this.messenger.subscribe(`${this.name}:rpcEndpointUnavailable`, (payload) => trackRpcServiceUnavailable(this.messenger, this
|
|
427
|
-
this.messenger.subscribe(`${this.name}:rpcEndpointDegraded`, (payload) => trackRpcServiceDegraded(this.messenger, this
|
|
446
|
+
this.messenger.subscribe(`${this.name}:rpcEndpointUnavailable`, (payload) => trackRpcServiceUnavailable(this.messenger, __classPrivateFieldGet(this, _NetworkController_analyticsOptions, "f"), payload));
|
|
447
|
+
this.messenger.subscribe(`${this.name}:rpcEndpointDegraded`, (payload) => trackRpcServiceDegraded(this.messenger, __classPrivateFieldGet(this, _NetworkController_analyticsOptions, "f"), payload));
|
|
428
448
|
this.messenger.subscribe(
|
|
429
449
|
// eslint-disable-next-line no-restricted-syntax
|
|
430
450
|
'RemoteFeatureFlagController:stateChange', (rpcFailoverMode) => {
|
|
431
|
-
this
|
|
451
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateRpcFailover).call(this, rpcFailoverMode);
|
|
432
452
|
}, getRpcFailoverMode);
|
|
433
|
-
this.messenger.subscribe('ConfigRegistryController:stateChanged', (caipChainIds) => this
|
|
453
|
+
this.messenger.subscribe('ConfigRegistryController:stateChanged', (caipChainIds) => __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_autoAddNetworksFromConfigRegistry).call(this, caipChainIds), selectEvmAutoEnabledNetworksChainIds);
|
|
434
454
|
}
|
|
435
455
|
/**
|
|
436
456
|
* Returns the EthQuery instance for the currently selected network.
|
|
@@ -439,31 +459,7 @@ export class NetworkController extends BaseController {
|
|
|
439
459
|
* initialized.
|
|
440
460
|
*/
|
|
441
461
|
getEthQuery() {
|
|
442
|
-
return this
|
|
443
|
-
}
|
|
444
|
-
/**
|
|
445
|
-
* Applies the given RPC failover mode by reconstructing all network clients
|
|
446
|
-
* that were configured with failover URLs so that the new mode takes effect.
|
|
447
|
-
* Network client IDs are preserved so as not to invalidate state in other
|
|
448
|
-
* controllers.
|
|
449
|
-
*
|
|
450
|
-
* @param newMode - The RPC failover mode to apply.
|
|
451
|
-
*/
|
|
452
|
-
#updateRpcFailover(newMode) {
|
|
453
|
-
if (this.#rpcFailoverMode === newMode) {
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
const autoManagedNetworkClientRegistry = this.#ensureAutoManagedNetworkClientRegistryPopulated();
|
|
457
|
-
for (const networkClientsById of Object.values(autoManagedNetworkClientRegistry)) {
|
|
458
|
-
for (const networkClientId of Object.keys(networkClientsById)) {
|
|
459
|
-
const networkClient = networkClientsById[networkClientId];
|
|
460
|
-
if (networkClient.configuration.failoverRpcUrls &&
|
|
461
|
-
networkClient.configuration.failoverRpcUrls.length > 0) {
|
|
462
|
-
networkClient.setRpcFailoverMode(newMode);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
this.#rpcFailoverMode = newMode;
|
|
462
|
+
return __classPrivateFieldGet(this, _NetworkController_ethQuery, "f");
|
|
467
463
|
}
|
|
468
464
|
/**
|
|
469
465
|
* Accesses the provider and block tracker for the currently selected network.
|
|
@@ -473,8 +469,8 @@ export class NetworkController extends BaseController {
|
|
|
473
469
|
*/
|
|
474
470
|
getProviderAndBlockTracker() {
|
|
475
471
|
return {
|
|
476
|
-
provider: this
|
|
477
|
-
blockTracker: this
|
|
472
|
+
provider: __classPrivateFieldGet(this, _NetworkController_providerProxy, "f"),
|
|
473
|
+
blockTracker: __classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f"),
|
|
478
474
|
};
|
|
479
475
|
}
|
|
480
476
|
/**
|
|
@@ -483,10 +479,10 @@ export class NetworkController extends BaseController {
|
|
|
483
479
|
* @returns an object with the provider and block tracker proxies for the currently selected network.
|
|
484
480
|
*/
|
|
485
481
|
getSelectedNetworkClient() {
|
|
486
|
-
if (this
|
|
482
|
+
if (__classPrivateFieldGet(this, _NetworkController_providerProxy, "f") && __classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f")) {
|
|
487
483
|
return {
|
|
488
|
-
provider: this
|
|
489
|
-
blockTracker: this
|
|
484
|
+
provider: __classPrivateFieldGet(this, _NetworkController_providerProxy, "f"),
|
|
485
|
+
blockTracker: __classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f"),
|
|
490
486
|
};
|
|
491
487
|
}
|
|
492
488
|
return undefined;
|
|
@@ -510,14 +506,14 @@ export class NetworkController extends BaseController {
|
|
|
510
506
|
* @returns The network clients registered so far, keyed by ID.
|
|
511
507
|
*/
|
|
512
508
|
getNetworkClientRegistry() {
|
|
513
|
-
const autoManagedNetworkClientRegistry = this
|
|
509
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
514
510
|
return Object.assign({}, autoManagedNetworkClientRegistry[NetworkClientType.Infura], autoManagedNetworkClientRegistry[NetworkClientType.Custom]);
|
|
515
511
|
}
|
|
516
512
|
getNetworkClientById(networkClientId) {
|
|
517
513
|
if (!networkClientId) {
|
|
518
514
|
throw new Error('No network client ID was provided.');
|
|
519
515
|
}
|
|
520
|
-
const autoManagedNetworkClientRegistry = this
|
|
516
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
521
517
|
const infuraNetworkClient = autoManagedNetworkClientRegistry[NetworkClientType.Infura][networkClientId];
|
|
522
518
|
if (infuraNetworkClient) {
|
|
523
519
|
return infuraNetworkClient;
|
|
@@ -528,30 +524,6 @@ export class NetworkController extends BaseController {
|
|
|
528
524
|
}
|
|
529
525
|
return customNetworkClient;
|
|
530
526
|
}
|
|
531
|
-
/**
|
|
532
|
-
* Executes a series of steps to switch the network:
|
|
533
|
-
*
|
|
534
|
-
* 1. Notifies subscribers via the messenger that the network is about to be
|
|
535
|
-
* switched (and, really, that the global provider and block tracker proxies
|
|
536
|
-
* will be re-pointed to a new network).
|
|
537
|
-
* 2. Looks up a known and preinitialized network client matching the given
|
|
538
|
-
* ID and uses it to re-point the aforementioned provider and block tracker
|
|
539
|
-
* proxies.
|
|
540
|
-
* 3. Notifies subscribers via the messenger that the network has switched.
|
|
541
|
-
* 4. Captures metadata for the newly switched network in state.
|
|
542
|
-
*
|
|
543
|
-
* @param networkClientId - The ID of a network client that requests will be
|
|
544
|
-
* routed through (either the name of an Infura network or the ID of a custom
|
|
545
|
-
* network configuration).
|
|
546
|
-
* @param options - Options for this method.
|
|
547
|
-
* @param options.updateState - Allows for updating state.
|
|
548
|
-
*/
|
|
549
|
-
async #refreshNetwork(networkClientId, options = {}) {
|
|
550
|
-
this.messenger.publish('NetworkController:networkWillChange', this.state);
|
|
551
|
-
this.#applyNetworkSelection(networkClientId, options);
|
|
552
|
-
this.messenger.publish('NetworkController:networkDidChange', this.state);
|
|
553
|
-
await this.lookupNetwork();
|
|
554
|
-
}
|
|
555
527
|
/**
|
|
556
528
|
* Initialize the NetworkController:
|
|
557
529
|
* - Apply the RPC failover mode from the `corePlatformRpcFailoverMode` remote feature flag;
|
|
@@ -560,68 +532,9 @@ export class NetworkController extends BaseController {
|
|
|
560
532
|
*/
|
|
561
533
|
init() {
|
|
562
534
|
const state = this.messenger.call('RemoteFeatureFlagController:getState');
|
|
563
|
-
this
|
|
564
|
-
this
|
|
565
|
-
this
|
|
566
|
-
}
|
|
567
|
-
/**
|
|
568
|
-
* Uses a request for the latest block to gather the following information on
|
|
569
|
-
* the given network:
|
|
570
|
-
*
|
|
571
|
-
* - The connectivity status: whether it is available, geo-blocked (Infura
|
|
572
|
-
* only), unavailable, or unknown
|
|
573
|
-
* - The capabilities status: whether it supports EIP-1559, whether it does
|
|
574
|
-
* not, or whether it is unknown
|
|
575
|
-
*
|
|
576
|
-
* @param networkClientId - The ID of the network client to inspect.
|
|
577
|
-
* If no ID is provided, uses the currently selected network.
|
|
578
|
-
* @returns The resulting metadata for the network.
|
|
579
|
-
*/
|
|
580
|
-
async #determineNetworkMetadata(networkClientId) {
|
|
581
|
-
const networkClient = this.getNetworkClientById(networkClientId);
|
|
582
|
-
const isInfura = networkClient.configuration.type === NetworkClientType.Infura;
|
|
583
|
-
let networkStatus;
|
|
584
|
-
let isEIP1559Compatible;
|
|
585
|
-
try {
|
|
586
|
-
isEIP1559Compatible =
|
|
587
|
-
await this.#determineEIP1559Compatibility(networkClientId);
|
|
588
|
-
networkStatus = NetworkStatus.Available;
|
|
589
|
-
}
|
|
590
|
-
catch (error) {
|
|
591
|
-
debugLog('NetworkController: lookupNetwork: ', error);
|
|
592
|
-
if (isErrorWithCode(error)) {
|
|
593
|
-
let responseBody;
|
|
594
|
-
if (isInfura &&
|
|
595
|
-
hasProperty(error, 'message') &&
|
|
596
|
-
typeof error.message === 'string') {
|
|
597
|
-
try {
|
|
598
|
-
responseBody = JSON.parse(error.message);
|
|
599
|
-
}
|
|
600
|
-
catch {
|
|
601
|
-
// error.message must not be JSON
|
|
602
|
-
this.#log?.warn('NetworkController: lookupNetwork: json parse error: ', error);
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
if (isPlainObject(responseBody) &&
|
|
606
|
-
responseBody.error === INFURA_BLOCKED_KEY) {
|
|
607
|
-
networkStatus = NetworkStatus.Blocked;
|
|
608
|
-
}
|
|
609
|
-
else if (error.code === errorCodes.rpc.internal) {
|
|
610
|
-
networkStatus = NetworkStatus.Unknown;
|
|
611
|
-
this.#log?.warn('NetworkController: lookupNetwork: rpc internal error: ', error);
|
|
612
|
-
}
|
|
613
|
-
else {
|
|
614
|
-
networkStatus = NetworkStatus.Unavailable;
|
|
615
|
-
this.#log?.warn('NetworkController: lookupNetwork: ', error);
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
else {
|
|
619
|
-
debugLog('NetworkController - could not determine network status', error);
|
|
620
|
-
networkStatus = NetworkStatus.Unknown;
|
|
621
|
-
this.#log?.warn('NetworkController: lookupNetwork: ', error);
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
return { isInfura, networkStatus, isEIP1559Compatible };
|
|
535
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateRpcFailover).call(this, getRpcFailoverMode(state));
|
|
536
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_applyNetworkSelection).call(this, this.state.selectedNetworkClientId);
|
|
537
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_autoAddNetworksFromConfigRegistry).call(this, selectEvmAutoEnabledNetworksChainIds(this.messenger.call('ConfigRegistryController:getState')));
|
|
625
538
|
}
|
|
626
539
|
/**
|
|
627
540
|
* Uses a request for the latest block to gather the following information on
|
|
@@ -637,10 +550,10 @@ export class NetworkController extends BaseController {
|
|
|
637
550
|
*/
|
|
638
551
|
async lookupNetwork(networkClientId) {
|
|
639
552
|
if (networkClientId) {
|
|
640
|
-
await this
|
|
553
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_lookupGivenNetwork).call(this, networkClientId);
|
|
641
554
|
}
|
|
642
555
|
else {
|
|
643
|
-
await this
|
|
556
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_lookupSelectedNetwork).call(this);
|
|
644
557
|
}
|
|
645
558
|
}
|
|
646
559
|
/**
|
|
@@ -660,129 +573,7 @@ export class NetworkController extends BaseController {
|
|
|
660
573
|
// right now.
|
|
661
574
|
/* istanbul ignore next */
|
|
662
575
|
async lookupNetworkByClientId(networkClientId) {
|
|
663
|
-
await this
|
|
664
|
-
}
|
|
665
|
-
/**
|
|
666
|
-
* Uses a request for the latest block to gather the following information on
|
|
667
|
-
* the given network, persisting it to state:
|
|
668
|
-
*
|
|
669
|
-
* - The connectivity status: whether the network is available, geo-blocked
|
|
670
|
-
* (Infura only), unavailable, or unknown
|
|
671
|
-
* - The feature compatibility status: whether the network supports EIP-1559,
|
|
672
|
-
* whether it does not, or whether it is unknown
|
|
673
|
-
*
|
|
674
|
-
* @param networkClientId - The ID of the network client to inspect.
|
|
675
|
-
*/
|
|
676
|
-
async #lookupGivenNetwork(networkClientId) {
|
|
677
|
-
const { networkStatus, isEIP1559Compatible } = await this.#determineNetworkMetadata(networkClientId);
|
|
678
|
-
this.#updateMetadataForNetwork(networkClientId, {
|
|
679
|
-
networkStatus,
|
|
680
|
-
isEIP1559Compatible,
|
|
681
|
-
});
|
|
682
|
-
}
|
|
683
|
-
/**
|
|
684
|
-
* Uses a request for the latest block to gather the following information on
|
|
685
|
-
* the currently selected network, persisting it to state:
|
|
686
|
-
*
|
|
687
|
-
* - The connectivity status: whether the network is available, geo-blocked
|
|
688
|
-
* (Infura only), unavailable, or unknown
|
|
689
|
-
* - The feature compatibility status: whether the network supports EIP-1559,
|
|
690
|
-
* whether it does not, or whether it is unknown
|
|
691
|
-
*
|
|
692
|
-
* Note that it is possible for the current network to be switched while this
|
|
693
|
-
* method is running. If that is the case, it will exit early (as this method
|
|
694
|
-
* will also run for the new network).
|
|
695
|
-
*/
|
|
696
|
-
async #lookupSelectedNetwork() {
|
|
697
|
-
if (!this.#ethQuery) {
|
|
698
|
-
return;
|
|
699
|
-
}
|
|
700
|
-
let networkChanged = false;
|
|
701
|
-
const listener = () => {
|
|
702
|
-
networkChanged = true;
|
|
703
|
-
try {
|
|
704
|
-
this.messenger.unsubscribe('NetworkController:networkDidChange', listener);
|
|
705
|
-
}
|
|
706
|
-
catch (error) {
|
|
707
|
-
// In theory, this `catch` should not be necessary given that this error
|
|
708
|
-
// would occur "inside" of the call to `#determineEIP1559Compatibility`
|
|
709
|
-
// below and so it should be caught by the `try`/`catch` below (it is
|
|
710
|
-
// impossible to reproduce in tests for that reason). However, somehow
|
|
711
|
-
// it occurs within Mobile and so we have to add our own `try`/`catch`
|
|
712
|
-
// here.
|
|
713
|
-
/* istanbul ignore next */
|
|
714
|
-
if (!(error instanceof Error) ||
|
|
715
|
-
error.message !==
|
|
716
|
-
'Subscription not found for event: NetworkController:networkDidChange') {
|
|
717
|
-
// Again, this error should not happen and is impossible to reproduce
|
|
718
|
-
// in tests.
|
|
719
|
-
/* istanbul ignore next */
|
|
720
|
-
throw error;
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
this.messenger.subscribe('NetworkController:networkDidChange', listener);
|
|
725
|
-
const { isInfura, networkStatus, isEIP1559Compatible } = await this.#determineNetworkMetadata(this.state.selectedNetworkClientId);
|
|
726
|
-
if (networkChanged) {
|
|
727
|
-
// If the network has changed, then `lookupNetwork` either has been or is
|
|
728
|
-
// in the process of being called, so we don't need to go further.
|
|
729
|
-
return;
|
|
730
|
-
}
|
|
731
|
-
try {
|
|
732
|
-
this.messenger.unsubscribe('NetworkController:networkDidChange', listener);
|
|
733
|
-
}
|
|
734
|
-
catch (error) {
|
|
735
|
-
if (!(error instanceof Error) ||
|
|
736
|
-
error.message !==
|
|
737
|
-
'Subscription not found for event: NetworkController:networkDidChange') {
|
|
738
|
-
throw error;
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
this.#updateMetadataForNetwork(this.state.selectedNetworkClientId, {
|
|
742
|
-
networkStatus,
|
|
743
|
-
isEIP1559Compatible,
|
|
744
|
-
});
|
|
745
|
-
if (isInfura) {
|
|
746
|
-
if (networkStatus === NetworkStatus.Available) {
|
|
747
|
-
this.messenger.publish('NetworkController:infuraIsUnblocked');
|
|
748
|
-
}
|
|
749
|
-
else if (networkStatus === NetworkStatus.Blocked) {
|
|
750
|
-
this.messenger.publish('NetworkController:infuraIsBlocked');
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
else {
|
|
754
|
-
// Always publish infuraIsUnblocked regardless of network status to
|
|
755
|
-
// prevent consumers from being stuck in a blocked state if they were
|
|
756
|
-
// previously connected to an Infura network that was blocked
|
|
757
|
-
this.messenger.publish('NetworkController:infuraIsUnblocked');
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
/**
|
|
761
|
-
* Updates the metadata for the given network in state.
|
|
762
|
-
*
|
|
763
|
-
* @param networkClientId - The associated network client ID.
|
|
764
|
-
* @param metadata - The metadata to store in state.
|
|
765
|
-
* @param metadata.networkStatus - The network status to store in state.
|
|
766
|
-
* @param metadata.isEIP1559Compatible - The EIP-1559 compatibility status to
|
|
767
|
-
* store in state.
|
|
768
|
-
*/
|
|
769
|
-
#updateMetadataForNetwork(networkClientId, metadata) {
|
|
770
|
-
this.update((state) => {
|
|
771
|
-
state.networksMetadata[networkClientId] ??= {
|
|
772
|
-
status: NetworkStatus.Unknown,
|
|
773
|
-
EIPS: {},
|
|
774
|
-
};
|
|
775
|
-
const newMetadata = state.networksMetadata[networkClientId];
|
|
776
|
-
newMetadata.status = metadata.networkStatus;
|
|
777
|
-
if ('isEIP1559Compatible' in metadata) {
|
|
778
|
-
if (metadata.isEIP1559Compatible === undefined) {
|
|
779
|
-
delete newMetadata.EIPS[1559];
|
|
780
|
-
}
|
|
781
|
-
else {
|
|
782
|
-
newMetadata.EIPS[1559] = metadata.isEIP1559Compatible;
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
});
|
|
576
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_lookupGivenNetwork).call(this, networkClientId);
|
|
786
577
|
}
|
|
787
578
|
/**
|
|
788
579
|
* Convenience method to update provider network type settings.
|
|
@@ -811,33 +602,8 @@ export class NetworkController extends BaseController {
|
|
|
811
602
|
* network client ID.
|
|
812
603
|
*/
|
|
813
604
|
async setActiveNetwork(networkClientId, options = {}) {
|
|
814
|
-
this
|
|
815
|
-
|
|
816
|
-
await this.#refreshNetwork(networkClientId, options);
|
|
817
|
-
}
|
|
818
|
-
/**
|
|
819
|
-
* Fetches the latest block for the network.
|
|
820
|
-
*
|
|
821
|
-
* @param networkClientId - The networkClientId to fetch the correct provider against which to check the latest block. Defaults to the selectedNetworkClientId.
|
|
822
|
-
* @returns A promise that either resolves to the block header or null if
|
|
823
|
-
* there is no latest block, or rejects with an error.
|
|
824
|
-
*/
|
|
825
|
-
#getLatestBlock(networkClientId = this.state.selectedNetworkClientId) {
|
|
826
|
-
const networkClient = this.getNetworkClientById(networkClientId);
|
|
827
|
-
const ethQuery = new EthQuery(networkClient.provider);
|
|
828
|
-
return new Promise((resolve, reject) => {
|
|
829
|
-
ethQuery.sendAsync({ method: 'eth_getBlockByNumber', params: ['latest', false] }, (error, block) => {
|
|
830
|
-
if (error) {
|
|
831
|
-
// This error comes from JsonRpcEngine, we don't control it.
|
|
832
|
-
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
833
|
-
reject(error);
|
|
834
|
-
}
|
|
835
|
-
else {
|
|
836
|
-
// TODO: Validate this type
|
|
837
|
-
resolve(block);
|
|
838
|
-
}
|
|
839
|
-
});
|
|
840
|
-
});
|
|
605
|
+
__classPrivateFieldSet(this, _NetworkController_previouslySelectedNetworkClientId, this.state.selectedNetworkClientId, "f");
|
|
606
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this, networkClientId, options);
|
|
841
607
|
}
|
|
842
608
|
/**
|
|
843
609
|
* Determines whether the network supports EIP-1559 by checking whether the
|
|
@@ -852,14 +618,14 @@ export class NetworkController extends BaseController {
|
|
|
852
618
|
if (networkClientId) {
|
|
853
619
|
return this.get1559CompatibilityWithNetworkClientId(networkClientId);
|
|
854
620
|
}
|
|
855
|
-
if (!this
|
|
621
|
+
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
856
622
|
return false;
|
|
857
623
|
}
|
|
858
624
|
const { EIPS } = this.state.networksMetadata[this.state.selectedNetworkClientId];
|
|
859
625
|
if (EIPS[1559] !== undefined) {
|
|
860
626
|
return EIPS[1559];
|
|
861
627
|
}
|
|
862
|
-
const isEIP1559Compatible = await this
|
|
628
|
+
const isEIP1559Compatible = await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, this.state.selectedNetworkClientId);
|
|
863
629
|
this.update((state) => {
|
|
864
630
|
if (isEIP1559Compatible !== undefined) {
|
|
865
631
|
state.networksMetadata[state.selectedNetworkClientId].EIPS[1559] =
|
|
@@ -878,28 +644,12 @@ export class NetworkController extends BaseController {
|
|
|
878
644
|
// may want to include some 'freshness' value - something to make sure we refetch this from time to time
|
|
879
645
|
return EIPS[1559];
|
|
880
646
|
}
|
|
881
|
-
/**
|
|
882
|
-
* Retrieves and checks the latest block from the currently selected
|
|
883
|
-
* network; if the block has a `baseFeePerGas` property, then we know
|
|
884
|
-
* that the network supports EIP-1559; otherwise it doesn't.
|
|
885
|
-
*
|
|
886
|
-
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility
|
|
887
|
-
* @returns A promise that resolves to `true` if the network supports EIP-1559,
|
|
888
|
-
* `false` otherwise, or `undefined` if unable to retrieve the last block.
|
|
889
|
-
*/
|
|
890
|
-
async #determineEIP1559Compatibility(networkClientId) {
|
|
891
|
-
const latestBlock = await this.#getLatestBlock(networkClientId);
|
|
892
|
-
if (!latestBlock) {
|
|
893
|
-
return undefined;
|
|
894
|
-
}
|
|
895
|
-
return latestBlock.baseFeePerGas !== undefined;
|
|
896
|
-
}
|
|
897
647
|
/**
|
|
898
648
|
* Ensures that the provider and block tracker proxies are pointed to the
|
|
899
649
|
* currently selected network and refreshes the metadata for the
|
|
900
650
|
*/
|
|
901
651
|
async resetConnection() {
|
|
902
|
-
await this
|
|
652
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this, this.state.selectedNetworkClientId);
|
|
903
653
|
}
|
|
904
654
|
/**
|
|
905
655
|
* Returns the network configuration that has been filed under the given chain
|
|
@@ -919,7 +669,7 @@ export class NetworkController extends BaseController {
|
|
|
919
669
|
* @returns The network configuration if one exists, or undefined.
|
|
920
670
|
*/
|
|
921
671
|
getNetworkConfigurationByNetworkClientId(networkClientId) {
|
|
922
|
-
return this
|
|
672
|
+
return __classPrivateFieldGet(this, _NetworkController_networkConfigurationsByNetworkClientId, "f").get(networkClientId);
|
|
923
673
|
}
|
|
924
674
|
/**
|
|
925
675
|
* Creates and registers network clients for the collection of Infura and
|
|
@@ -934,8 +684,8 @@ export class NetworkController extends BaseController {
|
|
|
934
684
|
*/
|
|
935
685
|
addNetwork(fields) {
|
|
936
686
|
const { rpcEndpoints: setOfRpcEndpointFields } = fields;
|
|
937
|
-
const autoManagedNetworkClientRegistry = this
|
|
938
|
-
this
|
|
687
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
688
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_validateNetworkFields).call(this, {
|
|
939
689
|
mode: 'add',
|
|
940
690
|
networkFields: fields,
|
|
941
691
|
autoManagedNetworkClientRegistry,
|
|
@@ -952,17 +702,17 @@ export class NetworkController extends BaseController {
|
|
|
952
702
|
rpcEndpoint,
|
|
953
703
|
};
|
|
954
704
|
});
|
|
955
|
-
const newNetworkConfiguration = this
|
|
705
|
+
const newNetworkConfiguration = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineNetworkConfigurationToPersist).call(this, {
|
|
956
706
|
networkFields: fields,
|
|
957
707
|
networkClientOperations,
|
|
958
708
|
});
|
|
959
|
-
this
|
|
709
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_registerNetworkClientsAsNeeded).call(this, {
|
|
960
710
|
networkFields: fields,
|
|
961
711
|
networkClientOperations,
|
|
962
712
|
autoManagedNetworkClientRegistry,
|
|
963
713
|
});
|
|
964
714
|
this.update((state) => {
|
|
965
|
-
this
|
|
715
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateNetworkConfigurations).call(this, {
|
|
966
716
|
state,
|
|
967
717
|
mode: 'add',
|
|
968
718
|
networkFields: fields,
|
|
@@ -1003,8 +753,8 @@ export class NetworkController extends BaseController {
|
|
|
1003
753
|
}
|
|
1004
754
|
const existingChainId = chainId;
|
|
1005
755
|
const { chainId: newChainId, rpcEndpoints: setOfNewRpcEndpointFields } = fields;
|
|
1006
|
-
const autoManagedNetworkClientRegistry = this
|
|
1007
|
-
this
|
|
756
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
757
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_validateNetworkFields).call(this, {
|
|
1008
758
|
mode: 'update',
|
|
1009
759
|
networkFields: fields,
|
|
1010
760
|
existingNetworkConfiguration,
|
|
@@ -1112,7 +862,7 @@ export class NetworkController extends BaseController {
|
|
|
1112
862
|
networkClientOperations.push(networkClientOperation);
|
|
1113
863
|
}
|
|
1114
864
|
}
|
|
1115
|
-
const updatedNetworkConfiguration = this
|
|
865
|
+
const updatedNetworkConfiguration = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineNetworkConfigurationToPersist).call(this, {
|
|
1116
866
|
networkFields: fields,
|
|
1117
867
|
networkClientOperations,
|
|
1118
868
|
});
|
|
@@ -1129,7 +879,7 @@ export class NetworkController extends BaseController {
|
|
|
1129
879
|
})) {
|
|
1130
880
|
throw new Error(`Could not update network: Cannot update RPC endpoints in such a way that the selected network '${this.state.selectedNetworkClientId}' would be removed without a replacement. Choose a different RPC endpoint as the selected network via the \`replacementSelectedRpcEndpointIndex\` option.`);
|
|
1131
881
|
}
|
|
1132
|
-
this
|
|
882
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_registerNetworkClientsAsNeeded).call(this, {
|
|
1133
883
|
networkFields: fields,
|
|
1134
884
|
networkClientOperations,
|
|
1135
885
|
autoManagedNetworkClientRegistry,
|
|
@@ -1155,7 +905,7 @@ export class NetworkController extends BaseController {
|
|
|
1155
905
|
rpcEndpointToSelect.networkClientId !== this.state.selectedNetworkClientId) {
|
|
1156
906
|
await this.setActiveNetwork(rpcEndpointToSelect.networkClientId, {
|
|
1157
907
|
updateState: (state) => {
|
|
1158
|
-
this
|
|
908
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateNetworkConfigurations).call(this, {
|
|
1159
909
|
state,
|
|
1160
910
|
mode: 'update',
|
|
1161
911
|
networkFields: fields,
|
|
@@ -1167,7 +917,7 @@ export class NetworkController extends BaseController {
|
|
|
1167
917
|
}
|
|
1168
918
|
else {
|
|
1169
919
|
this.update((state) => {
|
|
1170
|
-
this
|
|
920
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateNetworkConfigurations).call(this, {
|
|
1171
921
|
state,
|
|
1172
922
|
mode: 'update',
|
|
1173
923
|
networkFields: fields,
|
|
@@ -1176,7 +926,7 @@ export class NetworkController extends BaseController {
|
|
|
1176
926
|
});
|
|
1177
927
|
});
|
|
1178
928
|
}
|
|
1179
|
-
this
|
|
929
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_unregisterNetworkClientsAsNeeded).call(this, {
|
|
1180
930
|
networkClientOperations,
|
|
1181
931
|
autoManagedNetworkClientRegistry,
|
|
1182
932
|
});
|
|
@@ -1199,19 +949,19 @@ export class NetworkController extends BaseController {
|
|
|
1199
949
|
if (existingNetworkConfiguration.rpcEndpoints.some((rpcEndpoint) => rpcEndpoint.networkClientId === this.state.selectedNetworkClientId)) {
|
|
1200
950
|
throw new Error(`Cannot remove the currently selected network`);
|
|
1201
951
|
}
|
|
1202
|
-
const autoManagedNetworkClientRegistry = this
|
|
952
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
1203
953
|
const networkClientOperations = existingNetworkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
|
|
1204
954
|
return {
|
|
1205
955
|
type: 'remove',
|
|
1206
956
|
rpcEndpoint,
|
|
1207
957
|
};
|
|
1208
958
|
});
|
|
1209
|
-
this
|
|
959
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_unregisterNetworkClientsAsNeeded).call(this, {
|
|
1210
960
|
networkClientOperations,
|
|
1211
961
|
autoManagedNetworkClientRegistry,
|
|
1212
962
|
});
|
|
1213
963
|
this.update((state) => {
|
|
1214
|
-
this
|
|
964
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateNetworkConfigurations).call(this, {
|
|
1215
965
|
state,
|
|
1216
966
|
mode: 'remove',
|
|
1217
967
|
existingNetworkConfiguration,
|
|
@@ -1230,7 +980,7 @@ export class NetworkController extends BaseController {
|
|
|
1230
980
|
* to {@link resetConnection}.
|
|
1231
981
|
*/
|
|
1232
982
|
async rollbackToPreviousProvider() {
|
|
1233
|
-
await this
|
|
983
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this, __classPrivateFieldGet(this, _NetworkController_previouslySelectedNetworkClientId, "f"));
|
|
1234
984
|
}
|
|
1235
985
|
/**
|
|
1236
986
|
* Deactivates the controller, stopping any ongoing polling.
|
|
@@ -1240,7 +990,7 @@ export class NetworkController extends BaseController {
|
|
|
1240
990
|
// We're intentionally changing the signature of an extended method.
|
|
1241
991
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
1242
992
|
async destroy() {
|
|
1243
|
-
await this
|
|
993
|
+
await __classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f")?.destroy();
|
|
1244
994
|
}
|
|
1245
995
|
/**
|
|
1246
996
|
* Merges the given backup data into controller state.
|
|
@@ -1274,405 +1024,551 @@ export class NetworkController extends BaseController {
|
|
|
1274
1024
|
const { networkClientId } = networkConfiguration.rpcEndpoints[networkConfiguration.defaultRpcEndpointIndex];
|
|
1275
1025
|
return networkClientId;
|
|
1276
1026
|
}
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
const
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
!isSafeChainId(networkFields.chainId)) {
|
|
1291
|
-
throw new Error(`${errorMessagePrefix}: Invalid \`chainId\` '${networkFields.chainId}' (must start with "0x" and not exceed the maximum)`);
|
|
1027
|
+
}
|
|
1028
|
+
_NetworkController_ethQuery = new WeakMap(), _NetworkController_infuraProjectId = new WeakMap(), _NetworkController_failoverUrls = new WeakMap(), _NetworkController_previouslySelectedNetworkClientId = new WeakMap(), _NetworkController_providerProxy = new WeakMap(), _NetworkController_blockTrackerProxy = new WeakMap(), _NetworkController_autoManagedNetworkClientRegistry = new WeakMap(), _NetworkController_autoManagedNetworkClient = new WeakMap(), _NetworkController_log = new WeakMap(), _NetworkController_getRpcServiceOptions = new WeakMap(), _NetworkController_getBlockTrackerOptions = new WeakMap(), _NetworkController_analyticsOptions = new WeakMap(), _NetworkController_networkConfigurationsByNetworkClientId = new WeakMap(), _NetworkController_rpcFailoverMode = new WeakMap(), _NetworkController_instances = new WeakSet(), _NetworkController_updateRpcFailover = function _NetworkController_updateRpcFailover(newMode) {
|
|
1029
|
+
if (__classPrivateFieldGet(this, _NetworkController_rpcFailoverMode, "f") === newMode) {
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
1033
|
+
for (const networkClientsById of Object.values(autoManagedNetworkClientRegistry)) {
|
|
1034
|
+
for (const networkClientId of Object.keys(networkClientsById)) {
|
|
1035
|
+
const networkClient = networkClientsById[networkClientId];
|
|
1036
|
+
if (networkClient.configuration.failoverRpcUrls &&
|
|
1037
|
+
networkClient.configuration.failoverRpcUrls.length > 0) {
|
|
1038
|
+
networkClient.setRpcFailoverMode(newMode);
|
|
1039
|
+
}
|
|
1292
1040
|
}
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1041
|
+
}
|
|
1042
|
+
__classPrivateFieldSet(this, _NetworkController_rpcFailoverMode, newMode, "f");
|
|
1043
|
+
}, _NetworkController_refreshNetwork =
|
|
1044
|
+
/**
|
|
1045
|
+
* Executes a series of steps to switch the network:
|
|
1046
|
+
*
|
|
1047
|
+
* 1. Notifies subscribers via the messenger that the network is about to be
|
|
1048
|
+
* switched (and, really, that the global provider and block tracker proxies
|
|
1049
|
+
* will be re-pointed to a new network).
|
|
1050
|
+
* 2. Looks up a known and preinitialized network client matching the given
|
|
1051
|
+
* ID and uses it to re-point the aforementioned provider and block tracker
|
|
1052
|
+
* proxies.
|
|
1053
|
+
* 3. Notifies subscribers via the messenger that the network has switched.
|
|
1054
|
+
* 4. Captures metadata for the newly switched network in state.
|
|
1055
|
+
*
|
|
1056
|
+
* @param networkClientId - The ID of a network client that requests will be
|
|
1057
|
+
* routed through (either the name of an Infura network or the ID of a custom
|
|
1058
|
+
* network configuration).
|
|
1059
|
+
* @param options - Options for this method.
|
|
1060
|
+
* @param options.updateState - Allows for updating state.
|
|
1061
|
+
*/
|
|
1062
|
+
async function _NetworkController_refreshNetwork(networkClientId, options = {}) {
|
|
1063
|
+
this.messenger.publish('NetworkController:networkWillChange', this.state);
|
|
1064
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_applyNetworkSelection).call(this, networkClientId, options);
|
|
1065
|
+
this.messenger.publish('NetworkController:networkDidChange', this.state);
|
|
1066
|
+
await this.lookupNetwork();
|
|
1067
|
+
}, _NetworkController_determineNetworkMetadata =
|
|
1068
|
+
/**
|
|
1069
|
+
* Uses a request for the latest block to gather the following information on
|
|
1070
|
+
* the given network:
|
|
1071
|
+
*
|
|
1072
|
+
* - The connectivity status: whether it is available, geo-blocked (Infura
|
|
1073
|
+
* only), unavailable, or unknown
|
|
1074
|
+
* - The capabilities status: whether it supports EIP-1559, whether it does
|
|
1075
|
+
* not, or whether it is unknown
|
|
1076
|
+
*
|
|
1077
|
+
* @param networkClientId - The ID of the network client to inspect.
|
|
1078
|
+
* If no ID is provided, uses the currently selected network.
|
|
1079
|
+
* @returns The resulting metadata for the network.
|
|
1080
|
+
*/
|
|
1081
|
+
async function _NetworkController_determineNetworkMetadata(networkClientId) {
|
|
1082
|
+
const networkClient = this.getNetworkClientById(networkClientId);
|
|
1083
|
+
const isInfura = networkClient.configuration.type === NetworkClientType.Infura;
|
|
1084
|
+
let networkStatus;
|
|
1085
|
+
let isEIP1559Compatible;
|
|
1086
|
+
try {
|
|
1087
|
+
isEIP1559Compatible =
|
|
1088
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, networkClientId);
|
|
1089
|
+
networkStatus = NetworkStatus.Available;
|
|
1090
|
+
}
|
|
1091
|
+
catch (error) {
|
|
1092
|
+
debugLog('NetworkController: lookupNetwork: ', error);
|
|
1093
|
+
if (isErrorWithCode(error)) {
|
|
1094
|
+
let responseBody;
|
|
1095
|
+
if (isInfura &&
|
|
1096
|
+
hasProperty(error, 'message') &&
|
|
1097
|
+
typeof error.message === 'string') {
|
|
1098
|
+
try {
|
|
1099
|
+
responseBody = JSON.parse(error.message);
|
|
1298
1100
|
}
|
|
1299
|
-
|
|
1300
|
-
|
|
1101
|
+
catch {
|
|
1102
|
+
// error.message must not be JSON
|
|
1103
|
+
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: json parse error: ', error);
|
|
1301
1104
|
}
|
|
1302
1105
|
}
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
networkFields.blockExplorerUrls[networkFields.defaultBlockExplorerUrlIndex] === undefined
|
|
1307
|
-
: networkFields.defaultBlockExplorerUrlIndex !== undefined;
|
|
1308
|
-
if (isInvalidDefaultBlockExplorerUrlIndex) {
|
|
1309
|
-
throw new Error(`${errorMessagePrefix}: \`defaultBlockExplorerUrlIndex\` must refer to an entry in \`blockExplorerUrls\``);
|
|
1310
|
-
}
|
|
1311
|
-
if (networkFields.rpcEndpoints.length === 0) {
|
|
1312
|
-
throw new Error(`${errorMessagePrefix}: \`rpcEndpoints\` must be a non-empty array`);
|
|
1313
|
-
}
|
|
1314
|
-
for (const rpcEndpointFields of networkFields.rpcEndpoints) {
|
|
1315
|
-
if (!isValidUrl(rpcEndpointFields.url)) {
|
|
1316
|
-
throw new Error(`${errorMessagePrefix}: An entry in \`rpcEndpoints\` has invalid URL '${rpcEndpointFields.url}'`);
|
|
1317
|
-
}
|
|
1318
|
-
const networkClientId = 'networkClientId' in rpcEndpointFields
|
|
1319
|
-
? rpcEndpointFields.networkClientId
|
|
1320
|
-
: undefined;
|
|
1321
|
-
if (mode === 'update' &&
|
|
1322
|
-
networkClientId !== undefined &&
|
|
1323
|
-
rpcEndpointFields.type === RpcEndpointType.Custom &&
|
|
1324
|
-
!Object.values(autoManagedNetworkClientRegistry).some((networkClientsById) => networkClientId in networkClientsById)) {
|
|
1325
|
-
throw new Error(`${errorMessagePrefix}: RPC endpoint '${rpcEndpointFields.url}' refers to network client '${networkClientId}' that does not exist`);
|
|
1106
|
+
if (isPlainObject(responseBody) &&
|
|
1107
|
+
responseBody.error === INFURA_BLOCKED_KEY) {
|
|
1108
|
+
networkStatus = NetworkStatus.Blocked;
|
|
1326
1109
|
}
|
|
1327
|
-
if (
|
|
1328
|
-
|
|
1329
|
-
|
|
1110
|
+
else if (error.code === errorCodes.rpc.internal) {
|
|
1111
|
+
networkStatus = NetworkStatus.Unknown;
|
|
1112
|
+
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: rpc internal error: ', error);
|
|
1330
1113
|
}
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
: true);
|
|
1335
|
-
for (const networkConfiguration of networkConfigurationsForOtherChains) {
|
|
1336
|
-
const rpcEndpoint = networkConfiguration.rpcEndpoints.find((existingRpcEndpoint) => URI.equal(rpcEndpointFields.url, existingRpcEndpoint.url));
|
|
1337
|
-
if (rpcEndpoint) {
|
|
1338
|
-
if (mode === 'update') {
|
|
1339
|
-
throw new Error(`Could not update network to point to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`);
|
|
1340
|
-
}
|
|
1341
|
-
else {
|
|
1342
|
-
throw new Error(`Could not add network that points to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`);
|
|
1343
|
-
}
|
|
1344
|
-
}
|
|
1114
|
+
else {
|
|
1115
|
+
networkStatus = NetworkStatus.Unavailable;
|
|
1116
|
+
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: ', error);
|
|
1345
1117
|
}
|
|
1346
1118
|
}
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1119
|
+
else {
|
|
1120
|
+
debugLog('NetworkController - could not determine network status', error);
|
|
1121
|
+
networkStatus = NetworkStatus.Unknown;
|
|
1122
|
+
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: ', error);
|
|
1350
1123
|
}
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1124
|
+
}
|
|
1125
|
+
return { isInfura, networkStatus, isEIP1559Compatible };
|
|
1126
|
+
}, _NetworkController_lookupGivenNetwork =
|
|
1127
|
+
/**
|
|
1128
|
+
* Uses a request for the latest block to gather the following information on
|
|
1129
|
+
* the given network, persisting it to state:
|
|
1130
|
+
*
|
|
1131
|
+
* - The connectivity status: whether the network is available, geo-blocked
|
|
1132
|
+
* (Infura only), unavailable, or unknown
|
|
1133
|
+
* - The feature compatibility status: whether the network supports EIP-1559,
|
|
1134
|
+
* whether it does not, or whether it is unknown
|
|
1135
|
+
*
|
|
1136
|
+
* @param networkClientId - The ID of the network client to inspect.
|
|
1137
|
+
*/
|
|
1138
|
+
async function _NetworkController_lookupGivenNetwork(networkClientId) {
|
|
1139
|
+
const { networkStatus, isEIP1559Compatible } = await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineNetworkMetadata).call(this, networkClientId);
|
|
1140
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateMetadataForNetwork).call(this, networkClientId, {
|
|
1141
|
+
networkStatus,
|
|
1142
|
+
isEIP1559Compatible,
|
|
1143
|
+
});
|
|
1144
|
+
}, _NetworkController_lookupSelectedNetwork =
|
|
1145
|
+
/**
|
|
1146
|
+
* Uses a request for the latest block to gather the following information on
|
|
1147
|
+
* the currently selected network, persisting it to state:
|
|
1148
|
+
*
|
|
1149
|
+
* - The connectivity status: whether the network is available, geo-blocked
|
|
1150
|
+
* (Infura only), unavailable, or unknown
|
|
1151
|
+
* - The feature compatibility status: whether the network supports EIP-1559,
|
|
1152
|
+
* whether it does not, or whether it is unknown
|
|
1153
|
+
*
|
|
1154
|
+
* Note that it is possible for the current network to be switched while this
|
|
1155
|
+
* method is running. If that is the case, it will exit early (as this method
|
|
1156
|
+
* will also run for the new network).
|
|
1157
|
+
*/
|
|
1158
|
+
async function _NetworkController_lookupSelectedNetwork() {
|
|
1159
|
+
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
1160
|
+
return;
|
|
1161
|
+
}
|
|
1162
|
+
let networkChanged = false;
|
|
1163
|
+
const listener = () => {
|
|
1164
|
+
networkChanged = true;
|
|
1165
|
+
try {
|
|
1166
|
+
this.messenger.unsubscribe('NetworkController:networkDidChange', listener);
|
|
1167
|
+
}
|
|
1168
|
+
catch (error) {
|
|
1169
|
+
// In theory, this `catch` should not be necessary given that this error
|
|
1170
|
+
// would occur "inside" of the call to `#determineEIP1559Compatibility`
|
|
1171
|
+
// below and so it should be caught by the `try`/`catch` below (it is
|
|
1172
|
+
// impossible to reproduce in tests for that reason). However, somehow
|
|
1173
|
+
// it occurs within Mobile and so we have to add our own `try`/`catch`
|
|
1174
|
+
// here.
|
|
1175
|
+
/* istanbul ignore next */
|
|
1176
|
+
if (!(error instanceof Error) ||
|
|
1177
|
+
error.message !==
|
|
1178
|
+
'Subscription not found for event: NetworkController:networkDidChange') {
|
|
1179
|
+
// Again, this error should not happen and is impossible to reproduce
|
|
1180
|
+
// in tests.
|
|
1181
|
+
/* istanbul ignore next */
|
|
1182
|
+
throw error;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
};
|
|
1186
|
+
this.messenger.subscribe('NetworkController:networkDidChange', listener);
|
|
1187
|
+
const { isInfura, networkStatus, isEIP1559Compatible } = await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineNetworkMetadata).call(this, this.state.selectedNetworkClientId);
|
|
1188
|
+
if (networkChanged) {
|
|
1189
|
+
// If the network has changed, then `lookupNetwork` either has been or is
|
|
1190
|
+
// in the process of being called, so we don't need to go further.
|
|
1191
|
+
return;
|
|
1192
|
+
}
|
|
1193
|
+
try {
|
|
1194
|
+
this.messenger.unsubscribe('NetworkController:networkDidChange', listener);
|
|
1195
|
+
}
|
|
1196
|
+
catch (error) {
|
|
1197
|
+
if (!(error instanceof Error) ||
|
|
1198
|
+
error.message !==
|
|
1199
|
+
'Subscription not found for event: NetworkController:networkDidChange') {
|
|
1200
|
+
throw error;
|
|
1358
1201
|
}
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1202
|
+
}
|
|
1203
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateMetadataForNetwork).call(this, this.state.selectedNetworkClientId, {
|
|
1204
|
+
networkStatus,
|
|
1205
|
+
isEIP1559Compatible,
|
|
1206
|
+
});
|
|
1207
|
+
if (isInfura) {
|
|
1208
|
+
if (networkStatus === NetworkStatus.Available) {
|
|
1209
|
+
this.messenger.publish('NetworkController:infuraIsUnblocked');
|
|
1362
1210
|
}
|
|
1363
|
-
if (
|
|
1364
|
-
|
|
1365
|
-
throw new Error(`${errorMessagePrefix}: \`defaultRpcEndpointIndex\` must refer to an entry in \`rpcEndpoints\``);
|
|
1211
|
+
else if (networkStatus === NetworkStatus.Blocked) {
|
|
1212
|
+
this.messenger.publish('NetworkController:infuraIsBlocked');
|
|
1366
1213
|
}
|
|
1367
1214
|
}
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
* @param args.networkFields - The fields used to add or update a network.
|
|
1374
|
-
* @param args.networkClientOperations - Operations which were calculated for
|
|
1375
|
-
* updating the network client registry but which also map back to RPC
|
|
1376
|
-
* endpoints (and so can be used to save those RPC endpoints).
|
|
1377
|
-
* @returns The network configuration to persist.
|
|
1378
|
-
*/
|
|
1379
|
-
#determineNetworkConfigurationToPersist({ networkFields, networkClientOperations, }) {
|
|
1380
|
-
const rpcEndpointsToPersist = networkClientOperations
|
|
1381
|
-
.filter((networkClientOperation) => {
|
|
1382
|
-
return (networkClientOperation.type === 'add' ||
|
|
1383
|
-
networkClientOperation.type === 'noop');
|
|
1384
|
-
})
|
|
1385
|
-
.map((networkClientOperation) => networkClientOperation.rpcEndpoint)
|
|
1386
|
-
.concat(networkClientOperations
|
|
1387
|
-
.filter((networkClientOperation) => {
|
|
1388
|
-
return networkClientOperation.type === 'replace';
|
|
1389
|
-
})
|
|
1390
|
-
.map((networkClientOperation) => networkClientOperation.newRpcEndpoint));
|
|
1391
|
-
return { ...networkFields, rpcEndpoints: rpcEndpointsToPersist };
|
|
1215
|
+
else {
|
|
1216
|
+
// Always publish infuraIsUnblocked regardless of network status to
|
|
1217
|
+
// prevent consumers from being stuck in a blocked state if they were
|
|
1218
|
+
// previously connected to an Infura network that was blocked
|
|
1219
|
+
this.messenger.publish('NetworkController:infuraIsUnblocked');
|
|
1392
1220
|
}
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
const addedRpcEndpoints = networkClientOperations
|
|
1406
|
-
.filter((networkClientOperation) => {
|
|
1407
|
-
return networkClientOperation.type === 'add';
|
|
1408
|
-
})
|
|
1409
|
-
.map((networkClientOperation) => networkClientOperation.rpcEndpoint)
|
|
1410
|
-
.concat(networkClientOperations
|
|
1411
|
-
.filter((networkClientOperation) => {
|
|
1412
|
-
return networkClientOperation.type === 'replace';
|
|
1413
|
-
})
|
|
1414
|
-
.map((networkClientOperation) => networkClientOperation.newRpcEndpoint));
|
|
1415
|
-
const defaultFailoverUrls = this.#failoverUrls?.[networkFields.chainId];
|
|
1416
|
-
for (const addedRpcEndpoint of addedRpcEndpoints) {
|
|
1417
|
-
if (addedRpcEndpoint.type === RpcEndpointType.Infura) {
|
|
1418
|
-
autoManagedNetworkClientRegistry[NetworkClientType.Infura][addedRpcEndpoint.networkClientId] = createAutoManagedNetworkClient({
|
|
1419
|
-
networkClientId: addedRpcEndpoint.networkClientId,
|
|
1420
|
-
networkClientConfiguration: {
|
|
1421
|
-
type: NetworkClientType.Infura,
|
|
1422
|
-
chainId: networkFields.chainId,
|
|
1423
|
-
network: addedRpcEndpoint.networkClientId,
|
|
1424
|
-
failoverRpcUrls: defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
|
|
1425
|
-
infuraProjectId: this.#infuraProjectId,
|
|
1426
|
-
ticker: networkFields.nativeCurrency,
|
|
1427
|
-
},
|
|
1428
|
-
getRpcServiceOptions: this.#getRpcServiceOptions,
|
|
1429
|
-
getBlockTrackerOptions: this.#getBlockTrackerOptions,
|
|
1430
|
-
messenger: this.messenger,
|
|
1431
|
-
rpcFailoverMode: this.#rpcFailoverMode,
|
|
1432
|
-
logger: this.#log,
|
|
1433
|
-
});
|
|
1221
|
+
}, _NetworkController_updateMetadataForNetwork = function _NetworkController_updateMetadataForNetwork(networkClientId, metadata) {
|
|
1222
|
+
this.update((state) => {
|
|
1223
|
+
var _a;
|
|
1224
|
+
(_a = state.networksMetadata)[networkClientId] ?? (_a[networkClientId] = {
|
|
1225
|
+
status: NetworkStatus.Unknown,
|
|
1226
|
+
EIPS: {},
|
|
1227
|
+
});
|
|
1228
|
+
const newMetadata = state.networksMetadata[networkClientId];
|
|
1229
|
+
newMetadata.status = metadata.networkStatus;
|
|
1230
|
+
if ('isEIP1559Compatible' in metadata) {
|
|
1231
|
+
if (metadata.isEIP1559Compatible === undefined) {
|
|
1232
|
+
delete newMetadata.EIPS[1559];
|
|
1434
1233
|
}
|
|
1435
1234
|
else {
|
|
1436
|
-
|
|
1437
|
-
networkClientId: addedRpcEndpoint.networkClientId,
|
|
1438
|
-
networkClientConfiguration: {
|
|
1439
|
-
type: NetworkClientType.Custom,
|
|
1440
|
-
chainId: networkFields.chainId,
|
|
1441
|
-
failoverRpcUrls: defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
|
|
1442
|
-
rpcUrl: addedRpcEndpoint.url,
|
|
1443
|
-
ticker: networkFields.nativeCurrency,
|
|
1444
|
-
},
|
|
1445
|
-
getRpcServiceOptions: this.#getRpcServiceOptions,
|
|
1446
|
-
getBlockTrackerOptions: this.#getBlockTrackerOptions,
|
|
1447
|
-
messenger: this.messenger,
|
|
1448
|
-
rpcFailoverMode: this.#rpcFailoverMode,
|
|
1449
|
-
logger: this.#log,
|
|
1450
|
-
});
|
|
1235
|
+
newMetadata.EIPS[1559] = metadata.isEIP1559Compatible;
|
|
1451
1236
|
}
|
|
1452
1237
|
}
|
|
1238
|
+
});
|
|
1239
|
+
}, _NetworkController_getLatestBlock = function _NetworkController_getLatestBlock(networkClientId = this.state.selectedNetworkClientId) {
|
|
1240
|
+
const networkClient = this.getNetworkClientById(networkClientId);
|
|
1241
|
+
const ethQuery = new EthQuery(networkClient.provider);
|
|
1242
|
+
return new Promise((resolve, reject) => {
|
|
1243
|
+
ethQuery.sendAsync({ method: 'eth_getBlockByNumber', params: ['latest', false] }, (error, block) => {
|
|
1244
|
+
if (error) {
|
|
1245
|
+
// This error comes from JsonRpcEngine, we don't control it.
|
|
1246
|
+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
1247
|
+
reject(error);
|
|
1248
|
+
}
|
|
1249
|
+
else {
|
|
1250
|
+
// TODO: Validate this type
|
|
1251
|
+
resolve(block);
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
});
|
|
1255
|
+
}, _NetworkController_determineEIP1559Compatibility =
|
|
1256
|
+
/**
|
|
1257
|
+
* Retrieves and checks the latest block from the currently selected
|
|
1258
|
+
* network; if the block has a `baseFeePerGas` property, then we know
|
|
1259
|
+
* that the network supports EIP-1559; otherwise it doesn't.
|
|
1260
|
+
*
|
|
1261
|
+
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility
|
|
1262
|
+
* @returns A promise that resolves to `true` if the network supports EIP-1559,
|
|
1263
|
+
* `false` otherwise, or `undefined` if unable to retrieve the last block.
|
|
1264
|
+
*/
|
|
1265
|
+
async function _NetworkController_determineEIP1559Compatibility(networkClientId) {
|
|
1266
|
+
const latestBlock = await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getLatestBlock).call(this, networkClientId);
|
|
1267
|
+
if (!latestBlock) {
|
|
1268
|
+
return undefined;
|
|
1453
1269
|
}
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
.map((networkClientOperation) => networkClientOperation.oldRpcEndpoint));
|
|
1475
|
-
for (const rpcEndpoint of removedRpcEndpoints) {
|
|
1476
|
-
const networkClient = this.getNetworkClientById(rpcEndpoint.networkClientId);
|
|
1477
|
-
networkClient.destroy();
|
|
1478
|
-
delete autoManagedNetworkClientRegistry[networkClient.configuration.type][rpcEndpoint.networkClientId];
|
|
1270
|
+
return latestBlock.baseFeePerGas !== undefined;
|
|
1271
|
+
}, _NetworkController_validateNetworkFields = function _NetworkController_validateNetworkFields(args) {
|
|
1272
|
+
const { mode, networkFields, autoManagedNetworkClientRegistry } = args;
|
|
1273
|
+
const existingNetworkConfiguration = 'existingNetworkConfiguration' in args
|
|
1274
|
+
? args.existingNetworkConfiguration
|
|
1275
|
+
: null;
|
|
1276
|
+
const errorMessagePrefix = mode === 'update' ? 'Could not update network' : 'Could not add network';
|
|
1277
|
+
if (!isStrictHexString(networkFields.chainId) ||
|
|
1278
|
+
!isSafeChainId(networkFields.chainId)) {
|
|
1279
|
+
throw new Error(`${errorMessagePrefix}: Invalid \`chainId\` '${networkFields.chainId}' (must start with "0x" and not exceed the maximum)`);
|
|
1280
|
+
}
|
|
1281
|
+
if (networkFields.chainId !== existingNetworkConfiguration?.chainId) {
|
|
1282
|
+
const existingNetworkConfigurationViaChainId = this.state.networkConfigurationsByChainId[networkFields.chainId];
|
|
1283
|
+
if (existingNetworkConfigurationViaChainId !== undefined) {
|
|
1284
|
+
if (existingNetworkConfiguration === null) {
|
|
1285
|
+
throw new Error(`Could not add network for chain ${args.networkFields.chainId} as another network for that chain already exists ('${existingNetworkConfigurationViaChainId.name}')`);
|
|
1286
|
+
}
|
|
1287
|
+
else {
|
|
1288
|
+
throw new Error(`Cannot move network from chain ${existingNetworkConfiguration.chainId} to ${networkFields.chainId} as another network for that chain already exists ('${existingNetworkConfigurationViaChainId.name}')`);
|
|
1289
|
+
}
|
|
1479
1290
|
}
|
|
1480
1291
|
}
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
if (mode === 'remove' ||
|
|
1495
|
-
(mode === 'update' &&
|
|
1496
|
-
args.networkFields.chainId !==
|
|
1497
|
-
args.existingNetworkConfiguration.chainId)) {
|
|
1498
|
-
delete state.networkConfigurationsByChainId[args.existingNetworkConfiguration.chainId];
|
|
1292
|
+
const isInvalidDefaultBlockExplorerUrlIndex = networkFields.blockExplorerUrls.length > 0
|
|
1293
|
+
? networkFields.defaultBlockExplorerUrlIndex === undefined ||
|
|
1294
|
+
networkFields.blockExplorerUrls[networkFields.defaultBlockExplorerUrlIndex] === undefined
|
|
1295
|
+
: networkFields.defaultBlockExplorerUrlIndex !== undefined;
|
|
1296
|
+
if (isInvalidDefaultBlockExplorerUrlIndex) {
|
|
1297
|
+
throw new Error(`${errorMessagePrefix}: \`defaultBlockExplorerUrlIndex\` must refer to an entry in \`blockExplorerUrls\``);
|
|
1298
|
+
}
|
|
1299
|
+
if (networkFields.rpcEndpoints.length === 0) {
|
|
1300
|
+
throw new Error(`${errorMessagePrefix}: \`rpcEndpoints\` must be a non-empty array`);
|
|
1301
|
+
}
|
|
1302
|
+
for (const rpcEndpointFields of networkFields.rpcEndpoints) {
|
|
1303
|
+
if (!isValidUrl(rpcEndpointFields.url)) {
|
|
1304
|
+
throw new Error(`${errorMessagePrefix}: An entry in \`rpcEndpoints\` has invalid URL '${rpcEndpointFields.url}'`);
|
|
1499
1305
|
}
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1306
|
+
const networkClientId = 'networkClientId' in rpcEndpointFields
|
|
1307
|
+
? rpcEndpointFields.networkClientId
|
|
1308
|
+
: undefined;
|
|
1309
|
+
if (mode === 'update' &&
|
|
1310
|
+
networkClientId !== undefined &&
|
|
1311
|
+
rpcEndpointFields.type === RpcEndpointType.Custom &&
|
|
1312
|
+
!Object.values(autoManagedNetworkClientRegistry).some((networkClientsById) => networkClientId in networkClientsById)) {
|
|
1313
|
+
throw new Error(`${errorMessagePrefix}: RPC endpoint '${rpcEndpointFields.url}' refers to network client '${networkClientId}' that does not exist`);
|
|
1314
|
+
}
|
|
1315
|
+
if (networkFields.rpcEndpoints.some((otherRpcEndpointFields) => otherRpcEndpointFields !== rpcEndpointFields &&
|
|
1316
|
+
URI.equal(otherRpcEndpointFields.url, rpcEndpointFields.url))) {
|
|
1317
|
+
throw new Error(`${errorMessagePrefix}: Each entry in rpcEndpoints must have a unique URL`);
|
|
1318
|
+
}
|
|
1319
|
+
const networkConfigurationsForOtherChains = Object.values(this.state.networkConfigurationsByChainId).filter((networkConfiguration) => existingNetworkConfiguration
|
|
1320
|
+
? networkConfiguration.chainId !==
|
|
1321
|
+
existingNetworkConfiguration.chainId
|
|
1322
|
+
: true);
|
|
1323
|
+
for (const networkConfiguration of networkConfigurationsForOtherChains) {
|
|
1324
|
+
const rpcEndpoint = networkConfiguration.rpcEndpoints.find((existingRpcEndpoint) => URI.equal(rpcEndpointFields.url, existingRpcEndpoint.url));
|
|
1325
|
+
if (rpcEndpoint) {
|
|
1326
|
+
if (mode === 'update') {
|
|
1327
|
+
throw new Error(`Could not update network to point to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`);
|
|
1328
|
+
}
|
|
1329
|
+
else {
|
|
1330
|
+
throw new Error(`Could not add network that points to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`);
|
|
1331
|
+
}
|
|
1503
1332
|
}
|
|
1504
|
-
state.networkConfigurationsByChainId[args.networkFields.chainId] =
|
|
1505
|
-
args.networkConfigurationToPersist;
|
|
1506
1333
|
}
|
|
1507
|
-
this.#networkConfigurationsByNetworkClientId =
|
|
1508
|
-
buildNetworkConfigurationsByNetworkClientId(cloneDeep(state.networkConfigurationsByChainId));
|
|
1509
1334
|
}
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
* `getNetworkClientRegistry` will throw an error. This method checks to see if the
|
|
1514
|
-
* population step has happened yet, and if not, makes it happen.
|
|
1515
|
-
*
|
|
1516
|
-
* @returns The populated network client registry.
|
|
1517
|
-
*/
|
|
1518
|
-
#ensureAutoManagedNetworkClientRegistryPopulated() {
|
|
1519
|
-
return (this.#autoManagedNetworkClientRegistry ??=
|
|
1520
|
-
this.#createAutoManagedNetworkClientRegistry());
|
|
1335
|
+
if ([...new Set(networkFields.rpcEndpoints)].length <
|
|
1336
|
+
networkFields.rpcEndpoints.length) {
|
|
1337
|
+
throw new Error(`${errorMessagePrefix}: Each entry in rpcEndpoints must be unique`);
|
|
1521
1338
|
}
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1339
|
+
const networkClientIds = networkFields.rpcEndpoints
|
|
1340
|
+
.map((rpcEndpoint) => 'networkClientId' in rpcEndpoint
|
|
1341
|
+
? rpcEndpoint.networkClientId
|
|
1342
|
+
: undefined)
|
|
1343
|
+
.filter((networkClientId) => networkClientId !== undefined);
|
|
1344
|
+
if ([...new Set(networkClientIds)].length < networkClientIds.length) {
|
|
1345
|
+
throw new Error(`${errorMessagePrefix}: Each entry in rpcEndpoints must have a unique networkClientId`);
|
|
1346
|
+
}
|
|
1347
|
+
const infuraRpcEndpoints = networkFields.rpcEndpoints.filter((rpcEndpointFields) => rpcEndpointFields.type === RpcEndpointType.Infura);
|
|
1348
|
+
if (infuraRpcEndpoints.length > 1) {
|
|
1349
|
+
throw new Error(`${errorMessagePrefix}: There cannot be more than one Infura RPC endpoint`);
|
|
1350
|
+
}
|
|
1351
|
+
if (networkFields.rpcEndpoints[networkFields.defaultRpcEndpointIndex] ===
|
|
1352
|
+
undefined) {
|
|
1353
|
+
throw new Error(`${errorMessagePrefix}: \`defaultRpcEndpointIndex\` must refer to an entry in \`rpcEndpoints\``);
|
|
1354
|
+
}
|
|
1355
|
+
}, _NetworkController_determineNetworkConfigurationToPersist = function _NetworkController_determineNetworkConfigurationToPersist({ networkFields, networkClientOperations, }) {
|
|
1356
|
+
const rpcEndpointsToPersist = networkClientOperations
|
|
1357
|
+
.filter((networkClientOperation) => {
|
|
1358
|
+
return (networkClientOperation.type === 'add' ||
|
|
1359
|
+
networkClientOperation.type === 'noop');
|
|
1360
|
+
})
|
|
1361
|
+
.map((networkClientOperation) => networkClientOperation.rpcEndpoint)
|
|
1362
|
+
.concat(networkClientOperations
|
|
1363
|
+
.filter((networkClientOperation) => {
|
|
1364
|
+
return networkClientOperation.type === 'replace';
|
|
1365
|
+
})
|
|
1366
|
+
.map((networkClientOperation) => networkClientOperation.newRpcEndpoint));
|
|
1367
|
+
return { ...networkFields, rpcEndpoints: rpcEndpointsToPersist };
|
|
1368
|
+
}, _NetworkController_registerNetworkClientsAsNeeded = function _NetworkController_registerNetworkClientsAsNeeded({ networkFields, networkClientOperations, autoManagedNetworkClientRegistry, }) {
|
|
1369
|
+
const addedRpcEndpoints = networkClientOperations
|
|
1370
|
+
.filter((networkClientOperation) => {
|
|
1371
|
+
return networkClientOperation.type === 'add';
|
|
1372
|
+
})
|
|
1373
|
+
.map((networkClientOperation) => networkClientOperation.rpcEndpoint)
|
|
1374
|
+
.concat(networkClientOperations
|
|
1375
|
+
.filter((networkClientOperation) => {
|
|
1376
|
+
return networkClientOperation.type === 'replace';
|
|
1377
|
+
})
|
|
1378
|
+
.map((networkClientOperation) => networkClientOperation.newRpcEndpoint));
|
|
1379
|
+
const defaultFailoverUrls = __classPrivateFieldGet(this, _NetworkController_failoverUrls, "f")?.[networkFields.chainId];
|
|
1380
|
+
for (const addedRpcEndpoint of addedRpcEndpoints) {
|
|
1381
|
+
if (addedRpcEndpoint.type === RpcEndpointType.Infura) {
|
|
1382
|
+
autoManagedNetworkClientRegistry[NetworkClientType.Infura][addedRpcEndpoint.networkClientId] = createAutoManagedNetworkClient({
|
|
1383
|
+
networkClientId: addedRpcEndpoint.networkClientId,
|
|
1384
|
+
networkClientConfiguration: {
|
|
1385
|
+
type: NetworkClientType.Infura,
|
|
1386
|
+
chainId: networkFields.chainId,
|
|
1387
|
+
network: addedRpcEndpoint.networkClientId,
|
|
1388
|
+
failoverRpcUrls: defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
|
|
1389
|
+
infuraProjectId: __classPrivateFieldGet(this, _NetworkController_infuraProjectId, "f"),
|
|
1390
|
+
ticker: networkFields.nativeCurrency,
|
|
1391
|
+
},
|
|
1392
|
+
getRpcServiceOptions: __classPrivateFieldGet(this, _NetworkController_getRpcServiceOptions, "f"),
|
|
1393
|
+
getBlockTrackerOptions: __classPrivateFieldGet(this, _NetworkController_getBlockTrackerOptions, "f"),
|
|
1394
|
+
messenger: this.messenger,
|
|
1395
|
+
rpcFailoverMode: __classPrivateFieldGet(this, _NetworkController_rpcFailoverMode, "f"),
|
|
1396
|
+
logger: __classPrivateFieldGet(this, _NetworkController_log, "f"),
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
else {
|
|
1400
|
+
autoManagedNetworkClientRegistry[NetworkClientType.Custom][addedRpcEndpoint.networkClientId] = createAutoManagedNetworkClient({
|
|
1401
|
+
networkClientId: addedRpcEndpoint.networkClientId,
|
|
1402
|
+
networkClientConfiguration: {
|
|
1403
|
+
type: NetworkClientType.Custom,
|
|
1404
|
+
chainId: networkFields.chainId,
|
|
1405
|
+
failoverRpcUrls: defaultFailoverUrls ?? addedRpcEndpoint.failoverUrls,
|
|
1406
|
+
rpcUrl: addedRpcEndpoint.url,
|
|
1407
|
+
ticker: networkFields.nativeCurrency,
|
|
1408
|
+
},
|
|
1409
|
+
getRpcServiceOptions: __classPrivateFieldGet(this, _NetworkController_getRpcServiceOptions, "f"),
|
|
1410
|
+
getBlockTrackerOptions: __classPrivateFieldGet(this, _NetworkController_getBlockTrackerOptions, "f"),
|
|
1411
|
+
messenger: this.messenger,
|
|
1412
|
+
rpcFailoverMode: __classPrivateFieldGet(this, _NetworkController_rpcFailoverMode, "f"),
|
|
1413
|
+
logger: __classPrivateFieldGet(this, _NetworkController_log, "f"),
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
}, _NetworkController_unregisterNetworkClientsAsNeeded = function _NetworkController_unregisterNetworkClientsAsNeeded({ networkClientOperations, autoManagedNetworkClientRegistry, }) {
|
|
1418
|
+
const removedRpcEndpoints = networkClientOperations
|
|
1419
|
+
.filter((networkClientOperation) => {
|
|
1420
|
+
return networkClientOperation.type === 'remove';
|
|
1421
|
+
})
|
|
1422
|
+
.map((networkClientOperation) => networkClientOperation.rpcEndpoint)
|
|
1423
|
+
.concat(networkClientOperations
|
|
1424
|
+
.filter((networkClientOperation) => {
|
|
1425
|
+
return networkClientOperation.type === 'replace';
|
|
1426
|
+
})
|
|
1427
|
+
.map((networkClientOperation) => networkClientOperation.oldRpcEndpoint));
|
|
1428
|
+
for (const rpcEndpoint of removedRpcEndpoints) {
|
|
1429
|
+
const networkClient = this.getNetworkClientById(rpcEndpoint.networkClientId);
|
|
1430
|
+
networkClient.destroy();
|
|
1431
|
+
delete autoManagedNetworkClientRegistry[networkClient.configuration.type][rpcEndpoint.networkClientId];
|
|
1432
|
+
}
|
|
1433
|
+
}, _NetworkController_updateNetworkConfigurations = function _NetworkController_updateNetworkConfigurations(args) {
|
|
1434
|
+
const { state, mode } = args;
|
|
1435
|
+
if (mode === 'remove' ||
|
|
1436
|
+
(mode === 'update' &&
|
|
1437
|
+
args.networkFields.chainId !==
|
|
1438
|
+
args.existingNetworkConfiguration.chainId)) {
|
|
1439
|
+
delete state.networkConfigurationsByChainId[args.existingNetworkConfiguration.chainId];
|
|
1440
|
+
}
|
|
1441
|
+
if (mode === 'add' || mode === 'update') {
|
|
1442
|
+
if (!deepEqual(state.networkConfigurationsByChainId[args.networkFields.chainId], args.networkConfigurationToPersist)) {
|
|
1443
|
+
args.networkConfigurationToPersist.lastUpdatedAt = Date.now();
|
|
1444
|
+
}
|
|
1445
|
+
state.networkConfigurationsByChainId[args.networkFields.chainId] =
|
|
1446
|
+
args.networkConfigurationToPersist;
|
|
1447
|
+
}
|
|
1448
|
+
__classPrivateFieldSet(this, _NetworkController_networkConfigurationsByNetworkClientId, buildNetworkConfigurationsByNetworkClientId(cloneDeep(state.networkConfigurationsByChainId)), "f");
|
|
1449
|
+
}, _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated = function _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated() {
|
|
1450
|
+
return (__classPrivateFieldSet(this, _NetworkController_autoManagedNetworkClientRegistry, __classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClientRegistry, "f") ?? __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_createAutoManagedNetworkClientRegistry).call(this), "f"));
|
|
1451
|
+
}, _NetworkController_createAutoManagedNetworkClientRegistry = function _NetworkController_createAutoManagedNetworkClientRegistry() {
|
|
1452
|
+
const chainIds = knownKeysOf(this.state.networkConfigurationsByChainId);
|
|
1453
|
+
const networkClientsWithIds = chainIds.flatMap((chainId) => {
|
|
1454
|
+
const networkConfiguration = this.state.networkConfigurationsByChainId[chainId];
|
|
1455
|
+
const defaultFailoverUrls = __classPrivateFieldGet(this, _NetworkController_failoverUrls, "f")?.[chainId];
|
|
1456
|
+
return networkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
|
|
1457
|
+
if (rpcEndpoint.type === RpcEndpointType.Infura) {
|
|
1458
|
+
const infuraNetworkName = deriveInfuraNetworkNameFromRpcEndpointUrl(rpcEndpoint.url);
|
|
1556
1459
|
return [
|
|
1557
1460
|
rpcEndpoint.networkClientId,
|
|
1558
1461
|
createAutoManagedNetworkClient({
|
|
1559
1462
|
networkClientId: rpcEndpoint.networkClientId,
|
|
1560
1463
|
networkClientConfiguration: {
|
|
1561
|
-
type: NetworkClientType.
|
|
1562
|
-
|
|
1464
|
+
type: NetworkClientType.Infura,
|
|
1465
|
+
network: infuraNetworkName,
|
|
1563
1466
|
failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
|
|
1564
|
-
|
|
1467
|
+
infuraProjectId: __classPrivateFieldGet(this, _NetworkController_infuraProjectId, "f"),
|
|
1468
|
+
chainId: networkConfiguration.chainId,
|
|
1565
1469
|
ticker: networkConfiguration.nativeCurrency,
|
|
1566
1470
|
},
|
|
1567
|
-
getRpcServiceOptions: this
|
|
1568
|
-
getBlockTrackerOptions: this
|
|
1471
|
+
getRpcServiceOptions: __classPrivateFieldGet(this, _NetworkController_getRpcServiceOptions, "f"),
|
|
1472
|
+
getBlockTrackerOptions: __classPrivateFieldGet(this, _NetworkController_getBlockTrackerOptions, "f"),
|
|
1569
1473
|
messenger: this.messenger,
|
|
1570
|
-
rpcFailoverMode: this
|
|
1571
|
-
logger: this
|
|
1474
|
+
rpcFailoverMode: __classPrivateFieldGet(this, _NetworkController_rpcFailoverMode, "f"),
|
|
1475
|
+
logger: __classPrivateFieldGet(this, _NetworkController_log, "f"),
|
|
1572
1476
|
}),
|
|
1573
1477
|
];
|
|
1574
|
-
}
|
|
1478
|
+
}
|
|
1479
|
+
return [
|
|
1480
|
+
rpcEndpoint.networkClientId,
|
|
1481
|
+
createAutoManagedNetworkClient({
|
|
1482
|
+
networkClientId: rpcEndpoint.networkClientId,
|
|
1483
|
+
networkClientConfiguration: {
|
|
1484
|
+
type: NetworkClientType.Custom,
|
|
1485
|
+
chainId: networkConfiguration.chainId,
|
|
1486
|
+
failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
|
|
1487
|
+
rpcUrl: rpcEndpoint.url,
|
|
1488
|
+
ticker: networkConfiguration.nativeCurrency,
|
|
1489
|
+
},
|
|
1490
|
+
getRpcServiceOptions: __classPrivateFieldGet(this, _NetworkController_getRpcServiceOptions, "f"),
|
|
1491
|
+
getBlockTrackerOptions: __classPrivateFieldGet(this, _NetworkController_getBlockTrackerOptions, "f"),
|
|
1492
|
+
messenger: this.messenger,
|
|
1493
|
+
rpcFailoverMode: __classPrivateFieldGet(this, _NetworkController_rpcFailoverMode, "f"),
|
|
1494
|
+
logger: __classPrivateFieldGet(this, _NetworkController_log, "f"),
|
|
1495
|
+
}),
|
|
1496
|
+
];
|
|
1575
1497
|
});
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
}
|
|
1584
|
-
}
|
|
1585
|
-
|
|
1586
|
-
|
|
1498
|
+
});
|
|
1499
|
+
return networkClientsWithIds.reduce((obj, [networkClientId, networkClient]) => {
|
|
1500
|
+
return {
|
|
1501
|
+
...obj,
|
|
1502
|
+
[networkClient.configuration.type]: {
|
|
1503
|
+
...obj[networkClient.configuration.type],
|
|
1504
|
+
[networkClientId]: networkClient,
|
|
1505
|
+
},
|
|
1506
|
+
};
|
|
1507
|
+
}, {
|
|
1508
|
+
[NetworkClientType.Custom]: {},
|
|
1509
|
+
[NetworkClientType.Infura]: {},
|
|
1510
|
+
});
|
|
1511
|
+
}, _NetworkController_applyNetworkSelection = function _NetworkController_applyNetworkSelection(networkClientId, { updateState, } = {}) {
|
|
1512
|
+
__classPrivateFieldSet(this, _NetworkController_autoManagedNetworkClient, this.getNetworkClientById(networkClientId), "f");
|
|
1513
|
+
this.update((state) => {
|
|
1514
|
+
var _a;
|
|
1515
|
+
state.selectedNetworkClientId = networkClientId;
|
|
1516
|
+
(_a = state.networksMetadata)[networkClientId] ?? (_a[networkClientId] = {
|
|
1517
|
+
status: NetworkStatus.Unknown,
|
|
1518
|
+
EIPS: {},
|
|
1587
1519
|
});
|
|
1520
|
+
updateState?.(state);
|
|
1521
|
+
});
|
|
1522
|
+
if (__classPrivateFieldGet(this, _NetworkController_providerProxy, "f")) {
|
|
1523
|
+
__classPrivateFieldGet(this, _NetworkController_providerProxy, "f").setTarget(__classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClient, "f").provider);
|
|
1588
1524
|
}
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
* {@link getSelectedNetworkClient}) to point to the same ones within the
|
|
1592
|
-
* given network client, thereby magically switching any consumers using these
|
|
1593
|
-
* proxies to use the new network.
|
|
1594
|
-
*
|
|
1595
|
-
* Also refreshes the EthQuery instance accessible via the `getEthQuery`
|
|
1596
|
-
* action to wrap the provider from the new network client. Note that this is
|
|
1597
|
-
* not a proxy, so consumers will need to call `getEthQuery` again after the
|
|
1598
|
-
* network switch.
|
|
1599
|
-
*
|
|
1600
|
-
* @param networkClientId - The ID of a network client that requests will be
|
|
1601
|
-
* routed through (either the name of an Infura network or the ID of a custom
|
|
1602
|
-
* network configuration).
|
|
1603
|
-
* @param options - Options for this method.
|
|
1604
|
-
* @param options.updateState - Allows for updating state.
|
|
1605
|
-
* @throws if no network client could be found matching the given ID.
|
|
1606
|
-
*/
|
|
1607
|
-
#applyNetworkSelection(networkClientId, { updateState, } = {}) {
|
|
1608
|
-
this.#autoManagedNetworkClient = this.getNetworkClientById(networkClientId);
|
|
1609
|
-
this.update((state) => {
|
|
1610
|
-
state.selectedNetworkClientId = networkClientId;
|
|
1611
|
-
state.networksMetadata[networkClientId] ??= {
|
|
1612
|
-
status: NetworkStatus.Unknown,
|
|
1613
|
-
EIPS: {},
|
|
1614
|
-
};
|
|
1615
|
-
updateState?.(state);
|
|
1616
|
-
});
|
|
1617
|
-
if (this.#providerProxy) {
|
|
1618
|
-
this.#providerProxy.setTarget(this.#autoManagedNetworkClient.provider);
|
|
1619
|
-
}
|
|
1620
|
-
else {
|
|
1621
|
-
this.#providerProxy = createSwappableProxy(this.#autoManagedNetworkClient.provider);
|
|
1622
|
-
}
|
|
1623
|
-
if (this.#blockTrackerProxy) {
|
|
1624
|
-
this.#blockTrackerProxy.setTarget(this.#autoManagedNetworkClient.blockTracker);
|
|
1625
|
-
}
|
|
1626
|
-
else {
|
|
1627
|
-
this.#blockTrackerProxy = createEventEmitterProxy(this.#autoManagedNetworkClient.blockTracker, {
|
|
1628
|
-
eventFilter: 'skipInternal',
|
|
1629
|
-
});
|
|
1630
|
-
}
|
|
1631
|
-
this.#ethQuery = new EthQuery(this.#providerProxy);
|
|
1525
|
+
else {
|
|
1526
|
+
__classPrivateFieldSet(this, _NetworkController_providerProxy, createSwappableProxy(__classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClient, "f").provider), "f");
|
|
1632
1527
|
}
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
? {
|
|
1651
|
-
type: RpcEndpointType.Infura,
|
|
1652
|
-
networkClientId: registryNetworkConfig.rpcProviders.default.networkClientId,
|
|
1653
|
-
url: registryNetworkConfig.rpcProviders.default
|
|
1654
|
-
.url,
|
|
1655
|
-
}
|
|
1656
|
-
: {
|
|
1657
|
-
type: RpcEndpointType.Custom,
|
|
1658
|
-
url: registryNetworkConfig.rpcProviders.default.url,
|
|
1659
|
-
};
|
|
1660
|
-
this.addNetwork({
|
|
1661
|
-
chainId: hexChainId,
|
|
1662
|
-
name: registryNetworkConfig.name,
|
|
1663
|
-
nativeCurrency: registryNetworkConfig.assets.native.symbol,
|
|
1664
|
-
rpcEndpoints: [rpcEndpoint],
|
|
1665
|
-
defaultRpcEndpointIndex: 0,
|
|
1666
|
-
blockExplorerUrls: [registryNetworkConfig.blockExplorerUrls.default],
|
|
1667
|
-
defaultBlockExplorerUrlIndex: 0,
|
|
1668
|
-
});
|
|
1669
|
-
}
|
|
1670
|
-
catch (error) {
|
|
1671
|
-
const capturedError = error instanceof Error ? error : new Error(String(error));
|
|
1672
|
-
this.#log?.error(`Failed to auto-enable network for chain ID ${caipChainId}: ${capturedError}`);
|
|
1673
|
-
this.messenger.captureException?.(capturedError);
|
|
1528
|
+
if (__classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f")) {
|
|
1529
|
+
__classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f").setTarget(__classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClient, "f").blockTracker);
|
|
1530
|
+
}
|
|
1531
|
+
else {
|
|
1532
|
+
__classPrivateFieldSet(this, _NetworkController_blockTrackerProxy, createEventEmitterProxy(__classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClient, "f").blockTracker, {
|
|
1533
|
+
eventFilter: 'skipInternal',
|
|
1534
|
+
}), "f");
|
|
1535
|
+
}
|
|
1536
|
+
__classPrivateFieldSet(this, _NetworkController_ethQuery, new EthQuery(__classPrivateFieldGet(this, _NetworkController_providerProxy, "f")), "f");
|
|
1537
|
+
}, _NetworkController_autoAddNetworksFromConfigRegistry = function _NetworkController_autoAddNetworksFromConfigRegistry(caipChainIds) {
|
|
1538
|
+
for (const caipChainId of caipChainIds) {
|
|
1539
|
+
try {
|
|
1540
|
+
const registryNetworkConfig = this.messenger.call('ConfigRegistryController:getNetworkConfigByCaip2ChainId', caipChainId);
|
|
1541
|
+
const hexChainId = numberToHex(Number(parseCaipChainId(caipChainId).reference));
|
|
1542
|
+
if (!registryNetworkConfig ||
|
|
1543
|
+
this.state.networkConfigurationsByChainId[hexChainId]) {
|
|
1544
|
+
continue;
|
|
1674
1545
|
}
|
|
1546
|
+
const rpcEndpoint = registryNetworkConfig.rpcProviders.default.type === 'infura'
|
|
1547
|
+
? {
|
|
1548
|
+
type: RpcEndpointType.Infura,
|
|
1549
|
+
networkClientId: registryNetworkConfig.rpcProviders.default.networkClientId,
|
|
1550
|
+
url: registryNetworkConfig.rpcProviders.default
|
|
1551
|
+
.url,
|
|
1552
|
+
}
|
|
1553
|
+
: {
|
|
1554
|
+
type: RpcEndpointType.Custom,
|
|
1555
|
+
url: registryNetworkConfig.rpcProviders.default.url,
|
|
1556
|
+
};
|
|
1557
|
+
this.addNetwork({
|
|
1558
|
+
chainId: hexChainId,
|
|
1559
|
+
name: registryNetworkConfig.name,
|
|
1560
|
+
nativeCurrency: registryNetworkConfig.assets.native.symbol,
|
|
1561
|
+
rpcEndpoints: [rpcEndpoint],
|
|
1562
|
+
defaultRpcEndpointIndex: 0,
|
|
1563
|
+
blockExplorerUrls: [registryNetworkConfig.blockExplorerUrls.default],
|
|
1564
|
+
defaultBlockExplorerUrlIndex: 0,
|
|
1565
|
+
});
|
|
1566
|
+
}
|
|
1567
|
+
catch (error) {
|
|
1568
|
+
const capturedError = error instanceof Error ? error : new Error(String(error));
|
|
1569
|
+
__classPrivateFieldGet(this, _NetworkController_log, "f")?.error(`Failed to auto-enable network for chain ID ${caipChainId}: ${capturedError}`);
|
|
1570
|
+
this.messenger.captureException?.(capturedError);
|
|
1675
1571
|
}
|
|
1676
1572
|
}
|
|
1677
|
-
}
|
|
1678
|
-
//# sourceMappingURL=NetworkController.
|
|
1573
|
+
};
|
|
1574
|
+
//# sourceMappingURL=NetworkController.mjs.map
|