@metamask-previews/network-controller 22.0.2-preview-f4bb6719 → 22.0.2-preview-50bb1e2c

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.
@@ -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_networkConfigurationsByNetworkClientId, _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_networkConfigurationsByNetworkClientId, _NetworkController_refreshNetwork, _NetworkController_getLatestBlock, _NetworkController_determineEIP1559Compatibility, _NetworkController_dangerouslySetNetworkConfiguration, _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.getNetworkConfigurations = exports.getDefaultNetworkControllerState = exports.knownKeysOf = exports.RpcEndpointType = void 0;
42
42
  const base_controller_1 = require("@metamask/base-controller");
@@ -336,6 +336,18 @@ class NetworkController extends base_controller_1.BaseController {
336
336
  // TODO: Either fix this lint violation or explain why it's necessary to ignore.
337
337
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
338
338
  `${this.name}:getSelectedNetworkClient`, this.getSelectedNetworkClient.bind(this));
339
+ this.messagingSystem.registerActionHandler(
340
+ // ESLint is mistaken here; `name` is a string.
341
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
342
+ `${this.name}:addNetwork`, this.addNetwork.bind(this));
343
+ this.messagingSystem.registerActionHandler(
344
+ // ESLint is mistaken here; `name` is a string.
345
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
346
+ `${this.name}:removeNetwork`, this.removeNetwork.bind(this));
347
+ this.messagingSystem.registerActionHandler(
348
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
349
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
350
+ `${this.name}:dangerouslySetNetworkConfiguration`, __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_dangerouslySetNetworkConfiguration).bind(this));
339
351
  }
340
352
  /**
341
353
  * Accesses the provider and block tracker for the currently selected network.
@@ -708,7 +720,7 @@ class NetworkController extends base_controller_1.BaseController {
708
720
  const rpcEndpoint = defaultOrCustomRpcEndpointFields.type === RpcEndpointType.Custom
709
721
  ? {
710
722
  ...defaultOrCustomRpcEndpointFields,
711
- networkClientId: (0, uuid_1.v4)(),
723
+ networkClientId: defaultOrCustomRpcEndpointFields.networkClientId ?? (0, uuid_1.v4)(),
712
724
  }
713
725
  : defaultOrCustomRpcEndpointFields;
714
726
  return {
@@ -987,6 +999,7 @@ class NetworkController extends base_controller_1.BaseController {
987
999
  });
988
1000
  });
989
1001
  __classPrivateFieldSet(this, _NetworkController_networkConfigurationsByNetworkClientId, buildNetworkConfigurationsByNetworkClientId(this.state.networkConfigurationsByChainId), "f");
1002
+ this.messagingSystem.publish('NetworkController:networkRemoved', existingNetworkConfiguration);
990
1003
  }
991
1004
  /**
992
1005
  * Assuming that the network has been previously switched, switches to this
@@ -1094,6 +1107,97 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1094
1107
  return undefined;
1095
1108
  }
1096
1109
  return latestBlock.baseFeePerGas !== undefined;
1110
+ }, _NetworkController_dangerouslySetNetworkConfiguration =
1111
+ /**
1112
+ * This is used to override an existing network configuration.
1113
+ * This is only meant for internal use only and not to be exposed via the UI.
1114
+ * It is used as part of "Network Syncing", to sync networks, RPCs and block explorers cross devices.
1115
+ *
1116
+ * This will subsequently update the network client registry; state.networksMetadata, and state.selectedNetworkClientId
1117
+ * @param networkConfiguration - the network configuration to override
1118
+ */
1119
+ async function _NetworkController_dangerouslySetNetworkConfiguration(networkConfiguration) {
1120
+ const prevNetworkConfig = networkConfiguration.chainId in this.state.networkConfigurationsByChainId
1121
+ ? this.state.networkConfigurationsByChainId[networkConfiguration.chainId]
1122
+ : undefined;
1123
+ if (!prevNetworkConfig) {
1124
+ // We only want to perform overrides, not add new network configurations
1125
+ return;
1126
+ }
1127
+ // Update Registry (remove old and add new)
1128
+ const updateRegistry = () => {
1129
+ // Unregister old networks we want to override
1130
+ const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
1131
+ const networkClientRemoveOperations = prevNetworkConfig.rpcEndpoints.map((rpcEndpoint) => {
1132
+ return {
1133
+ type: 'remove',
1134
+ rpcEndpoint,
1135
+ };
1136
+ });
1137
+ __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_unregisterNetworkClientsAsNeeded).call(this, {
1138
+ networkClientOperations: networkClientRemoveOperations,
1139
+ autoManagedNetworkClientRegistry,
1140
+ });
1141
+ // Register new networks we want to override
1142
+ const networkClientAddOperations = networkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
1143
+ return {
1144
+ type: 'add',
1145
+ rpcEndpoint,
1146
+ };
1147
+ });
1148
+ __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_registerNetworkClientsAsNeeded).call(this, {
1149
+ networkFields: networkConfiguration,
1150
+ networkClientOperations: networkClientAddOperations,
1151
+ autoManagedNetworkClientRegistry,
1152
+ });
1153
+ };
1154
+ // Replace the networkConfiguration with our new networkConfiguration
1155
+ // This is a full replace (no merging)
1156
+ const replaceNetworkConfiguration = () => {
1157
+ // Update State
1158
+ this.update((state) => {
1159
+ state.networkConfigurationsByChainId[networkConfiguration.chainId] =
1160
+ networkConfiguration;
1161
+ });
1162
+ // Update Cache
1163
+ __classPrivateFieldSet(this, _NetworkController_networkConfigurationsByNetworkClientId, buildNetworkConfigurationsByNetworkClientId(this.state.networkConfigurationsByChainId), "f");
1164
+ };
1165
+ // Updates the NetworksMetadata State
1166
+ const updateNetworksMetadata = async () => {
1167
+ // Remove old metadata state
1168
+ this.update((state) => {
1169
+ prevNetworkConfig.rpcEndpoints.forEach((r) => {
1170
+ if (state.networksMetadata?.[r.networkClientId]) {
1171
+ delete state.networksMetadata[r.networkClientId];
1172
+ }
1173
+ });
1174
+ });
1175
+ // Add new metadata state
1176
+ for (const r of networkConfiguration.rpcEndpoints) {
1177
+ await this.lookupNetwork(r.networkClientId);
1178
+ }
1179
+ };
1180
+ // Update selectedNetworkId State
1181
+ // Will try to keep the same OR will select a new RPC from new network OR any network (edge case)
1182
+ const updateSelectedNetworkId = async () => {
1183
+ const selectedClientId = this.state.selectedNetworkClientId;
1184
+ const wasClientIdReplaced = prevNetworkConfig.rpcEndpoints.some((r) => r.networkClientId === selectedClientId);
1185
+ const doesExistInNewNetwork = networkConfiguration.rpcEndpoints.some((r) => r.networkClientId === selectedClientId);
1186
+ const shouldUpdateSelectedNetworkId = wasClientIdReplaced && !doesExistInNewNetwork;
1187
+ if (shouldUpdateSelectedNetworkId) {
1188
+ // Update the clientId to "something" that exists
1189
+ const newRPCClientId = networkConfiguration.rpcEndpoints.find((r) => r.networkClientId in this.state.networksMetadata)?.networkClientId;
1190
+ const anyRPCClientId = Object.keys(this.state.networksMetadata)[0];
1191
+ /* istanbul ignore next: anyRPCClientId and selectedClientId are fallbacks and should be impossible to reach */
1192
+ const newlySelectedNetwork = newRPCClientId ?? anyRPCClientId ?? selectedClientId;
1193
+ await __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this, newlySelectedNetwork);
1194
+ }
1195
+ };
1196
+ // Execute Set Network Config
1197
+ updateRegistry();
1198
+ replaceNetworkConfiguration();
1199
+ await updateNetworksMetadata();
1200
+ await updateSelectedNetworkId();
1097
1201
  }, _NetworkController_validateNetworkFields = function _NetworkController_validateNetworkFields(args) {
1098
1202
  const { mode, networkFields, autoManagedNetworkClientRegistry } = args;
1099
1203
  const existingNetworkConfiguration = 'existingNetworkConfiguration' in args
@@ -1280,6 +1384,9 @@ async function _NetworkController_determineEIP1559Compatibility(networkClientId)
1280
1384
  delete state.networkConfigurationsByChainId[args.existingNetworkConfiguration.chainId];
1281
1385
  }
1282
1386
  if (mode === 'add' || mode === 'update') {
1387
+ if (JSON.stringify(state.networkConfigurationsByChainId[args.networkFields.chainId]) !== JSON.stringify(args.networkConfigurationToPersist)) {
1388
+ args.networkConfigurationToPersist.lastUpdatedAt = Date.now();
1389
+ }
1283
1390
  state.networkConfigurationsByChainId[args.networkFields.chainId] =
1284
1391
  args.networkConfigurationToPersist;
1285
1392
  }