@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
|
@@ -0,0 +1,999 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
|
+
|
|
3
|
+
var _chunkZKNI7MD3js = require('./chunk-ZKNI7MD3.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _chunkLTT6WJXCjs = require('./chunk-LTT6WJXC.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
var _chunkVGYLDDJBjs = require('./chunk-VGYLDDJB.js');
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
var _chunkZ4BLTVTBjs = require('./chunk-Z4BLTVTB.js');
|
|
17
|
+
|
|
18
|
+
// src/NetworkController.ts
|
|
19
|
+
var _basecontroller = require('@metamask/base-controller');
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
var _controllerutils = require('@metamask/controller-utils');
|
|
29
|
+
var _ethquery = require('@metamask/eth-query'); var _ethquery2 = _interopRequireDefault(_ethquery);
|
|
30
|
+
var _rpcerrors = require('@metamask/rpc-errors');
|
|
31
|
+
var _swappableobjproxy = require('@metamask/swappable-obj-proxy');
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
var _utils = require('@metamask/utils');
|
|
37
|
+
var _assert = require('assert');
|
|
38
|
+
var _uuid = require('uuid');
|
|
39
|
+
var log = _chunkVGYLDDJBjs.createModuleLogger.call(void 0, _chunkVGYLDDJBjs.projectLogger, "NetworkController");
|
|
40
|
+
function knownKeysOf(object) {
|
|
41
|
+
return Object.keys(object);
|
|
42
|
+
}
|
|
43
|
+
function assertOfType(value, validate, message) {
|
|
44
|
+
_assert.strict.ok(validate(value), message);
|
|
45
|
+
}
|
|
46
|
+
function pick(object, keys) {
|
|
47
|
+
const pickedObject = keys.reduce(
|
|
48
|
+
(finalObject, key) => {
|
|
49
|
+
return { ...finalObject, [key]: object[key] };
|
|
50
|
+
},
|
|
51
|
+
{}
|
|
52
|
+
);
|
|
53
|
+
assertOfType(
|
|
54
|
+
pickedObject,
|
|
55
|
+
() => keys.every((key) => key in pickedObject),
|
|
56
|
+
"The reduce did not produce an object with all of the desired keys."
|
|
57
|
+
);
|
|
58
|
+
return pickedObject;
|
|
59
|
+
}
|
|
60
|
+
function isErrorWithCode(error) {
|
|
61
|
+
return typeof error === "object" && error !== null && "code" in error;
|
|
62
|
+
}
|
|
63
|
+
function buildInfuraNetworkClientId(infuraNetworkOrProviderConfig) {
|
|
64
|
+
if (typeof infuraNetworkOrProviderConfig === "string") {
|
|
65
|
+
return infuraNetworkOrProviderConfig;
|
|
66
|
+
}
|
|
67
|
+
return infuraNetworkOrProviderConfig.type;
|
|
68
|
+
}
|
|
69
|
+
function buildCustomNetworkClientId(...args) {
|
|
70
|
+
if (args.length === 1) {
|
|
71
|
+
return args[0];
|
|
72
|
+
}
|
|
73
|
+
const [{ id, rpcUrl }, networkConfigurations] = args;
|
|
74
|
+
if (id === void 0) {
|
|
75
|
+
const matchingNetworkConfiguration = Object.values(
|
|
76
|
+
networkConfigurations
|
|
77
|
+
).find((networkConfiguration) => {
|
|
78
|
+
return networkConfiguration.rpcUrl === rpcUrl.toLowerCase();
|
|
79
|
+
});
|
|
80
|
+
if (matchingNetworkConfiguration) {
|
|
81
|
+
return matchingNetworkConfiguration.id;
|
|
82
|
+
}
|
|
83
|
+
return rpcUrl.toLowerCase();
|
|
84
|
+
}
|
|
85
|
+
return id;
|
|
86
|
+
}
|
|
87
|
+
function isInfuraProviderConfig(providerConfig) {
|
|
88
|
+
return _controllerutils.isInfuraNetworkType.call(void 0, providerConfig.type);
|
|
89
|
+
}
|
|
90
|
+
function isCustomProviderConfig(providerConfig) {
|
|
91
|
+
return providerConfig.type === _controllerutils.NetworkType.rpc;
|
|
92
|
+
}
|
|
93
|
+
function validateCustomProviderConfig(providerConfig) {
|
|
94
|
+
if (providerConfig.chainId === void 0) {
|
|
95
|
+
throw new Error("chainId must be provided for custom RPC endpoints");
|
|
96
|
+
}
|
|
97
|
+
if (providerConfig.rpcUrl === void 0) {
|
|
98
|
+
throw new Error("rpcUrl must be provided for custom RPC endpoints");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
var name = "NetworkController";
|
|
102
|
+
var defaultState = {
|
|
103
|
+
selectedNetworkClientId: _controllerutils.NetworkType.mainnet,
|
|
104
|
+
providerConfig: {
|
|
105
|
+
type: _controllerutils.NetworkType.mainnet,
|
|
106
|
+
chainId: _controllerutils.ChainId.mainnet,
|
|
107
|
+
ticker: _controllerutils.NetworksTicker.mainnet
|
|
108
|
+
},
|
|
109
|
+
networksMetadata: {},
|
|
110
|
+
networkConfigurations: {}
|
|
111
|
+
};
|
|
112
|
+
var _ethQuery, _infuraProjectId, _trackMetaMetricsEvent, _previousProviderConfig, _providerProxy, _blockTrackerProxy, _autoManagedNetworkClientRegistry, _refreshNetwork, refreshNetwork_fn, _getLatestBlock, getLatestBlock_fn, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn, _createAutoManagedNetworkClientRegistry, createAutoManagedNetworkClientRegistry_fn, _buildIdentifiedInfuraNetworkClientConfigurations, buildIdentifiedInfuraNetworkClientConfigurations_fn, _buildIdentifiedCustomNetworkClientConfigurations, buildIdentifiedCustomNetworkClientConfigurations_fn, _buildIdentifiedNetworkClientConfigurationsFromProviderConfig, buildIdentifiedNetworkClientConfigurationsFromProviderConfig_fn, _applyNetworkSelection, applyNetworkSelection_fn;
|
|
113
|
+
var NetworkController = class extends _basecontroller.BaseController {
|
|
114
|
+
constructor({
|
|
115
|
+
messenger,
|
|
116
|
+
state,
|
|
117
|
+
infuraProjectId,
|
|
118
|
+
trackMetaMetricsEvent
|
|
119
|
+
}) {
|
|
120
|
+
super({
|
|
121
|
+
name,
|
|
122
|
+
metadata: {
|
|
123
|
+
selectedNetworkClientId: {
|
|
124
|
+
persist: true,
|
|
125
|
+
anonymous: false
|
|
126
|
+
},
|
|
127
|
+
networksMetadata: {
|
|
128
|
+
persist: true,
|
|
129
|
+
anonymous: false
|
|
130
|
+
},
|
|
131
|
+
providerConfig: {
|
|
132
|
+
persist: true,
|
|
133
|
+
anonymous: false
|
|
134
|
+
},
|
|
135
|
+
networkConfigurations: {
|
|
136
|
+
persist: true,
|
|
137
|
+
anonymous: false
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
messenger,
|
|
141
|
+
state: { ...defaultState, ...state }
|
|
142
|
+
});
|
|
143
|
+
/**
|
|
144
|
+
* Executes a series of steps to apply the changes to the provider config:
|
|
145
|
+
*
|
|
146
|
+
* 1. Notifies subscribers that the network is about to change.
|
|
147
|
+
* 2. Looks up a known and preinitialized network client matching the provider
|
|
148
|
+
* config and re-points the provider and block tracker proxy to it.
|
|
149
|
+
* 3. Notifies subscribers that the network has changed.
|
|
150
|
+
*/
|
|
151
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _refreshNetwork);
|
|
152
|
+
/**
|
|
153
|
+
* Fetches the latest block for the network.
|
|
154
|
+
*
|
|
155
|
+
* @param networkClientId - The networkClientId to fetch the correct provider against which to check the latest block. Defaults to the selectedNetworkClientId.
|
|
156
|
+
* @returns A promise that either resolves to the block header or null if
|
|
157
|
+
* there is no latest block, or rejects with an error.
|
|
158
|
+
*/
|
|
159
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getLatestBlock);
|
|
160
|
+
/**
|
|
161
|
+
* Retrieves and checks the latest block from the currently selected
|
|
162
|
+
* network; if the block has a `baseFeePerGas` property, then we know
|
|
163
|
+
* that the network supports EIP-1559; otherwise it doesn't.
|
|
164
|
+
*
|
|
165
|
+
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility
|
|
166
|
+
* @returns A promise that resolves to `true` if the network supports EIP-1559,
|
|
167
|
+
* `false` otherwise, or `undefined` if unable to retrieve the last block.
|
|
168
|
+
*/
|
|
169
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _determineEIP1559Compatibility);
|
|
170
|
+
/**
|
|
171
|
+
* Before accessing or switching the network, the registry of network clients
|
|
172
|
+
* needs to be populated. Otherwise, `#applyNetworkSelection` and
|
|
173
|
+
* `getNetworkClientRegistry` will throw an error. This method checks to see if the
|
|
174
|
+
* population step has happened yet, and if not, makes it happen.
|
|
175
|
+
*
|
|
176
|
+
* @returns The populated network client registry.
|
|
177
|
+
*/
|
|
178
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated);
|
|
179
|
+
/**
|
|
180
|
+
* Constructs the registry of network clients based on the set of built-in
|
|
181
|
+
* networks as well as the custom networks in state.
|
|
182
|
+
*
|
|
183
|
+
* @returns The network clients keyed by ID.
|
|
184
|
+
*/
|
|
185
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _createAutoManagedNetworkClientRegistry);
|
|
186
|
+
/**
|
|
187
|
+
* Constructs the list of network clients for built-in networks (that is,
|
|
188
|
+
* the subset of the networks we know Infura supports that consumers do not
|
|
189
|
+
* need to explicitly add).
|
|
190
|
+
*
|
|
191
|
+
* @returns The network clients.
|
|
192
|
+
*/
|
|
193
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _buildIdentifiedInfuraNetworkClientConfigurations);
|
|
194
|
+
/**
|
|
195
|
+
* Constructs the list of network clients for custom networks (that is, those
|
|
196
|
+
* which consumers have added via `networkConfigurations`).
|
|
197
|
+
*
|
|
198
|
+
* @returns The network clients.
|
|
199
|
+
*/
|
|
200
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _buildIdentifiedCustomNetworkClientConfigurations);
|
|
201
|
+
/**
|
|
202
|
+
* Converts the provider config object in state to a network client
|
|
203
|
+
* configuration object.
|
|
204
|
+
*
|
|
205
|
+
* @returns The network client config.
|
|
206
|
+
* @throws If the provider config is of type "rpc" and lacks either a
|
|
207
|
+
* `chainId` or an `rpcUrl`.
|
|
208
|
+
*/
|
|
209
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _buildIdentifiedNetworkClientConfigurationsFromProviderConfig);
|
|
210
|
+
/**
|
|
211
|
+
* Uses the information in the provider config object to look up a known and
|
|
212
|
+
* preinitialized network client. Once a network client is found, updates the
|
|
213
|
+
* provider and block tracker proxy to point to those from the network client,
|
|
214
|
+
* then finally creates an EthQuery that points to the provider proxy.
|
|
215
|
+
*
|
|
216
|
+
* @throws If no network client could be found matching the current provider
|
|
217
|
+
* config.
|
|
218
|
+
*/
|
|
219
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _applyNetworkSelection);
|
|
220
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _ethQuery, void 0);
|
|
221
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _infuraProjectId, void 0);
|
|
222
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _trackMetaMetricsEvent, void 0);
|
|
223
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _previousProviderConfig, void 0);
|
|
224
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _providerProxy, void 0);
|
|
225
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _blockTrackerProxy, void 0);
|
|
226
|
+
_chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _autoManagedNetworkClientRegistry, void 0);
|
|
227
|
+
if (!infuraProjectId || typeof infuraProjectId !== "string") {
|
|
228
|
+
throw new Error("Invalid Infura project ID");
|
|
229
|
+
}
|
|
230
|
+
_chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _infuraProjectId, infuraProjectId);
|
|
231
|
+
_chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _trackMetaMetricsEvent, trackMetaMetricsEvent);
|
|
232
|
+
this.messagingSystem.registerActionHandler(
|
|
233
|
+
`${this.name}:getProviderConfig`,
|
|
234
|
+
() => {
|
|
235
|
+
return this.state.providerConfig;
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
this.messagingSystem.registerActionHandler(
|
|
239
|
+
`${this.name}:getEthQuery`,
|
|
240
|
+
() => {
|
|
241
|
+
return _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _ethQuery);
|
|
242
|
+
}
|
|
243
|
+
);
|
|
244
|
+
this.messagingSystem.registerActionHandler(
|
|
245
|
+
`${this.name}:getNetworkClientById`,
|
|
246
|
+
this.getNetworkClientById.bind(this)
|
|
247
|
+
);
|
|
248
|
+
this.messagingSystem.registerActionHandler(
|
|
249
|
+
`${this.name}:getEIP1559Compatibility`,
|
|
250
|
+
this.getEIP1559Compatibility.bind(this)
|
|
251
|
+
);
|
|
252
|
+
this.messagingSystem.registerActionHandler(
|
|
253
|
+
`${this.name}:setActiveNetwork`,
|
|
254
|
+
this.setActiveNetwork.bind(this)
|
|
255
|
+
);
|
|
256
|
+
this.messagingSystem.registerActionHandler(
|
|
257
|
+
`${this.name}:setProviderType`,
|
|
258
|
+
this.setProviderType.bind(this)
|
|
259
|
+
);
|
|
260
|
+
this.messagingSystem.registerActionHandler(
|
|
261
|
+
`${this.name}:findNetworkClientIdByChainId`,
|
|
262
|
+
this.findNetworkClientIdByChainId.bind(this)
|
|
263
|
+
);
|
|
264
|
+
this.messagingSystem.registerActionHandler(
|
|
265
|
+
`${this.name}:getNetworkConfigurationByNetworkClientId`,
|
|
266
|
+
this.getNetworkConfigurationByNetworkClientId.bind(this)
|
|
267
|
+
);
|
|
268
|
+
_chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _previousProviderConfig, this.state.providerConfig);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Accesses the provider and block tracker for the currently selected network.
|
|
272
|
+
*
|
|
273
|
+
* @returns The proxy and block tracker proxies.
|
|
274
|
+
*/
|
|
275
|
+
getProviderAndBlockTracker() {
|
|
276
|
+
return {
|
|
277
|
+
provider: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _providerProxy),
|
|
278
|
+
blockTracker: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _blockTrackerProxy)
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Returns all of the network clients that have been created so far, keyed by
|
|
283
|
+
* their identifier in the network client registry. This collection represents
|
|
284
|
+
* not only built-in networks but also any custom networks that consumers have
|
|
285
|
+
* added.
|
|
286
|
+
*
|
|
287
|
+
* @returns The list of known network clients.
|
|
288
|
+
*/
|
|
289
|
+
getNetworkClientRegistry() {
|
|
290
|
+
const autoManagedNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
291
|
+
return Object.assign(
|
|
292
|
+
{},
|
|
293
|
+
autoManagedNetworkClientRegistry["infura" /* Infura */],
|
|
294
|
+
autoManagedNetworkClientRegistry["custom" /* Custom */]
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
getNetworkClientById(networkClientId) {
|
|
298
|
+
if (!networkClientId) {
|
|
299
|
+
throw new Error("No network client ID was provided.");
|
|
300
|
+
}
|
|
301
|
+
const autoManagedNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
302
|
+
if (_controllerutils.isInfuraNetworkType.call(void 0, networkClientId)) {
|
|
303
|
+
const infuraNetworkClient = autoManagedNetworkClientRegistry["infura" /* Infura */][networkClientId];
|
|
304
|
+
if (!infuraNetworkClient) {
|
|
305
|
+
throw new Error(
|
|
306
|
+
`No Infura network client was found with the ID "${networkClientId}".`
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
return infuraNetworkClient;
|
|
310
|
+
}
|
|
311
|
+
const customNetworkClient = autoManagedNetworkClientRegistry["custom" /* Custom */][networkClientId];
|
|
312
|
+
if (!customNetworkClient) {
|
|
313
|
+
throw new Error(
|
|
314
|
+
`No custom network client was found with the ID "${networkClientId}".`
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
return customNetworkClient;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Populates the network clients and establishes the initial network based on
|
|
321
|
+
* the provider configuration in state.
|
|
322
|
+
*/
|
|
323
|
+
async initializeProvider() {
|
|
324
|
+
_chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
325
|
+
_chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _applyNetworkSelection, applyNetworkSelection_fn).call(this);
|
|
326
|
+
await this.lookupNetwork();
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Refreshes the network meta with EIP-1559 support and the network status
|
|
330
|
+
* based on the given network client ID.
|
|
331
|
+
*
|
|
332
|
+
* @param networkClientId - The ID of the network client to update.
|
|
333
|
+
*/
|
|
334
|
+
async lookupNetworkByClientId(networkClientId) {
|
|
335
|
+
const isInfura = _controllerutils.isInfuraNetworkType.call(void 0, networkClientId);
|
|
336
|
+
let updatedNetworkStatus;
|
|
337
|
+
let updatedIsEIP1559Compatible;
|
|
338
|
+
try {
|
|
339
|
+
updatedIsEIP1559Compatible = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn).call(this, networkClientId);
|
|
340
|
+
updatedNetworkStatus = "available" /* Available */;
|
|
341
|
+
} catch (error) {
|
|
342
|
+
if (isErrorWithCode(error)) {
|
|
343
|
+
let responseBody;
|
|
344
|
+
if (isInfura && _utils.hasProperty.call(void 0, error, "message") && typeof error.message === "string") {
|
|
345
|
+
try {
|
|
346
|
+
responseBody = JSON.parse(error.message);
|
|
347
|
+
} catch {
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (_utils.isPlainObject.call(void 0, responseBody) && responseBody.error === _chunkZKNI7MD3js.INFURA_BLOCKED_KEY) {
|
|
351
|
+
updatedNetworkStatus = "blocked" /* Blocked */;
|
|
352
|
+
} else if (error.code === _rpcerrors.errorCodes.rpc.internal) {
|
|
353
|
+
updatedNetworkStatus = "unknown" /* Unknown */;
|
|
354
|
+
} else {
|
|
355
|
+
updatedNetworkStatus = "unavailable" /* Unavailable */;
|
|
356
|
+
}
|
|
357
|
+
} else if (typeof Error !== "undefined" && _utils.hasProperty.call(void 0, error, "message") && typeof error.message === "string" && error.message.includes(
|
|
358
|
+
"No custom network client was found with the ID"
|
|
359
|
+
)) {
|
|
360
|
+
throw error;
|
|
361
|
+
} else {
|
|
362
|
+
log("NetworkController - could not determine network status", error);
|
|
363
|
+
updatedNetworkStatus = "unknown" /* Unknown */;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
this.update((state) => {
|
|
367
|
+
if (state.networksMetadata[networkClientId] === void 0) {
|
|
368
|
+
state.networksMetadata[networkClientId] = {
|
|
369
|
+
status: "unknown" /* Unknown */,
|
|
370
|
+
EIPS: {}
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
const meta = state.networksMetadata[networkClientId];
|
|
374
|
+
meta.status = updatedNetworkStatus;
|
|
375
|
+
if (updatedIsEIP1559Compatible === void 0) {
|
|
376
|
+
delete meta.EIPS[1559];
|
|
377
|
+
} else {
|
|
378
|
+
meta.EIPS[1559] = updatedIsEIP1559Compatible;
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Performs side effects after switching to a network. If the network is
|
|
384
|
+
* available, updates the network state with the network ID of the network and
|
|
385
|
+
* stores whether the network supports EIP-1559; otherwise clears said
|
|
386
|
+
* information about the network that may have been previously stored.
|
|
387
|
+
*
|
|
388
|
+
* @param networkClientId - (Optional) The ID of the network client to update.
|
|
389
|
+
* If no ID is provided, uses the currently selected network.
|
|
390
|
+
* @fires infuraIsBlocked if the network is Infura-supported and is blocking
|
|
391
|
+
* requests.
|
|
392
|
+
* @fires infuraIsUnblocked if the network is Infura-supported and is not
|
|
393
|
+
* blocking requests, or if the network is not Infura-supported.
|
|
394
|
+
*/
|
|
395
|
+
async lookupNetwork(networkClientId) {
|
|
396
|
+
if (networkClientId) {
|
|
397
|
+
await this.lookupNetworkByClientId(networkClientId);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
if (!_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _ethQuery)) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const isInfura = isInfuraProviderConfig(this.state.providerConfig);
|
|
404
|
+
let networkChanged = false;
|
|
405
|
+
const listener = () => {
|
|
406
|
+
networkChanged = true;
|
|
407
|
+
this.messagingSystem.unsubscribe(
|
|
408
|
+
"NetworkController:networkDidChange",
|
|
409
|
+
listener
|
|
410
|
+
);
|
|
411
|
+
};
|
|
412
|
+
this.messagingSystem.subscribe(
|
|
413
|
+
"NetworkController:networkDidChange",
|
|
414
|
+
listener
|
|
415
|
+
);
|
|
416
|
+
let updatedNetworkStatus;
|
|
417
|
+
let updatedIsEIP1559Compatible;
|
|
418
|
+
try {
|
|
419
|
+
const isEIP1559Compatible = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn).call(this, this.state.selectedNetworkClientId);
|
|
420
|
+
updatedNetworkStatus = "available" /* Available */;
|
|
421
|
+
updatedIsEIP1559Compatible = isEIP1559Compatible;
|
|
422
|
+
} catch (error) {
|
|
423
|
+
if (isErrorWithCode(error)) {
|
|
424
|
+
let responseBody;
|
|
425
|
+
if (isInfura && _utils.hasProperty.call(void 0, error, "message") && typeof error.message === "string") {
|
|
426
|
+
try {
|
|
427
|
+
responseBody = JSON.parse(error.message);
|
|
428
|
+
} catch {
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
if (_utils.isPlainObject.call(void 0, responseBody) && responseBody.error === _chunkZKNI7MD3js.INFURA_BLOCKED_KEY) {
|
|
432
|
+
updatedNetworkStatus = "blocked" /* Blocked */;
|
|
433
|
+
} else if (error.code === _rpcerrors.errorCodes.rpc.internal) {
|
|
434
|
+
updatedNetworkStatus = "unknown" /* Unknown */;
|
|
435
|
+
} else {
|
|
436
|
+
updatedNetworkStatus = "unavailable" /* Unavailable */;
|
|
437
|
+
}
|
|
438
|
+
} else {
|
|
439
|
+
log("NetworkController - could not determine network status", error);
|
|
440
|
+
updatedNetworkStatus = "unknown" /* Unknown */;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (networkChanged) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
this.messagingSystem.unsubscribe(
|
|
447
|
+
"NetworkController:networkDidChange",
|
|
448
|
+
listener
|
|
449
|
+
);
|
|
450
|
+
this.update((state) => {
|
|
451
|
+
const meta = state.networksMetadata[state.selectedNetworkClientId];
|
|
452
|
+
meta.status = updatedNetworkStatus;
|
|
453
|
+
if (updatedIsEIP1559Compatible === void 0) {
|
|
454
|
+
delete meta.EIPS[1559];
|
|
455
|
+
} else {
|
|
456
|
+
meta.EIPS[1559] = updatedIsEIP1559Compatible;
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
if (isInfura) {
|
|
460
|
+
if (updatedNetworkStatus === "available" /* Available */) {
|
|
461
|
+
this.messagingSystem.publish("NetworkController:infuraIsUnblocked");
|
|
462
|
+
} else if (updatedNetworkStatus === "blocked" /* Blocked */) {
|
|
463
|
+
this.messagingSystem.publish("NetworkController:infuraIsBlocked");
|
|
464
|
+
}
|
|
465
|
+
} else {
|
|
466
|
+
this.messagingSystem.publish("NetworkController:infuraIsUnblocked");
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Convenience method to update provider network type settings.
|
|
471
|
+
*
|
|
472
|
+
* @param type - Human readable network name.
|
|
473
|
+
* @deprecated This has been replaced by `setActiveNetwork`, and will be
|
|
474
|
+
* removed in a future release
|
|
475
|
+
*/
|
|
476
|
+
async setProviderType(type) {
|
|
477
|
+
_assert.strict.notStrictEqual(
|
|
478
|
+
type,
|
|
479
|
+
_controllerutils.NetworkType.rpc,
|
|
480
|
+
`NetworkController - cannot call "setProviderType" with type "${_controllerutils.NetworkType.rpc}". Use "setActiveNetwork"`
|
|
481
|
+
);
|
|
482
|
+
_assert.strict.ok(
|
|
483
|
+
_controllerutils.isInfuraNetworkType.call(void 0, type),
|
|
484
|
+
`Unknown Infura provider type "${type}".`
|
|
485
|
+
);
|
|
486
|
+
await this.setActiveNetwork(type);
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Convenience method to update provider RPC settings.
|
|
490
|
+
*
|
|
491
|
+
* @param networkConfigurationIdOrType - The unique id for the network configuration to set as the active provider,
|
|
492
|
+
* or the type of a built-in network.
|
|
493
|
+
*/
|
|
494
|
+
async setActiveNetwork(networkConfigurationIdOrType) {
|
|
495
|
+
_chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _previousProviderConfig, this.state.providerConfig);
|
|
496
|
+
let targetNetwork;
|
|
497
|
+
if (_controllerutils.isInfuraNetworkType.call(void 0, networkConfigurationIdOrType)) {
|
|
498
|
+
const ticker = _controllerutils.NetworksTicker[networkConfigurationIdOrType];
|
|
499
|
+
targetNetwork = {
|
|
500
|
+
chainId: _controllerutils.ChainId[networkConfigurationIdOrType],
|
|
501
|
+
id: void 0,
|
|
502
|
+
rpcPrefs: _controllerutils.BUILT_IN_NETWORKS[networkConfigurationIdOrType].rpcPrefs,
|
|
503
|
+
rpcUrl: void 0,
|
|
504
|
+
nickname: void 0,
|
|
505
|
+
ticker,
|
|
506
|
+
type: networkConfigurationIdOrType
|
|
507
|
+
};
|
|
508
|
+
} else {
|
|
509
|
+
if (!Object.keys(this.state.networkConfigurations).includes(
|
|
510
|
+
networkConfigurationIdOrType
|
|
511
|
+
)) {
|
|
512
|
+
throw new Error(
|
|
513
|
+
`networkConfigurationId ${networkConfigurationIdOrType} does not match a configured networkConfiguration or built-in network type`
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
targetNetwork = {
|
|
517
|
+
...this.state.networkConfigurations[networkConfigurationIdOrType],
|
|
518
|
+
type: _controllerutils.NetworkType.rpc
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
_chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
522
|
+
this.update((state) => {
|
|
523
|
+
state.providerConfig = targetNetwork;
|
|
524
|
+
});
|
|
525
|
+
await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _refreshNetwork, refreshNetwork_fn).call(this);
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Determines whether the network supports EIP-1559 by checking whether the
|
|
529
|
+
* latest block has a `baseFeePerGas` property, then updates state
|
|
530
|
+
* appropriately.
|
|
531
|
+
*
|
|
532
|
+
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility.
|
|
533
|
+
* @returns A promise that resolves to true if the network supports EIP-1559
|
|
534
|
+
* , false otherwise, or `undefined` if unable to determine the compatibility.
|
|
535
|
+
*/
|
|
536
|
+
async getEIP1559Compatibility(networkClientId) {
|
|
537
|
+
if (networkClientId) {
|
|
538
|
+
return this.get1559CompatibilityWithNetworkClientId(networkClientId);
|
|
539
|
+
}
|
|
540
|
+
if (!_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _ethQuery)) {
|
|
541
|
+
return false;
|
|
542
|
+
}
|
|
543
|
+
const { EIPS } = this.state.networksMetadata[this.state.selectedNetworkClientId];
|
|
544
|
+
if (EIPS[1559] !== void 0) {
|
|
545
|
+
return EIPS[1559];
|
|
546
|
+
}
|
|
547
|
+
const isEIP1559Compatible = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn).call(this, this.state.selectedNetworkClientId);
|
|
548
|
+
this.update((state) => {
|
|
549
|
+
if (isEIP1559Compatible !== void 0) {
|
|
550
|
+
state.networksMetadata[state.selectedNetworkClientId].EIPS[1559] = isEIP1559Compatible;
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
return isEIP1559Compatible;
|
|
554
|
+
}
|
|
555
|
+
async get1559CompatibilityWithNetworkClientId(networkClientId) {
|
|
556
|
+
let metadata = this.state.networksMetadata[networkClientId];
|
|
557
|
+
if (metadata === void 0) {
|
|
558
|
+
await this.lookupNetwork(networkClientId);
|
|
559
|
+
metadata = this.state.networksMetadata[networkClientId];
|
|
560
|
+
}
|
|
561
|
+
const { EIPS } = metadata;
|
|
562
|
+
return EIPS[1559];
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Re-initializes the provider and block tracker for the current network.
|
|
566
|
+
*/
|
|
567
|
+
async resetConnection() {
|
|
568
|
+
_chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
569
|
+
await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _refreshNetwork, refreshNetwork_fn).call(this);
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Returns a configuration object for the network identified by the given
|
|
573
|
+
* network client ID. If given an Infura network type, constructs one based on
|
|
574
|
+
* what we know about the network; otherwise attempts locates a network
|
|
575
|
+
* configuration in state that corresponds to the network client ID.
|
|
576
|
+
*
|
|
577
|
+
* @param networkClientId - The network client ID.
|
|
578
|
+
* @returns The configuration for the referenced network if one exists, or
|
|
579
|
+
* undefined otherwise.
|
|
580
|
+
*/
|
|
581
|
+
getNetworkConfigurationByNetworkClientId(networkClientId) {
|
|
582
|
+
if (_controllerutils.isInfuraNetworkType.call(void 0, networkClientId)) {
|
|
583
|
+
const rpcUrl = `https://${networkClientId}.infura.io/v3/${_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _infuraProjectId)}`;
|
|
584
|
+
return {
|
|
585
|
+
rpcUrl,
|
|
586
|
+
..._controllerutils.BUILT_IN_NETWORKS[networkClientId]
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
return this.state.networkConfigurations[networkClientId];
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Adds a new custom network or updates the information for an existing
|
|
593
|
+
* network.
|
|
594
|
+
*
|
|
595
|
+
* This may involve updating the `networkConfigurations` property in
|
|
596
|
+
* state as well and/or adding a new network client to the network client
|
|
597
|
+
* registry. The `rpcUrl` and `chainId` of the given object are used to
|
|
598
|
+
* determine which action to take:
|
|
599
|
+
*
|
|
600
|
+
* - If the `rpcUrl` corresponds to an existing network configuration
|
|
601
|
+
* (case-insensitively), then it is overwritten with the object. Furthermore,
|
|
602
|
+
* if the `chainId` is different from the existing network configuration, then
|
|
603
|
+
* the existing network client is replaced with a new one.
|
|
604
|
+
* - If the `rpcUrl` does not correspond to an existing network configuration
|
|
605
|
+
* (case-insensitively), then the object is used to add a new network
|
|
606
|
+
* configuration along with a new network client.
|
|
607
|
+
*
|
|
608
|
+
* @param networkConfiguration - The network configuration to add or update.
|
|
609
|
+
* @param options - Additional configuration options.
|
|
610
|
+
* @param options.referrer - Used to create a metrics event; the site from which the call originated, or 'metamask' for internal calls.
|
|
611
|
+
* @param options.source - Used to create a metrics event; where the event originated (i.e. from a dapp or from the network form).
|
|
612
|
+
* @param options.setActive - If true, switches to the network upon adding or updating it (default: false).
|
|
613
|
+
* @returns The ID for the added or updated network configuration.
|
|
614
|
+
*/
|
|
615
|
+
async upsertNetworkConfiguration(networkConfiguration, {
|
|
616
|
+
referrer,
|
|
617
|
+
source,
|
|
618
|
+
setActive = false
|
|
619
|
+
}) {
|
|
620
|
+
const sanitizedNetworkConfiguration = pick(
|
|
621
|
+
networkConfiguration,
|
|
622
|
+
["rpcUrl", "chainId", "ticker", "nickname", "rpcPrefs"]
|
|
623
|
+
);
|
|
624
|
+
const { rpcUrl, chainId, ticker } = sanitizedNetworkConfiguration;
|
|
625
|
+
_utils.assertIsStrictHexString.call(void 0, chainId);
|
|
626
|
+
if (!_controllerutils.isSafeChainId.call(void 0, chainId)) {
|
|
627
|
+
throw new Error(
|
|
628
|
+
`Invalid chain ID "${chainId}": numerical value greater than max safe value.`
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
if (!rpcUrl) {
|
|
632
|
+
throw new Error(
|
|
633
|
+
"An rpcUrl is required to add or update network configuration"
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
if (!referrer || !source) {
|
|
637
|
+
throw new Error(
|
|
638
|
+
"referrer and source are required arguments for adding or updating a network configuration"
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
try {
|
|
642
|
+
new URL(rpcUrl);
|
|
643
|
+
} catch (e) {
|
|
644
|
+
if (e.message.includes("Invalid URL")) {
|
|
645
|
+
throw new Error("rpcUrl must be a valid URL");
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
if (!ticker) {
|
|
649
|
+
throw new Error(
|
|
650
|
+
"A ticker is required to add or update networkConfiguration"
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
const autoManagedNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
654
|
+
const existingNetworkConfiguration = Object.values(
|
|
655
|
+
this.state.networkConfigurations
|
|
656
|
+
).find(
|
|
657
|
+
(networkConfig) => networkConfig.rpcUrl.toLowerCase() === rpcUrl.toLowerCase()
|
|
658
|
+
);
|
|
659
|
+
const upsertedNetworkConfigurationId = existingNetworkConfiguration ? existingNetworkConfiguration.id : _uuid.v4.call(void 0, );
|
|
660
|
+
const networkClientId = buildCustomNetworkClientId(
|
|
661
|
+
upsertedNetworkConfigurationId
|
|
662
|
+
);
|
|
663
|
+
const customNetworkClientRegistry = autoManagedNetworkClientRegistry["custom" /* Custom */];
|
|
664
|
+
const existingAutoManagedNetworkClient = customNetworkClientRegistry[networkClientId];
|
|
665
|
+
const shouldDestroyExistingNetworkClient = existingAutoManagedNetworkClient && existingAutoManagedNetworkClient.configuration.chainId !== chainId;
|
|
666
|
+
if (shouldDestroyExistingNetworkClient) {
|
|
667
|
+
existingAutoManagedNetworkClient.destroy();
|
|
668
|
+
}
|
|
669
|
+
if (!existingAutoManagedNetworkClient || shouldDestroyExistingNetworkClient) {
|
|
670
|
+
customNetworkClientRegistry[networkClientId] = _chunkLTT6WJXCjs.createAutoManagedNetworkClient.call(void 0, {
|
|
671
|
+
type: "custom" /* Custom */,
|
|
672
|
+
chainId,
|
|
673
|
+
rpcUrl,
|
|
674
|
+
ticker
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
this.update((state) => {
|
|
678
|
+
state.networkConfigurations[upsertedNetworkConfigurationId] = {
|
|
679
|
+
id: upsertedNetworkConfigurationId,
|
|
680
|
+
...sanitizedNetworkConfiguration
|
|
681
|
+
};
|
|
682
|
+
});
|
|
683
|
+
if (!existingNetworkConfiguration) {
|
|
684
|
+
_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _trackMetaMetricsEvent).call(this, {
|
|
685
|
+
event: "Custom Network Added",
|
|
686
|
+
category: "Network",
|
|
687
|
+
referrer: {
|
|
688
|
+
url: referrer
|
|
689
|
+
},
|
|
690
|
+
properties: {
|
|
691
|
+
chain_id: chainId,
|
|
692
|
+
symbol: ticker,
|
|
693
|
+
source
|
|
694
|
+
}
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
if (setActive) {
|
|
698
|
+
await this.setActiveNetwork(upsertedNetworkConfigurationId);
|
|
699
|
+
}
|
|
700
|
+
return upsertedNetworkConfigurationId;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Removes a custom network from state.
|
|
704
|
+
*
|
|
705
|
+
* This involves updating the `networkConfigurations` property in state as
|
|
706
|
+
* well and removing the network client that corresponds to the network from
|
|
707
|
+
* the client registry.
|
|
708
|
+
*
|
|
709
|
+
* @param networkConfigurationId - The ID of an existing network
|
|
710
|
+
* configuration.
|
|
711
|
+
*/
|
|
712
|
+
removeNetworkConfiguration(networkConfigurationId) {
|
|
713
|
+
if (!this.state.networkConfigurations[networkConfigurationId]) {
|
|
714
|
+
throw new Error(
|
|
715
|
+
`networkConfigurationId ${networkConfigurationId} does not match a configured networkConfiguration`
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
const autoManagedNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
719
|
+
const networkClientId = buildCustomNetworkClientId(networkConfigurationId);
|
|
720
|
+
this.update((state) => {
|
|
721
|
+
delete state.networkConfigurations[networkConfigurationId];
|
|
722
|
+
});
|
|
723
|
+
const customNetworkClientRegistry = autoManagedNetworkClientRegistry["custom" /* Custom */];
|
|
724
|
+
const existingAutoManagedNetworkClient = customNetworkClientRegistry[networkClientId];
|
|
725
|
+
existingAutoManagedNetworkClient.destroy();
|
|
726
|
+
delete customNetworkClientRegistry[networkClientId];
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Switches to the previously selected network, assuming that there is one
|
|
730
|
+
* (if not and `initializeProvider` has not been previously called, then this
|
|
731
|
+
* method is equivalent to calling `resetConnection`).
|
|
732
|
+
*/
|
|
733
|
+
async rollbackToPreviousProvider() {
|
|
734
|
+
_chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
735
|
+
this.update((state) => {
|
|
736
|
+
state.providerConfig = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _previousProviderConfig);
|
|
737
|
+
});
|
|
738
|
+
await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _refreshNetwork, refreshNetwork_fn).call(this);
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Deactivates the controller, stopping any ongoing polling.
|
|
742
|
+
*
|
|
743
|
+
* In-progress requests will not be aborted.
|
|
744
|
+
*/
|
|
745
|
+
async destroy() {
|
|
746
|
+
await _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _blockTrackerProxy)?.destroy();
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* Updates the controller using the given backup data.
|
|
750
|
+
*
|
|
751
|
+
* @param backup - The data that has been backed up.
|
|
752
|
+
* @param backup.networkConfigurations - Network configurations in the backup.
|
|
753
|
+
*/
|
|
754
|
+
loadBackup({
|
|
755
|
+
networkConfigurations
|
|
756
|
+
}) {
|
|
757
|
+
this.update((state) => {
|
|
758
|
+
state.networkConfigurations = {
|
|
759
|
+
...state.networkConfigurations,
|
|
760
|
+
...networkConfigurations
|
|
761
|
+
};
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Searches for a network configuration ID with the given ChainID and returns it.
|
|
766
|
+
*
|
|
767
|
+
* @param chainId - ChainId to search for
|
|
768
|
+
* @returns networkClientId of the network configuration with the given chainId
|
|
769
|
+
*/
|
|
770
|
+
findNetworkClientIdByChainId(chainId) {
|
|
771
|
+
const networkClients = this.getNetworkClientRegistry();
|
|
772
|
+
const networkClientEntry = Object.entries(networkClients).find(
|
|
773
|
+
([_, networkClient]) => networkClient.configuration.chainId === chainId
|
|
774
|
+
);
|
|
775
|
+
if (networkClientEntry === void 0) {
|
|
776
|
+
throw new Error("Couldn't find networkClientId for chainId");
|
|
777
|
+
}
|
|
778
|
+
return networkClientEntry[0];
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
_ethQuery = new WeakMap();
|
|
782
|
+
_infuraProjectId = new WeakMap();
|
|
783
|
+
_trackMetaMetricsEvent = new WeakMap();
|
|
784
|
+
_previousProviderConfig = new WeakMap();
|
|
785
|
+
_providerProxy = new WeakMap();
|
|
786
|
+
_blockTrackerProxy = new WeakMap();
|
|
787
|
+
_autoManagedNetworkClientRegistry = new WeakMap();
|
|
788
|
+
_refreshNetwork = new WeakSet();
|
|
789
|
+
refreshNetwork_fn = async function() {
|
|
790
|
+
this.messagingSystem.publish(
|
|
791
|
+
"NetworkController:networkWillChange",
|
|
792
|
+
this.state
|
|
793
|
+
);
|
|
794
|
+
_chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _applyNetworkSelection, applyNetworkSelection_fn).call(this);
|
|
795
|
+
this.messagingSystem.publish(
|
|
796
|
+
"NetworkController:networkDidChange",
|
|
797
|
+
this.state
|
|
798
|
+
);
|
|
799
|
+
await this.lookupNetwork();
|
|
800
|
+
};
|
|
801
|
+
_getLatestBlock = new WeakSet();
|
|
802
|
+
getLatestBlock_fn = function(networkClientId) {
|
|
803
|
+
if (networkClientId === void 0) {
|
|
804
|
+
networkClientId = this.state.selectedNetworkClientId;
|
|
805
|
+
}
|
|
806
|
+
const networkClient = this.getNetworkClientById(networkClientId);
|
|
807
|
+
const ethQuery = new (0, _ethquery2.default)(networkClient.provider);
|
|
808
|
+
return new Promise((resolve, reject) => {
|
|
809
|
+
ethQuery.sendAsync(
|
|
810
|
+
{ method: "eth_getBlockByNumber", params: ["latest", false] },
|
|
811
|
+
(error, block) => {
|
|
812
|
+
if (error) {
|
|
813
|
+
reject(error);
|
|
814
|
+
} else {
|
|
815
|
+
resolve(block);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
);
|
|
819
|
+
});
|
|
820
|
+
};
|
|
821
|
+
_determineEIP1559Compatibility = new WeakSet();
|
|
822
|
+
determineEIP1559Compatibility_fn = async function(networkClientId) {
|
|
823
|
+
const latestBlock = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getLatestBlock, getLatestBlock_fn).call(this, networkClientId);
|
|
824
|
+
if (!latestBlock) {
|
|
825
|
+
return void 0;
|
|
826
|
+
}
|
|
827
|
+
return latestBlock.baseFeePerGas !== void 0;
|
|
828
|
+
};
|
|
829
|
+
_ensureAutoManagedNetworkClientRegistryPopulated = new WeakSet();
|
|
830
|
+
ensureAutoManagedNetworkClientRegistryPopulated_fn = function() {
|
|
831
|
+
const autoManagedNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClientRegistry) ?? _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _createAutoManagedNetworkClientRegistry, createAutoManagedNetworkClientRegistry_fn).call(this);
|
|
832
|
+
_chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _autoManagedNetworkClientRegistry, autoManagedNetworkClientRegistry);
|
|
833
|
+
return autoManagedNetworkClientRegistry;
|
|
834
|
+
};
|
|
835
|
+
_createAutoManagedNetworkClientRegistry = new WeakSet();
|
|
836
|
+
createAutoManagedNetworkClientRegistry_fn = function() {
|
|
837
|
+
return [
|
|
838
|
+
..._chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _buildIdentifiedInfuraNetworkClientConfigurations, buildIdentifiedInfuraNetworkClientConfigurations_fn).call(this),
|
|
839
|
+
..._chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _buildIdentifiedCustomNetworkClientConfigurations, buildIdentifiedCustomNetworkClientConfigurations_fn).call(this),
|
|
840
|
+
..._chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _buildIdentifiedNetworkClientConfigurationsFromProviderConfig, buildIdentifiedNetworkClientConfigurationsFromProviderConfig_fn).call(this)
|
|
841
|
+
].reduce(
|
|
842
|
+
(registry, [networkClientType, networkClientId, networkClientConfiguration]) => {
|
|
843
|
+
const autoManagedNetworkClient = _chunkLTT6WJXCjs.createAutoManagedNetworkClient.call(void 0,
|
|
844
|
+
networkClientConfiguration
|
|
845
|
+
);
|
|
846
|
+
if (networkClientId in registry[networkClientType]) {
|
|
847
|
+
return registry;
|
|
848
|
+
}
|
|
849
|
+
return {
|
|
850
|
+
...registry,
|
|
851
|
+
[networkClientType]: {
|
|
852
|
+
...registry[networkClientType],
|
|
853
|
+
[networkClientId]: autoManagedNetworkClient
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
},
|
|
857
|
+
{
|
|
858
|
+
["infura" /* Infura */]: {},
|
|
859
|
+
["custom" /* Custom */]: {}
|
|
860
|
+
}
|
|
861
|
+
);
|
|
862
|
+
};
|
|
863
|
+
_buildIdentifiedInfuraNetworkClientConfigurations = new WeakSet();
|
|
864
|
+
buildIdentifiedInfuraNetworkClientConfigurations_fn = function() {
|
|
865
|
+
return knownKeysOf(_controllerutils.InfuraNetworkType).map((network) => {
|
|
866
|
+
const networkClientId = buildInfuraNetworkClientId(network);
|
|
867
|
+
const networkClientConfiguration = {
|
|
868
|
+
type: "infura" /* Infura */,
|
|
869
|
+
network,
|
|
870
|
+
infuraProjectId: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _infuraProjectId),
|
|
871
|
+
chainId: _controllerutils.BUILT_IN_NETWORKS[network].chainId,
|
|
872
|
+
ticker: _controllerutils.BUILT_IN_NETWORKS[network].ticker
|
|
873
|
+
};
|
|
874
|
+
return [
|
|
875
|
+
"infura" /* Infura */,
|
|
876
|
+
networkClientId,
|
|
877
|
+
networkClientConfiguration
|
|
878
|
+
];
|
|
879
|
+
});
|
|
880
|
+
};
|
|
881
|
+
_buildIdentifiedCustomNetworkClientConfigurations = new WeakSet();
|
|
882
|
+
buildIdentifiedCustomNetworkClientConfigurations_fn = function() {
|
|
883
|
+
return Object.entries(this.state.networkConfigurations).map(
|
|
884
|
+
([networkConfigurationId, networkConfiguration]) => {
|
|
885
|
+
if (networkConfiguration.chainId === void 0) {
|
|
886
|
+
throw new Error("chainId must be provided for custom RPC endpoints");
|
|
887
|
+
}
|
|
888
|
+
if (networkConfiguration.rpcUrl === void 0) {
|
|
889
|
+
throw new Error("rpcUrl must be provided for custom RPC endpoints");
|
|
890
|
+
}
|
|
891
|
+
const networkClientId = buildCustomNetworkClientId(
|
|
892
|
+
networkConfigurationId
|
|
893
|
+
);
|
|
894
|
+
const networkClientConfiguration = {
|
|
895
|
+
type: "custom" /* Custom */,
|
|
896
|
+
chainId: networkConfiguration.chainId,
|
|
897
|
+
rpcUrl: networkConfiguration.rpcUrl,
|
|
898
|
+
ticker: networkConfiguration.ticker
|
|
899
|
+
};
|
|
900
|
+
return [
|
|
901
|
+
"custom" /* Custom */,
|
|
902
|
+
networkClientId,
|
|
903
|
+
networkClientConfiguration
|
|
904
|
+
];
|
|
905
|
+
}
|
|
906
|
+
);
|
|
907
|
+
};
|
|
908
|
+
_buildIdentifiedNetworkClientConfigurationsFromProviderConfig = new WeakSet();
|
|
909
|
+
buildIdentifiedNetworkClientConfigurationsFromProviderConfig_fn = function() {
|
|
910
|
+
const { providerConfig } = this.state;
|
|
911
|
+
if (isCustomProviderConfig(providerConfig)) {
|
|
912
|
+
validateCustomProviderConfig(providerConfig);
|
|
913
|
+
const networkClientId = buildCustomNetworkClientId(
|
|
914
|
+
providerConfig,
|
|
915
|
+
this.state.networkConfigurations
|
|
916
|
+
);
|
|
917
|
+
const networkClientConfiguration = {
|
|
918
|
+
chainId: providerConfig.chainId,
|
|
919
|
+
rpcUrl: providerConfig.rpcUrl,
|
|
920
|
+
type: "custom" /* Custom */,
|
|
921
|
+
ticker: providerConfig.ticker
|
|
922
|
+
};
|
|
923
|
+
return [
|
|
924
|
+
["custom" /* Custom */, networkClientId, networkClientConfiguration]
|
|
925
|
+
];
|
|
926
|
+
}
|
|
927
|
+
if (isInfuraProviderConfig(providerConfig)) {
|
|
928
|
+
return [];
|
|
929
|
+
}
|
|
930
|
+
throw new Error(`Unrecognized network type: '${providerConfig.type}'`);
|
|
931
|
+
};
|
|
932
|
+
_applyNetworkSelection = new WeakSet();
|
|
933
|
+
applyNetworkSelection_fn = function() {
|
|
934
|
+
if (!_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClientRegistry)) {
|
|
935
|
+
throw new Error(
|
|
936
|
+
"initializeProvider must be called first in order to switch the network"
|
|
937
|
+
);
|
|
938
|
+
}
|
|
939
|
+
const { providerConfig } = this.state;
|
|
940
|
+
let autoManagedNetworkClient;
|
|
941
|
+
let networkClientId;
|
|
942
|
+
if (isInfuraProviderConfig(providerConfig)) {
|
|
943
|
+
const networkClientType = "infura" /* Infura */;
|
|
944
|
+
networkClientId = buildInfuraNetworkClientId(providerConfig);
|
|
945
|
+
const builtInNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClientRegistry)[networkClientType];
|
|
946
|
+
autoManagedNetworkClient = builtInNetworkClientRegistry[networkClientId];
|
|
947
|
+
if (!autoManagedNetworkClient) {
|
|
948
|
+
throw new Error(
|
|
949
|
+
`Could not find custom network matching ${networkClientId}`
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
} else if (isCustomProviderConfig(providerConfig)) {
|
|
953
|
+
validateCustomProviderConfig(providerConfig);
|
|
954
|
+
const networkClientType = "custom" /* Custom */;
|
|
955
|
+
networkClientId = buildCustomNetworkClientId(
|
|
956
|
+
providerConfig,
|
|
957
|
+
this.state.networkConfigurations
|
|
958
|
+
);
|
|
959
|
+
const customNetworkClientRegistry = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _autoManagedNetworkClientRegistry)[networkClientType];
|
|
960
|
+
autoManagedNetworkClient = customNetworkClientRegistry[networkClientId];
|
|
961
|
+
if (!autoManagedNetworkClient) {
|
|
962
|
+
throw new Error(
|
|
963
|
+
`Could not find built-in network matching ${networkClientId}`
|
|
964
|
+
);
|
|
965
|
+
}
|
|
966
|
+
} else {
|
|
967
|
+
throw new Error("Could not determine type of provider config");
|
|
968
|
+
}
|
|
969
|
+
this.update((state) => {
|
|
970
|
+
state.selectedNetworkClientId = networkClientId;
|
|
971
|
+
if (state.networksMetadata[networkClientId] === void 0) {
|
|
972
|
+
state.networksMetadata[networkClientId] = {
|
|
973
|
+
status: "unknown" /* Unknown */,
|
|
974
|
+
EIPS: {}
|
|
975
|
+
};
|
|
976
|
+
}
|
|
977
|
+
});
|
|
978
|
+
const { provider, blockTracker } = autoManagedNetworkClient;
|
|
979
|
+
if (_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _providerProxy)) {
|
|
980
|
+
_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _providerProxy).setTarget(provider);
|
|
981
|
+
} else {
|
|
982
|
+
_chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _providerProxy, _swappableobjproxy.createEventEmitterProxy.call(void 0, provider));
|
|
983
|
+
}
|
|
984
|
+
if (_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _blockTrackerProxy)) {
|
|
985
|
+
_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _blockTrackerProxy).setTarget(blockTracker);
|
|
986
|
+
} else {
|
|
987
|
+
_chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _blockTrackerProxy, _swappableobjproxy.createEventEmitterProxy.call(void 0, blockTracker, {
|
|
988
|
+
eventFilter: "skipInternal"
|
|
989
|
+
}));
|
|
990
|
+
}
|
|
991
|
+
_chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _ethQuery, new (0, _ethquery2.default)(_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _providerProxy)));
|
|
992
|
+
};
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
exports.knownKeysOf = knownKeysOf; exports.defaultState = defaultState; exports.NetworkController = NetworkController;
|
|
999
|
+
//# sourceMappingURL=chunk-VNCJZRDU.js.map
|