@metamask-previews/network-controller 36.0.0-preview-c61613ed2 → 36.0.0-preview-3866c0ff1
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 +1 -0
- package/dist/{NetworkController-method-action-types.d.mts → NetworkController-method-action-types.d.ts} +2 -2
- package/dist/NetworkController-method-action-types.d.ts.map +1 -0
- package/dist/{NetworkController-method-action-types.mjs → NetworkController-method-action-types.js} +1 -1
- package/dist/NetworkController-method-action-types.js.map +1 -0
- package/dist/{NetworkController.d.cts → NetworkController.d.ts} +37 -29
- package/dist/NetworkController.d.ts.map +1 -0
- package/dist/{NetworkController.mjs → NetworkController.js} +716 -612
- package/dist/NetworkController.js.map +1 -0
- package/dist/{constants.d.cts → constants.d.ts} +1 -1
- package/dist/constants.d.ts.map +1 -0
- package/dist/{constants.mjs → constants.js} +1 -1
- package/dist/constants.js.map +1 -0
- package/dist/{create-auto-managed-network-client.d.cts → create-auto-managed-network-client.d.ts} +7 -7
- package/dist/create-auto-managed-network-client.d.ts.map +1 -0
- package/dist/{create-auto-managed-network-client.mjs → create-auto-managed-network-client.js} +4 -4
- package/dist/create-auto-managed-network-client.js.map +1 -0
- package/dist/{create-network-client.d.cts → create-network-client.d.ts} +7 -7
- package/dist/create-network-client.d.ts.map +1 -0
- package/dist/{create-network-client.mjs → create-network-client.js} +15 -14
- package/dist/create-network-client.js.map +1 -0
- package/dist/{index.d.cts → index.d.ts} +16 -16
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +4 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +4 -0
- package/dist/logger.js.map +1 -0
- package/dist/rpc-service/{abstract-rpc-service.d.cts → abstract-rpc-service.d.ts} +2 -2
- package/dist/rpc-service/abstract-rpc-service.d.ts.map +1 -0
- package/dist/rpc-service/abstract-rpc-service.js +2 -0
- package/dist/rpc-service/abstract-rpc-service.js.map +1 -0
- package/dist/rpc-service/{rpc-service-chain.d.cts → rpc-service-chain.d.ts} +6 -6
- package/dist/rpc-service/rpc-service-chain.d.ts.map +1 -0
- package/dist/rpc-service/{rpc-service-chain.mjs → rpc-service-chain.js} +57 -70
- package/dist/rpc-service/rpc-service-chain.js.map +1 -0
- package/dist/rpc-service/{rpc-service-requestable.d.cts → rpc-service-requestable.d.ts} +4 -4
- package/dist/rpc-service/rpc-service-requestable.d.ts.map +1 -0
- package/dist/rpc-service/rpc-service-requestable.js +2 -0
- package/dist/rpc-service/rpc-service-requestable.js.map +1 -0
- package/dist/rpc-service/{rpc-service.d.mts → rpc-service.d.ts} +6 -6
- package/dist/rpc-service/rpc-service.d.ts.map +1 -0
- package/dist/rpc-service/{rpc-service.mjs → rpc-service.js} +206 -197
- package/dist/rpc-service/rpc-service.js.map +1 -0
- package/dist/rpc-service/{shared.d.cts → shared.d.ts} +2 -2
- package/dist/rpc-service/shared.d.ts.map +1 -0
- package/dist/rpc-service/shared.js +2 -0
- package/dist/rpc-service/shared.js.map +1 -0
- package/dist/{rpc-service-analytics.d.mts → rpc-service-analytics.d.ts} +3 -3
- package/dist/rpc-service-analytics.d.ts.map +1 -0
- package/dist/{rpc-service-analytics.mjs → rpc-service-analytics.js} +4 -4
- package/dist/rpc-service-analytics.js.map +1 -0
- package/dist/{selectors.d.mts → selectors.d.ts} +2 -2
- package/dist/selectors.d.ts.map +1 -0
- package/dist/{selectors.mjs → selectors.js} +1 -1
- package/dist/selectors.js.map +1 -0
- package/dist/{types.d.cts → types.d.ts} +5 -5
- package/dist/types.d.ts.map +1 -0
- package/dist/{types.mjs → types.js} +1 -1
- package/dist/types.js.map +1 -0
- package/package.json +14 -18
- package/dist/NetworkController-method-action-types.cjs +0 -7
- package/dist/NetworkController-method-action-types.cjs.map +0 -1
- package/dist/NetworkController-method-action-types.d.cts +0 -268
- package/dist/NetworkController-method-action-types.d.cts.map +0 -1
- package/dist/NetworkController-method-action-types.d.mts.map +0 -1
- package/dist/NetworkController-method-action-types.mjs.map +0 -1
- package/dist/NetworkController.cjs +0 -1599
- package/dist/NetworkController.cjs.map +0 -1
- package/dist/NetworkController.d.cts.map +0 -1
- package/dist/NetworkController.d.mts +0 -956
- package/dist/NetworkController.d.mts.map +0 -1
- package/dist/NetworkController.mjs.map +0 -1
- package/dist/constants.cjs +0 -46
- package/dist/constants.cjs.map +0 -1
- package/dist/constants.d.cts.map +0 -1
- package/dist/constants.d.mts +0 -42
- package/dist/constants.d.mts.map +0 -1
- package/dist/constants.mjs.map +0 -1
- package/dist/create-auto-managed-network-client.cjs +0 -149
- package/dist/create-auto-managed-network-client.cjs.map +0 -1
- package/dist/create-auto-managed-network-client.d.cts.map +0 -1
- package/dist/create-auto-managed-network-client.d.mts +0 -74
- package/dist/create-auto-managed-network-client.d.mts.map +0 -1
- package/dist/create-auto-managed-network-client.mjs.map +0 -1
- package/dist/create-network-client.cjs +0 -439
- package/dist/create-network-client.cjs.map +0 -1
- package/dist/create-network-client.d.cts.map +0 -1
- package/dist/create-network-client.d.mts +0 -60
- package/dist/create-network-client.d.mts.map +0 -1
- package/dist/create-network-client.mjs.map +0 -1
- package/dist/index.cjs +0 -31
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts +0 -16
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -6
- package/dist/index.mjs.map +0 -1
- package/dist/logger.cjs +0 -7
- package/dist/logger.cjs.map +0 -1
- package/dist/logger.d.cts +0 -5
- package/dist/logger.d.cts.map +0 -1
- package/dist/logger.d.mts +0 -5
- package/dist/logger.d.mts.map +0 -1
- package/dist/logger.mjs +0 -4
- package/dist/logger.mjs.map +0 -1
- package/dist/rpc-service/abstract-rpc-service.cjs +0 -3
- package/dist/rpc-service/abstract-rpc-service.cjs.map +0 -1
- package/dist/rpc-service/abstract-rpc-service.d.cts.map +0 -1
- package/dist/rpc-service/abstract-rpc-service.d.mts +0 -17
- package/dist/rpc-service/abstract-rpc-service.d.mts.map +0 -1
- package/dist/rpc-service/abstract-rpc-service.mjs +0 -2
- package/dist/rpc-service/abstract-rpc-service.mjs.map +0 -1
- package/dist/rpc-service/rpc-service-chain.cjs +0 -304
- package/dist/rpc-service/rpc-service-chain.cjs.map +0 -1
- package/dist/rpc-service/rpc-service-chain.d.cts.map +0 -1
- package/dist/rpc-service/rpc-service-chain.d.mts +0 -169
- package/dist/rpc-service/rpc-service-chain.d.mts.map +0 -1
- package/dist/rpc-service/rpc-service-chain.mjs.map +0 -1
- package/dist/rpc-service/rpc-service-requestable.cjs +0 -3
- package/dist/rpc-service/rpc-service-requestable.cjs.map +0 -1
- package/dist/rpc-service/rpc-service-requestable.d.cts.map +0 -1
- package/dist/rpc-service/rpc-service-requestable.d.mts +0 -63
- package/dist/rpc-service/rpc-service-requestable.d.mts.map +0 -1
- package/dist/rpc-service/rpc-service-requestable.mjs +0 -2
- package/dist/rpc-service/rpc-service-requestable.mjs.map +0 -1
- package/dist/rpc-service/rpc-service.cjs +0 -540
- package/dist/rpc-service/rpc-service.cjs.map +0 -1
- package/dist/rpc-service/rpc-service.d.cts +0 -247
- package/dist/rpc-service/rpc-service.d.cts.map +0 -1
- package/dist/rpc-service/rpc-service.d.mts.map +0 -1
- package/dist/rpc-service/rpc-service.mjs.map +0 -1
- package/dist/rpc-service/shared.cjs +0 -3
- package/dist/rpc-service/shared.cjs.map +0 -1
- package/dist/rpc-service/shared.d.cts.map +0 -1
- package/dist/rpc-service/shared.d.mts +0 -32
- package/dist/rpc-service/shared.d.mts.map +0 -1
- package/dist/rpc-service/shared.mjs +0 -2
- package/dist/rpc-service/shared.mjs.map +0 -1
- package/dist/rpc-service-analytics.cjs +0 -180
- package/dist/rpc-service-analytics.cjs.map +0 -1
- package/dist/rpc-service-analytics.d.cts +0 -85
- package/dist/rpc-service-analytics.d.cts.map +0 -1
- package/dist/rpc-service-analytics.d.mts.map +0 -1
- package/dist/rpc-service-analytics.mjs.map +0 -1
- package/dist/selectors.cjs +0 -16
- package/dist/selectors.cjs.map +0 -1
- package/dist/selectors.d.cts +0 -22
- package/dist/selectors.d.cts.map +0 -1
- package/dist/selectors.d.mts.map +0 -1
- package/dist/selectors.mjs.map +0 -1
- package/dist/types.cjs +0 -12
- package/dist/types.cjs.map +0 -1
- package/dist/types.d.cts.map +0 -1
- package/dist/types.d.mts +0 -49
- package/dist/types.d.mts.map +0 -1
- package/dist/types.mjs.map +0 -1
|
@@ -1,43 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
import {
|
|
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";
|
|
1
|
+
import { BaseController } from '@metamask/base-controller';
|
|
2
|
+
import { selectEvmAutoEnabledNetworksChainIds } from '@metamask/config-registry-controller';
|
|
3
|
+
import { InfuraNetworkType, CustomNetworkType, NetworkType, isSafeChainId, isInfuraNetworkType, ChainId, NetworksTicker, NetworkNickname, BUILT_IN_CUSTOM_NETWORKS_RPC, BUILT_IN_NETWORKS, DEFAULT_INFURA_NETWORKS, } from '@metamask/controller-utils';
|
|
4
|
+
import EthQuery from '@metamask/eth-query';
|
|
5
|
+
import { errorCodes } from '@metamask/rpc-errors';
|
|
6
|
+
import { createEventEmitterProxy, createSwappableProxy, } from '@metamask/swappable-obj-proxy';
|
|
7
|
+
import { hasProperty, isPlainObject, isStrictHexString, numberToHex, parseCaipChainId, } from '@metamask/utils';
|
|
8
|
+
import deepEqual from 'fast-deep-equal';
|
|
9
|
+
import { produce } from 'immer';
|
|
10
|
+
import { cloneDeep } from 'lodash-es';
|
|
11
|
+
import { createSelector } from 'reselect';
|
|
12
|
+
import * as URI from 'uri-js';
|
|
13
|
+
import { v4 as uuidV4 } from 'uuid';
|
|
14
|
+
import { DEPRECATED_NETWORKS, INFURA_BLOCKED_KEY, NetworkStatus, } from './constants.js';
|
|
15
|
+
import { createAutoManagedNetworkClient } from './create-auto-managed-network-client.js';
|
|
16
|
+
import { projectLogger, createModuleLogger } from './logger.js';
|
|
17
|
+
import { trackRpcServiceDegraded, trackRpcServiceUnavailable, } from './rpc-service-analytics.js';
|
|
18
|
+
import { getRpcFailoverMode } from './selectors.js';
|
|
19
|
+
import { NetworkClientType } from './types.js';
|
|
41
20
|
const debugLog = createModuleLogger(projectLogger, 'NetworkController');
|
|
42
21
|
const INFURA_URL_REGEX = /^https:\/\/(?<networkName>[^.]+)\.infura\.io\/v\d+\/(?<apiKey>.+)$/u;
|
|
43
22
|
/**
|
|
@@ -359,6 +338,20 @@ function buildNetworkConfigurationsByNetworkClientId(networkConfigurationsByChai
|
|
|
359
338
|
* Controller that creates and manages an Ethereum network provider.
|
|
360
339
|
*/
|
|
361
340
|
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';
|
|
362
355
|
/**
|
|
363
356
|
* Constructs a NetworkController.
|
|
364
357
|
*
|
|
@@ -400,57 +393,44 @@ export class NetworkController extends BaseController {
|
|
|
400
393
|
messenger,
|
|
401
394
|
state: correctedInitialState,
|
|
402
395
|
});
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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, {
|
|
396
|
+
this.#infuraProjectId = infuraProjectId;
|
|
397
|
+
this.#failoverUrls = failoverUrls;
|
|
398
|
+
this.#log = log;
|
|
399
|
+
this.#getRpcServiceOptions = getRpcServiceOptions;
|
|
400
|
+
this.#getBlockTrackerOptions = getBlockTrackerOptions;
|
|
401
|
+
this.#analyticsOptions = {
|
|
424
402
|
isRpcEndpointUrlPublic: () => false,
|
|
425
403
|
rpcServiceEventsSampleRate: 0,
|
|
426
404
|
...analyticsOptions,
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
|
|
405
|
+
};
|
|
406
|
+
this.#previouslySelectedNetworkClientId =
|
|
407
|
+
this.state.selectedNetworkClientId;
|
|
408
|
+
this.#networkConfigurationsByNetworkClientId =
|
|
409
|
+
buildNetworkConfigurationsByNetworkClientId(this.state.networkConfigurationsByChainId);
|
|
430
410
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
431
411
|
this.messenger.subscribe(`${this.name}:rpcEndpointChainUnavailable`, ({ networkClientId }) => {
|
|
432
|
-
|
|
412
|
+
this.#updateMetadataForNetwork(networkClientId, {
|
|
433
413
|
networkStatus: NetworkStatus.Unavailable,
|
|
434
414
|
});
|
|
435
415
|
});
|
|
436
416
|
this.messenger.subscribe(`${this.name}:rpcEndpointChainDegraded`, ({ networkClientId }) => {
|
|
437
|
-
|
|
417
|
+
this.#updateMetadataForNetwork(networkClientId, {
|
|
438
418
|
networkStatus: NetworkStatus.Degraded,
|
|
439
419
|
});
|
|
440
420
|
});
|
|
441
421
|
this.messenger.subscribe(`${this.name}:rpcEndpointChainAvailable`, ({ networkClientId }) => {
|
|
442
|
-
|
|
422
|
+
this.#updateMetadataForNetwork(networkClientId, {
|
|
443
423
|
networkStatus: NetworkStatus.Available,
|
|
444
424
|
});
|
|
445
425
|
});
|
|
446
|
-
this.messenger.subscribe(`${this.name}:rpcEndpointUnavailable`, (payload) => trackRpcServiceUnavailable(this.messenger,
|
|
447
|
-
this.messenger.subscribe(`${this.name}:rpcEndpointDegraded`, (payload) => trackRpcServiceDegraded(this.messenger,
|
|
426
|
+
this.messenger.subscribe(`${this.name}:rpcEndpointUnavailable`, (payload) => trackRpcServiceUnavailable(this.messenger, this.#analyticsOptions, payload));
|
|
427
|
+
this.messenger.subscribe(`${this.name}:rpcEndpointDegraded`, (payload) => trackRpcServiceDegraded(this.messenger, this.#analyticsOptions, payload));
|
|
448
428
|
this.messenger.subscribe(
|
|
449
429
|
// eslint-disable-next-line no-restricted-syntax
|
|
450
430
|
'RemoteFeatureFlagController:stateChange', (rpcFailoverMode) => {
|
|
451
|
-
|
|
431
|
+
this.#updateRpcFailover(rpcFailoverMode);
|
|
452
432
|
}, getRpcFailoverMode);
|
|
453
|
-
this.messenger.subscribe('ConfigRegistryController:stateChanged', (caipChainIds) =>
|
|
433
|
+
this.messenger.subscribe('ConfigRegistryController:stateChanged', (caipChainIds) => this.#autoAddNetworksFromConfigRegistry(caipChainIds), selectEvmAutoEnabledNetworksChainIds);
|
|
454
434
|
}
|
|
455
435
|
/**
|
|
456
436
|
* Returns the EthQuery instance for the currently selected network.
|
|
@@ -459,7 +439,31 @@ export class NetworkController extends BaseController {
|
|
|
459
439
|
* initialized.
|
|
460
440
|
*/
|
|
461
441
|
getEthQuery() {
|
|
462
|
-
return
|
|
442
|
+
return this.#ethQuery;
|
|
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;
|
|
463
467
|
}
|
|
464
468
|
/**
|
|
465
469
|
* Accesses the provider and block tracker for the currently selected network.
|
|
@@ -469,8 +473,8 @@ export class NetworkController extends BaseController {
|
|
|
469
473
|
*/
|
|
470
474
|
getProviderAndBlockTracker() {
|
|
471
475
|
return {
|
|
472
|
-
provider:
|
|
473
|
-
blockTracker:
|
|
476
|
+
provider: this.#providerProxy,
|
|
477
|
+
blockTracker: this.#blockTrackerProxy,
|
|
474
478
|
};
|
|
475
479
|
}
|
|
476
480
|
/**
|
|
@@ -479,10 +483,10 @@ export class NetworkController extends BaseController {
|
|
|
479
483
|
* @returns an object with the provider and block tracker proxies for the currently selected network.
|
|
480
484
|
*/
|
|
481
485
|
getSelectedNetworkClient() {
|
|
482
|
-
if (
|
|
486
|
+
if (this.#providerProxy && this.#blockTrackerProxy) {
|
|
483
487
|
return {
|
|
484
|
-
provider:
|
|
485
|
-
blockTracker:
|
|
488
|
+
provider: this.#providerProxy,
|
|
489
|
+
blockTracker: this.#blockTrackerProxy,
|
|
486
490
|
};
|
|
487
491
|
}
|
|
488
492
|
return undefined;
|
|
@@ -506,14 +510,14 @@ export class NetworkController extends BaseController {
|
|
|
506
510
|
* @returns The network clients registered so far, keyed by ID.
|
|
507
511
|
*/
|
|
508
512
|
getNetworkClientRegistry() {
|
|
509
|
-
const autoManagedNetworkClientRegistry =
|
|
513
|
+
const autoManagedNetworkClientRegistry = this.#ensureAutoManagedNetworkClientRegistryPopulated();
|
|
510
514
|
return Object.assign({}, autoManagedNetworkClientRegistry[NetworkClientType.Infura], autoManagedNetworkClientRegistry[NetworkClientType.Custom]);
|
|
511
515
|
}
|
|
512
516
|
getNetworkClientById(networkClientId) {
|
|
513
517
|
if (!networkClientId) {
|
|
514
518
|
throw new Error('No network client ID was provided.');
|
|
515
519
|
}
|
|
516
|
-
const autoManagedNetworkClientRegistry =
|
|
520
|
+
const autoManagedNetworkClientRegistry = this.#ensureAutoManagedNetworkClientRegistryPopulated();
|
|
517
521
|
const infuraNetworkClient = autoManagedNetworkClientRegistry[NetworkClientType.Infura][networkClientId];
|
|
518
522
|
if (infuraNetworkClient) {
|
|
519
523
|
return infuraNetworkClient;
|
|
@@ -524,6 +528,30 @@ export class NetworkController extends BaseController {
|
|
|
524
528
|
}
|
|
525
529
|
return customNetworkClient;
|
|
526
530
|
}
|
|
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
|
+
}
|
|
527
555
|
/**
|
|
528
556
|
* Initialize the NetworkController:
|
|
529
557
|
* - Apply the RPC failover mode from the `corePlatformRpcFailoverMode` remote feature flag;
|
|
@@ -532,9 +560,68 @@ export class NetworkController extends BaseController {
|
|
|
532
560
|
*/
|
|
533
561
|
init() {
|
|
534
562
|
const state = this.messenger.call('RemoteFeatureFlagController:getState');
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
563
|
+
this.#updateRpcFailover(getRpcFailoverMode(state));
|
|
564
|
+
this.#applyNetworkSelection(this.state.selectedNetworkClientId);
|
|
565
|
+
this.#autoAddNetworksFromConfigRegistry(selectEvmAutoEnabledNetworksChainIds(this.messenger.call('ConfigRegistryController:getState')));
|
|
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 };
|
|
538
625
|
}
|
|
539
626
|
/**
|
|
540
627
|
* Uses a request for the latest block to gather the following information on
|
|
@@ -550,10 +637,10 @@ export class NetworkController extends BaseController {
|
|
|
550
637
|
*/
|
|
551
638
|
async lookupNetwork(networkClientId) {
|
|
552
639
|
if (networkClientId) {
|
|
553
|
-
await
|
|
640
|
+
await this.#lookupGivenNetwork(networkClientId);
|
|
554
641
|
}
|
|
555
642
|
else {
|
|
556
|
-
await
|
|
643
|
+
await this.#lookupSelectedNetwork();
|
|
557
644
|
}
|
|
558
645
|
}
|
|
559
646
|
/**
|
|
@@ -573,7 +660,129 @@ export class NetworkController extends BaseController {
|
|
|
573
660
|
// right now.
|
|
574
661
|
/* istanbul ignore next */
|
|
575
662
|
async lookupNetworkByClientId(networkClientId) {
|
|
576
|
-
await
|
|
663
|
+
await this.#lookupGivenNetwork(networkClientId);
|
|
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
|
+
});
|
|
577
786
|
}
|
|
578
787
|
/**
|
|
579
788
|
* Convenience method to update provider network type settings.
|
|
@@ -602,8 +811,33 @@ export class NetworkController extends BaseController {
|
|
|
602
811
|
* network client ID.
|
|
603
812
|
*/
|
|
604
813
|
async setActiveNetwork(networkClientId, options = {}) {
|
|
605
|
-
|
|
606
|
-
|
|
814
|
+
this.#previouslySelectedNetworkClientId =
|
|
815
|
+
this.state.selectedNetworkClientId;
|
|
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
|
+
});
|
|
607
841
|
}
|
|
608
842
|
/**
|
|
609
843
|
* Determines whether the network supports EIP-1559 by checking whether the
|
|
@@ -618,14 +852,14 @@ export class NetworkController extends BaseController {
|
|
|
618
852
|
if (networkClientId) {
|
|
619
853
|
return this.get1559CompatibilityWithNetworkClientId(networkClientId);
|
|
620
854
|
}
|
|
621
|
-
if (!
|
|
855
|
+
if (!this.#ethQuery) {
|
|
622
856
|
return false;
|
|
623
857
|
}
|
|
624
858
|
const { EIPS } = this.state.networksMetadata[this.state.selectedNetworkClientId];
|
|
625
859
|
if (EIPS[1559] !== undefined) {
|
|
626
860
|
return EIPS[1559];
|
|
627
861
|
}
|
|
628
|
-
const isEIP1559Compatible = await
|
|
862
|
+
const isEIP1559Compatible = await this.#determineEIP1559Compatibility(this.state.selectedNetworkClientId);
|
|
629
863
|
this.update((state) => {
|
|
630
864
|
if (isEIP1559Compatible !== undefined) {
|
|
631
865
|
state.networksMetadata[state.selectedNetworkClientId].EIPS[1559] =
|
|
@@ -644,12 +878,28 @@ export class NetworkController extends BaseController {
|
|
|
644
878
|
// may want to include some 'freshness' value - something to make sure we refetch this from time to time
|
|
645
879
|
return EIPS[1559];
|
|
646
880
|
}
|
|
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
|
+
}
|
|
647
897
|
/**
|
|
648
898
|
* Ensures that the provider and block tracker proxies are pointed to the
|
|
649
899
|
* currently selected network and refreshes the metadata for the
|
|
650
900
|
*/
|
|
651
901
|
async resetConnection() {
|
|
652
|
-
await
|
|
902
|
+
await this.#refreshNetwork(this.state.selectedNetworkClientId);
|
|
653
903
|
}
|
|
654
904
|
/**
|
|
655
905
|
* Returns the network configuration that has been filed under the given chain
|
|
@@ -669,7 +919,7 @@ export class NetworkController extends BaseController {
|
|
|
669
919
|
* @returns The network configuration if one exists, or undefined.
|
|
670
920
|
*/
|
|
671
921
|
getNetworkConfigurationByNetworkClientId(networkClientId) {
|
|
672
|
-
return
|
|
922
|
+
return this.#networkConfigurationsByNetworkClientId.get(networkClientId);
|
|
673
923
|
}
|
|
674
924
|
/**
|
|
675
925
|
* Creates and registers network clients for the collection of Infura and
|
|
@@ -684,8 +934,8 @@ export class NetworkController extends BaseController {
|
|
|
684
934
|
*/
|
|
685
935
|
addNetwork(fields) {
|
|
686
936
|
const { rpcEndpoints: setOfRpcEndpointFields } = fields;
|
|
687
|
-
const autoManagedNetworkClientRegistry =
|
|
688
|
-
|
|
937
|
+
const autoManagedNetworkClientRegistry = this.#ensureAutoManagedNetworkClientRegistryPopulated();
|
|
938
|
+
this.#validateNetworkFields({
|
|
689
939
|
mode: 'add',
|
|
690
940
|
networkFields: fields,
|
|
691
941
|
autoManagedNetworkClientRegistry,
|
|
@@ -702,17 +952,17 @@ export class NetworkController extends BaseController {
|
|
|
702
952
|
rpcEndpoint,
|
|
703
953
|
};
|
|
704
954
|
});
|
|
705
|
-
const newNetworkConfiguration =
|
|
955
|
+
const newNetworkConfiguration = this.#determineNetworkConfigurationToPersist({
|
|
706
956
|
networkFields: fields,
|
|
707
957
|
networkClientOperations,
|
|
708
958
|
});
|
|
709
|
-
|
|
959
|
+
this.#registerNetworkClientsAsNeeded({
|
|
710
960
|
networkFields: fields,
|
|
711
961
|
networkClientOperations,
|
|
712
962
|
autoManagedNetworkClientRegistry,
|
|
713
963
|
});
|
|
714
964
|
this.update((state) => {
|
|
715
|
-
|
|
965
|
+
this.#updateNetworkConfigurations({
|
|
716
966
|
state,
|
|
717
967
|
mode: 'add',
|
|
718
968
|
networkFields: fields,
|
|
@@ -753,8 +1003,8 @@ export class NetworkController extends BaseController {
|
|
|
753
1003
|
}
|
|
754
1004
|
const existingChainId = chainId;
|
|
755
1005
|
const { chainId: newChainId, rpcEndpoints: setOfNewRpcEndpointFields } = fields;
|
|
756
|
-
const autoManagedNetworkClientRegistry =
|
|
757
|
-
|
|
1006
|
+
const autoManagedNetworkClientRegistry = this.#ensureAutoManagedNetworkClientRegistryPopulated();
|
|
1007
|
+
this.#validateNetworkFields({
|
|
758
1008
|
mode: 'update',
|
|
759
1009
|
networkFields: fields,
|
|
760
1010
|
existingNetworkConfiguration,
|
|
@@ -862,7 +1112,7 @@ export class NetworkController extends BaseController {
|
|
|
862
1112
|
networkClientOperations.push(networkClientOperation);
|
|
863
1113
|
}
|
|
864
1114
|
}
|
|
865
|
-
const updatedNetworkConfiguration =
|
|
1115
|
+
const updatedNetworkConfiguration = this.#determineNetworkConfigurationToPersist({
|
|
866
1116
|
networkFields: fields,
|
|
867
1117
|
networkClientOperations,
|
|
868
1118
|
});
|
|
@@ -879,7 +1129,7 @@ export class NetworkController extends BaseController {
|
|
|
879
1129
|
})) {
|
|
880
1130
|
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.`);
|
|
881
1131
|
}
|
|
882
|
-
|
|
1132
|
+
this.#registerNetworkClientsAsNeeded({
|
|
883
1133
|
networkFields: fields,
|
|
884
1134
|
networkClientOperations,
|
|
885
1135
|
autoManagedNetworkClientRegistry,
|
|
@@ -905,7 +1155,7 @@ export class NetworkController extends BaseController {
|
|
|
905
1155
|
rpcEndpointToSelect.networkClientId !== this.state.selectedNetworkClientId) {
|
|
906
1156
|
await this.setActiveNetwork(rpcEndpointToSelect.networkClientId, {
|
|
907
1157
|
updateState: (state) => {
|
|
908
|
-
|
|
1158
|
+
this.#updateNetworkConfigurations({
|
|
909
1159
|
state,
|
|
910
1160
|
mode: 'update',
|
|
911
1161
|
networkFields: fields,
|
|
@@ -917,7 +1167,7 @@ export class NetworkController extends BaseController {
|
|
|
917
1167
|
}
|
|
918
1168
|
else {
|
|
919
1169
|
this.update((state) => {
|
|
920
|
-
|
|
1170
|
+
this.#updateNetworkConfigurations({
|
|
921
1171
|
state,
|
|
922
1172
|
mode: 'update',
|
|
923
1173
|
networkFields: fields,
|
|
@@ -926,7 +1176,7 @@ export class NetworkController extends BaseController {
|
|
|
926
1176
|
});
|
|
927
1177
|
});
|
|
928
1178
|
}
|
|
929
|
-
|
|
1179
|
+
this.#unregisterNetworkClientsAsNeeded({
|
|
930
1180
|
networkClientOperations,
|
|
931
1181
|
autoManagedNetworkClientRegistry,
|
|
932
1182
|
});
|
|
@@ -949,19 +1199,19 @@ export class NetworkController extends BaseController {
|
|
|
949
1199
|
if (existingNetworkConfiguration.rpcEndpoints.some((rpcEndpoint) => rpcEndpoint.networkClientId === this.state.selectedNetworkClientId)) {
|
|
950
1200
|
throw new Error(`Cannot remove the currently selected network`);
|
|
951
1201
|
}
|
|
952
|
-
const autoManagedNetworkClientRegistry =
|
|
1202
|
+
const autoManagedNetworkClientRegistry = this.#ensureAutoManagedNetworkClientRegistryPopulated();
|
|
953
1203
|
const networkClientOperations = existingNetworkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
|
|
954
1204
|
return {
|
|
955
1205
|
type: 'remove',
|
|
956
1206
|
rpcEndpoint,
|
|
957
1207
|
};
|
|
958
1208
|
});
|
|
959
|
-
|
|
1209
|
+
this.#unregisterNetworkClientsAsNeeded({
|
|
960
1210
|
networkClientOperations,
|
|
961
1211
|
autoManagedNetworkClientRegistry,
|
|
962
1212
|
});
|
|
963
1213
|
this.update((state) => {
|
|
964
|
-
|
|
1214
|
+
this.#updateNetworkConfigurations({
|
|
965
1215
|
state,
|
|
966
1216
|
mode: 'remove',
|
|
967
1217
|
existingNetworkConfiguration,
|
|
@@ -980,7 +1230,7 @@ export class NetworkController extends BaseController {
|
|
|
980
1230
|
* to {@link resetConnection}.
|
|
981
1231
|
*/
|
|
982
1232
|
async rollbackToPreviousProvider() {
|
|
983
|
-
await
|
|
1233
|
+
await this.#refreshNetwork(this.#previouslySelectedNetworkClientId);
|
|
984
1234
|
}
|
|
985
1235
|
/**
|
|
986
1236
|
* Deactivates the controller, stopping any ongoing polling.
|
|
@@ -990,7 +1240,7 @@ export class NetworkController extends BaseController {
|
|
|
990
1240
|
// We're intentionally changing the signature of an extended method.
|
|
991
1241
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
992
1242
|
async destroy() {
|
|
993
|
-
await
|
|
1243
|
+
await this.#blockTrackerProxy?.destroy();
|
|
994
1244
|
}
|
|
995
1245
|
/**
|
|
996
1246
|
* Merges the given backup data into controller state.
|
|
@@ -1024,551 +1274,405 @@ export class NetworkController extends BaseController {
|
|
|
1024
1274
|
const { networkClientId } = networkConfiguration.rpcEndpoints[networkConfiguration.defaultRpcEndpointIndex];
|
|
1025
1275
|
return networkClientId;
|
|
1026
1276
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1277
|
+
/**
|
|
1278
|
+
* Ensure that the given fields which will be used to either add or update a
|
|
1279
|
+
* network are valid.
|
|
1280
|
+
*
|
|
1281
|
+
* @param args - The arguments.
|
|
1282
|
+
*/
|
|
1283
|
+
#validateNetworkFields(args) {
|
|
1284
|
+
const { mode, networkFields, autoManagedNetworkClientRegistry } = args;
|
|
1285
|
+
const existingNetworkConfiguration = 'existingNetworkConfiguration' in args
|
|
1286
|
+
? args.existingNetworkConfiguration
|
|
1287
|
+
: null;
|
|
1288
|
+
const errorMessagePrefix = mode === 'update' ? 'Could not update network' : 'Could not add network';
|
|
1289
|
+
if (!isStrictHexString(networkFields.chainId) ||
|
|
1290
|
+
!isSafeChainId(networkFields.chainId)) {
|
|
1291
|
+
throw new Error(`${errorMessagePrefix}: Invalid \`chainId\` '${networkFields.chainId}' (must start with "0x" and not exceed the maximum)`);
|
|
1040
1292
|
}
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
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);
|
|
1293
|
+
if (networkFields.chainId !== existingNetworkConfiguration?.chainId) {
|
|
1294
|
+
const existingNetworkConfigurationViaChainId = this.state.networkConfigurationsByChainId[networkFields.chainId];
|
|
1295
|
+
if (existingNetworkConfigurationViaChainId !== undefined) {
|
|
1296
|
+
if (existingNetworkConfiguration === null) {
|
|
1297
|
+
throw new Error(`Could not add network for chain ${args.networkFields.chainId} as another network for that chain already exists ('${existingNetworkConfigurationViaChainId.name}')`);
|
|
1100
1298
|
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: json parse error: ', error);
|
|
1299
|
+
else {
|
|
1300
|
+
throw new Error(`Cannot move network from chain ${existingNetworkConfiguration.chainId} to ${networkFields.chainId} as another network for that chain already exists ('${existingNetworkConfigurationViaChainId.name}')`);
|
|
1104
1301
|
}
|
|
1105
1302
|
}
|
|
1106
|
-
if (isPlainObject(responseBody) &&
|
|
1107
|
-
responseBody.error === INFURA_BLOCKED_KEY) {
|
|
1108
|
-
networkStatus = NetworkStatus.Blocked;
|
|
1109
|
-
}
|
|
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);
|
|
1113
|
-
}
|
|
1114
|
-
else {
|
|
1115
|
-
networkStatus = NetworkStatus.Unavailable;
|
|
1116
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: ', error);
|
|
1117
|
-
}
|
|
1118
1303
|
}
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1304
|
+
const isInvalidDefaultBlockExplorerUrlIndex = networkFields.blockExplorerUrls.length > 0
|
|
1305
|
+
? networkFields.defaultBlockExplorerUrlIndex === undefined ||
|
|
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\``);
|
|
1123
1310
|
}
|
|
1124
|
-
|
|
1125
|
-
|
|
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);
|
|
1311
|
+
if (networkFields.rpcEndpoints.length === 0) {
|
|
1312
|
+
throw new Error(`${errorMessagePrefix}: \`rpcEndpoints\` must be a non-empty array`);
|
|
1167
1313
|
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
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`);
|
|
1326
|
+
}
|
|
1327
|
+
if (networkFields.rpcEndpoints.some((otherRpcEndpointFields) => otherRpcEndpointFields !== rpcEndpointFields &&
|
|
1328
|
+
URI.equal(otherRpcEndpointFields.url, rpcEndpointFields.url))) {
|
|
1329
|
+
throw new Error(`${errorMessagePrefix}: Each entry in rpcEndpoints must have a unique URL`);
|
|
1330
|
+
}
|
|
1331
|
+
const networkConfigurationsForOtherChains = Object.values(this.state.networkConfigurationsByChainId).filter((networkConfiguration) => existingNetworkConfiguration
|
|
1332
|
+
? networkConfiguration.chainId !==
|
|
1333
|
+
existingNetworkConfiguration.chainId
|
|
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
|
+
}
|
|
1183
1345
|
}
|
|
1184
1346
|
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
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;
|
|
1347
|
+
if ([...new Set(networkFields.rpcEndpoints)].length <
|
|
1348
|
+
networkFields.rpcEndpoints.length) {
|
|
1349
|
+
throw new Error(`${errorMessagePrefix}: Each entry in rpcEndpoints must be unique`);
|
|
1201
1350
|
}
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
this.messenger.publish('NetworkController:infuraIsUnblocked');
|
|
1351
|
+
const networkClientIds = networkFields.rpcEndpoints
|
|
1352
|
+
.map((rpcEndpoint) => 'networkClientId' in rpcEndpoint
|
|
1353
|
+
? rpcEndpoint.networkClientId
|
|
1354
|
+
: undefined)
|
|
1355
|
+
.filter((networkClientId) => networkClientId !== undefined);
|
|
1356
|
+
if ([...new Set(networkClientIds)].length < networkClientIds.length) {
|
|
1357
|
+
throw new Error(`${errorMessagePrefix}: Each entry in rpcEndpoints must have a unique networkClientId`);
|
|
1210
1358
|
}
|
|
1211
|
-
|
|
1212
|
-
|
|
1359
|
+
const infuraRpcEndpoints = networkFields.rpcEndpoints.filter((rpcEndpointFields) => rpcEndpointFields.type === RpcEndpointType.Infura);
|
|
1360
|
+
if (infuraRpcEndpoints.length > 1) {
|
|
1361
|
+
throw new Error(`${errorMessagePrefix}: There cannot be more than one Infura RPC endpoint`);
|
|
1213
1362
|
}
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
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');
|
|
1220
|
-
}
|
|
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];
|
|
1233
|
-
}
|
|
1234
|
-
else {
|
|
1235
|
-
newMetadata.EIPS[1559] = metadata.isEIP1559Compatible;
|
|
1236
|
-
}
|
|
1363
|
+
if (networkFields.rpcEndpoints[networkFields.defaultRpcEndpointIndex] ===
|
|
1364
|
+
undefined) {
|
|
1365
|
+
throw new Error(`${errorMessagePrefix}: \`defaultRpcEndpointIndex\` must refer to an entry in \`rpcEndpoints\``);
|
|
1237
1366
|
}
|
|
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;
|
|
1269
1367
|
}
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1368
|
+
/**
|
|
1369
|
+
* Constructs a network configuration that will be persisted to state when
|
|
1370
|
+
* adding or updating a network.
|
|
1371
|
+
*
|
|
1372
|
+
* @param args - The arguments to this function.
|
|
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 };
|
|
1280
1392
|
}
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1393
|
+
/**
|
|
1394
|
+
* Creates and registers network clients using the given operations calculated
|
|
1395
|
+
* as a part of adding or updating a network.
|
|
1396
|
+
*
|
|
1397
|
+
* @param args - The arguments to this function.
|
|
1398
|
+
* @param args.networkFields - The fields used to add or update a network.
|
|
1399
|
+
* @param args.networkClientOperations - Dictate which network clients need to
|
|
1400
|
+
* be created.
|
|
1401
|
+
* @param args.autoManagedNetworkClientRegistry - The network client registry
|
|
1402
|
+
* to update.
|
|
1403
|
+
*/
|
|
1404
|
+
#registerNetworkClientsAsNeeded({ networkFields, networkClientOperations, autoManagedNetworkClientRegistry, }) {
|
|
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
|
+
});
|
|
1286
1434
|
}
|
|
1287
1435
|
else {
|
|
1288
|
-
|
|
1436
|
+
autoManagedNetworkClientRegistry[NetworkClientType.Custom][addedRpcEndpoint.networkClientId] = createAutoManagedNetworkClient({
|
|
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
|
+
});
|
|
1289
1451
|
}
|
|
1290
1452
|
}
|
|
1291
1453
|
}
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
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
|
-
}
|
|
1332
|
-
}
|
|
1454
|
+
/**
|
|
1455
|
+
* Destroys and removes network clients using the given operations calculated
|
|
1456
|
+
* as a part of updating or removing a network.
|
|
1457
|
+
*
|
|
1458
|
+
* @param args - The arguments to this function.
|
|
1459
|
+
* @param args.networkClientOperations - Dictate which network clients to
|
|
1460
|
+
* remove.
|
|
1461
|
+
* @param args.autoManagedNetworkClientRegistry - The network client registry
|
|
1462
|
+
* to update.
|
|
1463
|
+
*/
|
|
1464
|
+
#unregisterNetworkClientsAsNeeded({ networkClientOperations, autoManagedNetworkClientRegistry, }) {
|
|
1465
|
+
const removedRpcEndpoints = networkClientOperations
|
|
1466
|
+
.filter((networkClientOperation) => {
|
|
1467
|
+
return networkClientOperation.type === 'remove';
|
|
1468
|
+
})
|
|
1469
|
+
.map((networkClientOperation) => networkClientOperation.rpcEndpoint)
|
|
1470
|
+
.concat(networkClientOperations
|
|
1471
|
+
.filter((networkClientOperation) => {
|
|
1472
|
+
return networkClientOperation.type === 'replace';
|
|
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];
|
|
1333
1479
|
}
|
|
1334
1480
|
}
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
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
|
-
});
|
|
1481
|
+
/**
|
|
1482
|
+
* Updates `networkConfigurationsByChainId` in state depending on whether a
|
|
1483
|
+
* network is being added, updated, or removed.
|
|
1484
|
+
*
|
|
1485
|
+
* - The existing network configuration will be removed when a network is
|
|
1486
|
+
* being filed under a different chain or removed.
|
|
1487
|
+
* - A network configuration will be stored when a network is being added or
|
|
1488
|
+
* when a network is being updated.
|
|
1489
|
+
*
|
|
1490
|
+
* @param args - The arguments to this function.
|
|
1491
|
+
*/
|
|
1492
|
+
#updateNetworkConfigurations(args) {
|
|
1493
|
+
const { state, mode } = args;
|
|
1494
|
+
if (mode === 'remove' ||
|
|
1495
|
+
(mode === 'update' &&
|
|
1496
|
+
args.networkFields.chainId !==
|
|
1497
|
+
args.existingNetworkConfiguration.chainId)) {
|
|
1498
|
+
delete state.networkConfigurationsByChainId[args.existingNetworkConfiguration.chainId];
|
|
1398
1499
|
}
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
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
|
-
});
|
|
1500
|
+
if (mode === 'add' || mode === 'update') {
|
|
1501
|
+
if (!deepEqual(state.networkConfigurationsByChainId[args.networkFields.chainId], args.networkConfigurationToPersist)) {
|
|
1502
|
+
args.networkConfigurationToPersist.lastUpdatedAt = Date.now();
|
|
1503
|
+
}
|
|
1504
|
+
state.networkConfigurationsByChainId[args.networkFields.chainId] =
|
|
1505
|
+
args.networkConfigurationToPersist;
|
|
1415
1506
|
}
|
|
1507
|
+
this.#networkConfigurationsByNetworkClientId =
|
|
1508
|
+
buildNetworkConfigurationsByNetworkClientId(cloneDeep(state.networkConfigurationsByChainId));
|
|
1416
1509
|
}
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
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;
|
|
1510
|
+
/**
|
|
1511
|
+
* Before accessing or switching the network, the registry of network clients
|
|
1512
|
+
* needs to be populated. Otherwise, `#applyNetworkSelection` and
|
|
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());
|
|
1447
1521
|
}
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
const
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1522
|
+
/**
|
|
1523
|
+
* Constructs the registry of network clients based on the set of default
|
|
1524
|
+
* and custom networks in state.
|
|
1525
|
+
*
|
|
1526
|
+
* @returns The network clients keyed by ID.
|
|
1527
|
+
*/
|
|
1528
|
+
#createAutoManagedNetworkClientRegistry() {
|
|
1529
|
+
const chainIds = knownKeysOf(this.state.networkConfigurationsByChainId);
|
|
1530
|
+
const networkClientsWithIds = chainIds.flatMap((chainId) => {
|
|
1531
|
+
const networkConfiguration = this.state.networkConfigurationsByChainId[chainId];
|
|
1532
|
+
const defaultFailoverUrls = this.#failoverUrls?.[chainId];
|
|
1533
|
+
return networkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
|
|
1534
|
+
if (rpcEndpoint.type === RpcEndpointType.Infura) {
|
|
1535
|
+
const infuraNetworkName = deriveInfuraNetworkNameFromRpcEndpointUrl(rpcEndpoint.url);
|
|
1536
|
+
return [
|
|
1537
|
+
rpcEndpoint.networkClientId,
|
|
1538
|
+
createAutoManagedNetworkClient({
|
|
1539
|
+
networkClientId: rpcEndpoint.networkClientId,
|
|
1540
|
+
networkClientConfiguration: {
|
|
1541
|
+
type: NetworkClientType.Infura,
|
|
1542
|
+
network: infuraNetworkName,
|
|
1543
|
+
failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
|
|
1544
|
+
infuraProjectId: this.#infuraProjectId,
|
|
1545
|
+
chainId: networkConfiguration.chainId,
|
|
1546
|
+
ticker: networkConfiguration.nativeCurrency,
|
|
1547
|
+
},
|
|
1548
|
+
getRpcServiceOptions: this.#getRpcServiceOptions,
|
|
1549
|
+
getBlockTrackerOptions: this.#getBlockTrackerOptions,
|
|
1550
|
+
messenger: this.messenger,
|
|
1551
|
+
rpcFailoverMode: this.#rpcFailoverMode,
|
|
1552
|
+
logger: this.#log,
|
|
1553
|
+
}),
|
|
1554
|
+
];
|
|
1555
|
+
}
|
|
1459
1556
|
return [
|
|
1460
1557
|
rpcEndpoint.networkClientId,
|
|
1461
1558
|
createAutoManagedNetworkClient({
|
|
1462
1559
|
networkClientId: rpcEndpoint.networkClientId,
|
|
1463
1560
|
networkClientConfiguration: {
|
|
1464
|
-
type: NetworkClientType.
|
|
1465
|
-
network: infuraNetworkName,
|
|
1466
|
-
failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
|
|
1467
|
-
infuraProjectId: __classPrivateFieldGet(this, _NetworkController_infuraProjectId, "f"),
|
|
1561
|
+
type: NetworkClientType.Custom,
|
|
1468
1562
|
chainId: networkConfiguration.chainId,
|
|
1563
|
+
failoverRpcUrls: defaultFailoverUrls ?? rpcEndpoint.failoverUrls,
|
|
1564
|
+
rpcUrl: rpcEndpoint.url,
|
|
1469
1565
|
ticker: networkConfiguration.nativeCurrency,
|
|
1470
1566
|
},
|
|
1471
|
-
getRpcServiceOptions:
|
|
1472
|
-
getBlockTrackerOptions:
|
|
1567
|
+
getRpcServiceOptions: this.#getRpcServiceOptions,
|
|
1568
|
+
getBlockTrackerOptions: this.#getBlockTrackerOptions,
|
|
1473
1569
|
messenger: this.messenger,
|
|
1474
|
-
rpcFailoverMode:
|
|
1475
|
-
logger:
|
|
1570
|
+
rpcFailoverMode: this.#rpcFailoverMode,
|
|
1571
|
+
logger: this.#log,
|
|
1476
1572
|
}),
|
|
1477
1573
|
];
|
|
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
|
-
];
|
|
1574
|
+
});
|
|
1497
1575
|
});
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
}
|
|
1506
|
-
}
|
|
1507
|
-
|
|
1508
|
-
|
|
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: {},
|
|
1576
|
+
return networkClientsWithIds.reduce((obj, [networkClientId, networkClient]) => {
|
|
1577
|
+
return {
|
|
1578
|
+
...obj,
|
|
1579
|
+
[networkClient.configuration.type]: {
|
|
1580
|
+
...obj[networkClient.configuration.type],
|
|
1581
|
+
[networkClientId]: networkClient,
|
|
1582
|
+
},
|
|
1583
|
+
};
|
|
1584
|
+
}, {
|
|
1585
|
+
[NetworkClientType.Custom]: {},
|
|
1586
|
+
[NetworkClientType.Infura]: {},
|
|
1519
1587
|
});
|
|
1520
|
-
updateState?.(state);
|
|
1521
|
-
});
|
|
1522
|
-
if (__classPrivateFieldGet(this, _NetworkController_providerProxy, "f")) {
|
|
1523
|
-
__classPrivateFieldGet(this, _NetworkController_providerProxy, "f").setTarget(__classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClient, "f").provider);
|
|
1524
|
-
}
|
|
1525
|
-
else {
|
|
1526
|
-
__classPrivateFieldSet(this, _NetworkController_providerProxy, createSwappableProxy(__classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClient, "f").provider), "f");
|
|
1527
|
-
}
|
|
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
1588
|
}
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1589
|
+
/**
|
|
1590
|
+
* Updates the global provider and block tracker proxies (accessible via
|
|
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',
|
|
1565
1629
|
});
|
|
1566
1630
|
}
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1631
|
+
this.#ethQuery = new EthQuery(this.#providerProxy);
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* Adds networks to state and registers network clients for the given CAIP-2
|
|
1635
|
+
* chain IDs if they are not already present. Configurations for these
|
|
1636
|
+
* networks are retrieved from ConfigRegistryController.
|
|
1637
|
+
*
|
|
1638
|
+
* @param caipChainIds - The CAIP-2 chain IDs of the networks to enable.
|
|
1639
|
+
*/
|
|
1640
|
+
#autoAddNetworksFromConfigRegistry(caipChainIds) {
|
|
1641
|
+
for (const caipChainId of caipChainIds) {
|
|
1642
|
+
try {
|
|
1643
|
+
const registryNetworkConfig = this.messenger.call('ConfigRegistryController:getNetworkConfigByCaip2ChainId', caipChainId);
|
|
1644
|
+
const hexChainId = numberToHex(Number(parseCaipChainId(caipChainId).reference));
|
|
1645
|
+
if (!registryNetworkConfig ||
|
|
1646
|
+
this.state.networkConfigurationsByChainId[hexChainId]) {
|
|
1647
|
+
continue;
|
|
1648
|
+
}
|
|
1649
|
+
const rpcEndpoint = registryNetworkConfig.rpcProviders.default.type === 'infura'
|
|
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);
|
|
1674
|
+
}
|
|
1571
1675
|
}
|
|
1572
1676
|
}
|
|
1573
|
-
}
|
|
1574
|
-
//# sourceMappingURL=NetworkController.
|
|
1677
|
+
}
|
|
1678
|
+
//# sourceMappingURL=NetworkController.js.map
|