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