@metamask/network-controller 8.0.0 → 10.0.0
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 +81 -1
- package/dist/NetworkController.d.ts +87 -22
- package/dist/NetworkController.d.ts.map +1 -1
- package/dist/NetworkController.js +202 -122
- package/dist/NetworkController.js.map +1 -1
- package/dist/constants.d.ts +8 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +8 -1
- package/dist/constants.js.map +1 -1
- package/dist/create-network-client.d.ts +40 -0
- package/dist/create-network-client.d.ts.map +1 -0
- package/dist/create-network-client.js +153 -0
- package/dist/create-network-client.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- 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 +5 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +13 -8
|
@@ -22,31 +22,39 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
22
22
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
23
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
24
|
};
|
|
25
|
-
var _NetworkController_instances, _NetworkController_ethQuery, _NetworkController_infuraProjectId, _NetworkController_trackMetaMetricsEvent,
|
|
25
|
+
var _NetworkController_instances, _NetworkController_ethQuery, _NetworkController_infuraProjectId, _NetworkController_trackMetaMetricsEvent, _NetworkController_previousProviderConfig, _NetworkController_providerProxy, _NetworkController_blockTrackerProxy, _NetworkController_configureProvider, _NetworkController_refreshNetwork, _NetworkController_registerProvider, _NetworkController_setupInfuraProvider, _NetworkController_setupStandardProvider, _NetworkController_updateProvider, _NetworkController_getNetworkId, _NetworkController_getLatestBlock, _NetworkController_determineEIP1559Compatibility, _NetworkController_setProviderAndBlockTracker;
|
|
26
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
27
|
exports.NetworkController = exports.defaultState = void 0;
|
|
28
|
-
const eth_query_1 = __importDefault(require("eth-query"));
|
|
29
|
-
const provider_1 = __importDefault(require("web3-provider-engine/subproviders/provider"));
|
|
30
|
-
const createProvider_1 = __importDefault(require("eth-json-rpc-infura/src/createProvider"));
|
|
31
|
-
const zero_1 = __importDefault(require("web3-provider-engine/zero"));
|
|
32
28
|
const swappable_obj_proxy_1 = require("@metamask/swappable-obj-proxy");
|
|
33
|
-
const
|
|
29
|
+
const eth_query_1 = __importDefault(require("eth-query"));
|
|
30
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
34
31
|
const uuid_1 = require("uuid");
|
|
35
32
|
const eth_rpc_errors_1 = require("eth-rpc-errors");
|
|
36
|
-
const base_controller_1 = require("@metamask/base-controller");
|
|
37
33
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
38
34
|
const utils_1 = require("@metamask/utils");
|
|
39
35
|
const constants_1 = require("./constants");
|
|
36
|
+
const logger_1 = require("./logger");
|
|
37
|
+
const create_network_client_1 = require("./create-network-client");
|
|
38
|
+
const log = (0, logger_1.createModuleLogger)(logger_1.projectLogger, 'NetworkController');
|
|
40
39
|
/**
|
|
41
|
-
*
|
|
42
|
-
* number
|
|
40
|
+
* Convert the given value into a valid network ID. The ID is accepted
|
|
41
|
+
* as either a number, a decimal string, or a 0x-prefixed hex string.
|
|
43
42
|
*
|
|
44
|
-
* @param value - The
|
|
43
|
+
* @param value - The network ID to convert, in an unknown format.
|
|
44
|
+
* @returns A valid network ID (as a decimal string)
|
|
45
|
+
* @throws If the given value cannot be safely parsed.
|
|
45
46
|
*/
|
|
46
|
-
function
|
|
47
|
-
if (
|
|
48
|
-
|
|
47
|
+
function convertNetworkId(value) {
|
|
48
|
+
if (typeof value === 'number' && !Number.isNaN(value)) {
|
|
49
|
+
return `${value}`;
|
|
50
|
+
}
|
|
51
|
+
else if ((0, utils_1.isStrictHexString)(value)) {
|
|
52
|
+
return `${(0, controller_utils_1.convertHexToDecimal)(value)}`;
|
|
49
53
|
}
|
|
54
|
+
else if (typeof value === 'string' && /^\d+$/u.test(value)) {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
throw new Error(`Cannot parse as a valid network ID: '${value}'`);
|
|
50
58
|
}
|
|
51
59
|
/**
|
|
52
60
|
* Type guard for determining whether the given value is an error object with a
|
|
@@ -60,16 +68,28 @@ function assertNetworkId(value) {
|
|
|
60
68
|
function isErrorWithCode(error) {
|
|
61
69
|
return typeof error === 'object' && error !== null && 'code' in error;
|
|
62
70
|
}
|
|
63
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Returns whether the given argument is a type that our Infura middleware
|
|
73
|
+
* recognizes.
|
|
74
|
+
*
|
|
75
|
+
* @param type - A type to compare.
|
|
76
|
+
* @returns True or false, depending on whether the given type is one that our
|
|
77
|
+
* Infura middleware recognizes.
|
|
78
|
+
*/
|
|
79
|
+
function isInfuraProviderType(type) {
|
|
80
|
+
return Object.keys(controller_utils_1.InfuraNetworkType).includes(type);
|
|
81
|
+
}
|
|
64
82
|
const name = 'NetworkController';
|
|
65
83
|
exports.defaultState = {
|
|
66
84
|
networkId: null,
|
|
67
85
|
networkStatus: constants_1.NetworkStatus.Unknown,
|
|
68
86
|
providerConfig: {
|
|
69
87
|
type: controller_utils_1.NetworkType.mainnet,
|
|
70
|
-
chainId: controller_utils_1.
|
|
88
|
+
chainId: controller_utils_1.ChainId.mainnet,
|
|
89
|
+
},
|
|
90
|
+
networkDetails: {
|
|
91
|
+
EIPS: {},
|
|
71
92
|
},
|
|
72
|
-
networkDetails: { isEIP1559Compatible: false },
|
|
73
93
|
networkConfigurations: {},
|
|
74
94
|
};
|
|
75
95
|
/**
|
|
@@ -108,11 +128,12 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
108
128
|
_NetworkController_ethQuery.set(this, void 0);
|
|
109
129
|
_NetworkController_infuraProjectId.set(this, void 0);
|
|
110
130
|
_NetworkController_trackMetaMetricsEvent.set(this, void 0);
|
|
111
|
-
|
|
112
|
-
_NetworkController_previousNetworkSpecifier.set(this, void 0);
|
|
113
|
-
_NetworkController_provider.set(this, void 0);
|
|
131
|
+
_NetworkController_previousProviderConfig.set(this, void 0);
|
|
114
132
|
_NetworkController_providerProxy.set(this, void 0);
|
|
115
133
|
_NetworkController_blockTrackerProxy.set(this, void 0);
|
|
134
|
+
if (!infuraProjectId || typeof infuraProjectId !== 'string') {
|
|
135
|
+
throw new Error('Invalid Infura project ID');
|
|
136
|
+
}
|
|
116
137
|
__classPrivateFieldSet(this, _NetworkController_infuraProjectId, infuraProjectId, "f");
|
|
117
138
|
__classPrivateFieldSet(this, _NetworkController_trackMetaMetricsEvent, trackMetaMetricsEvent, "f");
|
|
118
139
|
this.messagingSystem.registerActionHandler(`${this.name}:getProviderConfig`, () => {
|
|
@@ -121,7 +142,7 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
121
142
|
this.messagingSystem.registerActionHandler(`${this.name}:getEthQuery`, () => {
|
|
122
143
|
return __classPrivateFieldGet(this, _NetworkController_ethQuery, "f");
|
|
123
144
|
});
|
|
124
|
-
__classPrivateFieldSet(this,
|
|
145
|
+
__classPrivateFieldSet(this, _NetworkController_previousProviderConfig, this.state.providerConfig, "f");
|
|
125
146
|
}
|
|
126
147
|
getProviderAndBlockTracker() {
|
|
127
148
|
return {
|
|
@@ -137,8 +158,8 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
137
158
|
*/
|
|
138
159
|
initializeProvider() {
|
|
139
160
|
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
-
const { type,
|
|
141
|
-
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_configureProvider).call(this, type,
|
|
161
|
+
const { type, rpcUrl, chainId } = this.state.providerConfig;
|
|
162
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_configureProvider).call(this, type, rpcUrl, chainId);
|
|
142
163
|
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_registerProvider).call(this);
|
|
143
164
|
yield this.lookupNetwork();
|
|
144
165
|
});
|
|
@@ -148,40 +169,94 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
148
169
|
* available, updates the network state with the network ID of the network and
|
|
149
170
|
* stores whether the network supports EIP-1559; otherwise clears said
|
|
150
171
|
* information about the network that may have been previously stored.
|
|
172
|
+
*
|
|
173
|
+
* @fires infuraIsBlocked if the network is Infura-supported and is blocking
|
|
174
|
+
* requests.
|
|
175
|
+
* @fires infuraIsUnblocked if the network is Infura-supported and is not
|
|
176
|
+
* blocking requests, or if the network is not Infura-supported.
|
|
151
177
|
*/
|
|
152
178
|
lookupNetwork() {
|
|
153
179
|
return __awaiter(this, void 0, void 0, function* () {
|
|
154
180
|
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
155
181
|
return;
|
|
156
182
|
}
|
|
157
|
-
const
|
|
183
|
+
const isInfura = isInfuraProviderType(this.state.providerConfig.type);
|
|
184
|
+
let networkChanged = false;
|
|
185
|
+
const listener = () => {
|
|
186
|
+
networkChanged = true;
|
|
187
|
+
this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
|
|
188
|
+
};
|
|
189
|
+
this.messagingSystem.subscribe('NetworkController:networkDidChange', listener);
|
|
190
|
+
let updatedNetworkStatus;
|
|
191
|
+
let updatedNetworkId = null;
|
|
192
|
+
let updatedIsEIP1559Compatible;
|
|
158
193
|
try {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
194
|
+
const [networkId, isEIP1559Compatible] = yield Promise.all([
|
|
195
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getNetworkId).call(this),
|
|
196
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this),
|
|
197
|
+
]);
|
|
198
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Available;
|
|
199
|
+
updatedNetworkId = networkId;
|
|
200
|
+
updatedIsEIP1559Compatible = isEIP1559Compatible;
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
if (isErrorWithCode(error)) {
|
|
204
|
+
let responseBody;
|
|
205
|
+
if (isInfura &&
|
|
206
|
+
(0, utils_1.hasProperty)(error, 'message') &&
|
|
207
|
+
typeof error.message === 'string') {
|
|
208
|
+
try {
|
|
209
|
+
responseBody = JSON.parse(error.message);
|
|
210
|
+
}
|
|
211
|
+
catch (_a) {
|
|
212
|
+
// error.message must not be JSON
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if ((0, utils_1.isPlainObject)(responseBody) &&
|
|
216
|
+
responseBody.error === constants_1.INFURA_BLOCKED_KEY) {
|
|
217
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Blocked;
|
|
218
|
+
}
|
|
219
|
+
else if (error.code === eth_rpc_errors_1.errorCodes.rpc.internal) {
|
|
220
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
|
|
166
221
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
222
|
+
else {
|
|
223
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Unavailable;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
log('NetworkController - could not determine network status', error);
|
|
228
|
+
updatedNetworkStatus = constants_1.NetworkStatus.Unknown;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (networkChanged) {
|
|
232
|
+
// If the network has changed, then `lookupNetwork` either has been or is
|
|
233
|
+
// in the process of being called, so we don't need to go further.
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
this.messagingSystem.unsubscribe('NetworkController:networkDidChange', listener);
|
|
237
|
+
this.update((state) => {
|
|
238
|
+
state.networkId = updatedNetworkId;
|
|
239
|
+
state.networkStatus = updatedNetworkStatus;
|
|
240
|
+
if (updatedIsEIP1559Compatible === undefined) {
|
|
241
|
+
delete state.networkDetails.EIPS[1559];
|
|
171
242
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
243
|
+
else {
|
|
244
|
+
state.networkDetails.EIPS[1559] = updatedIsEIP1559Compatible;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
if (isInfura) {
|
|
248
|
+
if (updatedNetworkStatus === constants_1.NetworkStatus.Available) {
|
|
249
|
+
this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
|
|
250
|
+
}
|
|
251
|
+
else if (updatedNetworkStatus === constants_1.NetworkStatus.Blocked) {
|
|
252
|
+
this.messagingSystem.publish('NetworkController:infuraIsBlocked');
|
|
180
253
|
}
|
|
181
|
-
this.messagingSystem.publish(`NetworkController:providerConfigChange`, this.state.providerConfig);
|
|
182
254
|
}
|
|
183
|
-
|
|
184
|
-
|
|
255
|
+
else {
|
|
256
|
+
// Always publish infuraIsUnblocked regardless of network status to
|
|
257
|
+
// prevent consumers from being stuck in a blocked state if they were
|
|
258
|
+
// previously connected to an Infura network that was blocked
|
|
259
|
+
this.messagingSystem.publish('NetworkController:infuraIsUnblocked');
|
|
185
260
|
}
|
|
186
261
|
});
|
|
187
262
|
}
|
|
@@ -192,7 +267,7 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
192
267
|
*/
|
|
193
268
|
setProviderType(type) {
|
|
194
269
|
return __awaiter(this, void 0, void 0, function* () {
|
|
195
|
-
|
|
270
|
+
__classPrivateFieldSet(this, _NetworkController_previousProviderConfig, this.state.providerConfig, "f");
|
|
196
271
|
// If testnet the ticker symbol should use a testnet prefix
|
|
197
272
|
const ticker = type in controller_utils_1.NetworksTicker && controller_utils_1.NetworksTicker[type].length > 0
|
|
198
273
|
? controller_utils_1.NetworksTicker[type]
|
|
@@ -200,9 +275,9 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
200
275
|
this.update((state) => {
|
|
201
276
|
state.providerConfig.type = type;
|
|
202
277
|
state.providerConfig.ticker = ticker;
|
|
203
|
-
state.providerConfig.chainId = controller_utils_1.
|
|
278
|
+
state.providerConfig.chainId = controller_utils_1.ChainId[type];
|
|
204
279
|
state.providerConfig.rpcPrefs = controller_utils_1.BUILT_IN_NETWORKS[type].rpcPrefs;
|
|
205
|
-
state.providerConfig.
|
|
280
|
+
state.providerConfig.rpcUrl = undefined;
|
|
206
281
|
state.providerConfig.nickname = undefined;
|
|
207
282
|
state.providerConfig.id = undefined;
|
|
208
283
|
});
|
|
@@ -216,14 +291,14 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
216
291
|
*/
|
|
217
292
|
setActiveNetwork(networkConfigurationId) {
|
|
218
293
|
return __awaiter(this, void 0, void 0, function* () {
|
|
219
|
-
|
|
294
|
+
__classPrivateFieldSet(this, _NetworkController_previousProviderConfig, this.state.providerConfig, "f");
|
|
220
295
|
const targetNetwork = this.state.networkConfigurations[networkConfigurationId];
|
|
221
296
|
if (!targetNetwork) {
|
|
222
297
|
throw new Error(`networkConfigurationId ${networkConfigurationId} does not match a configured networkConfiguration`);
|
|
223
298
|
}
|
|
224
299
|
this.update((state) => {
|
|
225
300
|
state.providerConfig.type = controller_utils_1.NetworkType.rpc;
|
|
226
|
-
state.providerConfig.
|
|
301
|
+
state.providerConfig.rpcUrl = targetNetwork.rpcUrl;
|
|
227
302
|
state.providerConfig.chainId = targetNetwork.chainId;
|
|
228
303
|
state.providerConfig.ticker = targetNetwork.ticker;
|
|
229
304
|
state.providerConfig.nickname = targetNetwork.nickname;
|
|
@@ -233,19 +308,27 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
233
308
|
yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this);
|
|
234
309
|
});
|
|
235
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* Determines whether the network supports EIP-1559 by checking whether the
|
|
313
|
+
* latest block has a `baseFeePerGas` property, then updates state
|
|
314
|
+
* appropriately.
|
|
315
|
+
*
|
|
316
|
+
* @returns A promise that resolves to true if the network supports EIP-1559
|
|
317
|
+
* and false otherwise.
|
|
318
|
+
*/
|
|
236
319
|
getEIP1559Compatibility() {
|
|
237
320
|
return __awaiter(this, void 0, void 0, function* () {
|
|
238
|
-
const {
|
|
239
|
-
if (
|
|
240
|
-
return
|
|
321
|
+
const { EIPS } = this.state.networkDetails;
|
|
322
|
+
if (EIPS[1559] !== undefined) {
|
|
323
|
+
return EIPS[1559];
|
|
241
324
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (networkDetails.isEIP1559Compatible !== isEIP1559Compatible) {
|
|
245
|
-
this.update((state) => {
|
|
246
|
-
state.networkDetails.isEIP1559Compatible = isEIP1559Compatible;
|
|
247
|
-
});
|
|
325
|
+
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
326
|
+
return false;
|
|
248
327
|
}
|
|
328
|
+
const isEIP1559Compatible = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_determineEIP1559Compatibility).call(this);
|
|
329
|
+
this.update((state) => {
|
|
330
|
+
state.networkDetails.EIPS[1559] = isEIP1559Compatible;
|
|
331
|
+
});
|
|
249
332
|
return isEIP1559Compatible;
|
|
250
333
|
});
|
|
251
334
|
}
|
|
@@ -277,7 +360,7 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
277
360
|
var _a;
|
|
278
361
|
return __awaiter(this, void 0, void 0, function* () {
|
|
279
362
|
(0, utils_1.assertIsStrictHexString)(chainId);
|
|
280
|
-
if (!(0, controller_utils_1.isSafeChainId)(
|
|
363
|
+
if (!(0, controller_utils_1.isSafeChainId)(chainId)) {
|
|
281
364
|
throw new Error(`Invalid chain ID "${chainId}": numerical value greater than max safe value.`);
|
|
282
365
|
}
|
|
283
366
|
if (!rpcUrl) {
|
|
@@ -287,7 +370,6 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
287
370
|
throw new Error('referrer and source are required arguments for adding or updating a network configuration');
|
|
288
371
|
}
|
|
289
372
|
try {
|
|
290
|
-
// eslint-disable-next-line no-new
|
|
291
373
|
new URL(rpcUrl);
|
|
292
374
|
}
|
|
293
375
|
catch (e) {
|
|
@@ -345,129 +427,128 @@ class NetworkController extends base_controller_1.BaseControllerV2 {
|
|
|
345
427
|
});
|
|
346
428
|
}
|
|
347
429
|
/**
|
|
348
|
-
*
|
|
430
|
+
* Switches to the previous network, assuming that the current network is
|
|
431
|
+
* different than the initial network (if it is, then this is equivalent to
|
|
432
|
+
* calling `resetConnection`).
|
|
349
433
|
*/
|
|
350
434
|
rollbackToPreviousProvider() {
|
|
351
435
|
return __awaiter(this, void 0, void 0, function* () {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
436
|
+
this.update((state) => {
|
|
437
|
+
state.providerConfig = __classPrivateFieldGet(this, _NetworkController_previousProviderConfig, "f");
|
|
438
|
+
});
|
|
439
|
+
yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_refreshNetwork).call(this);
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Deactivates the controller, stopping any ongoing polling.
|
|
444
|
+
*
|
|
445
|
+
* In-progress requests will not be aborted.
|
|
446
|
+
*/
|
|
447
|
+
destroy() {
|
|
448
|
+
var _a;
|
|
449
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
450
|
+
yield ((_a = __classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f")) === null || _a === void 0 ? void 0 : _a.destroy());
|
|
359
451
|
});
|
|
360
452
|
}
|
|
361
453
|
}
|
|
362
454
|
exports.NetworkController = NetworkController;
|
|
363
|
-
_NetworkController_ethQuery = new WeakMap(), _NetworkController_infuraProjectId = new WeakMap(), _NetworkController_trackMetaMetricsEvent = new WeakMap(),
|
|
455
|
+
_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_instances = new WeakSet(), _NetworkController_configureProvider = function _NetworkController_configureProvider(type, rpcUrl, chainId) {
|
|
364
456
|
switch (type) {
|
|
365
457
|
case controller_utils_1.NetworkType.mainnet:
|
|
366
458
|
case controller_utils_1.NetworkType.goerli:
|
|
367
459
|
case controller_utils_1.NetworkType.sepolia:
|
|
368
460
|
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_setupInfuraProvider).call(this, type);
|
|
369
461
|
break;
|
|
370
|
-
case controller_utils_1.NetworkType.localhost:
|
|
371
|
-
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_setupStandardProvider).call(this, LOCALHOST_RPC_URL);
|
|
372
|
-
break;
|
|
373
462
|
case controller_utils_1.NetworkType.rpc:
|
|
374
|
-
|
|
375
|
-
|
|
463
|
+
if (chainId === undefined) {
|
|
464
|
+
throw new Error('chainId must be provided for custom RPC endpoints');
|
|
465
|
+
}
|
|
466
|
+
if (rpcUrl === undefined) {
|
|
467
|
+
throw new Error('rpcUrl must be provided for custom RPC endpoints');
|
|
468
|
+
}
|
|
469
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_setupStandardProvider).call(this, rpcUrl, chainId);
|
|
376
470
|
break;
|
|
377
471
|
default:
|
|
378
472
|
throw new Error(`Unrecognized network type: '${type}'`);
|
|
379
473
|
}
|
|
380
474
|
}, _NetworkController_refreshNetwork = function _NetworkController_refreshNetwork() {
|
|
381
475
|
return __awaiter(this, void 0, void 0, function* () {
|
|
476
|
+
this.messagingSystem.publish('NetworkController:networkWillChange');
|
|
382
477
|
this.update((state) => {
|
|
383
478
|
state.networkId = null;
|
|
384
479
|
state.networkStatus = constants_1.NetworkStatus.Unknown;
|
|
385
|
-
state.networkDetails = {
|
|
480
|
+
state.networkDetails = {
|
|
481
|
+
EIPS: {},
|
|
482
|
+
};
|
|
386
483
|
});
|
|
387
|
-
const {
|
|
388
|
-
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_configureProvider).call(this, type,
|
|
484
|
+
const { rpcUrl, type, chainId } = this.state.providerConfig;
|
|
485
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_configureProvider).call(this, type, rpcUrl, chainId);
|
|
486
|
+
this.messagingSystem.publish('NetworkController:networkDidChange');
|
|
389
487
|
yield this.lookupNetwork();
|
|
390
488
|
});
|
|
391
489
|
}, _NetworkController_registerProvider = function _NetworkController_registerProvider() {
|
|
392
490
|
const { provider } = this.getProviderAndBlockTracker();
|
|
393
491
|
if (provider) {
|
|
394
|
-
provider.on('error', __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_verifyNetwork).bind(this));
|
|
395
492
|
__classPrivateFieldSet(this, _NetworkController_ethQuery, new eth_query_1.default(provider), "f");
|
|
396
493
|
}
|
|
397
494
|
}, _NetworkController_setupInfuraProvider = function _NetworkController_setupInfuraProvider(type) {
|
|
398
|
-
const
|
|
495
|
+
const { provider, blockTracker } = (0, create_network_client_1.createNetworkClient)({
|
|
399
496
|
network: type,
|
|
400
|
-
|
|
497
|
+
infuraProjectId: __classPrivateFieldGet(this, _NetworkController_infuraProjectId, "f"),
|
|
498
|
+
type: create_network_client_1.NetworkClientType.Infura,
|
|
401
499
|
});
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
engineParams: {
|
|
406
|
-
blockTrackerProvider: infuraProvider,
|
|
407
|
-
pollingInterval: 12000,
|
|
408
|
-
},
|
|
409
|
-
};
|
|
410
|
-
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateProvider).call(this, (0, zero_1.default)(config));
|
|
411
|
-
}, _NetworkController_setupStandardProvider = function _NetworkController_setupStandardProvider(rpcTarget, chainId, ticker, nickname) {
|
|
412
|
-
const config = {
|
|
500
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateProvider).call(this, provider, blockTracker);
|
|
501
|
+
}, _NetworkController_setupStandardProvider = function _NetworkController_setupStandardProvider(rpcUrl, chainId) {
|
|
502
|
+
const { provider, blockTracker } = (0, create_network_client_1.createNetworkClient)({
|
|
413
503
|
chainId,
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateProvider).call(this, (0, zero_1.default)(config));
|
|
420
|
-
}, _NetworkController_updateProvider = function _NetworkController_updateProvider(provider) {
|
|
421
|
-
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_safelyStopProvider).call(this, __classPrivateFieldGet(this, _NetworkController_provider, "f"));
|
|
504
|
+
rpcUrl,
|
|
505
|
+
type: create_network_client_1.NetworkClientType.Custom,
|
|
506
|
+
});
|
|
507
|
+
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_updateProvider).call(this, provider, blockTracker);
|
|
508
|
+
}, _NetworkController_updateProvider = function _NetworkController_updateProvider(provider, blockTracker) {
|
|
422
509
|
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_setProviderAndBlockTracker).call(this, {
|
|
423
510
|
provider,
|
|
424
|
-
blockTracker
|
|
511
|
+
blockTracker,
|
|
425
512
|
});
|
|
426
513
|
__classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_registerProvider).call(this);
|
|
427
|
-
}, _NetworkController_safelyStopProvider = function _NetworkController_safelyStopProvider(provider) {
|
|
428
|
-
setTimeout(() => {
|
|
429
|
-
provider === null || provider === void 0 ? void 0 : provider.stop();
|
|
430
|
-
}, 500);
|
|
431
|
-
}, _NetworkController_verifyNetwork = function _NetworkController_verifyNetwork() {
|
|
432
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
433
|
-
if (this.state.networkStatus !== constants_1.NetworkStatus.Available) {
|
|
434
|
-
yield this.lookupNetwork();
|
|
435
|
-
}
|
|
436
|
-
});
|
|
437
514
|
}, _NetworkController_getNetworkId = function _NetworkController_getNetworkId() {
|
|
438
515
|
return __awaiter(this, void 0, void 0, function* () {
|
|
439
516
|
const possibleNetworkId = yield new Promise((resolve, reject) => {
|
|
517
|
+
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
518
|
+
throw new Error('Provider has not been initialized');
|
|
519
|
+
}
|
|
440
520
|
__classPrivateFieldGet(this, _NetworkController_ethQuery, "f").sendAsync({ method: 'net_version' }, (error, result) => {
|
|
441
521
|
if (error) {
|
|
442
522
|
reject(error);
|
|
443
523
|
}
|
|
444
524
|
else {
|
|
525
|
+
// TODO: Validate this type
|
|
445
526
|
resolve(result);
|
|
446
527
|
}
|
|
447
528
|
});
|
|
448
529
|
});
|
|
449
|
-
|
|
450
|
-
return possibleNetworkId;
|
|
530
|
+
return convertNetworkId(possibleNetworkId);
|
|
451
531
|
});
|
|
452
|
-
}, _NetworkController_setCurrentAsPreviousProvider = function _NetworkController_setCurrentAsPreviousProvider() {
|
|
453
|
-
const { type, id } = this.state.providerConfig;
|
|
454
|
-
if (type === controller_utils_1.NetworkType.rpc && id) {
|
|
455
|
-
__classPrivateFieldSet(this, _NetworkController_previousNetworkSpecifier, id, "f");
|
|
456
|
-
}
|
|
457
|
-
else {
|
|
458
|
-
__classPrivateFieldSet(this, _NetworkController_previousNetworkSpecifier, type, "f");
|
|
459
|
-
}
|
|
460
532
|
}, _NetworkController_getLatestBlock = function _NetworkController_getLatestBlock() {
|
|
461
533
|
return new Promise((resolve, reject) => {
|
|
534
|
+
if (!__classPrivateFieldGet(this, _NetworkController_ethQuery, "f")) {
|
|
535
|
+
throw new Error('Provider has not been initialized');
|
|
536
|
+
}
|
|
462
537
|
__classPrivateFieldGet(this, _NetworkController_ethQuery, "f").sendAsync({ method: 'eth_getBlockByNumber', params: ['latest', false] }, (error, block) => {
|
|
463
538
|
if (error) {
|
|
464
539
|
reject(error);
|
|
465
540
|
}
|
|
466
541
|
else {
|
|
542
|
+
// TODO: Validate this type
|
|
467
543
|
resolve(block);
|
|
468
544
|
}
|
|
469
545
|
});
|
|
470
546
|
});
|
|
547
|
+
}, _NetworkController_determineEIP1559Compatibility = function _NetworkController_determineEIP1559Compatibility() {
|
|
548
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
549
|
+
const latestBlock = yield __classPrivateFieldGet(this, _NetworkController_instances, "m", _NetworkController_getLatestBlock).call(this);
|
|
550
|
+
return (latestBlock === null || latestBlock === void 0 ? void 0 : latestBlock.baseFeePerGas) !== undefined;
|
|
551
|
+
});
|
|
471
552
|
}, _NetworkController_setProviderAndBlockTracker = function _NetworkController_setProviderAndBlockTracker({ provider, blockTracker, }) {
|
|
472
553
|
if (__classPrivateFieldGet(this, _NetworkController_providerProxy, "f")) {
|
|
473
554
|
__classPrivateFieldGet(this, _NetworkController_providerProxy, "f").setTarget(provider);
|
|
@@ -475,7 +556,6 @@ _NetworkController_ethQuery = new WeakMap(), _NetworkController_infuraProjectId
|
|
|
475
556
|
else {
|
|
476
557
|
__classPrivateFieldSet(this, _NetworkController_providerProxy, (0, swappable_obj_proxy_1.createEventEmitterProxy)(provider), "f");
|
|
477
558
|
}
|
|
478
|
-
__classPrivateFieldSet(this, _NetworkController_provider, provider, "f");
|
|
479
559
|
if (__classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f")) {
|
|
480
560
|
__classPrivateFieldGet(this, _NetworkController_blockTrackerProxy, "f").setTarget(blockTracker);
|
|
481
561
|
}
|