@metamask-previews/network-controller 23.1.0-preview-62c1e797 → 23.1.0-preview-49487623
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/NetworkController.cjs +58 -5
- package/dist/NetworkController.cjs.map +1 -1
- package/dist/NetworkController.d.cts +8 -2
- package/dist/NetworkController.d.cts.map +1 -1
- package/dist/NetworkController.d.mts +8 -2
- package/dist/NetworkController.d.mts.map +1 -1
- package/dist/NetworkController.mjs +59 -6
- package/dist/NetworkController.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -106,9 +106,32 @@ const controllerName = 'NetworkController';
|
|
|
106
106
|
* Constructs a value for the state property `networkConfigurationsByChainId`
|
|
107
107
|
* which will be used if it has not been provided to the constructor.
|
|
108
108
|
*
|
|
109
|
+
* @param [defaultNetworks] - An array of Hex Chain IDs representing the default networks to be included.
|
|
109
110
|
* @returns The default value for `networkConfigurationsByChainId`.
|
|
110
111
|
*/
|
|
111
|
-
function getDefaultNetworkConfigurationsByChainId() {
|
|
112
|
+
function getDefaultNetworkConfigurationsByChainId(defaultNetworks) {
|
|
113
|
+
const infuraNetworks = getDefaultInfuraNetworkConfigurationsByChainId();
|
|
114
|
+
const customNetworks = getDefaultCustomNetworkConfigurationsByChainId();
|
|
115
|
+
// If no default networks are provided, use the default Infura networks
|
|
116
|
+
if (!defaultNetworks || defaultNetworks.length === 0) {
|
|
117
|
+
return infuraNetworks;
|
|
118
|
+
}
|
|
119
|
+
return defaultNetworks.reduce((obj, chainId) => {
|
|
120
|
+
if ((0, utils_1.hasProperty)(infuraNetworks, chainId)) {
|
|
121
|
+
obj[chainId] = infuraNetworks[chainId];
|
|
122
|
+
}
|
|
123
|
+
else if ((0, utils_1.hasProperty)(customNetworks, chainId)) {
|
|
124
|
+
obj[chainId] = customNetworks[chainId];
|
|
125
|
+
}
|
|
126
|
+
return obj;
|
|
127
|
+
}, {});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Constructs a `networkConfigurationsByChainId` object for all default Infura networks.
|
|
131
|
+
*
|
|
132
|
+
* @returns The `networkConfigurationsByChainId` object of all Infura networks.
|
|
133
|
+
*/
|
|
134
|
+
function getDefaultInfuraNetworkConfigurationsByChainId() {
|
|
112
135
|
return Object.values(controller_utils_1.InfuraNetworkType).reduce((obj, infuraNetworkType) => {
|
|
113
136
|
const chainId = controller_utils_1.ChainId[infuraNetworkType];
|
|
114
137
|
const rpcEndpointUrl =
|
|
@@ -133,15 +156,42 @@ function getDefaultNetworkConfigurationsByChainId() {
|
|
|
133
156
|
return { ...obj, [chainId]: networkConfiguration };
|
|
134
157
|
}, {});
|
|
135
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Constructs a `networkConfigurationsByChainId` object for all default custom networks.
|
|
161
|
+
*
|
|
162
|
+
* @returns The `networkConfigurationsByChainId` object of all custom networks.
|
|
163
|
+
*/
|
|
164
|
+
function getDefaultCustomNetworkConfigurationsByChainId() {
|
|
165
|
+
const { ticker, rpcPrefs } = controller_utils_1.BUILT_IN_NETWORKS[controller_utils_1.BuiltInNetworkName.MegaETHTestnet];
|
|
166
|
+
return {
|
|
167
|
+
[controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.MegaETHTestnet]]: {
|
|
168
|
+
blockExplorerUrls: [rpcPrefs.blockExplorerUrl],
|
|
169
|
+
chainId: controller_utils_1.ChainId[controller_utils_1.BuiltInNetworkName.MegaETHTestnet],
|
|
170
|
+
defaultRpcEndpointIndex: 0,
|
|
171
|
+
defaultBlockExplorerUrlIndex: 0,
|
|
172
|
+
name: controller_utils_1.NetworkNickname[controller_utils_1.BuiltInNetworkName.MegaETHTestnet],
|
|
173
|
+
nativeCurrency: ticker,
|
|
174
|
+
rpcEndpoints: [
|
|
175
|
+
{
|
|
176
|
+
failoverUrls: [],
|
|
177
|
+
networkClientId: controller_utils_1.BuiltInNetworkName.MegaETHTestnet,
|
|
178
|
+
type: RpcEndpointType.Custom,
|
|
179
|
+
url: controller_utils_1.BUILT_IN_CUSTOM_NETWORKS_RPC.MEGAETH_TESTNET,
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
136
185
|
/**
|
|
137
186
|
* Constructs properties for the NetworkController state whose values will be
|
|
138
187
|
* used if not provided to the constructor.
|
|
139
188
|
*
|
|
189
|
+
* @param [defaultNetworks] - An array of Hex Chain IDs representing the default networks to be included.
|
|
140
190
|
* @returns The default NetworkController state.
|
|
141
191
|
*/
|
|
142
|
-
function getDefaultNetworkControllerState() {
|
|
192
|
+
function getDefaultNetworkControllerState(defaultNetworks) {
|
|
143
193
|
const networksMetadata = {};
|
|
144
|
-
const networkConfigurationsByChainId = getDefaultNetworkConfigurationsByChainId();
|
|
194
|
+
const networkConfigurationsByChainId = getDefaultNetworkConfigurationsByChainId(defaultNetworks);
|
|
145
195
|
return {
|
|
146
196
|
selectedNetworkClientId: controller_utils_1.InfuraNetworkType.mainnet,
|
|
147
197
|
networksMetadata,
|
|
@@ -293,8 +343,11 @@ class NetworkController extends base_controller_1.BaseController {
|
|
|
293
343
|
* @param options - The options; see {@link NetworkControllerOptions}.
|
|
294
344
|
*/
|
|
295
345
|
constructor(options) {
|
|
296
|
-
const { messenger, state, infuraProjectId, log, getRpcServiceOptions } = options;
|
|
297
|
-
const initialState = {
|
|
346
|
+
const { messenger, state, infuraProjectId, log, getRpcServiceOptions, defaultNetworks, } = options;
|
|
347
|
+
const initialState = {
|
|
348
|
+
...getDefaultNetworkControllerState(defaultNetworks),
|
|
349
|
+
...state,
|
|
350
|
+
};
|
|
298
351
|
validateNetworkControllerState(initialState);
|
|
299
352
|
if (!infuraProjectId || typeof infuraProjectId !== 'string') {
|
|
300
353
|
throw new Error('Invalid Infura project ID');
|