@metamask-previews/network-controller 12.1.1-preview.3dbf14f
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 +261 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/NetworkController.d.ts +361 -0
- package/dist/NetworkController.d.ts.map +1 -0
- package/dist/NetworkController.js +812 -0
- package/dist/NetworkController.js.map +1 -0
- package/dist/constants.d.ts +28 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +32 -0
- package/dist/constants.js.map +1 -0
- package/dist/create-auto-managed-network-client.d.ts +47 -0
- package/dist/create-auto-managed-network-client.d.ts.map +1 -0
- package/dist/create-auto-managed-network-client.js +120 -0
- package/dist/create-auto-managed-network-client.js.map +1 -0
- package/dist/create-network-client.d.ts +19 -0
- package/dist/create-network-client.d.ts.map +1 -0
- package/dist/create-network-client.js +148 -0
- package/dist/create-network-client.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +5 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +7 -0
- package/dist/logger.js.map +1 -0
- package/dist/types.d.ts +37 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/package.json +70 -0
|
@@ -0,0 +1,812 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
16
|
+
};
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
var _NetworkController_instances, _NetworkController_ethQuery, _NetworkController_infuraProjectId, _NetworkController_trackMetaMetricsEvent, _NetworkController_previousProviderConfig, _NetworkController_providerProxy, _NetworkController_provider, _NetworkController_blockTrackerProxy, _NetworkController_autoManagedNetworkClientRegistry, _NetworkController_refreshNetwork, _NetworkController_getLatestBlock, _NetworkController_determineEIP1559Compatibility, _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated, _NetworkController_createAutoManagedNetworkClientRegistry, _NetworkController_buildIdentifiedInfuraNetworkClientConfigurations, _NetworkController_buildIdentifiedCustomNetworkClientConfigurations, _NetworkController_buildIdentifiedNetworkClientConfigurationsFromProviderConfig, _NetworkController_applyNetworkSelection;
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.NetworkController = exports.defaultState = exports.knownKeysOf = void 0;
|
|
28
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
29
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
30
|
+
const eth_query_1 = __importDefault(require("@metamask/eth-query"));
|
|
31
|
+
const swappable_obj_proxy_1 = require("@metamask/swappable-obj-proxy");
|
|
32
|
+
const utils_1 = require("@metamask/utils");
|
|
33
|
+
const assert_1 = require("assert");
|
|
34
|
+
const eth_rpc_errors_1 = require("eth-rpc-errors");
|
|
35
|
+
const uuid_1 = require("uuid");
|
|
36
|
+
const constants_1 = require("./constants");
|
|
37
|
+
const create_auto_managed_network_client_1 = require("./create-auto-managed-network-client");
|
|
38
|
+
const logger_1 = require("./logger");
|
|
39
|
+
const types_1 = require("./types");
|
|
40
|
+
const log = (0, logger_1.createModuleLogger)(logger_1.projectLogger, 'NetworkController');
|
|
41
|
+
/**
|
|
42
|
+
* `Object.keys()` is intentionally generic: it returns the keys of an object,
|
|
43
|
+
* but it cannot make guarantees about the contents of that object, so the type
|
|
44
|
+
* of the keys is merely `string[]`. While this is technically accurate, it is
|
|
45
|
+
* also unnecessary if we have an object that we own and whose contents are
|
|
46
|
+
* known exactly.
|
|
47
|
+
*
|
|
48
|
+
* TODO: Move to @metamask/utils.
|
|
49
|
+
*
|
|
50
|
+
* @param object - The object.
|
|
51
|
+
* @returns The keys of an object, typed according to the type of the object
|
|
52
|
+
* itself.
|
|
53
|
+
*/
|
|
54
|
+
function knownKeysOf(object) {
|
|
55
|
+
return Object.keys(object);
|
|
56
|
+
}
|
|
57
|
+
exports.knownKeysOf = knownKeysOf;
|
|
58
|
+
/**
|
|
59
|
+
* Asserts that the given value is of the given type if the given validation
|
|
60
|
+
* function returns a truthy result.
|
|
61
|
+
*
|
|
62
|
+
* @param value - The value to validate.
|
|
63
|
+
* @param validate - A function used to validate that the value is of the given
|
|
64
|
+
* type. Takes the `value` as an argument and is expected to return true or
|
|
65
|
+
* false.
|
|
66
|
+
* @param message - The message to throw if the function does not return a
|
|
67
|
+
* truthy result.
|
|
68
|
+
* @throws if the function does not return a truthy result.
|
|
69
|
+
*/
|
|
70
|
+
function assertOfType(value, validate, message) {
|
|
71
|
+
assert_1.strict.ok(validate(value), message);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns a portion of the given object with only the given keys.
|
|
75
|
+
*
|
|
76
|
+
* @param object - An object.
|
|
77
|
+
* @param keys - The keys to pick from the object.
|
|
78
|
+
* @returns the portion of the object.
|
|
79
|
+
*/
|
|
80
|
+
function pick(object, keys) {
|
|
81
|
+
const pickedObject = keys.reduce((finalObject, key) => {
|
|
82
|
+
return Object.assign(Object.assign({}, finalObject), { [key]: object[key] });
|
|
83
|
+
}, {});
|
|
84
|
+
assertOfType(pickedObject, () => keys.every((key) => key in pickedObject), 'The reduce did not produce an object with all of the desired keys.');
|
|
85
|
+
return pickedObject;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Type guard for determining whether the given value is an error object with a
|
|
89
|
+
* `code` property, such as an instance of Error.
|
|
90
|
+
*
|
|
91
|
+
* TODO: Move this to @metamask/utils.
|
|
92
|
+
*
|
|
93
|
+
* @param error - The object to check.
|
|
94
|
+
* @returns True if `error` has a `code`, false otherwise.
|
|
95
|
+
*/
|
|
96
|
+
function isErrorWithCode(error) {
|
|
97
|
+
return typeof error === 'object' && error !== null && 'code' in error;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns whether the given argument is a type that our Infura middleware
|
|
101
|
+
* recognizes.
|
|
102
|
+
*
|
|
103
|
+
* @param type - A type to compare.
|
|
104
|
+
* @returns True or false, depending on whether the given type is one that our
|
|
105
|
+
* Infura middleware recognizes.
|
|
106
|
+
*/
|
|
107
|
+
function isInfuraProviderType(type) {
|
|
108
|
+
return Object.keys(controller_utils_1.InfuraNetworkType).includes(type);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Builds an identifier for an Infura network client for lookup purposes.
|
|
112
|
+
*
|
|
113
|
+
* @param infuraNetworkOrProviderConfig - The name of an Infura network or a
|
|
114
|
+
* provider config.
|
|
115
|
+
* @returns The built identifier.
|
|
116
|
+
*/
|
|
117
|
+
function buildInfuraNetworkClientId(infuraNetworkOrProviderConfig) {
|
|
118
|
+
if (typeof infuraNetworkOrProviderConfig === 'string') {
|
|
119
|
+
return infuraNetworkOrProviderConfig;
|
|
120
|
+
}
|
|
121
|
+
return infuraNetworkOrProviderConfig.type;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Builds an identifier for a custom network client for lookup purposes.
|
|
125
|
+
*
|
|
126
|
+
* @param args - This function can be called two ways:
|
|
127
|
+
* 1. The ID of a network configuration.
|
|
128
|
+
* 2. A provider config and a set of network configurations.
|
|
129
|
+
* @returns The built identifier.
|
|
130
|
+
*/
|
|
131
|
+
function buildCustomNetworkClientId(...args) {
|
|
132
|
+
if (args.length === 1) {
|
|
133
|
+
return args[0];
|
|
134
|
+
}
|
|
135
|
+
const [{ id, rpcUrl }, networkConfigurations] = args;
|
|
136
|
+
if (id === undefined) {
|
|
137
|
+
const matchingNetworkConfiguration = Object.values(networkConfigurations).find((networkConfiguration) => {
|
|
138
|
+
return networkConfiguration.rpcUrl === rpcUrl.toLowerCase();
|
|
139
|
+
});
|
|
140
|
+
if (matchingNetworkConfiguration) {
|
|
141
|
+
return matchingNetworkConfiguration.id;
|
|
142
|
+
}
|
|
143
|
+
return rpcUrl.toLowerCase();
|
|
144
|
+
}
|
|
145
|
+
return id;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Returns whether the given provider config refers to an Infura network.
|
|
149
|
+
*
|
|
150
|
+
* @param providerConfig - The provider config.
|
|
151
|
+
* @returns True if the provider config refers to an Infura network, false
|
|
152
|
+
* otherwise.
|
|
153
|
+
*/
|
|
154
|
+
function isInfuraProviderConfig(providerConfig) {
|
|
155
|
+
return isInfuraProviderType(providerConfig.type);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Returns whether the given provider config refers to an Infura network.
|
|
159
|
+
*
|
|
160
|
+
* @param providerConfig - The provider config.
|
|
161
|
+
* @returns True if the provider config refers to an Infura network, false
|
|
162
|
+
* otherwise.
|
|
163
|
+
*/
|
|
164
|
+
function isCustomProviderConfig(providerConfig) {
|
|
165
|
+
return providerConfig.type === controller_utils_1.NetworkType.rpc;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* As a provider config represents the settings that are used to interface with
|
|
169
|
+
* an RPC endpoint, it must have both a chain ID and an RPC URL if it represents
|
|
170
|
+
* a custom network. These properties _should_ be set as they are validated in
|
|
171
|
+
* the UI when a user adds a custom network, but just to be safe we validate
|
|
172
|
+
* them here.
|
|
173
|
+
*
|
|
174
|
+
* In addition, historically the `rpcUrl` property on the ProviderConfig type
|
|
175
|
+
* has been optional, even though it should not be. Making this non-optional
|
|
176
|
+
* would be a breaking change, so this function types the provider config
|
|
177
|
+
* correctly so that we don't have to check `rpcUrl` in other places.
|
|
178
|
+
*
|
|
179
|
+
* @param providerConfig - A provider config.
|
|
180
|
+
* @throws if the provider config does not have a chain ID or an RPC URL.
|
|
181
|
+
*/
|
|
182
|
+
function validateCustomProviderConfig(providerConfig) {
|
|
183
|
+
if (providerConfig.chainId === undefined) {
|
|
184
|
+
throw new Error('chainId must be provided for custom RPC endpoints');
|
|
185
|
+
}
|
|
186
|
+
if (providerConfig.rpcUrl === undefined) {
|
|
187
|
+
throw new Error('rpcUrl must be provided for custom RPC endpoints');
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const name = 'NetworkController';
|
|
191
|
+
exports.defaultState = {
|
|
192
|
+
selectedNetworkClientId: controller_utils_1.NetworkType.mainnet,
|
|
193
|
+
providerConfig: {
|
|
194
|
+
type: controller_utils_1.NetworkType.mainnet,
|
|
195
|
+
chainId: controller_utils_1.ChainId.mainnet,
|
|
196
|
+
ticker: controller_utils_1.NetworksTicker.mainnet,
|
|
197
|
+
},
|
|
198
|
+
networksMetadata: {},
|
|
199
|
+
networkConfigurations: {},
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* Controller that creates and manages an Ethereum network provider.
|
|
203
|
+
*/
|
|
204
|
+
class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
205
|
+
constructor({ messenger, state, infuraProjectId, trackMetaMetricsEvent, }) {
|
|
206
|
+
super({
|
|
207
|
+
name,
|
|
208
|
+
metadata: {
|
|
209
|
+
selectedNetworkClientId: {
|
|
210
|
+
persist: true,
|
|
211
|
+
anonymous: false,
|
|
212
|
+
},
|
|
213
|
+
networksMetadata: {
|
|
214
|
+
persist: true,
|
|
215
|
+
anonymous: false,
|
|
216
|
+
},
|
|
217
|
+
providerConfig: {
|
|
218
|
+
persist: true,
|
|
219
|
+
anonymous: false,
|
|
220
|
+
},
|
|
221
|
+
networkConfigurations: {
|
|
222
|
+
persist: true,
|
|
223
|
+
anonymous: false,
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
messenger,
|
|
227
|
+
state: Object.assign(Object.assign({}, exports.defaultState), state),
|
|
228
|
+
});
|
|
229
|
+
_NetworkController_instances.add(this);
|
|
230
|
+
_NetworkController_ethQuery.set(this, void 0);
|
|
231
|
+
_NetworkController_infuraProjectId.set(this, void 0);
|
|
232
|
+
_NetworkController_trackMetaMetricsEvent.set(this, void 0);
|
|
233
|
+
_NetworkController_previousProviderConfig.set(this, void 0);
|
|
234
|
+
_NetworkController_providerProxy.set(this, void 0);
|
|
235
|
+
_NetworkController_provider.set(this, void 0);
|
|
236
|
+
_NetworkController_blockTrackerProxy.set(this, void 0);
|
|
237
|
+
_NetworkController_autoManagedNetworkClientRegistry.set(this, void 0);
|
|
238
|
+
if (!infuraProjectId || typeof infuraProjectId !== 'string') {
|
|
239
|
+
throw new Error('Invalid Infura project ID');
|
|
240
|
+
}
|
|
241
|
+
__classPrivateFieldSet(this, _NetworkController_infuraProjectId, infuraProjectId, "f");
|
|
242
|
+
__classPrivateFieldSet(this, _NetworkController_trackMetaMetricsEvent, trackMetaMetricsEvent, "f");
|
|
243
|
+
this.messagingSystem.registerActionHandler(`${this.name}:getProviderConfig`, () => {
|
|
244
|
+
return this.state.providerConfig;
|
|
245
|
+
});
|
|
246
|
+
this.messagingSystem.registerActionHandler(`${this.name}:getEthQuery`, () => {
|
|
247
|
+
return __classPrivateFieldGet(this, _NetworkController_ethQuery, "f");
|
|
248
|
+
});
|
|
249
|
+
__classPrivateFieldSet(this, _NetworkController_previousProviderConfig, this.state.providerConfig, "f");
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Accesses the provider and block tracker for the currently selected network.
|
|
253
|
+
*
|
|
254
|
+
* @returns The proxy and block tracker proxies.
|
|
255
|
+
*/
|
|
256
|
+
getProviderAndBlockTracker() {
|
|
257
|
+
return {
|
|
258
|
+
provider: __classPrivateFieldGet(this, _NetworkController_providerProxy, "f"),
|
|
259
|
+
blockTracker: __classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f"),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Returns all of the network clients that have been created so far, keyed by
|
|
264
|
+
* their identifier in the network client registry. This collection represents
|
|
265
|
+
* not only built-in networks but also any custom networks that consumers have
|
|
266
|
+
* added.
|
|
267
|
+
*
|
|
268
|
+
* @returns The list of known network clients.
|
|
269
|
+
*/
|
|
270
|
+
getNetworkClientRegistry() {
|
|
271
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
272
|
+
return Object.assign({}, autoManagedNetworkClientRegistry[types_1.NetworkClientType.Infura], autoManagedNetworkClientRegistry[types_1.NetworkClientType.Custom]);
|
|
273
|
+
}
|
|
274
|
+
getNetworkClientById(networkClientId) {
|
|
275
|
+
if (!networkClientId) {
|
|
276
|
+
throw new Error('No network client ID was provided.');
|
|
277
|
+
}
|
|
278
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
279
|
+
if (isInfuraProviderType(networkClientId)) {
|
|
280
|
+
const infuraNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Infura][networkClientId];
|
|
281
|
+
if (!infuraNetworkClient) {
|
|
282
|
+
throw new Error(`No Infura network client was found with the ID "${networkClientId}".`);
|
|
283
|
+
}
|
|
284
|
+
return infuraNetworkClient;
|
|
285
|
+
}
|
|
286
|
+
const customNetworkClient = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Custom][networkClientId];
|
|
287
|
+
if (!customNetworkClient) {
|
|
288
|
+
throw new Error(`No custom network client was found with the ID "${networkClientId}".`);
|
|
289
|
+
}
|
|
290
|
+
return customNetworkClient;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Populates the network clients and establishes the initial network based on
|
|
294
|
+
* the provider configuration in state.
|
|
295
|
+
*/
|
|
296
|
+
initializeProvider() {
|
|
297
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
298
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
299
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_applyNetworkSelection).call(this);
|
|
300
|
+
yield this.lookupNetwork();
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Performs side effects after switching to a network. If the network is
|
|
305
|
+
* available, updates the network state with the network ID of the network and
|
|
306
|
+
* stores whether the network supports EIP-1559; otherwise clears said
|
|
307
|
+
* information about the network that may have been previously stored.
|
|
308
|
+
*
|
|
309
|
+
* @fires infuraIsBlocked if the network is Infura-supported and is blocking
|
|
310
|
+
* requests.
|
|
311
|
+
* @fires infuraIsUnblocked if the network is Infura-supported and is not
|
|
312
|
+
* blocking requests, or if the network is not Infura-supported.
|
|
313
|
+
*/
|
|
314
|
+
lookupNetwork() {
|
|
315
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
316
|
+
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const isInfura = isInfuraProviderConfig(this.state.providerConfig);
|
|
320
|
+
let networkChanged = false;
|
|
321
|
+
const listener = () => {
|
|
322
|
+
networkChanged = true;
|
|
323
|
+
this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
|
|
324
|
+
};
|
|
325
|
+
this.messagingSystem.subscribe('NetworkController:networkDidChange', listener);
|
|
326
|
+
let updatedNetworkStatus;
|
|
327
|
+
let updatedIsEIP1559Compatible;
|
|
328
|
+
try {
|
|
329
|
+
const isEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this);
|
|
330
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Available;
|
|
331
|
+
updatedIsEIP1559Compatible = isEIP1559Compatible;
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
if (isErrorWithCode(error)) {
|
|
335
|
+
let responseBody;
|
|
336
|
+
if (isInfura &&
|
|
337
|
+
(0, utils_1.hasProperty)(error, 'message') &&
|
|
338
|
+
typeof error.message === 'string') {
|
|
339
|
+
try {
|
|
340
|
+
responseBody = JSON.parse(error.message);
|
|
341
|
+
}
|
|
342
|
+
catch (_a) {
|
|
343
|
+
// error.message must not be JSON
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
if ((0, utils_1.isPlainObject)(responseBody) &&
|
|
347
|
+
responseBody.error === constants_1.INFURA_BLOCKED_KEY) {
|
|
348
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Blocked;
|
|
349
|
+
}
|
|
350
|
+
else if (error.code === eth_rpc_errors_1.errorCodes.rpc.internal) {
|
|
351
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Unavailable;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
log('NetworkController - could not determine network status', error);
|
|
359
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (networkChanged) {
|
|
363
|
+
// If the network has changed, then `lookupNetwork` either has been or is
|
|
364
|
+
// in the process of being called, so we don't need to go further.
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
|
|
368
|
+
this.update((state) => {
|
|
369
|
+
const meta = state.networksMetadata[state.selectedNetworkClientId];
|
|
370
|
+
meta.status = updatedNetworkStatus;
|
|
371
|
+
if (updatedIsEIP1559Compatible === undefined) {
|
|
372
|
+
delete meta.EIPS[1559];
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
meta.EIPS[1559] = updatedIsEIP1559Compatible;
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
if (isInfura) {
|
|
379
|
+
if (updatedNetworkStatus === constants_1.NetworkStatus.Available) {
|
|
380
|
+
this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
|
|
381
|
+
}
|
|
382
|
+
else if (updatedNetworkStatus === constants_1.NetworkStatus.Blocked) {
|
|
383
|
+
this.messagingSystem.publish('NetworkController:infuraIsBlocked');
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
// Always publish infuraIsUnblocked regardless of network status to
|
|
388
|
+
// prevent consumers from being stuck in a blocked state if they were
|
|
389
|
+
// previously connected to an Infura network that was blocked
|
|
390
|
+
this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Convenience method to update provider network type settings.
|
|
396
|
+
*
|
|
397
|
+
* @param type - Human readable network name.
|
|
398
|
+
*/
|
|
399
|
+
setProviderType(type) {
|
|
400
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
401
|
+
assert_1.strict.notStrictEqual(type, controller_utils_1.NetworkType.rpc, `NetworkController - cannot call "setProviderType" with type "${controller_utils_1.NetworkType.rpc}". Use "setActiveNetwork"`);
|
|
402
|
+
assert_1.strict.ok(isInfuraProviderType(type), `Unknown Infura provider type "${type}".`);
|
|
403
|
+
__classPrivateFieldSet(this, _NetworkController_previousProviderConfig, this.state.providerConfig, "f");
|
|
404
|
+
// If testnet the ticker symbol should use a testnet prefix
|
|
405
|
+
const ticker = type in controller_utils_1.NetworksTicker && controller_utils_1.NetworksTicker[type].length > 0
|
|
406
|
+
? controller_utils_1.NetworksTicker[type]
|
|
407
|
+
: 'ETH';
|
|
408
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
409
|
+
this.update((state) => {
|
|
410
|
+
state.providerConfig.type = type;
|
|
411
|
+
state.providerConfig.ticker = ticker;
|
|
412
|
+
state.providerConfig.chainId = controller_utils_1.ChainId[type];
|
|
413
|
+
state.providerConfig.rpcPrefs = controller_utils_1.BUILT_IN_NETWORKS[type].rpcPrefs;
|
|
414
|
+
state.providerConfig.rpcUrl = undefined;
|
|
415
|
+
state.providerConfig.nickname = undefined;
|
|
416
|
+
state.providerConfig.id = undefined;
|
|
417
|
+
});
|
|
418
|
+
yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this);
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Convenience method to update provider RPC settings.
|
|
423
|
+
*
|
|
424
|
+
* @param networkConfigurationId - The unique id for the network configuration to set as the active provider.
|
|
425
|
+
*/
|
|
426
|
+
setActiveNetwork(networkConfigurationId) {
|
|
427
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
428
|
+
__classPrivateFieldSet(this, _NetworkController_previousProviderConfig, this.state.providerConfig, "f");
|
|
429
|
+
const targetNetwork = this.state.networkConfigurations[networkConfigurationId];
|
|
430
|
+
if (!targetNetwork) {
|
|
431
|
+
throw new Error(`networkConfigurationId ${networkConfigurationId} does not match a configured networkConfiguration`);
|
|
432
|
+
}
|
|
433
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
434
|
+
this.update((state) => {
|
|
435
|
+
state.providerConfig.type = controller_utils_1.NetworkType.rpc;
|
|
436
|
+
state.providerConfig.rpcUrl = targetNetwork.rpcUrl;
|
|
437
|
+
state.providerConfig.chainId = targetNetwork.chainId;
|
|
438
|
+
state.providerConfig.ticker = targetNetwork.ticker;
|
|
439
|
+
state.providerConfig.nickname = targetNetwork.nickname;
|
|
440
|
+
state.providerConfig.rpcPrefs = targetNetwork.rpcPrefs;
|
|
441
|
+
state.providerConfig.id = targetNetwork.id;
|
|
442
|
+
});
|
|
443
|
+
yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this);
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Determines whether the network supports EIP-1559 by checking whether the
|
|
448
|
+
* latest block has a `baseFeePerGas` property, then updates state
|
|
449
|
+
* appropriately.
|
|
450
|
+
*
|
|
451
|
+
* @returns A promise that resolves to true if the network supports EIP-1559
|
|
452
|
+
* , false otherwise, or `undefined` if unable to determine the compatibility.
|
|
453
|
+
*/
|
|
454
|
+
getEIP1559Compatibility() {
|
|
455
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
456
|
+
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
const { EIPS } = this.state.networksMetadata[this.state.selectedNetworkClientId];
|
|
460
|
+
if (EIPS[1559] !== undefined) {
|
|
461
|
+
return EIPS[1559];
|
|
462
|
+
}
|
|
463
|
+
const isEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this);
|
|
464
|
+
this.update((state) => {
|
|
465
|
+
if (isEIP1559Compatible !== undefined) {
|
|
466
|
+
state.networksMetadata[state.selectedNetworkClientId].EIPS[1559] =
|
|
467
|
+
isEIP1559Compatible;
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
return isEIP1559Compatible;
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Re-initializes the provider and block tracker for the current network.
|
|
475
|
+
*/
|
|
476
|
+
resetConnection() {
|
|
477
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
478
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
479
|
+
yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this);
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Adds a new custom network or updates the information for an existing
|
|
484
|
+
* network.
|
|
485
|
+
*
|
|
486
|
+
* This may involve updating the `networkConfigurations` property in
|
|
487
|
+
* state as well and/or adding a new network client to the network client
|
|
488
|
+
* registry. The `rpcUrl` and `chainId` of the given object are used to
|
|
489
|
+
* determine which action to take:
|
|
490
|
+
*
|
|
491
|
+
* - If the `rpcUrl` corresponds to an existing network configuration
|
|
492
|
+
* (case-insensitively), then it is overwritten with the object. Furthermore,
|
|
493
|
+
* if the `chainId` is different from the existing network configuration, then
|
|
494
|
+
* the existing network client is replaced with a new one.
|
|
495
|
+
* - If the `rpcUrl` does not correspond to an existing network configuration
|
|
496
|
+
* (case-insensitively), then the object is used to add a new network
|
|
497
|
+
* configuration along with a new network client.
|
|
498
|
+
*
|
|
499
|
+
* @param networkConfiguration - The network configuration to add or update.
|
|
500
|
+
* @param options - Additional configuration options.
|
|
501
|
+
* @param options.referrer - Used to create a metrics event; the site from which the call originated, or 'metamask' for internal calls.
|
|
502
|
+
* @param options.source - Used to create a metrics event; where the event originated (i.e. from a dapp or from the network form).
|
|
503
|
+
* @param options.setActive - If true, switches to the network upon adding or updating it (default: false).
|
|
504
|
+
* @returns The ID for the added or updated network configuration.
|
|
505
|
+
*/
|
|
506
|
+
upsertNetworkConfiguration(networkConfiguration, { referrer, source, setActive = false, }) {
|
|
507
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
508
|
+
const sanitizedNetworkConfiguration = pick(networkConfiguration, ['rpcUrl', 'chainId', 'ticker', 'nickname', 'rpcPrefs']);
|
|
509
|
+
const { rpcUrl, chainId, ticker } = sanitizedNetworkConfiguration;
|
|
510
|
+
(0, utils_1.assertIsStrictHexString)(chainId);
|
|
511
|
+
if (!(0, controller_utils_1.isSafeChainId)(chainId)) {
|
|
512
|
+
throw new Error(`Invalid chain ID "${chainId}": numerical value greater than max safe value.`);
|
|
513
|
+
}
|
|
514
|
+
if (!rpcUrl) {
|
|
515
|
+
throw new Error('An rpcUrl is required to add or update network configuration');
|
|
516
|
+
}
|
|
517
|
+
if (!referrer || !source) {
|
|
518
|
+
throw new Error('referrer and source are required arguments for adding or updating a network configuration');
|
|
519
|
+
}
|
|
520
|
+
try {
|
|
521
|
+
new URL(rpcUrl);
|
|
522
|
+
}
|
|
523
|
+
catch (e) {
|
|
524
|
+
if (e.message.includes('Invalid URL')) {
|
|
525
|
+
throw new Error('rpcUrl must be a valid URL');
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
if (!ticker) {
|
|
529
|
+
throw new Error('A ticker is required to add or update networkConfiguration');
|
|
530
|
+
}
|
|
531
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
532
|
+
const existingNetworkConfiguration = Object.values(this.state.networkConfigurations).find((networkConfig) => networkConfig.rpcUrl.toLowerCase() === rpcUrl.toLowerCase());
|
|
533
|
+
const upsertedNetworkConfigurationId = existingNetworkConfiguration
|
|
534
|
+
? existingNetworkConfiguration.id
|
|
535
|
+
: (0, uuid_1.v4)();
|
|
536
|
+
const networkClientId = buildCustomNetworkClientId(upsertedNetworkConfigurationId);
|
|
537
|
+
this.update((state) => {
|
|
538
|
+
state.networkConfigurations[upsertedNetworkConfigurationId] = Object.assign({ id: upsertedNetworkConfigurationId }, sanitizedNetworkConfiguration);
|
|
539
|
+
});
|
|
540
|
+
const customNetworkClientRegistry = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Custom];
|
|
541
|
+
const existingAutoManagedNetworkClient = customNetworkClientRegistry[networkClientId];
|
|
542
|
+
const shouldDestroyExistingNetworkClient = existingAutoManagedNetworkClient &&
|
|
543
|
+
existingAutoManagedNetworkClient.configuration.chainId !== chainId;
|
|
544
|
+
if (shouldDestroyExistingNetworkClient) {
|
|
545
|
+
existingAutoManagedNetworkClient.destroy();
|
|
546
|
+
}
|
|
547
|
+
if (!existingAutoManagedNetworkClient ||
|
|
548
|
+
shouldDestroyExistingNetworkClient) {
|
|
549
|
+
customNetworkClientRegistry[networkClientId] =
|
|
550
|
+
(0, create_auto_managed_network_client_1.createAutoManagedNetworkClient)({
|
|
551
|
+
type: types_1.NetworkClientType.Custom,
|
|
552
|
+
chainId,
|
|
553
|
+
rpcUrl,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
if (!existingNetworkConfiguration) {
|
|
557
|
+
__classPrivateFieldGet(this, _NetworkController_trackMetaMetricsEvent, "f").call(this, {
|
|
558
|
+
event: 'Custom Network Added',
|
|
559
|
+
category: 'Network',
|
|
560
|
+
referrer: {
|
|
561
|
+
url: referrer,
|
|
562
|
+
},
|
|
563
|
+
properties: {
|
|
564
|
+
chain_id: chainId,
|
|
565
|
+
symbol: ticker,
|
|
566
|
+
source,
|
|
567
|
+
},
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
if (setActive) {
|
|
571
|
+
yield this.setActiveNetwork(upsertedNetworkConfigurationId);
|
|
572
|
+
}
|
|
573
|
+
return upsertedNetworkConfigurationId;
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Removes a custom network from state.
|
|
578
|
+
*
|
|
579
|
+
* This involves updating the `networkConfigurations` property in state as
|
|
580
|
+
* well and removing the network client that corresponds to the network from
|
|
581
|
+
* the client registry.
|
|
582
|
+
*
|
|
583
|
+
* @param networkConfigurationId - The ID of an existing network
|
|
584
|
+
* configuration.
|
|
585
|
+
*/
|
|
586
|
+
removeNetworkConfiguration(networkConfigurationId) {
|
|
587
|
+
if (!this.state.networkConfigurations[networkConfigurationId]) {
|
|
588
|
+
throw new Error(`networkConfigurationId ${networkConfigurationId} does not match a configured networkConfiguration`);
|
|
589
|
+
}
|
|
590
|
+
const autoManagedNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
591
|
+
const networkClientId = buildCustomNetworkClientId(networkConfigurationId);
|
|
592
|
+
this.update((state) => {
|
|
593
|
+
delete state.networkConfigurations[networkConfigurationId];
|
|
594
|
+
});
|
|
595
|
+
const customNetworkClientRegistry = autoManagedNetworkClientRegistry[types_1.NetworkClientType.Custom];
|
|
596
|
+
const existingAutoManagedNetworkClient = customNetworkClientRegistry[networkClientId];
|
|
597
|
+
existingAutoManagedNetworkClient.destroy();
|
|
598
|
+
delete customNetworkClientRegistry[networkClientId];
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Switches to the previously selected network, assuming that there is one
|
|
602
|
+
* (if not and `initializeProvider` has not been previously called, then this
|
|
603
|
+
* method is equivalent to calling `resetConnection`).
|
|
604
|
+
*/
|
|
605
|
+
rollbackToPreviousProvider() {
|
|
606
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
607
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated).call(this);
|
|
608
|
+
this.update((state) => {
|
|
609
|
+
state.providerConfig = __classPrivateFieldGet(this, _NetworkController_previousProviderConfig, "f");
|
|
610
|
+
});
|
|
611
|
+
yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this);
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* Deactivates the controller, stopping any ongoing polling.
|
|
616
|
+
*
|
|
617
|
+
* In-progress requests will not be aborted.
|
|
618
|
+
*/
|
|
619
|
+
destroy() {
|
|
620
|
+
var _a;
|
|
621
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
622
|
+
yield ((_a = __classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f")) === null || _a === void 0 ? void 0 : _a.destroy());
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Updates the controller using the given backup data.
|
|
627
|
+
*
|
|
628
|
+
* @param backup - The data that has been backed up.
|
|
629
|
+
* @param backup.networkConfigurations - Network configurations in the backup.
|
|
630
|
+
*/
|
|
631
|
+
loadBackup({ networkConfigurations, }) {
|
|
632
|
+
this.update((state) => {
|
|
633
|
+
state.networkConfigurations = Object.assign(Object.assign({}, state.networkConfigurations), networkConfigurations);
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Searches for a network configuration ID with the given ChainID and returns it.
|
|
638
|
+
*
|
|
639
|
+
* @param chainId - ChainId to search for
|
|
640
|
+
* @returns networkClientId of the network configuration with the given chainId
|
|
641
|
+
*/
|
|
642
|
+
findNetworkClientIdByChainId(chainId) {
|
|
643
|
+
const networkClients = this.getNetworkClientRegistry();
|
|
644
|
+
const networkClientEntry = Object.entries(networkClients).find(([_, networkClient]) => networkClient.configuration.chainId === chainId);
|
|
645
|
+
if (networkClientEntry === undefined) {
|
|
646
|
+
throw new Error("Couldn't find networkClientId for chainId");
|
|
647
|
+
}
|
|
648
|
+
return networkClientEntry[0];
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
exports.NetworkController = NetworkController;
|
|
652
|
+
_NetworkController_ethQuery = new WeakMap(), _NetworkController_infuraProjectId = new WeakMap(), _NetworkController_trackMetaMetricsEvent = new WeakMap(), _NetworkController_previousProviderConfig = new WeakMap(), _NetworkController_providerProxy = new WeakMap(), _NetworkController_provider = new WeakMap(), _NetworkController_blockTrackerProxy = new WeakMap(), _NetworkController_autoManagedNetworkClientRegistry = new WeakMap(), _NetworkController_instances = new WeakSet(), _NetworkController_refreshNetwork = function _NetworkController_refreshNetwork() {
|
|
653
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
654
|
+
this.messagingSystem.publish('NetworkController:networkWillChange');
|
|
655
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_applyNetworkSelection).call(this);
|
|
656
|
+
this.messagingSystem.publish('NetworkController:networkDidChange');
|
|
657
|
+
yield this.lookupNetwork();
|
|
658
|
+
});
|
|
659
|
+
}, _NetworkController_getLatestBlock = function _NetworkController_getLatestBlock() {
|
|
660
|
+
return new Promise((resolve, reject) => {
|
|
661
|
+
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
662
|
+
throw new Error('Provider has not been initialized');
|
|
663
|
+
}
|
|
664
|
+
__classPrivateFieldGet(this, _NetworkController_ethQuery, "f").sendAsync({ method: 'eth_getBlockByNumber', params: ['latest', false] }, (error, block) => {
|
|
665
|
+
if (error) {
|
|
666
|
+
reject(error);
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
// TODO: Validate this type
|
|
670
|
+
resolve(block);
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
});
|
|
674
|
+
}, _NetworkController_determineEIP1559Compatibility = function _NetworkController_determineEIP1559Compatibility() {
|
|
675
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
676
|
+
const latestBlock = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getLatestBlock).call(this);
|
|
677
|
+
if (!latestBlock) {
|
|
678
|
+
return undefined;
|
|
679
|
+
}
|
|
680
|
+
return latestBlock.baseFeePerGas !== undefined;
|
|
681
|
+
});
|
|
682
|
+
}, _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated = function _NetworkController_ensureAutoManagedNetworkClientRegistryPopulated() {
|
|
683
|
+
var _a;
|
|
684
|
+
const autoManagedNetworkClientRegistry = (_a = __classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClientRegistry, "f")) !== null && _a !== void 0 ? _a : __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_createAutoManagedNetworkClientRegistry).call(this);
|
|
685
|
+
__classPrivateFieldSet(this, _NetworkController_autoManagedNetworkClientRegistry, autoManagedNetworkClientRegistry, "f");
|
|
686
|
+
return autoManagedNetworkClientRegistry;
|
|
687
|
+
}, _NetworkController_createAutoManagedNetworkClientRegistry = function _NetworkController_createAutoManagedNetworkClientRegistry() {
|
|
688
|
+
return [
|
|
689
|
+
...__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_buildIdentifiedInfuraNetworkClientConfigurations).call(this),
|
|
690
|
+
...__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_buildIdentifiedCustomNetworkClientConfigurations).call(this),
|
|
691
|
+
...__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_buildIdentifiedNetworkClientConfigurationsFromProviderConfig).call(this),
|
|
692
|
+
].reduce((registry, [networkClientType, networkClientId, networkClientConfiguration]) => {
|
|
693
|
+
const autoManagedNetworkClient = (0, create_auto_managed_network_client_1.createAutoManagedNetworkClient)(networkClientConfiguration);
|
|
694
|
+
if (networkClientId in registry[networkClientType]) {
|
|
695
|
+
return registry;
|
|
696
|
+
}
|
|
697
|
+
return Object.assign(Object.assign({}, registry), { [networkClientType]: Object.assign(Object.assign({}, registry[networkClientType]), { [networkClientId]: autoManagedNetworkClient }) });
|
|
698
|
+
}, {
|
|
699
|
+
[types_1.NetworkClientType.Infura]: {},
|
|
700
|
+
[types_1.NetworkClientType.Custom]: {},
|
|
701
|
+
});
|
|
702
|
+
}, _NetworkController_buildIdentifiedInfuraNetworkClientConfigurations = function _NetworkController_buildIdentifiedInfuraNetworkClientConfigurations() {
|
|
703
|
+
return knownKeysOf(controller_utils_1.InfuraNetworkType).map((network) => {
|
|
704
|
+
const networkClientId = buildInfuraNetworkClientId(network);
|
|
705
|
+
const networkClientConfiguration = {
|
|
706
|
+
type: types_1.NetworkClientType.Infura,
|
|
707
|
+
network,
|
|
708
|
+
infuraProjectId: __classPrivateFieldGet(this, _NetworkController_infuraProjectId, "f"),
|
|
709
|
+
chainId: controller_utils_1.BUILT_IN_NETWORKS[network].chainId,
|
|
710
|
+
};
|
|
711
|
+
return [
|
|
712
|
+
types_1.NetworkClientType.Infura,
|
|
713
|
+
networkClientId,
|
|
714
|
+
networkClientConfiguration,
|
|
715
|
+
];
|
|
716
|
+
});
|
|
717
|
+
}, _NetworkController_buildIdentifiedCustomNetworkClientConfigurations = function _NetworkController_buildIdentifiedCustomNetworkClientConfigurations() {
|
|
718
|
+
return Object.entries(this.state.networkConfigurations).map(([networkConfigurationId, networkConfiguration]) => {
|
|
719
|
+
if (networkConfiguration.chainId === undefined) {
|
|
720
|
+
throw new Error('chainId must be provided for custom RPC endpoints');
|
|
721
|
+
}
|
|
722
|
+
if (networkConfiguration.rpcUrl === undefined) {
|
|
723
|
+
throw new Error('rpcUrl must be provided for custom RPC endpoints');
|
|
724
|
+
}
|
|
725
|
+
const networkClientId = buildCustomNetworkClientId(networkConfigurationId);
|
|
726
|
+
const networkClientConfiguration = {
|
|
727
|
+
type: types_1.NetworkClientType.Custom,
|
|
728
|
+
chainId: networkConfiguration.chainId,
|
|
729
|
+
rpcUrl: networkConfiguration.rpcUrl,
|
|
730
|
+
};
|
|
731
|
+
return [
|
|
732
|
+
types_1.NetworkClientType.Custom,
|
|
733
|
+
networkClientId,
|
|
734
|
+
networkClientConfiguration,
|
|
735
|
+
];
|
|
736
|
+
});
|
|
737
|
+
}, _NetworkController_buildIdentifiedNetworkClientConfigurationsFromProviderConfig = function _NetworkController_buildIdentifiedNetworkClientConfigurationsFromProviderConfig() {
|
|
738
|
+
const { providerConfig } = this.state;
|
|
739
|
+
if (isCustomProviderConfig(providerConfig)) {
|
|
740
|
+
validateCustomProviderConfig(providerConfig);
|
|
741
|
+
const networkClientId = buildCustomNetworkClientId(providerConfig, this.state.networkConfigurations);
|
|
742
|
+
const networkClientConfiguration = {
|
|
743
|
+
chainId: providerConfig.chainId,
|
|
744
|
+
rpcUrl: providerConfig.rpcUrl,
|
|
745
|
+
type: types_1.NetworkClientType.Custom,
|
|
746
|
+
};
|
|
747
|
+
return [
|
|
748
|
+
[types_1.NetworkClientType.Custom, networkClientId, networkClientConfiguration],
|
|
749
|
+
];
|
|
750
|
+
}
|
|
751
|
+
if (isInfuraProviderConfig(providerConfig)) {
|
|
752
|
+
return [];
|
|
753
|
+
}
|
|
754
|
+
throw new Error(`Unrecognized network type: '${providerConfig.type}'`);
|
|
755
|
+
}, _NetworkController_applyNetworkSelection = function _NetworkController_applyNetworkSelection() {
|
|
756
|
+
if (!__classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClientRegistry, "f")) {
|
|
757
|
+
throw new Error('initializeProvider must be called first in order to switch the network');
|
|
758
|
+
}
|
|
759
|
+
const { providerConfig } = this.state;
|
|
760
|
+
let autoManagedNetworkClient;
|
|
761
|
+
let networkClientId;
|
|
762
|
+
if (isInfuraProviderConfig(providerConfig)) {
|
|
763
|
+
const networkClientType = types_1.NetworkClientType.Infura;
|
|
764
|
+
networkClientId = buildInfuraNetworkClientId(providerConfig);
|
|
765
|
+
const builtInNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClientRegistry, "f")[networkClientType];
|
|
766
|
+
autoManagedNetworkClient =
|
|
767
|
+
builtInNetworkClientRegistry[networkClientId];
|
|
768
|
+
if (!autoManagedNetworkClient) {
|
|
769
|
+
throw new Error(`Could not find custom network matching ${networkClientId}`);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
else if (isCustomProviderConfig(providerConfig)) {
|
|
773
|
+
validateCustomProviderConfig(providerConfig);
|
|
774
|
+
const networkClientType = types_1.NetworkClientType.Custom;
|
|
775
|
+
networkClientId = buildCustomNetworkClientId(providerConfig, this.state.networkConfigurations);
|
|
776
|
+
const customNetworkClientRegistry = __classPrivateFieldGet(this, _NetworkController_autoManagedNetworkClientRegistry, "f")[networkClientType];
|
|
777
|
+
autoManagedNetworkClient = customNetworkClientRegistry[networkClientId];
|
|
778
|
+
if (!autoManagedNetworkClient) {
|
|
779
|
+
throw new Error(`Could not find built-in network matching ${networkClientId}`);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
else {
|
|
783
|
+
throw new Error('Could not determine type of provider config');
|
|
784
|
+
}
|
|
785
|
+
this.update((state) => {
|
|
786
|
+
state.selectedNetworkClientId = networkClientId;
|
|
787
|
+
if (state.networksMetadata[networkClientId] === undefined) {
|
|
788
|
+
state.networksMetadata[networkClientId] = {
|
|
789
|
+
status: constants_1.NetworkStatus.Unknown,
|
|
790
|
+
EIPS: {},
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
});
|
|
794
|
+
const { provider, blockTracker } = autoManagedNetworkClient;
|
|
795
|
+
if (__classPrivateFieldGet(this, _NetworkController_providerProxy, "f")) {
|
|
796
|
+
__classPrivateFieldGet(this, _NetworkController_providerProxy, "f").setTarget(provider);
|
|
797
|
+
}
|
|
798
|
+
else {
|
|
799
|
+
__classPrivateFieldSet(this, _NetworkController_providerProxy, (0, swappable_obj_proxy_1.createEventEmitterProxy)(provider), "f");
|
|
800
|
+
}
|
|
801
|
+
__classPrivateFieldSet(this, _NetworkController_provider, provider, "f");
|
|
802
|
+
if (__classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f")) {
|
|
803
|
+
__classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f").setTarget(blockTracker);
|
|
804
|
+
}
|
|
805
|
+
else {
|
|
806
|
+
__classPrivateFieldSet(this, _NetworkController_blockTrackerProxy, (0, swappable_obj_proxy_1.createEventEmitterProxy)(blockTracker, {
|
|
807
|
+
eventFilter: 'skipInternal',
|
|
808
|
+
}), "f");
|
|
809
|
+
}
|
|
810
|
+
__classPrivateFieldSet(this, _NetworkController_ethQuery, new eth_query_1.default(__classPrivateFieldGet(this, _NetworkController_providerProxy, "f")), "f");
|
|
811
|
+
};
|
|
812
|
+
//# sourceMappingURL=NetworkController.js.map
|