@metamask-previews/network-controller 24.1.0-preview-332b557 → 24.1.0-preview-09aab698

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 CHANGED
@@ -7,11 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ### Removed
11
-
12
- - **BREAKING:** Remove `lookupNetworkByClientId` ([#6308](https://github.com/MetaMask/core/pull/6308))
13
- - `lookupNetwork` already supports passing in a network client ID; please use this instead.
14
-
15
10
  ## [24.1.0]
16
11
 
17
12
  ### Added
@@ -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_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;
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;
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");
@@ -569,23 +569,209 @@ class NetworkController extends base_controller_1.BaseController {
569
569
  await this.lookupNetwork();
570
570
  }
571
571
  /**
572
- * Uses a request for the latest block to gather the following information on
573
- * the given or selected network, persisting it to state:
572
+ * Refreshes the network meta with EIP-1559 support and the network status
573
+ * based on the given network client ID.
574
574
  *
575
- * - The connectivity status: whether it is available, geo-blocked (Infura
576
- * only), unavailable, or unknown
577
- * - The capabilities status: whether it supports EIP-1559, whether it does
578
- * not, or whether it is unknown
575
+ * @param networkClientId - The ID of the network client to update.
576
+ */
577
+ async lookupNetworkByClientId(networkClientId) {
578
+ const isInfura = (0, controller_utils_1.isInfuraNetworkType)(networkClientId);
579
+ let updatedNetworkStatus;
580
+ let updatedIsEIP1559Compatible;
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
+ }
627
+ }
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
+ }
645
+ /**
646
+ * Persists the following metadata about the given or selected network to
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
579
652
  *
580
- * @param networkClientId - The ID of the network client to inspect.
653
+ * Note that it is possible for the network to be switched while this data is
654
+ * being collected. If that is the case, no metadata for the (now previously)
655
+ * selected network will be updated.
656
+ *
657
+ * @param networkClientId - The ID of the network client to update.
581
658
  * If no ID is provided, uses the currently selected network.
582
659
  */
583
660
  async lookupNetwork(networkClientId) {
584
661
  if (networkClientId) {
585
- await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_lookupGivenNetwork).call(this, networkClientId);
662
+ await this.lookupNetworkByClientId(networkClientId);
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
+ }
586
769
  }
587
770
  else {
588
- await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_lookupSelectedNetwork).call(this);
771
+ // Always publish infuraIsUnblocked regardless of network status to
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');
589
775
  }
590
776
  }
591
777
  /**
@@ -1082,171 +1268,6 @@ async function _NetworkController_refreshNetwork(networkClientId, options = {})
1082
1268
  __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_applyNetworkSelection).call(this, networkClientId, options);
1083
1269
  this.messagingSystem.publish('NetworkController:networkDidChange', this.state);
1084
1270
  await this.lookupNetwork();
1085
- }, _NetworkController_determineNetworkMetadata =
1086
- /**
1087
- * Uses a request for the latest block to gather the following information on
1088
- * the given network:
1089
- *
1090
- * - The connectivity status: whether it is available, geo-blocked (Infura
1091
- * only), unavailable, or unknown
1092
- * - The capabilities status: whether it supports EIP-1559, whether it does
1093
- * not, or whether it is unknown
1094
- *
1095
- * @param networkClientId - The ID of the network client to inspect.
1096
- * If no ID is provided, uses the currently selected network.
1097
- * @returns The resulting metadata for the network.
1098
- */
1099
- async function _NetworkController_determineNetworkMetadata(networkClientId) {
1100
- // Force TypeScript to use one of the two overloads explicitly
1101
- const networkClient = (0, controller_utils_1.isInfuraNetworkType)(networkClientId)
1102
- ? this.getNetworkClientById(networkClientId)
1103
- : this.getNetworkClientById(networkClientId);
1104
- const isInfura = networkClient.configuration.type === types_1.NetworkClientType.Infura;
1105
- let networkStatus;
1106
- let isEIP1559Compatible;
1107
- try {
1108
- isEIP1559Compatible =
1109
- await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this, networkClientId);
1110
- networkStatus = constants_1.NetworkStatus.Available;
1111
- }
1112
- catch (error) {
1113
- debugLog('NetworkController: lookupNetwork: ', error);
1114
- if (isErrorWithCode(error)) {
1115
- let responseBody;
1116
- if (isInfura &&
1117
- (0, utils_1.hasProperty)(error, 'message') &&
1118
- typeof error.message === 'string') {
1119
- try {
1120
- responseBody = JSON.parse(error.message);
1121
- }
1122
- catch {
1123
- // error.message must not be JSON
1124
- __classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: json parse error: ', error);
1125
- }
1126
- }
1127
- if ((0, utils_1.isPlainObject)(responseBody) &&
1128
- responseBody.error === constants_1.INFURA_BLOCKED_KEY) {
1129
- networkStatus = constants_1.NetworkStatus.Blocked;
1130
- }
1131
- else if (error.code === rpc_errors_1.errorCodes.rpc.internal) {
1132
- networkStatus = constants_1.NetworkStatus.Unknown;
1133
- __classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: rpc internal error: ', error);
1134
- }
1135
- else {
1136
- networkStatus = constants_1.NetworkStatus.Unavailable;
1137
- __classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: ', error);
1138
- }
1139
- }
1140
- else {
1141
- debugLog('NetworkController - could not determine network status', error);
1142
- networkStatus = constants_1.NetworkStatus.Unknown;
1143
- __classPrivateFieldGet(this, _NetworkController_log, "f")?.warn('NetworkController: lookupNetwork: ', error);
1144
- }
1145
- }
1146
- return { isInfura, networkStatus, isEIP1559Compatible };
1147
- }, _NetworkController_lookupGivenNetwork =
1148
- /**
1149
- * Uses a request for the latest block to gather the following information on
1150
- * the given network, persisting it to state:
1151
- *
1152
- * - The connectivity status: whether the network is available, geo-blocked
1153
- * (Infura only), unavailable, or unknown
1154
- * - The feature compatibility status: whether the network supports EIP-1559,
1155
- * whether it does not, or whether it is unknown
1156
- *
1157
- * @param networkClientId - The ID of the network client to inspect.
1158
- */
1159
- async function _NetworkController_lookupGivenNetwork(networkClientId) {
1160
- const { networkStatus, isEIP1559Compatible } = await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineNetworkMetadata).call(this, networkClientId);
1161
- __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateMetadataForNetwork).call(this, networkClientId, networkStatus, isEIP1559Compatible);
1162
- }, _NetworkController_lookupSelectedNetwork =
1163
- /**
1164
- * Uses a request for the latest block to gather the following information on
1165
- * the currently selected network, persisting it to state:
1166
- *
1167
- * - The connectivity status: whether the network is available, geo-blocked
1168
- * (Infura only), unavailable, or unknown
1169
- * - The feature compatibility status: whether the network supports EIP-1559,
1170
- * whether it does not, or whether it is unknown
1171
- *
1172
- * Note that it is possible for the current network to be switched while this
1173
- * method is running. If that is the case, it will exit early (as this method
1174
- * will also run for the new network).
1175
- */
1176
- async function _NetworkController_lookupSelectedNetwork() {
1177
- let networkChanged = false;
1178
- const listener = () => {
1179
- networkChanged = true;
1180
- try {
1181
- this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
1182
- }
1183
- catch (error) {
1184
- // In theory, this `catch` should not be necessary given that this error
1185
- // would occur "inside" of the call to `#determineEIP1559Compatibility`
1186
- // below and so it should be caught by the `try`/`catch` below (it is
1187
- // impossible to reproduce in tests for that reason). However, somehow
1188
- // it occurs within Mobile and so we have to add our own `try`/`catch`
1189
- // here.
1190
- /* istanbul ignore next */
1191
- if (!(error instanceof Error) ||
1192
- error.message !==
1193
- 'Subscription not found for event: NetworkController:networkDidChange') {
1194
- // Again, this error should not happen and is impossible to reproduce
1195
- // in tests.
1196
- /* istanbul ignore next */
1197
- throw error;
1198
- }
1199
- }
1200
- };
1201
- this.messagingSystem.subscribe('NetworkController:networkDidChange', listener);
1202
- const { isInfura, networkStatus, isEIP1559Compatible } = await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineNetworkMetadata).call(this, this.state.selectedNetworkClientId);
1203
- if (networkChanged) {
1204
- // If the network has changed, then `lookupNetwork` either has been or is
1205
- // in the process of being called, so we don't need to go further.
1206
- return;
1207
- }
1208
- try {
1209
- this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
1210
- }
1211
- catch (error) {
1212
- if (!(error instanceof Error) ||
1213
- error.message !==
1214
- 'Subscription not found for event: NetworkController:networkDidChange') {
1215
- throw error;
1216
- }
1217
- }
1218
- __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateMetadataForNetwork).call(this, this.state.selectedNetworkClientId, networkStatus, isEIP1559Compatible);
1219
- if (isInfura) {
1220
- if (networkStatus === constants_1.NetworkStatus.Available) {
1221
- this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
1222
- }
1223
- else if (networkStatus === constants_1.NetworkStatus.Blocked) {
1224
- this.messagingSystem.publish('NetworkController:infuraIsBlocked');
1225
- }
1226
- }
1227
- else {
1228
- // Always publish infuraIsUnblocked regardless of network status to
1229
- // prevent consumers from being stuck in a blocked state if they were
1230
- // previously connected to an Infura network that was blocked
1231
- this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
1232
- }
1233
- }, _NetworkController_updateMetadataForNetwork = function _NetworkController_updateMetadataForNetwork(networkClientId, networkStatus, isEIP1559Compatible) {
1234
- this.update((state) => {
1235
- if (state.networksMetadata[networkClientId] === undefined) {
1236
- state.networksMetadata[networkClientId] = {
1237
- status: constants_1.NetworkStatus.Unknown,
1238
- EIPS: {},
1239
- };
1240
- }
1241
- const meta = state.networksMetadata[networkClientId];
1242
- meta.status = networkStatus;
1243
- if (isEIP1559Compatible === undefined) {
1244
- delete meta.EIPS[1559];
1245
- }
1246
- else {
1247
- meta.EIPS[1559] = isEIP1559Compatible;
1248
- }
1249
- });
1250
1271
  }, _NetworkController_getLatestBlock = function _NetworkController_getLatestBlock(networkClientId) {
1251
1272
  if (networkClientId === undefined) {
1252
1273
  networkClientId = this.state.selectedNetworkClientId;