@metamask-previews/network-controller 20.0.0-preview-e4ec85f → 20.0.0-preview-ee06f305
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 +45 -0
- package/dist/NetworkController.js +4 -2
- package/dist/NetworkController.mjs +5 -3
- package/dist/chunk-DARVWUIF.mjs +1481 -0
- package/dist/chunk-DARVWUIF.mjs.map +1 -0
- package/dist/chunk-NGN5FE4G.js +1481 -0
- package/dist/chunk-NGN5FE4G.js.map +1 -0
- package/dist/index.js +4 -2
- package/dist/index.mjs +5 -3
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/NetworkController.d.ts +295 -97
- package/dist/types/NetworkController.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +3 -2
- package/dist/chunk-6PT34M4U.mjs +0 -949
- package/dist/chunk-6PT34M4U.mjs.map +0 -1
- package/dist/chunk-WT5ZBF4X.js +0 -949
- package/dist/chunk-WT5ZBF4X.js.map +0 -1
|
@@ -0,0 +1,1481 @@
|
|
|
1
|
+
import {
|
|
2
|
+
INFURA_BLOCKED_KEY
|
|
3
|
+
} from "./chunk-2QJYHWIP.mjs";
|
|
4
|
+
import {
|
|
5
|
+
createAutoManagedNetworkClient
|
|
6
|
+
} from "./chunk-TZA3CBEI.mjs";
|
|
7
|
+
import {
|
|
8
|
+
createModuleLogger,
|
|
9
|
+
projectLogger
|
|
10
|
+
} from "./chunk-VTLOAS2R.mjs";
|
|
11
|
+
import {
|
|
12
|
+
__privateAdd,
|
|
13
|
+
__privateGet,
|
|
14
|
+
__privateMethod,
|
|
15
|
+
__privateSet
|
|
16
|
+
} from "./chunk-XUI43LEZ.mjs";
|
|
17
|
+
|
|
18
|
+
// src/NetworkController.ts
|
|
19
|
+
import { BaseController } from "@metamask/base-controller";
|
|
20
|
+
import {
|
|
21
|
+
toHex,
|
|
22
|
+
InfuraNetworkType,
|
|
23
|
+
NetworkType,
|
|
24
|
+
isSafeChainId,
|
|
25
|
+
isInfuraNetworkType,
|
|
26
|
+
ChainId,
|
|
27
|
+
NetworksTicker,
|
|
28
|
+
NetworkNickname
|
|
29
|
+
} from "@metamask/controller-utils";
|
|
30
|
+
import EthQuery from "@metamask/eth-query";
|
|
31
|
+
import { errorCodes } from "@metamask/rpc-errors";
|
|
32
|
+
import { createEventEmitterProxy } from "@metamask/swappable-obj-proxy";
|
|
33
|
+
import {
|
|
34
|
+
hexToBigInt,
|
|
35
|
+
isStrictHexString,
|
|
36
|
+
hasProperty,
|
|
37
|
+
isPlainObject
|
|
38
|
+
} from "@metamask/utils";
|
|
39
|
+
import { strict as assert } from "assert";
|
|
40
|
+
import * as URI from "uri-js";
|
|
41
|
+
import { inspect } from "util";
|
|
42
|
+
import { v4 as uuidV4 } from "uuid";
|
|
43
|
+
var debugLog = createModuleLogger(projectLogger, "NetworkController");
|
|
44
|
+
var INFURA_URL_REGEX = /^https:\/\/(?<networkName>[^.]+)\.infura\.io\/v\d+\/(?<apiKey>.+)$/u;
|
|
45
|
+
var RpcEndpointType = /* @__PURE__ */ ((RpcEndpointType2) => {
|
|
46
|
+
RpcEndpointType2["Custom"] = "custom";
|
|
47
|
+
RpcEndpointType2["Infura"] = "infura";
|
|
48
|
+
return RpcEndpointType2;
|
|
49
|
+
})(RpcEndpointType || {});
|
|
50
|
+
function knownKeysOf(object) {
|
|
51
|
+
return Object.keys(object);
|
|
52
|
+
}
|
|
53
|
+
function isErrorWithCode(error) {
|
|
54
|
+
return typeof error === "object" && error !== null && "code" in error;
|
|
55
|
+
}
|
|
56
|
+
var controllerName = "NetworkController";
|
|
57
|
+
function getDefaultNetworkConfigurationsByChainId() {
|
|
58
|
+
return Object.values(InfuraNetworkType).reduce((obj, infuraNetworkType) => {
|
|
59
|
+
const chainId = ChainId[infuraNetworkType];
|
|
60
|
+
const rpcEndpointUrl = (
|
|
61
|
+
// False negative - this is a string.
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
63
|
+
`https://${infuraNetworkType}.infura.io/v3/{infuraProjectId}`
|
|
64
|
+
);
|
|
65
|
+
const networkConfiguration = {
|
|
66
|
+
blockExplorerUrls: [],
|
|
67
|
+
chainId,
|
|
68
|
+
defaultRpcEndpointIndex: 0,
|
|
69
|
+
name: NetworkNickname[infuraNetworkType],
|
|
70
|
+
nativeCurrency: NetworksTicker[infuraNetworkType],
|
|
71
|
+
rpcEndpoints: [
|
|
72
|
+
{
|
|
73
|
+
networkClientId: infuraNetworkType,
|
|
74
|
+
type: "infura" /* Infura */,
|
|
75
|
+
url: rpcEndpointUrl
|
|
76
|
+
}
|
|
77
|
+
]
|
|
78
|
+
};
|
|
79
|
+
return { ...obj, [chainId]: networkConfiguration };
|
|
80
|
+
}, {});
|
|
81
|
+
}
|
|
82
|
+
function getDefaultNetworkControllerState() {
|
|
83
|
+
const networksMetadata = {};
|
|
84
|
+
const networkConfigurationsByChainId = getDefaultNetworkConfigurationsByChainId();
|
|
85
|
+
return {
|
|
86
|
+
selectedNetworkClientId: InfuraNetworkType.mainnet,
|
|
87
|
+
networksMetadata,
|
|
88
|
+
networkConfigurationsByChainId
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function isValidUrl(url) {
|
|
92
|
+
const uri = URI.parse(url);
|
|
93
|
+
return uri.error === void 0 && (uri.scheme === "http" || uri.scheme === "https");
|
|
94
|
+
}
|
|
95
|
+
function deriveInfuraNetworkNameFromRpcEndpointUrl(rpcEndpointUrl) {
|
|
96
|
+
const match = INFURA_URL_REGEX.exec(rpcEndpointUrl);
|
|
97
|
+
if (match?.groups) {
|
|
98
|
+
if (isInfuraNetworkType(match.groups.networkName)) {
|
|
99
|
+
return match.groups.networkName;
|
|
100
|
+
}
|
|
101
|
+
throw new Error(`Unknown Infura network '${match.groups.networkName}'`);
|
|
102
|
+
}
|
|
103
|
+
throw new Error("Could not derive Infura network from RPC endpoint URL");
|
|
104
|
+
}
|
|
105
|
+
function validateNetworkControllerState(state) {
|
|
106
|
+
const networkConfigurationEntries = Object.entries(
|
|
107
|
+
state.networkConfigurationsByChainId
|
|
108
|
+
);
|
|
109
|
+
const networkClientIds = Object.values(
|
|
110
|
+
state.networkConfigurationsByChainId
|
|
111
|
+
).flatMap(
|
|
112
|
+
(networkConfiguration) => networkConfiguration.rpcEndpoints.map(
|
|
113
|
+
(rpcEndpoint) => rpcEndpoint.networkClientId
|
|
114
|
+
)
|
|
115
|
+
);
|
|
116
|
+
if (networkConfigurationEntries.length === 0) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
"NetworkController state is invalid: `networkConfigurationsByChainId` cannot be empty"
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
for (const [chainId, networkConfiguration] of networkConfigurationEntries) {
|
|
122
|
+
if (chainId !== networkConfiguration.chainId) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`NetworkController state has invalid \`networkConfigurationsByChainId\`: Network configuration '${networkConfiguration.name}' is filed under '${chainId}' which does not match its \`chainId\` of '${networkConfiguration.chainId}'`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const isInvalidDefaultBlockExplorerUrlIndex = networkConfiguration.blockExplorerUrls.length > 0 ? networkConfiguration.defaultBlockExplorerUrlIndex === void 0 || networkConfiguration.blockExplorerUrls[networkConfiguration.defaultBlockExplorerUrlIndex] === void 0 : networkConfiguration.defaultBlockExplorerUrlIndex !== void 0;
|
|
128
|
+
if (isInvalidDefaultBlockExplorerUrlIndex) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`NetworkController state has invalid \`networkConfigurationsByChainId\`: Network configuration '${networkConfiguration.name}' has a \`defaultBlockExplorerUrlIndex\` that does not refer to an entry in \`blockExplorerUrls\``
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (networkConfiguration.rpcEndpoints[networkConfiguration.defaultRpcEndpointIndex] === void 0) {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`NetworkController state has invalid \`networkConfigurationsByChainId\`: Network configuration '${networkConfiguration.name}' has a \`defaultRpcEndpointIndex\` that does not refer to an entry in \`rpcEndpoints\``
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if ([...new Set(networkClientIds)].length < networkClientIds.length) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
"NetworkController state has invalid `networkConfigurationsByChainId`: Every RPC endpoint across all network configurations must have a unique `networkClientId`"
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (!networkClientIds.includes(state.selectedNetworkClientId)) {
|
|
145
|
+
throw new Error(
|
|
146
|
+
`NetworkController state is invalid: \`selectedNetworkClientId\` ${inspect(
|
|
147
|
+
state.selectedNetworkClientId
|
|
148
|
+
)} does not refer to an RPC endpoint within a network configuration`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function buildNetworkConfigurationsByNetworkClientId(networkConfigurationsByChainId) {
|
|
153
|
+
return new Map(
|
|
154
|
+
Object.values(networkConfigurationsByChainId).flatMap(
|
|
155
|
+
(networkConfiguration) => {
|
|
156
|
+
return networkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
|
|
157
|
+
return [rpcEndpoint.networkClientId, networkConfiguration];
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
)
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
var _ethQuery, _infuraProjectId, _previouslySelectedNetworkClientId, _providerProxy, _blockTrackerProxy, _autoManagedNetworkClientRegistry, _autoManagedNetworkClient, _log, _networkConfigurationsByNetworkClientId, _refreshNetwork, refreshNetwork_fn, _getLatestBlock, getLatestBlock_fn, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn, _validateNetworkFields, validateNetworkFields_fn, _determineNetworkConfigurationToPersist, determineNetworkConfigurationToPersist_fn, _registerNetworkClientsAsNeeded, registerNetworkClientsAsNeeded_fn, _unregisterNetworkClientsAsNeeded, unregisterNetworkClientsAsNeeded_fn, _updateNetworkConfigurations, updateNetworkConfigurations_fn, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn, _createAutoManagedNetworkClientRegistry, createAutoManagedNetworkClientRegistry_fn, _applyNetworkSelection, applyNetworkSelection_fn;
|
|
164
|
+
var NetworkController = class extends BaseController {
|
|
165
|
+
constructor({
|
|
166
|
+
messenger,
|
|
167
|
+
state,
|
|
168
|
+
infuraProjectId,
|
|
169
|
+
log
|
|
170
|
+
}) {
|
|
171
|
+
const initialState = { ...getDefaultNetworkControllerState(), ...state };
|
|
172
|
+
validateNetworkControllerState(initialState);
|
|
173
|
+
if (!infuraProjectId || typeof infuraProjectId !== "string") {
|
|
174
|
+
throw new Error("Invalid Infura project ID");
|
|
175
|
+
}
|
|
176
|
+
super({
|
|
177
|
+
name: controllerName,
|
|
178
|
+
metadata: {
|
|
179
|
+
selectedNetworkClientId: {
|
|
180
|
+
persist: true,
|
|
181
|
+
anonymous: false
|
|
182
|
+
},
|
|
183
|
+
networksMetadata: {
|
|
184
|
+
persist: true,
|
|
185
|
+
anonymous: false
|
|
186
|
+
},
|
|
187
|
+
networkConfigurationsByChainId: {
|
|
188
|
+
persist: true,
|
|
189
|
+
anonymous: false
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
messenger,
|
|
193
|
+
state: initialState
|
|
194
|
+
});
|
|
195
|
+
/**
|
|
196
|
+
* Executes a series of steps to switch the network:
|
|
197
|
+
*
|
|
198
|
+
* 1. Notifies subscribers via the messenger that the network is about to be
|
|
199
|
+
* switched (and, really, that the global provider and block tracker proxies
|
|
200
|
+
* will be re-pointed to a new network).
|
|
201
|
+
* 2. Looks up a known and preinitialized network client matching the given
|
|
202
|
+
* ID and uses it to re-point the aforementioned provider and block tracker
|
|
203
|
+
* proxies.
|
|
204
|
+
* 3. Notifies subscribers via the messenger that the network has switched.
|
|
205
|
+
* 4. Captures metadata for the newly switched network in state.
|
|
206
|
+
*
|
|
207
|
+
* @param networkClientId - The ID of a network client that requests will be
|
|
208
|
+
* routed through (either the name of an Infura network or the ID of a custom
|
|
209
|
+
* network configuration).
|
|
210
|
+
* @param options - Options for this method.
|
|
211
|
+
* @param options.updateState - Allows for updating state.
|
|
212
|
+
*/
|
|
213
|
+
__privateAdd(this, _refreshNetwork);
|
|
214
|
+
/**
|
|
215
|
+
* Fetches the latest block for the network.
|
|
216
|
+
*
|
|
217
|
+
* @param networkClientId - The networkClientId to fetch the correct provider against which to check the latest block. Defaults to the selectedNetworkClientId.
|
|
218
|
+
* @returns A promise that either resolves to the block header or null if
|
|
219
|
+
* there is no latest block, or rejects with an error.
|
|
220
|
+
*/
|
|
221
|
+
__privateAdd(this, _getLatestBlock);
|
|
222
|
+
/**
|
|
223
|
+
* Retrieves and checks the latest block from the currently selected
|
|
224
|
+
* network; if the block has a `baseFeePerGas` property, then we know
|
|
225
|
+
* that the network supports EIP-1559; otherwise it doesn't.
|
|
226
|
+
*
|
|
227
|
+
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility
|
|
228
|
+
* @returns A promise that resolves to `true` if the network supports EIP-1559,
|
|
229
|
+
* `false` otherwise, or `undefined` if unable to retrieve the last block.
|
|
230
|
+
*/
|
|
231
|
+
__privateAdd(this, _determineEIP1559Compatibility);
|
|
232
|
+
/**
|
|
233
|
+
* Ensure that the given fields which will be used to either add or update a
|
|
234
|
+
* network are valid.
|
|
235
|
+
*
|
|
236
|
+
* @param args - The arguments.
|
|
237
|
+
*/
|
|
238
|
+
__privateAdd(this, _validateNetworkFields);
|
|
239
|
+
/**
|
|
240
|
+
* Constructs a network configuration that will be persisted to state when
|
|
241
|
+
* adding or updating a network.
|
|
242
|
+
*
|
|
243
|
+
* @param args - The arguments to this function.
|
|
244
|
+
* @param args.networkFields - The fields used to add or update a network.
|
|
245
|
+
* @param args.networkClientOperations - Operations which were calculated for
|
|
246
|
+
* updating the network client registry but which also map back to RPC
|
|
247
|
+
* endpoints (and so can be used to save those RPC endpoints).
|
|
248
|
+
* @returns The network configuration to persist.
|
|
249
|
+
*/
|
|
250
|
+
__privateAdd(this, _determineNetworkConfigurationToPersist);
|
|
251
|
+
/**
|
|
252
|
+
* Creates and registers network clients using the given operations calculated
|
|
253
|
+
* as a part of adding or updating a network.
|
|
254
|
+
*
|
|
255
|
+
* @param args - The arguments to this function.
|
|
256
|
+
* @param args.networkFields - The fields used to add or update a network.
|
|
257
|
+
* @param args.networkClientOperations - Dictate which network clients need to
|
|
258
|
+
* be created.
|
|
259
|
+
* @param args.autoManagedNetworkClientRegistry - The network client registry
|
|
260
|
+
* to update.
|
|
261
|
+
*/
|
|
262
|
+
__privateAdd(this, _registerNetworkClientsAsNeeded);
|
|
263
|
+
/**
|
|
264
|
+
* Destroys and removes network clients using the given operations calculated
|
|
265
|
+
* as a part of updating or removing a network.
|
|
266
|
+
*
|
|
267
|
+
* @param args - The arguments to this function.
|
|
268
|
+
* @param args.networkClientOperations - Dictate which network clients to
|
|
269
|
+
* remove.
|
|
270
|
+
* @param args.autoManagedNetworkClientRegistry - The network client registry
|
|
271
|
+
* to update.
|
|
272
|
+
*/
|
|
273
|
+
__privateAdd(this, _unregisterNetworkClientsAsNeeded);
|
|
274
|
+
/**
|
|
275
|
+
* Updates `networkConfigurationsByChainId` in state depending on whether a
|
|
276
|
+
* network is being added, updated, or removed.
|
|
277
|
+
*
|
|
278
|
+
* - The existing network configuration will be removed when a network is
|
|
279
|
+
* being filed under a different chain or removed.
|
|
280
|
+
* - A network configuration will be stored when a network is being added or
|
|
281
|
+
* when a network is being updated.
|
|
282
|
+
*
|
|
283
|
+
* @param args - The arguments to this function.
|
|
284
|
+
*/
|
|
285
|
+
__privateAdd(this, _updateNetworkConfigurations);
|
|
286
|
+
/**
|
|
287
|
+
* Before accessing or switching the network, the registry of network clients
|
|
288
|
+
* needs to be populated. Otherwise, `#applyNetworkSelection` and
|
|
289
|
+
* `getNetworkClientRegistry` will throw an error. This method checks to see if the
|
|
290
|
+
* population step has happened yet, and if not, makes it happen.
|
|
291
|
+
*
|
|
292
|
+
* @returns The populated network client registry.
|
|
293
|
+
*/
|
|
294
|
+
__privateAdd(this, _ensureAutoManagedNetworkClientRegistryPopulated);
|
|
295
|
+
/**
|
|
296
|
+
* Constructs the registry of network clients based on the set of default
|
|
297
|
+
* and custom networks in state.
|
|
298
|
+
*
|
|
299
|
+
* @returns The network clients keyed by ID.
|
|
300
|
+
*/
|
|
301
|
+
__privateAdd(this, _createAutoManagedNetworkClientRegistry);
|
|
302
|
+
/**
|
|
303
|
+
* Updates the global provider and block tracker proxies (accessible via
|
|
304
|
+
* {@link getSelectedNetworkClient}) to point to the same ones within the
|
|
305
|
+
* given network client, thereby magically switching any consumers using these
|
|
306
|
+
* proxies to use the new network.
|
|
307
|
+
*
|
|
308
|
+
* Also refreshes the EthQuery instance accessible via the `getEthQuery`
|
|
309
|
+
* action to wrap the provider from the new network client. Note that this is
|
|
310
|
+
* not a proxy, so consumers will need to call `getEthQuery` again after the
|
|
311
|
+
* network switch.
|
|
312
|
+
*
|
|
313
|
+
* @param networkClientId - The ID of a network client that requests will be
|
|
314
|
+
* routed through (either the name of an Infura network or the ID of a custom
|
|
315
|
+
* network configuration).
|
|
316
|
+
* @param options - Options for this method.
|
|
317
|
+
* @param options.updateState - Allows for updating state.
|
|
318
|
+
* @throws if no network client could be found matching the given ID.
|
|
319
|
+
*/
|
|
320
|
+
__privateAdd(this, _applyNetworkSelection);
|
|
321
|
+
__privateAdd(this, _ethQuery, void 0);
|
|
322
|
+
__privateAdd(this, _infuraProjectId, void 0);
|
|
323
|
+
__privateAdd(this, _previouslySelectedNetworkClientId, void 0);
|
|
324
|
+
__privateAdd(this, _providerProxy, void 0);
|
|
325
|
+
__privateAdd(this, _blockTrackerProxy, void 0);
|
|
326
|
+
__privateAdd(this, _autoManagedNetworkClientRegistry, void 0);
|
|
327
|
+
__privateAdd(this, _autoManagedNetworkClient, void 0);
|
|
328
|
+
__privateAdd(this, _log, void 0);
|
|
329
|
+
__privateAdd(this, _networkConfigurationsByNetworkClientId, void 0);
|
|
330
|
+
__privateSet(this, _infuraProjectId, infuraProjectId);
|
|
331
|
+
__privateSet(this, _log, log);
|
|
332
|
+
__privateSet(this, _previouslySelectedNetworkClientId, this.state.selectedNetworkClientId);
|
|
333
|
+
__privateSet(this, _networkConfigurationsByNetworkClientId, buildNetworkConfigurationsByNetworkClientId(
|
|
334
|
+
this.state.networkConfigurationsByChainId
|
|
335
|
+
));
|
|
336
|
+
this.messagingSystem.registerActionHandler(
|
|
337
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
338
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
339
|
+
`${this.name}:getEthQuery`,
|
|
340
|
+
() => {
|
|
341
|
+
return __privateGet(this, _ethQuery);
|
|
342
|
+
}
|
|
343
|
+
);
|
|
344
|
+
this.messagingSystem.registerActionHandler(
|
|
345
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
346
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
347
|
+
`${this.name}:getNetworkClientById`,
|
|
348
|
+
this.getNetworkClientById.bind(this)
|
|
349
|
+
);
|
|
350
|
+
this.messagingSystem.registerActionHandler(
|
|
351
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
352
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
353
|
+
`${this.name}:getEIP1559Compatibility`,
|
|
354
|
+
this.getEIP1559Compatibility.bind(this)
|
|
355
|
+
);
|
|
356
|
+
this.messagingSystem.registerActionHandler(
|
|
357
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
358
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
359
|
+
`${this.name}:setActiveNetwork`,
|
|
360
|
+
this.setActiveNetwork.bind(this)
|
|
361
|
+
);
|
|
362
|
+
this.messagingSystem.registerActionHandler(
|
|
363
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
364
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
365
|
+
`${this.name}:setProviderType`,
|
|
366
|
+
this.setProviderType.bind(this)
|
|
367
|
+
);
|
|
368
|
+
this.messagingSystem.registerActionHandler(
|
|
369
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
370
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
371
|
+
`${this.name}:findNetworkClientIdByChainId`,
|
|
372
|
+
this.findNetworkClientIdByChainId.bind(this)
|
|
373
|
+
);
|
|
374
|
+
this.messagingSystem.registerActionHandler(
|
|
375
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
376
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
377
|
+
`${this.name}:getNetworkConfigurationByChainId`,
|
|
378
|
+
this.getNetworkConfigurationByChainId.bind(this)
|
|
379
|
+
);
|
|
380
|
+
this.messagingSystem.registerActionHandler(
|
|
381
|
+
// ESLint is mistaken here; `name` is a string.
|
|
382
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
383
|
+
`${this.name}:getNetworkConfigurationByNetworkClientId`,
|
|
384
|
+
this.getNetworkConfigurationByNetworkClientId.bind(this)
|
|
385
|
+
);
|
|
386
|
+
this.messagingSystem.registerActionHandler(
|
|
387
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
388
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
389
|
+
`${this.name}:getSelectedNetworkClient`,
|
|
390
|
+
this.getSelectedNetworkClient.bind(this)
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Accesses the provider and block tracker for the currently selected network.
|
|
395
|
+
* @returns The proxy and block tracker proxies.
|
|
396
|
+
* @deprecated This method has been replaced by `getSelectedNetworkClient` (which has a more easily used return type) and will be removed in a future release.
|
|
397
|
+
*/
|
|
398
|
+
getProviderAndBlockTracker() {
|
|
399
|
+
return {
|
|
400
|
+
provider: __privateGet(this, _providerProxy),
|
|
401
|
+
blockTracker: __privateGet(this, _blockTrackerProxy)
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Accesses the provider and block tracker for the currently selected network.
|
|
406
|
+
*
|
|
407
|
+
* @returns an object with the provider and block tracker proxies for the currently selected network.
|
|
408
|
+
*/
|
|
409
|
+
getSelectedNetworkClient() {
|
|
410
|
+
if (__privateGet(this, _providerProxy) && __privateGet(this, _blockTrackerProxy)) {
|
|
411
|
+
return {
|
|
412
|
+
provider: __privateGet(this, _providerProxy),
|
|
413
|
+
blockTracker: __privateGet(this, _blockTrackerProxy)
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
return void 0;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Internally, the Infura and custom network clients are categorized by type
|
|
420
|
+
* so that when accessing either kind of network client, TypeScript knows
|
|
421
|
+
* which type to assign to the network client. For some cases it's more useful
|
|
422
|
+
* to be able to access network clients by ID instead of by type and then ID,
|
|
423
|
+
* so this function makes that possible.
|
|
424
|
+
*
|
|
425
|
+
* @returns The network clients registered so far, keyed by ID.
|
|
426
|
+
*/
|
|
427
|
+
getNetworkClientRegistry() {
|
|
428
|
+
const autoManagedNetworkClientRegistry = __privateMethod(this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
429
|
+
return Object.assign(
|
|
430
|
+
{},
|
|
431
|
+
autoManagedNetworkClientRegistry["infura" /* Infura */],
|
|
432
|
+
autoManagedNetworkClientRegistry["custom" /* Custom */]
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
getNetworkClientById(networkClientId) {
|
|
436
|
+
if (!networkClientId) {
|
|
437
|
+
throw new Error("No network client ID was provided.");
|
|
438
|
+
}
|
|
439
|
+
const autoManagedNetworkClientRegistry = __privateMethod(this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
440
|
+
if (isInfuraNetworkType(networkClientId)) {
|
|
441
|
+
const infuraNetworkClient = autoManagedNetworkClientRegistry["infura" /* Infura */][networkClientId];
|
|
442
|
+
if (!infuraNetworkClient) {
|
|
443
|
+
throw new Error(
|
|
444
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
445
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
446
|
+
`No Infura network client was found with the ID "${networkClientId}".`
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
return infuraNetworkClient;
|
|
450
|
+
}
|
|
451
|
+
const customNetworkClient = autoManagedNetworkClientRegistry["custom" /* Custom */][networkClientId];
|
|
452
|
+
if (!customNetworkClient) {
|
|
453
|
+
throw new Error(
|
|
454
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
455
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
456
|
+
`No custom network client was found with the ID "${networkClientId}".`
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
return customNetworkClient;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Ensures that network clients for Infura and custom RPC endpoints have been
|
|
463
|
+
* created. Then, consulting state, initializes and establishes the currently
|
|
464
|
+
* selected network client.
|
|
465
|
+
*/
|
|
466
|
+
async initializeProvider() {
|
|
467
|
+
__privateMethod(this, _applyNetworkSelection, applyNetworkSelection_fn).call(this, this.state.selectedNetworkClientId);
|
|
468
|
+
await this.lookupNetwork();
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Refreshes the network meta with EIP-1559 support and the network status
|
|
472
|
+
* based on the given network client ID.
|
|
473
|
+
*
|
|
474
|
+
* @param networkClientId - The ID of the network client to update.
|
|
475
|
+
*/
|
|
476
|
+
async lookupNetworkByClientId(networkClientId) {
|
|
477
|
+
const isInfura = isInfuraNetworkType(networkClientId);
|
|
478
|
+
let updatedNetworkStatus;
|
|
479
|
+
let updatedIsEIP1559Compatible;
|
|
480
|
+
try {
|
|
481
|
+
updatedIsEIP1559Compatible = await __privateMethod(this, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn).call(this, networkClientId);
|
|
482
|
+
updatedNetworkStatus = "available" /* Available */;
|
|
483
|
+
} catch (error) {
|
|
484
|
+
debugLog("NetworkController: lookupNetworkByClientId: ", error);
|
|
485
|
+
if (isErrorWithCode(error)) {
|
|
486
|
+
let responseBody;
|
|
487
|
+
if (isInfura && hasProperty(error, "message") && typeof error.message === "string") {
|
|
488
|
+
try {
|
|
489
|
+
responseBody = JSON.parse(error.message);
|
|
490
|
+
} catch {
|
|
491
|
+
__privateGet(this, _log)?.warn(
|
|
492
|
+
"NetworkController: lookupNetworkByClientId: json parse error: ",
|
|
493
|
+
error
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
if (isPlainObject(responseBody) && responseBody.error === INFURA_BLOCKED_KEY) {
|
|
498
|
+
updatedNetworkStatus = "blocked" /* Blocked */;
|
|
499
|
+
} else if (error.code === errorCodes.rpc.internal) {
|
|
500
|
+
updatedNetworkStatus = "unknown" /* Unknown */;
|
|
501
|
+
__privateGet(this, _log)?.warn(
|
|
502
|
+
"NetworkController: lookupNetworkByClientId: rpc internal error: ",
|
|
503
|
+
error
|
|
504
|
+
);
|
|
505
|
+
} else {
|
|
506
|
+
updatedNetworkStatus = "unavailable" /* Unavailable */;
|
|
507
|
+
__privateGet(this, _log)?.warn(
|
|
508
|
+
"NetworkController: lookupNetworkByClientId: ",
|
|
509
|
+
error
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
} else if (typeof Error !== "undefined" && hasProperty(error, "message") && typeof error.message === "string" && error.message.includes(
|
|
513
|
+
"No custom network client was found with the ID"
|
|
514
|
+
)) {
|
|
515
|
+
throw error;
|
|
516
|
+
} else {
|
|
517
|
+
debugLog(
|
|
518
|
+
"NetworkController - could not determine network status",
|
|
519
|
+
error
|
|
520
|
+
);
|
|
521
|
+
updatedNetworkStatus = "unknown" /* Unknown */;
|
|
522
|
+
__privateGet(this, _log)?.warn("NetworkController: lookupNetworkByClientId: ", error);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
this.update((state) => {
|
|
526
|
+
if (state.networksMetadata[networkClientId] === void 0) {
|
|
527
|
+
state.networksMetadata[networkClientId] = {
|
|
528
|
+
status: "unknown" /* Unknown */,
|
|
529
|
+
EIPS: {}
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
const meta = state.networksMetadata[networkClientId];
|
|
533
|
+
meta.status = updatedNetworkStatus;
|
|
534
|
+
if (updatedIsEIP1559Compatible === void 0) {
|
|
535
|
+
delete meta.EIPS[1559];
|
|
536
|
+
} else {
|
|
537
|
+
meta.EIPS[1559] = updatedIsEIP1559Compatible;
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Persists the following metadata about the given or selected network to
|
|
543
|
+
* state:
|
|
544
|
+
*
|
|
545
|
+
* - The status of the network, namely, whether it is available, geo-blocked
|
|
546
|
+
* (Infura only), or unavailable, or whether the status is unknown
|
|
547
|
+
* - Whether the network supports EIP-1559, or whether it is unknown
|
|
548
|
+
*
|
|
549
|
+
* Note that it is possible for the network to be switched while this data is
|
|
550
|
+
* being collected. If that is the case, no metadata for the (now previously)
|
|
551
|
+
* selected network will be updated.
|
|
552
|
+
*
|
|
553
|
+
* @param networkClientId - The ID of the network client to update.
|
|
554
|
+
* If no ID is provided, uses the currently selected network.
|
|
555
|
+
*/
|
|
556
|
+
async lookupNetwork(networkClientId) {
|
|
557
|
+
if (networkClientId) {
|
|
558
|
+
await this.lookupNetworkByClientId(networkClientId);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
if (!__privateGet(this, _ethQuery)) {
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
const isInfura = __privateGet(this, _autoManagedNetworkClient)?.configuration.type === "infura" /* Infura */;
|
|
565
|
+
let networkChanged = false;
|
|
566
|
+
const listener = () => {
|
|
567
|
+
networkChanged = true;
|
|
568
|
+
this.messagingSystem.unsubscribe(
|
|
569
|
+
"NetworkController:networkDidChange",
|
|
570
|
+
listener
|
|
571
|
+
);
|
|
572
|
+
};
|
|
573
|
+
this.messagingSystem.subscribe(
|
|
574
|
+
"NetworkController:networkDidChange",
|
|
575
|
+
listener
|
|
576
|
+
);
|
|
577
|
+
let updatedNetworkStatus;
|
|
578
|
+
let updatedIsEIP1559Compatible;
|
|
579
|
+
try {
|
|
580
|
+
const isEIP1559Compatible = await __privateMethod(this, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn).call(this, this.state.selectedNetworkClientId);
|
|
581
|
+
updatedNetworkStatus = "available" /* Available */;
|
|
582
|
+
updatedIsEIP1559Compatible = isEIP1559Compatible;
|
|
583
|
+
} catch (error) {
|
|
584
|
+
if (isErrorWithCode(error)) {
|
|
585
|
+
let responseBody;
|
|
586
|
+
if (isInfura && hasProperty(error, "message") && typeof error.message === "string") {
|
|
587
|
+
try {
|
|
588
|
+
responseBody = JSON.parse(error.message);
|
|
589
|
+
} catch (parseError) {
|
|
590
|
+
__privateGet(this, _log)?.warn(
|
|
591
|
+
"NetworkController: lookupNetwork: json parse error",
|
|
592
|
+
parseError
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
if (isPlainObject(responseBody) && responseBody.error === INFURA_BLOCKED_KEY) {
|
|
597
|
+
updatedNetworkStatus = "blocked" /* Blocked */;
|
|
598
|
+
} else if (error.code === errorCodes.rpc.internal) {
|
|
599
|
+
updatedNetworkStatus = "unknown" /* Unknown */;
|
|
600
|
+
__privateGet(this, _log)?.warn(
|
|
601
|
+
"NetworkController: lookupNetwork: rpc internal error",
|
|
602
|
+
error
|
|
603
|
+
);
|
|
604
|
+
} else {
|
|
605
|
+
updatedNetworkStatus = "unavailable" /* Unavailable */;
|
|
606
|
+
__privateGet(this, _log)?.warn("NetworkController: lookupNetwork: ", error);
|
|
607
|
+
}
|
|
608
|
+
} else {
|
|
609
|
+
debugLog(
|
|
610
|
+
"NetworkController - could not determine network status",
|
|
611
|
+
error
|
|
612
|
+
);
|
|
613
|
+
updatedNetworkStatus = "unknown" /* Unknown */;
|
|
614
|
+
__privateGet(this, _log)?.warn("NetworkController: lookupNetwork: ", error);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
if (networkChanged) {
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
this.messagingSystem.unsubscribe(
|
|
621
|
+
"NetworkController:networkDidChange",
|
|
622
|
+
listener
|
|
623
|
+
);
|
|
624
|
+
this.update((state) => {
|
|
625
|
+
const meta = state.networksMetadata[state.selectedNetworkClientId];
|
|
626
|
+
meta.status = updatedNetworkStatus;
|
|
627
|
+
if (updatedIsEIP1559Compatible === void 0) {
|
|
628
|
+
delete meta.EIPS[1559];
|
|
629
|
+
} else {
|
|
630
|
+
meta.EIPS[1559] = updatedIsEIP1559Compatible;
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
if (isInfura) {
|
|
634
|
+
if (updatedNetworkStatus === "available" /* Available */) {
|
|
635
|
+
this.messagingSystem.publish("NetworkController:infuraIsUnblocked");
|
|
636
|
+
} else if (updatedNetworkStatus === "blocked" /* Blocked */) {
|
|
637
|
+
this.messagingSystem.publish("NetworkController:infuraIsBlocked");
|
|
638
|
+
}
|
|
639
|
+
} else {
|
|
640
|
+
this.messagingSystem.publish("NetworkController:infuraIsUnblocked");
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Convenience method to update provider network type settings.
|
|
645
|
+
*
|
|
646
|
+
* @param type - Human readable network name.
|
|
647
|
+
* @deprecated This has been replaced by `setActiveNetwork`, and will be
|
|
648
|
+
* removed in a future release
|
|
649
|
+
*/
|
|
650
|
+
async setProviderType(type) {
|
|
651
|
+
assert.notStrictEqual(
|
|
652
|
+
type,
|
|
653
|
+
NetworkType.rpc,
|
|
654
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
655
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
656
|
+
`NetworkController - cannot call "setProviderType" with type "${NetworkType.rpc}". Use "setActiveNetwork"`
|
|
657
|
+
);
|
|
658
|
+
assert.ok(
|
|
659
|
+
isInfuraNetworkType(type),
|
|
660
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
661
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
662
|
+
`Unknown Infura provider type "${type}".`
|
|
663
|
+
);
|
|
664
|
+
await this.setActiveNetwork(type);
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Changes the selected network.
|
|
668
|
+
*
|
|
669
|
+
* @param networkClientId - The ID of a network client that will be used to
|
|
670
|
+
* make requests.
|
|
671
|
+
* @param options - Options for this method.
|
|
672
|
+
* @param options.updateState - Allows for updating state.
|
|
673
|
+
* @throws if no network client is associated with the given
|
|
674
|
+
* network client ID.
|
|
675
|
+
*/
|
|
676
|
+
async setActiveNetwork(networkClientId, options = {}) {
|
|
677
|
+
__privateSet(this, _previouslySelectedNetworkClientId, this.state.selectedNetworkClientId);
|
|
678
|
+
await __privateMethod(this, _refreshNetwork, refreshNetwork_fn).call(this, networkClientId, options);
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Determines whether the network supports EIP-1559 by checking whether the
|
|
682
|
+
* latest block has a `baseFeePerGas` property, then updates state
|
|
683
|
+
* appropriately.
|
|
684
|
+
*
|
|
685
|
+
* @param networkClientId - The networkClientId to fetch the correct provider against which to check 1559 compatibility.
|
|
686
|
+
* @returns A promise that resolves to true if the network supports EIP-1559
|
|
687
|
+
* , false otherwise, or `undefined` if unable to determine the compatibility.
|
|
688
|
+
*/
|
|
689
|
+
async getEIP1559Compatibility(networkClientId) {
|
|
690
|
+
if (networkClientId) {
|
|
691
|
+
return this.get1559CompatibilityWithNetworkClientId(networkClientId);
|
|
692
|
+
}
|
|
693
|
+
if (!__privateGet(this, _ethQuery)) {
|
|
694
|
+
return false;
|
|
695
|
+
}
|
|
696
|
+
const { EIPS } = this.state.networksMetadata[this.state.selectedNetworkClientId];
|
|
697
|
+
if (EIPS[1559] !== void 0) {
|
|
698
|
+
return EIPS[1559];
|
|
699
|
+
}
|
|
700
|
+
const isEIP1559Compatible = await __privateMethod(this, _determineEIP1559Compatibility, determineEIP1559Compatibility_fn).call(this, this.state.selectedNetworkClientId);
|
|
701
|
+
this.update((state) => {
|
|
702
|
+
if (isEIP1559Compatible !== void 0) {
|
|
703
|
+
state.networksMetadata[state.selectedNetworkClientId].EIPS[1559] = isEIP1559Compatible;
|
|
704
|
+
}
|
|
705
|
+
});
|
|
706
|
+
return isEIP1559Compatible;
|
|
707
|
+
}
|
|
708
|
+
async get1559CompatibilityWithNetworkClientId(networkClientId) {
|
|
709
|
+
let metadata = this.state.networksMetadata[networkClientId];
|
|
710
|
+
if (metadata === void 0) {
|
|
711
|
+
await this.lookupNetwork(networkClientId);
|
|
712
|
+
metadata = this.state.networksMetadata[networkClientId];
|
|
713
|
+
}
|
|
714
|
+
const { EIPS } = metadata;
|
|
715
|
+
return EIPS[1559];
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Ensures that the provider and block tracker proxies are pointed to the
|
|
719
|
+
* currently selected network and refreshes the metadata for the
|
|
720
|
+
*/
|
|
721
|
+
async resetConnection() {
|
|
722
|
+
await __privateMethod(this, _refreshNetwork, refreshNetwork_fn).call(this, this.state.selectedNetworkClientId);
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Returns the network configuration that has been filed under the given chain
|
|
726
|
+
* ID.
|
|
727
|
+
*
|
|
728
|
+
* @param chainId - The chain ID to use as a key.
|
|
729
|
+
* @returns The network configuration if one exists, or undefined.
|
|
730
|
+
*/
|
|
731
|
+
getNetworkConfigurationByChainId(chainId) {
|
|
732
|
+
return this.state.networkConfigurationsByChainId[chainId];
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Returns the network configuration that contains an RPC endpoint with the
|
|
736
|
+
* given network client ID.
|
|
737
|
+
*
|
|
738
|
+
* @param networkClientId - The network client ID to use as a key.
|
|
739
|
+
* @returns The network configuration if one exists, or undefined.
|
|
740
|
+
*/
|
|
741
|
+
getNetworkConfigurationByNetworkClientId(networkClientId) {
|
|
742
|
+
return __privateGet(this, _networkConfigurationsByNetworkClientId).get(networkClientId);
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Creates and registers network clients for the collection of Infura and
|
|
746
|
+
* custom RPC endpoints that can be used to make requests for a particular
|
|
747
|
+
* chain, storing the given configuration object in state for later reference.
|
|
748
|
+
*
|
|
749
|
+
* @param fields - The object that describes the new network/chain and lists
|
|
750
|
+
* the RPC endpoints which front that chain.
|
|
751
|
+
* @returns The newly added network configuration.
|
|
752
|
+
* @throws if any part of `fields` would produce invalid state.
|
|
753
|
+
* @see {@link NetworkConfiguration}
|
|
754
|
+
*/
|
|
755
|
+
addNetwork(fields) {
|
|
756
|
+
const { rpcEndpoints: setOfRpcEndpointFields } = fields;
|
|
757
|
+
const autoManagedNetworkClientRegistry = __privateMethod(this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
758
|
+
__privateMethod(this, _validateNetworkFields, validateNetworkFields_fn).call(this, {
|
|
759
|
+
mode: "add",
|
|
760
|
+
errorMessagePrefix: "Could not add network",
|
|
761
|
+
networkFields: fields,
|
|
762
|
+
autoManagedNetworkClientRegistry
|
|
763
|
+
});
|
|
764
|
+
const networkClientOperations = setOfRpcEndpointFields.map(
|
|
765
|
+
(defaultOrCustomRpcEndpointFields) => {
|
|
766
|
+
const rpcEndpoint = defaultOrCustomRpcEndpointFields.type === "custom" /* Custom */ ? {
|
|
767
|
+
...defaultOrCustomRpcEndpointFields,
|
|
768
|
+
networkClientId: uuidV4()
|
|
769
|
+
} : defaultOrCustomRpcEndpointFields;
|
|
770
|
+
return {
|
|
771
|
+
type: "add",
|
|
772
|
+
rpcEndpoint
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
);
|
|
776
|
+
const newNetworkConfiguration = __privateMethod(this, _determineNetworkConfigurationToPersist, determineNetworkConfigurationToPersist_fn).call(this, {
|
|
777
|
+
networkFields: fields,
|
|
778
|
+
networkClientOperations
|
|
779
|
+
});
|
|
780
|
+
__privateMethod(this, _registerNetworkClientsAsNeeded, registerNetworkClientsAsNeeded_fn).call(this, {
|
|
781
|
+
networkFields: fields,
|
|
782
|
+
networkClientOperations,
|
|
783
|
+
autoManagedNetworkClientRegistry
|
|
784
|
+
});
|
|
785
|
+
this.update((state) => {
|
|
786
|
+
__privateMethod(this, _updateNetworkConfigurations, updateNetworkConfigurations_fn).call(this, {
|
|
787
|
+
state,
|
|
788
|
+
mode: "add",
|
|
789
|
+
networkFields: fields,
|
|
790
|
+
networkConfigurationToPersist: newNetworkConfiguration
|
|
791
|
+
});
|
|
792
|
+
});
|
|
793
|
+
this.messagingSystem.publish(
|
|
794
|
+
`${controllerName}:networkAdded`,
|
|
795
|
+
newNetworkConfiguration
|
|
796
|
+
);
|
|
797
|
+
return newNetworkConfiguration;
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Updates the configuration for a previously stored network filed under the
|
|
801
|
+
* given chain ID, creating + registering new network clients to represent RPC
|
|
802
|
+
* endpoints that have been added and destroying + unregistering existing
|
|
803
|
+
* network clients for RPC endpoints that have been removed.
|
|
804
|
+
*
|
|
805
|
+
* Note that if `chainId` is changed, then all network clients associated with
|
|
806
|
+
* that chain will be removed and re-added, even if none of the RPC endpoints
|
|
807
|
+
* have changed.
|
|
808
|
+
*
|
|
809
|
+
* @param chainId - The chain ID associated with an existing network.
|
|
810
|
+
* @param fields - The object that describes the updates to the network/chain,
|
|
811
|
+
* including the new set of RPC endpoints which should front that chain.
|
|
812
|
+
* @param options - Options to provide.
|
|
813
|
+
* @param options.replacementSelectedRpcEndpointIndex - Usually you cannot
|
|
814
|
+
* remove an RPC endpoint that is being represented by the currently selected
|
|
815
|
+
* network client. This option allows you to specify another RPC endpoint
|
|
816
|
+
* (either an existing one or a new one) that should be used to select a new
|
|
817
|
+
* network instead.
|
|
818
|
+
* @returns The updated network configuration.
|
|
819
|
+
* @throws if `chainId` does not refer to an existing network configuration,
|
|
820
|
+
* if any part of `fields` would produce invalid state, etc.
|
|
821
|
+
* @see {@link NetworkConfiguration}
|
|
822
|
+
*/
|
|
823
|
+
async updateNetwork(chainId, fields, {
|
|
824
|
+
replacementSelectedRpcEndpointIndex
|
|
825
|
+
} = {}) {
|
|
826
|
+
const existingNetworkConfiguration = this.state.networkConfigurationsByChainId[chainId];
|
|
827
|
+
if (existingNetworkConfiguration === void 0) {
|
|
828
|
+
throw new Error(
|
|
829
|
+
`Could not update network: Cannot find network configuration for chain ${inspect(
|
|
830
|
+
chainId
|
|
831
|
+
)}`
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
const existingChainId = chainId;
|
|
835
|
+
const { chainId: newChainId, rpcEndpoints: setOfNewRpcEndpointFields } = fields;
|
|
836
|
+
const autoManagedNetworkClientRegistry = __privateMethod(this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
837
|
+
__privateMethod(this, _validateNetworkFields, validateNetworkFields_fn).call(this, {
|
|
838
|
+
mode: "update",
|
|
839
|
+
errorMessagePrefix: "Could not update network",
|
|
840
|
+
networkFields: fields,
|
|
841
|
+
existingNetworkConfiguration,
|
|
842
|
+
autoManagedNetworkClientRegistry
|
|
843
|
+
});
|
|
844
|
+
const networkClientOperations = [];
|
|
845
|
+
for (const newRpcEndpointFields of setOfNewRpcEndpointFields) {
|
|
846
|
+
const existingRpcEndpointForNoop = existingNetworkConfiguration.rpcEndpoints.find((rpcEndpoint) => {
|
|
847
|
+
return rpcEndpoint.type === newRpcEndpointFields.type && rpcEndpoint.url === newRpcEndpointFields.url && (rpcEndpoint.networkClientId === newRpcEndpointFields.networkClientId || newRpcEndpointFields.networkClientId === void 0);
|
|
848
|
+
});
|
|
849
|
+
const existingRpcEndpointForReplaceWhenChainChanged = existingNetworkConfiguration.rpcEndpoints.find((rpcEndpoint) => {
|
|
850
|
+
return rpcEndpoint.type === "infura" /* Infura */ && newRpcEndpointFields.type === "infura" /* Infura */ || rpcEndpoint.type === newRpcEndpointFields.type && rpcEndpoint.networkClientId === newRpcEndpointFields.networkClientId && rpcEndpoint.url === newRpcEndpointFields.url;
|
|
851
|
+
});
|
|
852
|
+
const existingRpcEndpointForReplaceWhenChainNotChanged = existingNetworkConfiguration.rpcEndpoints.find((rpcEndpoint) => {
|
|
853
|
+
return rpcEndpoint.type === newRpcEndpointFields.type && (rpcEndpoint.url === newRpcEndpointFields.url || rpcEndpoint.networkClientId === newRpcEndpointFields.networkClientId);
|
|
854
|
+
});
|
|
855
|
+
if (newChainId !== existingChainId && existingRpcEndpointForReplaceWhenChainChanged !== void 0) {
|
|
856
|
+
const newRpcEndpoint = newRpcEndpointFields.type === "infura" /* Infura */ ? newRpcEndpointFields : { ...newRpcEndpointFields, networkClientId: uuidV4() };
|
|
857
|
+
networkClientOperations.push({
|
|
858
|
+
type: "replace",
|
|
859
|
+
oldRpcEndpoint: existingRpcEndpointForReplaceWhenChainChanged,
|
|
860
|
+
newRpcEndpoint
|
|
861
|
+
});
|
|
862
|
+
} else if (existingRpcEndpointForNoop !== void 0) {
|
|
863
|
+
let newRpcEndpoint;
|
|
864
|
+
if (existingRpcEndpointForNoop.type === "infura" /* Infura */) {
|
|
865
|
+
newRpcEndpoint = existingRpcEndpointForNoop;
|
|
866
|
+
} else {
|
|
867
|
+
newRpcEndpoint = Object.assign({}, newRpcEndpointFields, {
|
|
868
|
+
networkClientId: existingRpcEndpointForNoop.networkClientId
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
networkClientOperations.push({
|
|
872
|
+
type: "noop",
|
|
873
|
+
rpcEndpoint: newRpcEndpoint
|
|
874
|
+
});
|
|
875
|
+
} else if (existingRpcEndpointForReplaceWhenChainNotChanged !== void 0) {
|
|
876
|
+
let newRpcEndpoint;
|
|
877
|
+
if (newRpcEndpointFields.type === "infura" /* Infura */) {
|
|
878
|
+
newRpcEndpoint = newRpcEndpointFields;
|
|
879
|
+
} else {
|
|
880
|
+
newRpcEndpoint = {
|
|
881
|
+
...newRpcEndpointFields,
|
|
882
|
+
networkClientId: uuidV4()
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
networkClientOperations.push({
|
|
886
|
+
type: "replace",
|
|
887
|
+
oldRpcEndpoint: existingRpcEndpointForReplaceWhenChainNotChanged,
|
|
888
|
+
newRpcEndpoint
|
|
889
|
+
});
|
|
890
|
+
} else {
|
|
891
|
+
const newRpcEndpoint = newRpcEndpointFields.type === "infura" /* Infura */ ? newRpcEndpointFields : { ...newRpcEndpointFields, networkClientId: uuidV4() };
|
|
892
|
+
const networkClientOperation = {
|
|
893
|
+
type: "add",
|
|
894
|
+
rpcEndpoint: newRpcEndpoint
|
|
895
|
+
};
|
|
896
|
+
networkClientOperations.push(networkClientOperation);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
for (const existingRpcEndpoint of existingNetworkConfiguration.rpcEndpoints) {
|
|
900
|
+
if (!networkClientOperations.some((networkClientOperation) => {
|
|
901
|
+
const otherRpcEndpoint = networkClientOperation.type === "replace" ? networkClientOperation.oldRpcEndpoint : networkClientOperation.rpcEndpoint;
|
|
902
|
+
return otherRpcEndpoint.type === existingRpcEndpoint.type && otherRpcEndpoint.networkClientId === existingRpcEndpoint.networkClientId && otherRpcEndpoint.url === existingRpcEndpoint.url;
|
|
903
|
+
})) {
|
|
904
|
+
const networkClientOperation = {
|
|
905
|
+
type: "remove",
|
|
906
|
+
rpcEndpoint: existingRpcEndpoint
|
|
907
|
+
};
|
|
908
|
+
networkClientOperations.push(networkClientOperation);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
const updatedNetworkConfiguration = __privateMethod(this, _determineNetworkConfigurationToPersist, determineNetworkConfigurationToPersist_fn).call(this, {
|
|
912
|
+
networkFields: fields,
|
|
913
|
+
networkClientOperations
|
|
914
|
+
});
|
|
915
|
+
if (replacementSelectedRpcEndpointIndex === void 0 && networkClientOperations.some((networkClientOperation) => {
|
|
916
|
+
return networkClientOperation.type === "remove" && networkClientOperation.rpcEndpoint.networkClientId === this.state.selectedNetworkClientId;
|
|
917
|
+
}) && !networkClientOperations.some((networkClientOperation) => {
|
|
918
|
+
return networkClientOperation.type === "replace" && networkClientOperation.oldRpcEndpoint.networkClientId === this.state.selectedNetworkClientId;
|
|
919
|
+
})) {
|
|
920
|
+
throw new Error(
|
|
921
|
+
// False negative - this is a string.
|
|
922
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
923
|
+
`Could not update network: Cannot update RPC endpoints in such a way that the selected network '${this.state.selectedNetworkClientId}' would be removed without a replacement. Choose a different RPC endpoint as the selected network via the \`replacementSelectedRpcEndpointIndex\` option.`
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
__privateMethod(this, _registerNetworkClientsAsNeeded, registerNetworkClientsAsNeeded_fn).call(this, {
|
|
927
|
+
networkFields: fields,
|
|
928
|
+
networkClientOperations,
|
|
929
|
+
autoManagedNetworkClientRegistry
|
|
930
|
+
});
|
|
931
|
+
const replacementSelectedRpcEndpointWithIndex = networkClientOperations.map(
|
|
932
|
+
(networkClientOperation, index) => [networkClientOperation, index]
|
|
933
|
+
).find(([networkClientOperation, _index]) => {
|
|
934
|
+
return networkClientOperation.type === "replace" && networkClientOperation.oldRpcEndpoint.networkClientId === this.state.selectedNetworkClientId;
|
|
935
|
+
});
|
|
936
|
+
const correctedReplacementSelectedRpcEndpointIndex = replacementSelectedRpcEndpointIndex ?? replacementSelectedRpcEndpointWithIndex?.[1];
|
|
937
|
+
let rpcEndpointToSelect;
|
|
938
|
+
if (correctedReplacementSelectedRpcEndpointIndex !== void 0) {
|
|
939
|
+
rpcEndpointToSelect = updatedNetworkConfiguration.rpcEndpoints[correctedReplacementSelectedRpcEndpointIndex];
|
|
940
|
+
if (rpcEndpointToSelect === void 0) {
|
|
941
|
+
throw new Error(
|
|
942
|
+
`Could not update network: \`replacementSelectedRpcEndpointIndex\` ${correctedReplacementSelectedRpcEndpointIndex} does not refer to an entry in \`rpcEndpoints\``
|
|
943
|
+
);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
if (rpcEndpointToSelect && rpcEndpointToSelect.networkClientId !== this.state.selectedNetworkClientId) {
|
|
947
|
+
await this.setActiveNetwork(rpcEndpointToSelect.networkClientId, {
|
|
948
|
+
updateState: (state) => {
|
|
949
|
+
__privateMethod(this, _updateNetworkConfigurations, updateNetworkConfigurations_fn).call(this, {
|
|
950
|
+
state,
|
|
951
|
+
mode: "update",
|
|
952
|
+
networkFields: fields,
|
|
953
|
+
networkConfigurationToPersist: updatedNetworkConfiguration,
|
|
954
|
+
existingNetworkConfiguration
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
});
|
|
958
|
+
} else {
|
|
959
|
+
this.update((state) => {
|
|
960
|
+
__privateMethod(this, _updateNetworkConfigurations, updateNetworkConfigurations_fn).call(this, {
|
|
961
|
+
state,
|
|
962
|
+
mode: "update",
|
|
963
|
+
networkFields: fields,
|
|
964
|
+
networkConfigurationToPersist: updatedNetworkConfiguration,
|
|
965
|
+
existingNetworkConfiguration
|
|
966
|
+
});
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
__privateMethod(this, _unregisterNetworkClientsAsNeeded, unregisterNetworkClientsAsNeeded_fn).call(this, {
|
|
970
|
+
networkClientOperations,
|
|
971
|
+
autoManagedNetworkClientRegistry
|
|
972
|
+
});
|
|
973
|
+
return updatedNetworkConfiguration;
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Destroys and unregisters the network identified by the given chain ID, also
|
|
977
|
+
* removing the associated network configuration from state.
|
|
978
|
+
*
|
|
979
|
+
* @param chainId - The chain ID associated with an existing network.
|
|
980
|
+
* @throws if `chainId` does not refer to an existing network configuration,
|
|
981
|
+
* or if the currently selected network is being removed.
|
|
982
|
+
* @see {@link NetworkConfiguration}
|
|
983
|
+
*/
|
|
984
|
+
removeNetwork(chainId) {
|
|
985
|
+
const existingNetworkConfiguration = this.state.networkConfigurationsByChainId[chainId];
|
|
986
|
+
if (existingNetworkConfiguration === void 0) {
|
|
987
|
+
throw new Error(
|
|
988
|
+
`Cannot find network configuration for chain ${inspect(chainId)}`
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
if (existingNetworkConfiguration.rpcEndpoints.some(
|
|
992
|
+
(rpcEndpoint) => rpcEndpoint.networkClientId === this.state.selectedNetworkClientId
|
|
993
|
+
)) {
|
|
994
|
+
throw new Error(`Cannot remove the currently selected network`);
|
|
995
|
+
}
|
|
996
|
+
const autoManagedNetworkClientRegistry = __privateMethod(this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
997
|
+
const networkClientOperations = existingNetworkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
|
|
998
|
+
return {
|
|
999
|
+
type: "remove",
|
|
1000
|
+
rpcEndpoint
|
|
1001
|
+
};
|
|
1002
|
+
});
|
|
1003
|
+
__privateMethod(this, _unregisterNetworkClientsAsNeeded, unregisterNetworkClientsAsNeeded_fn).call(this, {
|
|
1004
|
+
networkClientOperations,
|
|
1005
|
+
autoManagedNetworkClientRegistry
|
|
1006
|
+
});
|
|
1007
|
+
this.update((state) => {
|
|
1008
|
+
__privateMethod(this, _updateNetworkConfigurations, updateNetworkConfigurations_fn).call(this, {
|
|
1009
|
+
state,
|
|
1010
|
+
mode: "remove",
|
|
1011
|
+
existingNetworkConfiguration
|
|
1012
|
+
});
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Assuming that the network has been previously switched, switches to this
|
|
1017
|
+
* new network.
|
|
1018
|
+
*
|
|
1019
|
+
* If the network has not been previously switched, this method is equivalent
|
|
1020
|
+
* to {@link resetConnection}.
|
|
1021
|
+
*/
|
|
1022
|
+
async rollbackToPreviousProvider() {
|
|
1023
|
+
await __privateMethod(this, _refreshNetwork, refreshNetwork_fn).call(this, __privateGet(this, _previouslySelectedNetworkClientId));
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Deactivates the controller, stopping any ongoing polling.
|
|
1027
|
+
*
|
|
1028
|
+
* In-progress requests will not be aborted.
|
|
1029
|
+
*/
|
|
1030
|
+
async destroy() {
|
|
1031
|
+
await __privateGet(this, _blockTrackerProxy)?.destroy();
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* Merges the given backup data into controller state.
|
|
1035
|
+
*
|
|
1036
|
+
* @param backup - The data that has been backed up.
|
|
1037
|
+
* @param backup.networkConfigurationsByChainId - Network configurations,
|
|
1038
|
+
* keyed by chain ID.
|
|
1039
|
+
*/
|
|
1040
|
+
loadBackup({
|
|
1041
|
+
networkConfigurationsByChainId
|
|
1042
|
+
}) {
|
|
1043
|
+
this.update((state) => {
|
|
1044
|
+
state.networkConfigurationsByChainId = {
|
|
1045
|
+
...state.networkConfigurationsByChainId,
|
|
1046
|
+
...networkConfigurationsByChainId
|
|
1047
|
+
};
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Searches for a network configuration ID with the given ChainID and returns it.
|
|
1052
|
+
*
|
|
1053
|
+
* @param chainId - ChainId to search for
|
|
1054
|
+
* @returns networkClientId of the network configuration with the given chainId
|
|
1055
|
+
*/
|
|
1056
|
+
findNetworkClientIdByChainId(chainId) {
|
|
1057
|
+
const networkClients = this.getNetworkClientRegistry();
|
|
1058
|
+
const networkClientEntry = Object.entries(networkClients).find(
|
|
1059
|
+
([_, networkClient]) => networkClient.configuration.chainId === chainId
|
|
1060
|
+
);
|
|
1061
|
+
if (networkClientEntry === void 0) {
|
|
1062
|
+
throw new Error("Couldn't find networkClientId for chainId");
|
|
1063
|
+
}
|
|
1064
|
+
return networkClientEntry[0];
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
_ethQuery = new WeakMap();
|
|
1068
|
+
_infuraProjectId = new WeakMap();
|
|
1069
|
+
_previouslySelectedNetworkClientId = new WeakMap();
|
|
1070
|
+
_providerProxy = new WeakMap();
|
|
1071
|
+
_blockTrackerProxy = new WeakMap();
|
|
1072
|
+
_autoManagedNetworkClientRegistry = new WeakMap();
|
|
1073
|
+
_autoManagedNetworkClient = new WeakMap();
|
|
1074
|
+
_log = new WeakMap();
|
|
1075
|
+
_networkConfigurationsByNetworkClientId = new WeakMap();
|
|
1076
|
+
_refreshNetwork = new WeakSet();
|
|
1077
|
+
refreshNetwork_fn = async function(networkClientId, options = {}) {
|
|
1078
|
+
this.messagingSystem.publish(
|
|
1079
|
+
"NetworkController:networkWillChange",
|
|
1080
|
+
this.state
|
|
1081
|
+
);
|
|
1082
|
+
__privateMethod(this, _applyNetworkSelection, applyNetworkSelection_fn).call(this, networkClientId, options);
|
|
1083
|
+
this.messagingSystem.publish(
|
|
1084
|
+
"NetworkController:networkDidChange",
|
|
1085
|
+
this.state
|
|
1086
|
+
);
|
|
1087
|
+
await this.lookupNetwork();
|
|
1088
|
+
};
|
|
1089
|
+
_getLatestBlock = new WeakSet();
|
|
1090
|
+
getLatestBlock_fn = function(networkClientId) {
|
|
1091
|
+
if (networkClientId === void 0) {
|
|
1092
|
+
networkClientId = this.state.selectedNetworkClientId;
|
|
1093
|
+
}
|
|
1094
|
+
const networkClient = this.getNetworkClientById(networkClientId);
|
|
1095
|
+
const ethQuery = new EthQuery(networkClient.provider);
|
|
1096
|
+
return new Promise((resolve, reject) => {
|
|
1097
|
+
ethQuery.sendAsync(
|
|
1098
|
+
{ method: "eth_getBlockByNumber", params: ["latest", false] },
|
|
1099
|
+
(error, block) => {
|
|
1100
|
+
if (error) {
|
|
1101
|
+
reject(error);
|
|
1102
|
+
} else {
|
|
1103
|
+
resolve(block);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
);
|
|
1107
|
+
});
|
|
1108
|
+
};
|
|
1109
|
+
_determineEIP1559Compatibility = new WeakSet();
|
|
1110
|
+
determineEIP1559Compatibility_fn = async function(networkClientId) {
|
|
1111
|
+
const latestBlock = await __privateMethod(this, _getLatestBlock, getLatestBlock_fn).call(this, networkClientId);
|
|
1112
|
+
if (!latestBlock) {
|
|
1113
|
+
return void 0;
|
|
1114
|
+
}
|
|
1115
|
+
return latestBlock.baseFeePerGas !== void 0;
|
|
1116
|
+
};
|
|
1117
|
+
_validateNetworkFields = new WeakSet();
|
|
1118
|
+
validateNetworkFields_fn = function(args) {
|
|
1119
|
+
const {
|
|
1120
|
+
mode,
|
|
1121
|
+
networkFields,
|
|
1122
|
+
errorMessagePrefix,
|
|
1123
|
+
autoManagedNetworkClientRegistry
|
|
1124
|
+
} = args;
|
|
1125
|
+
const existingNetworkConfiguration = "existingNetworkConfiguration" in args ? args.existingNetworkConfiguration : null;
|
|
1126
|
+
if (!isStrictHexString(networkFields.chainId) || !isSafeChainId(networkFields.chainId)) {
|
|
1127
|
+
throw new Error(
|
|
1128
|
+
`${errorMessagePrefix}: Invalid \`chainId\` ${inspect(
|
|
1129
|
+
networkFields.chainId
|
|
1130
|
+
)} (must start with "0x" and not exceed the maximum)`
|
|
1131
|
+
);
|
|
1132
|
+
}
|
|
1133
|
+
if (existingNetworkConfiguration === null || networkFields.chainId !== existingNetworkConfiguration.chainId) {
|
|
1134
|
+
const existingNetworkConfigurationViaChainId = this.state.networkConfigurationsByChainId[networkFields.chainId];
|
|
1135
|
+
if (existingNetworkConfigurationViaChainId !== void 0) {
|
|
1136
|
+
if (existingNetworkConfiguration === null) {
|
|
1137
|
+
throw new Error(
|
|
1138
|
+
// False negative - these are strings.
|
|
1139
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1140
|
+
`Could not add network for chain ${args.networkFields.chainId} as another network for that chain already exists ('${existingNetworkConfigurationViaChainId.name}')`
|
|
1141
|
+
);
|
|
1142
|
+
} else {
|
|
1143
|
+
throw new Error(
|
|
1144
|
+
// False negative - these are strings.
|
|
1145
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1146
|
+
`Cannot move network from chain ${existingNetworkConfiguration.chainId} to ${networkFields.chainId} as another network for that chain already exists ('${existingNetworkConfigurationViaChainId.name}')`
|
|
1147
|
+
);
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
const isInvalidDefaultBlockExplorerUrlIndex = networkFields.blockExplorerUrls.length > 0 ? networkFields.defaultBlockExplorerUrlIndex === void 0 || networkFields.blockExplorerUrls[networkFields.defaultBlockExplorerUrlIndex] === void 0 : networkFields.defaultBlockExplorerUrlIndex !== void 0;
|
|
1152
|
+
if (isInvalidDefaultBlockExplorerUrlIndex) {
|
|
1153
|
+
throw new Error(
|
|
1154
|
+
`${errorMessagePrefix}: \`defaultBlockExplorerUrlIndex\` must refer to an entry in \`blockExplorerUrls\``
|
|
1155
|
+
);
|
|
1156
|
+
}
|
|
1157
|
+
if (networkFields.rpcEndpoints.length === 0) {
|
|
1158
|
+
throw new Error(
|
|
1159
|
+
`${errorMessagePrefix}: \`rpcEndpoints\` must be a non-empty array`
|
|
1160
|
+
);
|
|
1161
|
+
}
|
|
1162
|
+
for (const rpcEndpointFields of networkFields.rpcEndpoints) {
|
|
1163
|
+
if (!isValidUrl(rpcEndpointFields.url)) {
|
|
1164
|
+
throw new Error(
|
|
1165
|
+
`${errorMessagePrefix}: An entry in \`rpcEndpoints\` has invalid URL ${inspect(
|
|
1166
|
+
rpcEndpointFields.url
|
|
1167
|
+
)}`
|
|
1168
|
+
);
|
|
1169
|
+
}
|
|
1170
|
+
const networkClientId = "networkClientId" in rpcEndpointFields ? rpcEndpointFields.networkClientId : void 0;
|
|
1171
|
+
if (rpcEndpointFields.type === "custom" /* Custom */ && networkClientId !== void 0 && isInfuraNetworkType(networkClientId)) {
|
|
1172
|
+
throw new Error(
|
|
1173
|
+
// This is a string.
|
|
1174
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1175
|
+
`${errorMessagePrefix}: Custom RPC endpoint '${rpcEndpointFields.url}' has invalid network client ID '${networkClientId}'`
|
|
1176
|
+
);
|
|
1177
|
+
}
|
|
1178
|
+
if (mode === "update" && networkClientId !== void 0 && rpcEndpointFields.type === "custom" /* Custom */ && !Object.values(autoManagedNetworkClientRegistry).some(
|
|
1179
|
+
(networkClientsById) => networkClientId in networkClientsById
|
|
1180
|
+
)) {
|
|
1181
|
+
throw new Error(
|
|
1182
|
+
`${errorMessagePrefix}: RPC endpoint '${// This is a string.
|
|
1183
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1184
|
+
rpcEndpointFields.url}' refers to network client ${inspect(
|
|
1185
|
+
networkClientId
|
|
1186
|
+
)} that does not exist`
|
|
1187
|
+
);
|
|
1188
|
+
}
|
|
1189
|
+
if (networkFields.rpcEndpoints.some(
|
|
1190
|
+
(otherRpcEndpointFields) => otherRpcEndpointFields !== rpcEndpointFields && URI.equal(otherRpcEndpointFields.url, rpcEndpointFields.url)
|
|
1191
|
+
)) {
|
|
1192
|
+
throw new Error(
|
|
1193
|
+
`${errorMessagePrefix}: Each entry in rpcEndpoints must have a unique URL`
|
|
1194
|
+
);
|
|
1195
|
+
}
|
|
1196
|
+
const networkConfigurationsForOtherChains = Object.values(
|
|
1197
|
+
this.state.networkConfigurationsByChainId
|
|
1198
|
+
).filter(
|
|
1199
|
+
(networkConfiguration) => existingNetworkConfiguration ? networkConfiguration.chainId !== existingNetworkConfiguration.chainId : true
|
|
1200
|
+
);
|
|
1201
|
+
for (const networkConfiguration of networkConfigurationsForOtherChains) {
|
|
1202
|
+
const rpcEndpoint = networkConfiguration.rpcEndpoints.find(
|
|
1203
|
+
(existingRpcEndpoint) => URI.equal(rpcEndpointFields.url, existingRpcEndpoint.url)
|
|
1204
|
+
);
|
|
1205
|
+
if (rpcEndpoint) {
|
|
1206
|
+
throw new Error(
|
|
1207
|
+
mode === "update" ? (
|
|
1208
|
+
// False negative - these are strings.
|
|
1209
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1210
|
+
`Could not update network to point to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`
|
|
1211
|
+
) : (
|
|
1212
|
+
// False negative - these are strings.
|
|
1213
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1214
|
+
`Could not add network that points to same RPC endpoint as existing network for chain ${networkConfiguration.chainId} ('${networkConfiguration.name}')`
|
|
1215
|
+
)
|
|
1216
|
+
);
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
if ([...new Set(networkFields.rpcEndpoints)].length < networkFields.rpcEndpoints.length) {
|
|
1221
|
+
throw new Error(
|
|
1222
|
+
`${errorMessagePrefix}: Each entry in rpcEndpoints must be unique`
|
|
1223
|
+
);
|
|
1224
|
+
}
|
|
1225
|
+
const networkClientIds = networkFields.rpcEndpoints.map(
|
|
1226
|
+
(rpcEndpoint) => "networkClientId" in rpcEndpoint ? rpcEndpoint.networkClientId : void 0
|
|
1227
|
+
).filter(
|
|
1228
|
+
(networkClientId) => networkClientId !== void 0
|
|
1229
|
+
);
|
|
1230
|
+
if ([...new Set(networkClientIds)].length < networkClientIds.length) {
|
|
1231
|
+
throw new Error(
|
|
1232
|
+
`${errorMessagePrefix}: Each entry in rpcEndpoints must have a unique networkClientId`
|
|
1233
|
+
);
|
|
1234
|
+
}
|
|
1235
|
+
const infuraRpcEndpoints = networkFields.rpcEndpoints.filter(
|
|
1236
|
+
(rpcEndpointFields) => rpcEndpointFields.type === "infura" /* Infura */
|
|
1237
|
+
);
|
|
1238
|
+
if (infuraRpcEndpoints.length > 1) {
|
|
1239
|
+
throw new Error(
|
|
1240
|
+
`${errorMessagePrefix}: There cannot be more than one Infura RPC endpoint`
|
|
1241
|
+
);
|
|
1242
|
+
}
|
|
1243
|
+
const soleInfuraRpcEndpoint = infuraRpcEndpoints[0];
|
|
1244
|
+
if (soleInfuraRpcEndpoint) {
|
|
1245
|
+
const infuraNetworkName = deriveInfuraNetworkNameFromRpcEndpointUrl(
|
|
1246
|
+
soleInfuraRpcEndpoint.url
|
|
1247
|
+
);
|
|
1248
|
+
const infuraNetworkNickname = NetworkNickname[infuraNetworkName];
|
|
1249
|
+
const infuraChainId = ChainId[infuraNetworkName];
|
|
1250
|
+
if (networkFields.chainId !== infuraChainId) {
|
|
1251
|
+
throw new Error(
|
|
1252
|
+
mode === "add" ? (
|
|
1253
|
+
// This is a string.
|
|
1254
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1255
|
+
`Could not add network with chain ID ${networkFields.chainId} and Infura RPC endpoint for '${infuraNetworkNickname}' which represents ${infuraChainId}, as the two conflict`
|
|
1256
|
+
) : (
|
|
1257
|
+
// This is a string.
|
|
1258
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1259
|
+
`Could not update network with chain ID ${networkFields.chainId} and Infura RPC endpoint for '${infuraNetworkNickname}' which represents ${infuraChainId}, as the two conflict`
|
|
1260
|
+
)
|
|
1261
|
+
);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
if (networkFields.rpcEndpoints[networkFields.defaultRpcEndpointIndex] === void 0) {
|
|
1265
|
+
throw new Error(
|
|
1266
|
+
`${errorMessagePrefix}: \`defaultRpcEndpointIndex\` must refer to an entry in \`rpcEndpoints\``
|
|
1267
|
+
);
|
|
1268
|
+
}
|
|
1269
|
+
};
|
|
1270
|
+
_determineNetworkConfigurationToPersist = new WeakSet();
|
|
1271
|
+
determineNetworkConfigurationToPersist_fn = function({
|
|
1272
|
+
networkFields,
|
|
1273
|
+
networkClientOperations
|
|
1274
|
+
}) {
|
|
1275
|
+
const rpcEndpointsToPersist = networkClientOperations.filter(
|
|
1276
|
+
(networkClientOperation) => {
|
|
1277
|
+
return networkClientOperation.type === "add" || networkClientOperation.type === "noop";
|
|
1278
|
+
}
|
|
1279
|
+
).map((networkClientOperation) => networkClientOperation.rpcEndpoint).concat(
|
|
1280
|
+
networkClientOperations.filter(
|
|
1281
|
+
(networkClientOperation) => {
|
|
1282
|
+
return networkClientOperation.type === "replace";
|
|
1283
|
+
}
|
|
1284
|
+
).map(
|
|
1285
|
+
(networkClientOperation) => networkClientOperation.newRpcEndpoint
|
|
1286
|
+
)
|
|
1287
|
+
);
|
|
1288
|
+
return { ...networkFields, rpcEndpoints: rpcEndpointsToPersist };
|
|
1289
|
+
};
|
|
1290
|
+
_registerNetworkClientsAsNeeded = new WeakSet();
|
|
1291
|
+
registerNetworkClientsAsNeeded_fn = function({
|
|
1292
|
+
networkFields,
|
|
1293
|
+
networkClientOperations,
|
|
1294
|
+
autoManagedNetworkClientRegistry
|
|
1295
|
+
}) {
|
|
1296
|
+
const addedRpcEndpoints = networkClientOperations.filter(
|
|
1297
|
+
(networkClientOperation) => {
|
|
1298
|
+
return networkClientOperation.type === "add";
|
|
1299
|
+
}
|
|
1300
|
+
).map((networkClientOperation) => networkClientOperation.rpcEndpoint).concat(
|
|
1301
|
+
networkClientOperations.filter(
|
|
1302
|
+
(networkClientOperation) => {
|
|
1303
|
+
return networkClientOperation.type === "replace";
|
|
1304
|
+
}
|
|
1305
|
+
).map(
|
|
1306
|
+
(networkClientOperation) => networkClientOperation.newRpcEndpoint
|
|
1307
|
+
)
|
|
1308
|
+
);
|
|
1309
|
+
for (const addedRpcEndpoint of addedRpcEndpoints) {
|
|
1310
|
+
if (addedRpcEndpoint.type === "infura" /* Infura */) {
|
|
1311
|
+
autoManagedNetworkClientRegistry["infura" /* Infura */][addedRpcEndpoint.networkClientId] = createAutoManagedNetworkClient({
|
|
1312
|
+
type: "infura" /* Infura */,
|
|
1313
|
+
chainId: networkFields.chainId,
|
|
1314
|
+
network: addedRpcEndpoint.networkClientId,
|
|
1315
|
+
infuraProjectId: __privateGet(this, _infuraProjectId),
|
|
1316
|
+
ticker: networkFields.nativeCurrency
|
|
1317
|
+
});
|
|
1318
|
+
} else {
|
|
1319
|
+
autoManagedNetworkClientRegistry["custom" /* Custom */][addedRpcEndpoint.networkClientId] = createAutoManagedNetworkClient({
|
|
1320
|
+
type: "custom" /* Custom */,
|
|
1321
|
+
chainId: networkFields.chainId,
|
|
1322
|
+
rpcUrl: addedRpcEndpoint.url,
|
|
1323
|
+
ticker: networkFields.nativeCurrency
|
|
1324
|
+
});
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
_unregisterNetworkClientsAsNeeded = new WeakSet();
|
|
1329
|
+
unregisterNetworkClientsAsNeeded_fn = function({
|
|
1330
|
+
networkClientOperations,
|
|
1331
|
+
autoManagedNetworkClientRegistry
|
|
1332
|
+
}) {
|
|
1333
|
+
const removedRpcEndpoints = networkClientOperations.filter(
|
|
1334
|
+
(networkClientOperation) => {
|
|
1335
|
+
return networkClientOperation.type === "remove";
|
|
1336
|
+
}
|
|
1337
|
+
).map((networkClientOperation) => networkClientOperation.rpcEndpoint).concat(
|
|
1338
|
+
networkClientOperations.filter(
|
|
1339
|
+
(networkClientOperation) => {
|
|
1340
|
+
return networkClientOperation.type === "replace";
|
|
1341
|
+
}
|
|
1342
|
+
).map(
|
|
1343
|
+
(networkClientOperation) => networkClientOperation.oldRpcEndpoint
|
|
1344
|
+
)
|
|
1345
|
+
);
|
|
1346
|
+
for (const rpcEndpoint of removedRpcEndpoints) {
|
|
1347
|
+
const networkClient = this.getNetworkClientById(
|
|
1348
|
+
rpcEndpoint.networkClientId
|
|
1349
|
+
);
|
|
1350
|
+
networkClient.destroy();
|
|
1351
|
+
delete autoManagedNetworkClientRegistry[networkClient.configuration.type][rpcEndpoint.networkClientId];
|
|
1352
|
+
}
|
|
1353
|
+
};
|
|
1354
|
+
_updateNetworkConfigurations = new WeakSet();
|
|
1355
|
+
updateNetworkConfigurations_fn = function(args) {
|
|
1356
|
+
const { state, mode } = args;
|
|
1357
|
+
if (mode === "remove" || mode === "update" && args.networkFields.chainId !== args.existingNetworkConfiguration.chainId) {
|
|
1358
|
+
delete state.networkConfigurationsByChainId[args.existingNetworkConfiguration.chainId];
|
|
1359
|
+
}
|
|
1360
|
+
if (mode === "add" || mode === "update") {
|
|
1361
|
+
state.networkConfigurationsByChainId[args.networkFields.chainId] = args.networkConfigurationToPersist;
|
|
1362
|
+
}
|
|
1363
|
+
__privateSet(this, _networkConfigurationsByNetworkClientId, buildNetworkConfigurationsByNetworkClientId(
|
|
1364
|
+
this.state.networkConfigurationsByChainId
|
|
1365
|
+
));
|
|
1366
|
+
};
|
|
1367
|
+
_ensureAutoManagedNetworkClientRegistryPopulated = new WeakSet();
|
|
1368
|
+
ensureAutoManagedNetworkClientRegistryPopulated_fn = function() {
|
|
1369
|
+
return __privateGet(this, _autoManagedNetworkClientRegistry) ?? __privateSet(this, _autoManagedNetworkClientRegistry, __privateMethod(this, _createAutoManagedNetworkClientRegistry, createAutoManagedNetworkClientRegistry_fn).call(this));
|
|
1370
|
+
};
|
|
1371
|
+
_createAutoManagedNetworkClientRegistry = new WeakSet();
|
|
1372
|
+
createAutoManagedNetworkClientRegistry_fn = function() {
|
|
1373
|
+
const sortedChainIds = Object.keys(
|
|
1374
|
+
this.state.networkConfigurationsByChainId
|
|
1375
|
+
).map(hexToBigInt).sort().map(toHex);
|
|
1376
|
+
const networkClientsWithIds = sortedChainIds.flatMap((chainId) => {
|
|
1377
|
+
const networkConfiguration = this.state.networkConfigurationsByChainId[chainId];
|
|
1378
|
+
return networkConfiguration.rpcEndpoints.map((rpcEndpoint) => {
|
|
1379
|
+
if (rpcEndpoint.type === "infura" /* Infura */) {
|
|
1380
|
+
const infuraNetworkName = deriveInfuraNetworkNameFromRpcEndpointUrl(
|
|
1381
|
+
rpcEndpoint.url
|
|
1382
|
+
);
|
|
1383
|
+
return [
|
|
1384
|
+
rpcEndpoint.networkClientId,
|
|
1385
|
+
createAutoManagedNetworkClient({
|
|
1386
|
+
type: "infura" /* Infura */,
|
|
1387
|
+
network: infuraNetworkName,
|
|
1388
|
+
infuraProjectId: __privateGet(this, _infuraProjectId),
|
|
1389
|
+
chainId: networkConfiguration.chainId,
|
|
1390
|
+
ticker: networkConfiguration.nativeCurrency
|
|
1391
|
+
})
|
|
1392
|
+
];
|
|
1393
|
+
}
|
|
1394
|
+
return [
|
|
1395
|
+
rpcEndpoint.networkClientId,
|
|
1396
|
+
createAutoManagedNetworkClient({
|
|
1397
|
+
type: "custom" /* Custom */,
|
|
1398
|
+
chainId: networkConfiguration.chainId,
|
|
1399
|
+
rpcUrl: rpcEndpoint.url,
|
|
1400
|
+
ticker: networkConfiguration.nativeCurrency
|
|
1401
|
+
})
|
|
1402
|
+
];
|
|
1403
|
+
});
|
|
1404
|
+
});
|
|
1405
|
+
return networkClientsWithIds.reduce(
|
|
1406
|
+
(obj, [networkClientId, networkClient]) => {
|
|
1407
|
+
return {
|
|
1408
|
+
...obj,
|
|
1409
|
+
[networkClient.configuration.type]: {
|
|
1410
|
+
...obj[networkClient.configuration.type],
|
|
1411
|
+
[networkClientId]: networkClient
|
|
1412
|
+
}
|
|
1413
|
+
};
|
|
1414
|
+
},
|
|
1415
|
+
{
|
|
1416
|
+
["custom" /* Custom */]: {},
|
|
1417
|
+
["infura" /* Infura */]: {}
|
|
1418
|
+
}
|
|
1419
|
+
);
|
|
1420
|
+
};
|
|
1421
|
+
_applyNetworkSelection = new WeakSet();
|
|
1422
|
+
applyNetworkSelection_fn = function(networkClientId, {
|
|
1423
|
+
updateState
|
|
1424
|
+
} = {}) {
|
|
1425
|
+
const autoManagedNetworkClientRegistry = __privateMethod(this, _ensureAutoManagedNetworkClientRegistryPopulated, ensureAutoManagedNetworkClientRegistryPopulated_fn).call(this);
|
|
1426
|
+
let autoManagedNetworkClient;
|
|
1427
|
+
if (isInfuraNetworkType(networkClientId)) {
|
|
1428
|
+
const possibleAutoManagedNetworkClient = autoManagedNetworkClientRegistry["infura" /* Infura */][networkClientId];
|
|
1429
|
+
if (!possibleAutoManagedNetworkClient) {
|
|
1430
|
+
throw new Error(
|
|
1431
|
+
`No Infura network client found with ID ${inspect(networkClientId)}`
|
|
1432
|
+
);
|
|
1433
|
+
}
|
|
1434
|
+
autoManagedNetworkClient = possibleAutoManagedNetworkClient;
|
|
1435
|
+
} else {
|
|
1436
|
+
const possibleAutoManagedNetworkClient = autoManagedNetworkClientRegistry["custom" /* Custom */][networkClientId];
|
|
1437
|
+
if (!possibleAutoManagedNetworkClient) {
|
|
1438
|
+
throw new Error(
|
|
1439
|
+
`No network client found with ID ${inspect(networkClientId)}`
|
|
1440
|
+
);
|
|
1441
|
+
}
|
|
1442
|
+
autoManagedNetworkClient = possibleAutoManagedNetworkClient;
|
|
1443
|
+
}
|
|
1444
|
+
__privateSet(this, _autoManagedNetworkClient, autoManagedNetworkClient);
|
|
1445
|
+
this.update((state) => {
|
|
1446
|
+
state.selectedNetworkClientId = networkClientId;
|
|
1447
|
+
if (state.networksMetadata[networkClientId] === void 0) {
|
|
1448
|
+
state.networksMetadata[networkClientId] = {
|
|
1449
|
+
status: "unknown" /* Unknown */,
|
|
1450
|
+
EIPS: {}
|
|
1451
|
+
};
|
|
1452
|
+
}
|
|
1453
|
+
updateState?.(state);
|
|
1454
|
+
});
|
|
1455
|
+
if (__privateGet(this, _providerProxy)) {
|
|
1456
|
+
__privateGet(this, _providerProxy).setTarget(__privateGet(this, _autoManagedNetworkClient).provider);
|
|
1457
|
+
} else {
|
|
1458
|
+
__privateSet(this, _providerProxy, createEventEmitterProxy(
|
|
1459
|
+
__privateGet(this, _autoManagedNetworkClient).provider
|
|
1460
|
+
));
|
|
1461
|
+
}
|
|
1462
|
+
if (__privateGet(this, _blockTrackerProxy)) {
|
|
1463
|
+
__privateGet(this, _blockTrackerProxy).setTarget(
|
|
1464
|
+
__privateGet(this, _autoManagedNetworkClient).blockTracker
|
|
1465
|
+
);
|
|
1466
|
+
} else {
|
|
1467
|
+
__privateSet(this, _blockTrackerProxy, createEventEmitterProxy(
|
|
1468
|
+
__privateGet(this, _autoManagedNetworkClient).blockTracker,
|
|
1469
|
+
{ eventFilter: "skipInternal" }
|
|
1470
|
+
));
|
|
1471
|
+
}
|
|
1472
|
+
__privateSet(this, _ethQuery, new EthQuery(__privateGet(this, _providerProxy)));
|
|
1473
|
+
};
|
|
1474
|
+
|
|
1475
|
+
export {
|
|
1476
|
+
RpcEndpointType,
|
|
1477
|
+
knownKeysOf,
|
|
1478
|
+
getDefaultNetworkControllerState,
|
|
1479
|
+
NetworkController
|
|
1480
|
+
};
|
|
1481
|
+
//# sourceMappingURL=chunk-DARVWUIF.mjs.map
|