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