@metamask-previews/network-controller 24.1.0-preview-f3f2e8c8 → 24.1.0-preview-1f079244
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 +19 -0
- package/dist/NetworkController.cjs +226 -200
- package/dist/NetworkController.cjs.map +1 -1
- package/dist/NetworkController.d.cts +40 -21
- package/dist/NetworkController.d.cts.map +1 -1
- package/dist/NetworkController.d.mts +40 -21
- package/dist/NetworkController.d.mts.map +1 -1
- package/dist/NetworkController.mjs +226 -200
- package/dist/NetworkController.mjs.map +1 -1
- package/dist/create-auto-managed-network-client.cjs +3 -1
- package/dist/create-auto-managed-network-client.cjs.map +1 -1
- package/dist/create-auto-managed-network-client.d.cts +4 -1
- package/dist/create-auto-managed-network-client.d.cts.map +1 -1
- package/dist/create-auto-managed-network-client.d.mts +4 -1
- package/dist/create-auto-managed-network-client.d.mts.map +1 -1
- package/dist/create-auto-managed-network-client.mjs +3 -1
- package/dist/create-auto-managed-network-client.mjs.map +1 -1
- package/dist/create-network-client.cjs +3 -1
- package/dist/create-network-client.cjs.map +1 -1
- package/dist/create-network-client.d.cts +4 -1
- package/dist/create-network-client.d.cts.map +1 -1
- package/dist/create-network-client.d.mts +4 -1
- package/dist/create-network-client.d.mts.map +1 -1
- package/dist/create-network-client.mjs +3 -1
- package/dist/create-network-client.mjs.map +1 -1
- package/dist/rpc-service/rpc-service.cjs +17 -3
- package/dist/rpc-service/rpc-service.cjs.map +1 -1
- package/dist/rpc-service/rpc-service.d.cts +5 -0
- package/dist/rpc-service/rpc-service.d.cts.map +1 -1
- package/dist/rpc-service/rpc-service.d.mts +5 -0
- package/dist/rpc-service/rpc-service.d.mts.map +1 -1
- package/dist/rpc-service/rpc-service.mjs +19 -5
- package/dist/rpc-service/rpc-service.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Add two new controller state metadata properties: `includeInStateLogs` and `usedInUi` ([#6525](https://github.com/MetaMask/core/pull/6525))
|
|
13
|
+
- Add `lookupNetwork` option to `initializeProvider`, to allow for skipping the request used to populate metadata for the globally selected network ([#6575](https://github.com/MetaMask/core/pull/6575))
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
|
|
17
|
+
- Bump `@metamask/base-controller` from `^8.1.0` to `^8.3.0` ([#6355](https://github.com/MetaMask/core/pull/6355), [#6465](https://github.com/MetaMask/core/pull/6465))
|
|
18
|
+
- Rephrase "circuit broken" errors so they are more user-friendly ([#6423](https://github.com/MetaMask/core/pull/6423))
|
|
19
|
+
- These are errors produced when a request is made to an RPC endpoint after it returns too many consecutive 5xx responses and the underlying circuit is open.
|
|
20
|
+
- Bump `@metamask/utils` from `^11.4.2` to `^11.8.0` ([#6588](https://github.com/MetaMask/core/pull/6588))
|
|
21
|
+
|
|
22
|
+
### Deprecated
|
|
23
|
+
|
|
24
|
+
- Deprecate `lookupNetworkByClientId` ([#6308](https://github.com/MetaMask/core/pull/6308))
|
|
25
|
+
- `lookupNetwork` already supports passing in a network client ID; please use this going forward instead.
|
|
26
|
+
|
|
10
27
|
## [24.1.0]
|
|
11
28
|
|
|
12
29
|
### Added
|
|
@@ -18,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
18
35
|
|
|
19
36
|
- Bump `@metamask/base-controller` from `^8.0.1` to `^8.1.0` ([#6284](https://github.com/MetaMask/core/pull/6284))
|
|
20
37
|
- Bump `@metamask/controller-utils` from `^11.11.0` to `^11.12.0` ([#6303](https://github.com/MetaMask/core/pull/6303))
|
|
38
|
+
- This effectively changes the `onDegraded` property on `AbstractRpcService` so that the event listener payload may be an object with either a `endpointUrl` property, `error` + `endpointUrl` properties, or `value` + `endpointUrl` properties
|
|
39
|
+
- **NOTE:** Although `error` and `value` are new, optional properties, this change makes an inadvertent breaking change to the signature of the event listener due to how TypeScript compares function types. We have conciously decided not to re-release this change under a major version, so be advised.
|
|
21
40
|
|
|
22
41
|
## [24.0.1]
|
|
23
42
|
|
|
@@ -36,7 +36,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
36
36
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
37
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
38
|
};
|
|
39
|
-
var _NetworkController_instances, _NetworkController_ethQuery, _NetworkController_infuraProjectId, _NetworkController_previouslySelectedNetworkClientId, _NetworkController_providerProxy, _NetworkController_blockTrackerProxy, _NetworkController_autoManagedNetworkClientRegistry, _NetworkController_autoManagedNetworkClient, _NetworkController_log, _NetworkController_getRpcServiceOptions, _NetworkController_getBlockTrackerOptions, _NetworkController_networkConfigurationsByNetworkClientId, _NetworkController_isRpcFailoverEnabled, _NetworkController_updateRpcFailoverEnabled, _NetworkController_refreshNetwork, _NetworkController_getLatestBlock, _NetworkController_determineEIP1559Compatibility, _NetworkController_validateNetworkFields, _NetworkController_determineNetworkConfigurationToPersist, _NetworkController_registerNetworkClientsAsNeeded, _NetworkController_unregisterNetworkClientsAsNeeded, _NetworkController_updateNetworkConfigurations, _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated, _NetworkController_createAutoManagedNetworkClientRegistry, _NetworkController_applyNetworkSelection;
|
|
39
|
+
var _NetworkController_instances, _NetworkController_ethQuery, _NetworkController_infuraProjectId, _NetworkController_previouslySelectedNetworkClientId, _NetworkController_providerProxy, _NetworkController_blockTrackerProxy, _NetworkController_autoManagedNetworkClientRegistry, _NetworkController_autoManagedNetworkClient, _NetworkController_log, _NetworkController_getRpcServiceOptions, _NetworkController_getBlockTrackerOptions, _NetworkController_networkConfigurationsByNetworkClientId, _NetworkController_isRpcFailoverEnabled, _NetworkController_updateRpcFailoverEnabled, _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;
|
|
40
40
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
41
|
exports.NetworkController = exports.selectAvailableNetworkClientIds = exports.getAvailableNetworkClientIds = exports.selectNetworkConfigurations = exports.getNetworkConfigurations = exports.getDefaultNetworkControllerState = exports.knownKeysOf = exports.RpcEndpointType = void 0;
|
|
42
42
|
const base_controller_1 = require("@metamask/base-controller");
|
|
@@ -386,16 +386,22 @@ class NetworkController extends base_controller_1.BaseController {
|
|
|
386
386
|
name: controllerName,
|
|
387
387
|
metadata: {
|
|
388
388
|
selectedNetworkClientId: {
|
|
389
|
+
includeInStateLogs: true,
|
|
389
390
|
persist: true,
|
|
390
391
|
anonymous: false,
|
|
392
|
+
usedInUi: true,
|
|
391
393
|
},
|
|
392
394
|
networksMetadata: {
|
|
395
|
+
includeInStateLogs: true,
|
|
393
396
|
persist: true,
|
|
394
397
|
anonymous: false,
|
|
398
|
+
usedInUi: true,
|
|
395
399
|
},
|
|
396
400
|
networkConfigurationsByChainId: {
|
|
401
|
+
includeInStateLogs: true,
|
|
397
402
|
persist: true,
|
|
398
403
|
anonymous: false,
|
|
404
|
+
usedInUi: true,
|
|
399
405
|
},
|
|
400
406
|
},
|
|
401
407
|
messenger,
|
|
@@ -560,220 +566,68 @@ class NetworkController extends base_controller_1.BaseController {
|
|
|
560
566
|
return customNetworkClient;
|
|
561
567
|
}
|
|
562
568
|
/**
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
|
|
567
|
-
async initializeProvider() {
|
|
568
|
-
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_applyNetworkSelection).call(this, this.state.selectedNetworkClientId);
|
|
569
|
-
await this.lookupNetwork();
|
|
570
|
-
}
|
|
571
|
-
/**
|
|
572
|
-
* Refreshes the network meta with EIP-1559 support and the network status
|
|
573
|
-
* based on the given network client ID.
|
|
569
|
+
* Creates proxies for accessing the global network client and its block
|
|
570
|
+
* tracker. You must call this method in order to use
|
|
571
|
+
* `getProviderAndBlockTracker` (or its replacement,
|
|
572
|
+
* `getSelectedNetworkClient`).
|
|
574
573
|
*
|
|
575
|
-
* @param
|
|
574
|
+
* @param options - Optional arguments.
|
|
575
|
+
* @param options.lookupNetwork - Usually, metadata for the global network
|
|
576
|
+
* will be populated via a call to `lookupNetwork` after creating the provider
|
|
577
|
+
* and block tracker proxies. This allows for responding to the status of the
|
|
578
|
+
* global network after initializing this controller; however, it requires
|
|
579
|
+
* making a request to the network to do so. In the clients, where controllers
|
|
580
|
+
* are initialized before the UI is shown, this may be undesirable, as it
|
|
581
|
+
* means that if the user has just installed MetaMask, their IP address may be
|
|
582
|
+
* shared with a third party before they have a chance to finish onboarding.
|
|
583
|
+
* You can pass `false` if you'd like to disable this request and call
|
|
584
|
+
* `lookupNetwork` yourself.
|
|
576
585
|
*/
|
|
577
|
-
async
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
try {
|
|
582
|
-
updatedIsEIP1559Compatible =
|
|
583
|
-
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, networkClientId);
|
|
584
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Available;
|
|
585
|
-
}
|
|
586
|
-
catch (error) {
|
|
587
|
-
debugLog('NetworkController: lookupNetworkByClientId: ', error);
|
|
588
|
-
// TODO: mock ethQuery.sendAsync to throw error without error code
|
|
589
|
-
/* istanbul ignore else */
|
|
590
|
-
if (isErrorWithCode(error)) {
|
|
591
|
-
let responseBody;
|
|
592
|
-
if (isInfura &&
|
|
593
|
-
(0, utils_1.hasProperty)(error, 'message') &&
|
|
594
|
-
typeof error.message === 'string') {
|
|
595
|
-
try {
|
|
596
|
-
responseBody = JSON.parse(error.message);
|
|
597
|
-
}
|
|
598
|
-
catch {
|
|
599
|
-
// error.message must not be JSON
|
|
600
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetworkByClientId: json parse error: ', error);
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
if ((0, utils_1.isPlainObject)(responseBody) &&
|
|
604
|
-
responseBody.error === constants_1.INFURA_BLOCKED_KEY) {
|
|
605
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Blocked;
|
|
606
|
-
}
|
|
607
|
-
else if (error.code === rpc_errors_1.errorCodes.rpc.internal) {
|
|
608
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
|
|
609
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetworkByClientId: rpc internal error: ', error);
|
|
610
|
-
}
|
|
611
|
-
else {
|
|
612
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Unavailable;
|
|
613
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetworkByClientId: ', error);
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
else if (typeof Error !== 'undefined' &&
|
|
617
|
-
(0, utils_1.hasProperty)(error, 'message') &&
|
|
618
|
-
typeof error.message === 'string' &&
|
|
619
|
-
error.message.includes('No custom network client was found with the ID')) {
|
|
620
|
-
throw error;
|
|
621
|
-
}
|
|
622
|
-
else {
|
|
623
|
-
debugLog('NetworkController - could not determine network status', error);
|
|
624
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
|
|
625
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetworkByClientId: ', error);
|
|
626
|
-
}
|
|
586
|
+
async initializeProvider({ lookupNetwork = true, } = {}) {
|
|
587
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_applyNetworkSelection).call(this, this.state.selectedNetworkClientId);
|
|
588
|
+
if (lookupNetwork) {
|
|
589
|
+
await this.lookupNetwork();
|
|
627
590
|
}
|
|
628
|
-
this.update((state) => {
|
|
629
|
-
if (state.networksMetadata[networkClientId] === undefined) {
|
|
630
|
-
state.networksMetadata[networkClientId] = {
|
|
631
|
-
status: constants_1.NetworkStatus.Unknown,
|
|
632
|
-
EIPS: {},
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
const meta = state.networksMetadata[networkClientId];
|
|
636
|
-
meta.status = updatedNetworkStatus;
|
|
637
|
-
if (updatedIsEIP1559Compatible === undefined) {
|
|
638
|
-
delete meta.EIPS[1559];
|
|
639
|
-
}
|
|
640
|
-
else {
|
|
641
|
-
meta.EIPS[1559] = updatedIsEIP1559Compatible;
|
|
642
|
-
}
|
|
643
|
-
});
|
|
644
591
|
}
|
|
645
592
|
/**
|
|
646
|
-
*
|
|
647
|
-
* state:
|
|
648
|
-
*
|
|
649
|
-
* - The status of the network, namely, whether it is available, geo-blocked
|
|
650
|
-
* (Infura only), or unavailable, or whether the status is unknown
|
|
651
|
-
* - Whether the network supports EIP-1559, or whether it is unknown
|
|
593
|
+
* Uses a request for the latest block to gather the following information on
|
|
594
|
+
* the given or selected network, persisting it to state:
|
|
652
595
|
*
|
|
653
|
-
*
|
|
654
|
-
*
|
|
655
|
-
*
|
|
596
|
+
* - The connectivity status: whether it is available, geo-blocked (Infura
|
|
597
|
+
* only), unavailable, or unknown
|
|
598
|
+
* - The capabilities status: whether it supports EIP-1559, whether it does
|
|
599
|
+
* not, or whether it is unknown
|
|
656
600
|
*
|
|
657
|
-
* @param networkClientId - The ID of the network client to
|
|
601
|
+
* @param networkClientId - The ID of the network client to inspect.
|
|
658
602
|
* If no ID is provided, uses the currently selected network.
|
|
659
603
|
*/
|
|
660
604
|
async lookupNetwork(networkClientId) {
|
|
661
605
|
if (networkClientId) {
|
|
662
|
-
await this.
|
|
663
|
-
return;
|
|
664
|
-
}
|
|
665
|
-
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
666
|
-
return;
|
|
667
|
-
}
|
|
668
|
-
const isInfura = __classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClient, "f")?.configuration.type ===
|
|
669
|
-
types_1.NetworkClientType.Infura;
|
|
670
|
-
let networkChanged = false;
|
|
671
|
-
const listener = () => {
|
|
672
|
-
networkChanged = true;
|
|
673
|
-
try {
|
|
674
|
-
this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
|
|
675
|
-
}
|
|
676
|
-
catch (error) {
|
|
677
|
-
// In theory, this `catch` should not be necessary given that this error
|
|
678
|
-
// would occur "inside" of the call to `#determineEIP1559Compatibility`
|
|
679
|
-
// below and so it should be caught by the `try`/`catch` below (it is
|
|
680
|
-
// impossible to reproduce in tests for that reason). However, somehow
|
|
681
|
-
// it occurs within Mobile and so we have to add our own `try`/`catch`
|
|
682
|
-
// here.
|
|
683
|
-
/* istanbul ignore next */
|
|
684
|
-
if (!(error instanceof Error) ||
|
|
685
|
-
error.message !==
|
|
686
|
-
'Subscription not found for event: NetworkController:networkDidChange') {
|
|
687
|
-
// Again, this error should not happen and is impossible to reproduce
|
|
688
|
-
// in tests.
|
|
689
|
-
/* istanbul ignore next */
|
|
690
|
-
throw error;
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
};
|
|
694
|
-
this.messagingSystem.subscribe('NetworkController:networkDidChange', listener);
|
|
695
|
-
let updatedNetworkStatus;
|
|
696
|
-
let updatedIsEIP1559Compatible;
|
|
697
|
-
try {
|
|
698
|
-
const isEIP1559Compatible = await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, this.state.selectedNetworkClientId);
|
|
699
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Available;
|
|
700
|
-
updatedIsEIP1559Compatible = isEIP1559Compatible;
|
|
701
|
-
}
|
|
702
|
-
catch (error) {
|
|
703
|
-
// TODO: mock ethQuery.sendAsync to throw error without error code
|
|
704
|
-
/* istanbul ignore else */
|
|
705
|
-
if (isErrorWithCode(error)) {
|
|
706
|
-
let responseBody;
|
|
707
|
-
if (isInfura &&
|
|
708
|
-
(0, utils_1.hasProperty)(error, 'message') &&
|
|
709
|
-
typeof error.message === 'string') {
|
|
710
|
-
try {
|
|
711
|
-
responseBody = JSON.parse(error.message);
|
|
712
|
-
}
|
|
713
|
-
catch (parseError) {
|
|
714
|
-
// error.message must not be JSON
|
|
715
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: json parse error', parseError);
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
if ((0, utils_1.isPlainObject)(responseBody) &&
|
|
719
|
-
responseBody.error === constants_1.INFURA_BLOCKED_KEY) {
|
|
720
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Blocked;
|
|
721
|
-
}
|
|
722
|
-
else if (error.code === rpc_errors_1.errorCodes.rpc.internal) {
|
|
723
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
|
|
724
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: rpc internal error', error);
|
|
725
|
-
}
|
|
726
|
-
else {
|
|
727
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Unavailable;
|
|
728
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: ', error);
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
else {
|
|
732
|
-
debugLog('NetworkController - could not determine network status', error);
|
|
733
|
-
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
|
|
734
|
-
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: ', error);
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
if (networkChanged) {
|
|
738
|
-
// If the network has changed, then `lookupNetwork` either has been or is
|
|
739
|
-
// in the process of being called, so we don't need to go further.
|
|
740
|
-
return;
|
|
741
|
-
}
|
|
742
|
-
try {
|
|
743
|
-
this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
|
|
744
|
-
}
|
|
745
|
-
catch (error) {
|
|
746
|
-
if (!(error instanceof Error) ||
|
|
747
|
-
error.message !==
|
|
748
|
-
'Subscription not found for event: NetworkController:networkDidChange') {
|
|
749
|
-
throw error;
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
this.update((state) => {
|
|
753
|
-
const meta = state.networksMetadata[state.selectedNetworkClientId];
|
|
754
|
-
meta.status = updatedNetworkStatus;
|
|
755
|
-
if (updatedIsEIP1559Compatible === undefined) {
|
|
756
|
-
delete meta.EIPS[1559];
|
|
757
|
-
}
|
|
758
|
-
else {
|
|
759
|
-
meta.EIPS[1559] = updatedIsEIP1559Compatible;
|
|
760
|
-
}
|
|
761
|
-
});
|
|
762
|
-
if (isInfura) {
|
|
763
|
-
if (updatedNetworkStatus === constants_1.NetworkStatus.Available) {
|
|
764
|
-
this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
|
|
765
|
-
}
|
|
766
|
-
else if (updatedNetworkStatus === constants_1.NetworkStatus.Blocked) {
|
|
767
|
-
this.messagingSystem.publish('NetworkController:infuraIsBlocked');
|
|
768
|
-
}
|
|
606
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_lookupGivenNetwork).call(this, networkClientId);
|
|
769
607
|
}
|
|
770
608
|
else {
|
|
771
|
-
|
|
772
|
-
// prevent consumers from being stuck in a blocked state if they were
|
|
773
|
-
// previously connected to an Infura network that was blocked
|
|
774
|
-
this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
|
|
609
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_lookupSelectedNetwork).call(this);
|
|
775
610
|
}
|
|
776
611
|
}
|
|
612
|
+
/**
|
|
613
|
+
* Uses a request for the latest block to gather the following information on
|
|
614
|
+
* the given network, persisting it to state:
|
|
615
|
+
*
|
|
616
|
+
* - The connectivity status: whether the network is available, geo-blocked
|
|
617
|
+
* (Infura only), unavailable, or unknown
|
|
618
|
+
* - The feature compatibility status: whether the network supports EIP-1559,
|
|
619
|
+
* whether it does not, or whether it is unknown
|
|
620
|
+
*
|
|
621
|
+
* @param networkClientId - The ID of the network client to inspect.
|
|
622
|
+
* @deprecated Please use `lookupNetwork` and pass a network client ID
|
|
623
|
+
* instead. This method will be removed in a future major version.
|
|
624
|
+
*/
|
|
625
|
+
// We are planning on removing this so we aren't interested in testing this
|
|
626
|
+
// right now.
|
|
627
|
+
/* istanbul ignore next */
|
|
628
|
+
async lookupNetworkByClientId(networkClientId) {
|
|
629
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_lookupGivenNetwork).call(this, networkClientId);
|
|
630
|
+
}
|
|
777
631
|
/**
|
|
778
632
|
* Convenience method to update provider network type settings.
|
|
779
633
|
*
|
|
@@ -1268,6 +1122,174 @@ async function _NetworkController_refreshNetwork(networkClientId, options = {})
|
|
|
1268
1122
|
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_applyNetworkSelection).call(this, networkClientId, options);
|
|
1269
1123
|
this.messagingSystem.publish('NetworkController:networkDidChange', this.state);
|
|
1270
1124
|
await this.lookupNetwork();
|
|
1125
|
+
}, _NetworkController_determineNetworkMetadata =
|
|
1126
|
+
/**
|
|
1127
|
+
* Uses a request for the latest block to gather the following information on
|
|
1128
|
+
* the given network:
|
|
1129
|
+
*
|
|
1130
|
+
* - The connectivity status: whether it is available, geo-blocked (Infura
|
|
1131
|
+
* only), unavailable, or unknown
|
|
1132
|
+
* - The capabilities status: whether it supports EIP-1559, whether it does
|
|
1133
|
+
* not, or whether it is unknown
|
|
1134
|
+
*
|
|
1135
|
+
* @param networkClientId - The ID of the network client to inspect.
|
|
1136
|
+
* If no ID is provided, uses the currently selected network.
|
|
1137
|
+
* @returns The resulting metadata for the network.
|
|
1138
|
+
*/
|
|
1139
|
+
async function _NetworkController_determineNetworkMetadata(networkClientId) {
|
|
1140
|
+
// Force TypeScript to use one of the two overloads explicitly
|
|
1141
|
+
const networkClient = (0, controller_utils_1.isInfuraNetworkType)(networkClientId)
|
|
1142
|
+
? this.getNetworkClientById(networkClientId)
|
|
1143
|
+
: this.getNetworkClientById(networkClientId);
|
|
1144
|
+
const isInfura = networkClient.configuration.type === types_1.NetworkClientType.Infura;
|
|
1145
|
+
let networkStatus;
|
|
1146
|
+
let isEIP1559Compatible;
|
|
1147
|
+
try {
|
|
1148
|
+
isEIP1559Compatible =
|
|
1149
|
+
await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, networkClientId);
|
|
1150
|
+
networkStatus = constants_1.NetworkStatus.Available;
|
|
1151
|
+
}
|
|
1152
|
+
catch (error) {
|
|
1153
|
+
debugLog('NetworkController: lookupNetwork: ', error);
|
|
1154
|
+
if (isErrorWithCode(error)) {
|
|
1155
|
+
let responseBody;
|
|
1156
|
+
if (isInfura &&
|
|
1157
|
+
(0, utils_1.hasProperty)(error, 'message') &&
|
|
1158
|
+
typeof error.message === 'string') {
|
|
1159
|
+
try {
|
|
1160
|
+
responseBody = JSON.parse(error.message);
|
|
1161
|
+
}
|
|
1162
|
+
catch {
|
|
1163
|
+
// error.message must not be JSON
|
|
1164
|
+
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: json parse error: ', error);
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
if ((0, utils_1.isPlainObject)(responseBody) &&
|
|
1168
|
+
responseBody.error === constants_1.INFURA_BLOCKED_KEY) {
|
|
1169
|
+
networkStatus = constants_1.NetworkStatus.Blocked;
|
|
1170
|
+
}
|
|
1171
|
+
else if (error.code === rpc_errors_1.errorCodes.rpc.internal) {
|
|
1172
|
+
networkStatus = constants_1.NetworkStatus.Unknown;
|
|
1173
|
+
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: rpc internal error: ', error);
|
|
1174
|
+
}
|
|
1175
|
+
else {
|
|
1176
|
+
networkStatus = constants_1.NetworkStatus.Unavailable;
|
|
1177
|
+
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: ', error);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
else {
|
|
1181
|
+
debugLog('NetworkController - could not determine network status', error);
|
|
1182
|
+
networkStatus = constants_1.NetworkStatus.Unknown;
|
|
1183
|
+
__classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: ', error);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
return { isInfura, networkStatus, isEIP1559Compatible };
|
|
1187
|
+
}, _NetworkController_lookupGivenNetwork =
|
|
1188
|
+
/**
|
|
1189
|
+
* Uses a request for the latest block to gather the following information on
|
|
1190
|
+
* the given network, persisting it to state:
|
|
1191
|
+
*
|
|
1192
|
+
* - The connectivity status: whether the network is available, geo-blocked
|
|
1193
|
+
* (Infura only), unavailable, or unknown
|
|
1194
|
+
* - The feature compatibility status: whether the network supports EIP-1559,
|
|
1195
|
+
* whether it does not, or whether it is unknown
|
|
1196
|
+
*
|
|
1197
|
+
* @param networkClientId - The ID of the network client to inspect.
|
|
1198
|
+
*/
|
|
1199
|
+
async function _NetworkController_lookupGivenNetwork(networkClientId) {
|
|
1200
|
+
const { networkStatus, isEIP1559Compatible } = await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineNetworkMetadata).call(this, networkClientId);
|
|
1201
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateMetadataForNetwork).call(this, networkClientId, networkStatus, isEIP1559Compatible);
|
|
1202
|
+
}, _NetworkController_lookupSelectedNetwork =
|
|
1203
|
+
/**
|
|
1204
|
+
* Uses a request for the latest block to gather the following information on
|
|
1205
|
+
* the currently selected network, persisting it to state:
|
|
1206
|
+
*
|
|
1207
|
+
* - The connectivity status: whether the network is available, geo-blocked
|
|
1208
|
+
* (Infura only), unavailable, or unknown
|
|
1209
|
+
* - The feature compatibility status: whether the network supports EIP-1559,
|
|
1210
|
+
* whether it does not, or whether it is unknown
|
|
1211
|
+
*
|
|
1212
|
+
* Note that it is possible for the current network to be switched while this
|
|
1213
|
+
* method is running. If that is the case, it will exit early (as this method
|
|
1214
|
+
* will also run for the new network).
|
|
1215
|
+
*/
|
|
1216
|
+
async function _NetworkController_lookupSelectedNetwork() {
|
|
1217
|
+
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
let networkChanged = false;
|
|
1221
|
+
const listener = () => {
|
|
1222
|
+
networkChanged = true;
|
|
1223
|
+
try {
|
|
1224
|
+
this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
|
|
1225
|
+
}
|
|
1226
|
+
catch (error) {
|
|
1227
|
+
// In theory, this `catch` should not be necessary given that this error
|
|
1228
|
+
// would occur "inside" of the call to `#determineEIP1559Compatibility`
|
|
1229
|
+
// below and so it should be caught by the `try`/`catch` below (it is
|
|
1230
|
+
// impossible to reproduce in tests for that reason). However, somehow
|
|
1231
|
+
// it occurs within Mobile and so we have to add our own `try`/`catch`
|
|
1232
|
+
// here.
|
|
1233
|
+
/* istanbul ignore next */
|
|
1234
|
+
if (!(error instanceof Error) ||
|
|
1235
|
+
error.message !==
|
|
1236
|
+
'Subscription not found for event: NetworkController:networkDidChange') {
|
|
1237
|
+
// Again, this error should not happen and is impossible to reproduce
|
|
1238
|
+
// in tests.
|
|
1239
|
+
/* istanbul ignore next */
|
|
1240
|
+
throw error;
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
};
|
|
1244
|
+
this.messagingSystem.subscribe('NetworkController:networkDidChange', listener);
|
|
1245
|
+
const { isInfura, networkStatus, isEIP1559Compatible } = await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineNetworkMetadata).call(this, this.state.selectedNetworkClientId);
|
|
1246
|
+
if (networkChanged) {
|
|
1247
|
+
// If the network has changed, then `lookupNetwork` either has been or is
|
|
1248
|
+
// in the process of being called, so we don't need to go further.
|
|
1249
|
+
return;
|
|
1250
|
+
}
|
|
1251
|
+
try {
|
|
1252
|
+
this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
|
|
1253
|
+
}
|
|
1254
|
+
catch (error) {
|
|
1255
|
+
if (!(error instanceof Error) ||
|
|
1256
|
+
error.message !==
|
|
1257
|
+
'Subscription not found for event: NetworkController:networkDidChange') {
|
|
1258
|
+
throw error;
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateMetadataForNetwork).call(this, this.state.selectedNetworkClientId, networkStatus, isEIP1559Compatible);
|
|
1262
|
+
if (isInfura) {
|
|
1263
|
+
if (networkStatus === constants_1.NetworkStatus.Available) {
|
|
1264
|
+
this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
|
|
1265
|
+
}
|
|
1266
|
+
else if (networkStatus === constants_1.NetworkStatus.Blocked) {
|
|
1267
|
+
this.messagingSystem.publish('NetworkController:infuraIsBlocked');
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
else {
|
|
1271
|
+
// Always publish infuraIsUnblocked regardless of network status to
|
|
1272
|
+
// prevent consumers from being stuck in a blocked state if they were
|
|
1273
|
+
// previously connected to an Infura network that was blocked
|
|
1274
|
+
this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
|
|
1275
|
+
}
|
|
1276
|
+
}, _NetworkController_updateMetadataForNetwork = function _NetworkController_updateMetadataForNetwork(networkClientId, networkStatus, isEIP1559Compatible) {
|
|
1277
|
+
this.update((state) => {
|
|
1278
|
+
if (state.networksMetadata[networkClientId] === undefined) {
|
|
1279
|
+
state.networksMetadata[networkClientId] = {
|
|
1280
|
+
status: constants_1.NetworkStatus.Unknown,
|
|
1281
|
+
EIPS: {},
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
const meta = state.networksMetadata[networkClientId];
|
|
1285
|
+
meta.status = networkStatus;
|
|
1286
|
+
if (isEIP1559Compatible === undefined) {
|
|
1287
|
+
delete meta.EIPS[1559];
|
|
1288
|
+
}
|
|
1289
|
+
else {
|
|
1290
|
+
meta.EIPS[1559] = isEIP1559Compatible;
|
|
1291
|
+
}
|
|
1292
|
+
});
|
|
1271
1293
|
}, _NetworkController_getLatestBlock = function _NetworkController_getLatestBlock(networkClientId) {
|
|
1272
1294
|
if (networkClientId === undefined) {
|
|
1273
1295
|
networkClientId = this.state.selectedNetworkClientId;
|
|
@@ -1466,6 +1488,7 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
|
|
|
1466
1488
|
getBlockTrackerOptions: __classPrivateFieldGet(this, _NetworkController_getBlockTrackerOptions, "f"),
|
|
1467
1489
|
messenger: this.messagingSystem,
|
|
1468
1490
|
isRpcFailoverEnabled: __classPrivateFieldGet(this, _NetworkController_isRpcFailoverEnabled, "f"),
|
|
1491
|
+
logger: __classPrivateFieldGet(this, _NetworkController_log, "f"),
|
|
1469
1492
|
});
|
|
1470
1493
|
}
|
|
1471
1494
|
else {
|
|
@@ -1481,6 +1504,7 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
|
|
|
1481
1504
|
getBlockTrackerOptions: __classPrivateFieldGet(this, _NetworkController_getBlockTrackerOptions, "f"),
|
|
1482
1505
|
messenger: this.messagingSystem,
|
|
1483
1506
|
isRpcFailoverEnabled: __classPrivateFieldGet(this, _NetworkController_isRpcFailoverEnabled, "f"),
|
|
1507
|
+
logger: __classPrivateFieldGet(this, _NetworkController_log, "f"),
|
|
1484
1508
|
});
|
|
1485
1509
|
}
|
|
1486
1510
|
}
|
|
@@ -1540,6 +1564,7 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
|
|
|
1540
1564
|
getBlockTrackerOptions: __classPrivateFieldGet(this, _NetworkController_getBlockTrackerOptions, "f"),
|
|
1541
1565
|
messenger: this.messagingSystem,
|
|
1542
1566
|
isRpcFailoverEnabled: __classPrivateFieldGet(this, _NetworkController_isRpcFailoverEnabled, "f"),
|
|
1567
|
+
logger: __classPrivateFieldGet(this, _NetworkController_log, "f"),
|
|
1543
1568
|
}),
|
|
1544
1569
|
];
|
|
1545
1570
|
}
|
|
@@ -1557,6 +1582,7 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
|
|
|
1557
1582
|
getBlockTrackerOptions: __classPrivateFieldGet(this, _NetworkController_getBlockTrackerOptions, "f"),
|
|
1558
1583
|
messenger: this.messagingSystem,
|
|
1559
1584
|
isRpcFailoverEnabled: __classPrivateFieldGet(this, _NetworkController_isRpcFailoverEnabled, "f"),
|
|
1585
|
+
logger: __classPrivateFieldGet(this, _NetworkController_log, "f"),
|
|
1560
1586
|
}),
|
|
1561
1587
|
];
|
|
1562
1588
|
});
|