2021-05-07 21:38:24 +02:00
|
|
|
import { strict as assert } from 'assert';
|
2021-02-04 19:15:23 +01:00
|
|
|
import EventEmitter from 'events';
|
|
|
|
import { ComposedStore, ObservableStore } from '@metamask/obs-store';
|
|
|
|
import { JsonRpcEngine } from 'json-rpc-engine';
|
2022-12-20 18:28:09 +01:00
|
|
|
import {
|
|
|
|
providerFromEngine,
|
|
|
|
providerFromMiddleware,
|
|
|
|
} from 'eth-json-rpc-middleware';
|
2021-02-04 19:15:23 +01:00
|
|
|
import log from 'loglevel';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
createSwappableProxy,
|
|
|
|
createEventEmitterProxy,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from 'swappable-obj-proxy';
|
|
|
|
import EthQuery from 'eth-query';
|
2022-12-20 18:28:09 +01:00
|
|
|
import createFilterMiddleware from 'eth-json-rpc-filters';
|
|
|
|
import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager';
|
2020-05-20 17:57:45 +02:00
|
|
|
import {
|
|
|
|
INFURA_PROVIDER_TYPES,
|
2022-09-14 16:55:31 +02:00
|
|
|
BUILT_IN_NETWORKS,
|
2021-04-15 19:41:40 +02:00
|
|
|
INFURA_BLOCKED_KEY,
|
2022-04-21 21:09:41 +02:00
|
|
|
TEST_NETWORK_TICKER_MAP,
|
2022-09-14 16:55:31 +02:00
|
|
|
CHAIN_IDS,
|
|
|
|
NETWORK_TYPES,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../../shared/constants/network';
|
2021-01-21 00:37:18 +01:00
|
|
|
import {
|
|
|
|
isPrefixedFormattedHexString,
|
|
|
|
isSafeChainId,
|
2021-02-12 16:25:58 +01:00
|
|
|
} from '../../../../shared/modules/network.utils';
|
2021-04-15 19:41:40 +02:00
|
|
|
import getFetchWithTimeout from '../../../../shared/modules/fetch-with-timeout';
|
2021-02-04 19:15:23 +01:00
|
|
|
import createInfuraClient from './createInfuraClient';
|
|
|
|
import createJsonRpcClient from './createJsonRpcClient';
|
2017-05-18 23:54:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const env = process.env.METAMASK_ENV;
|
2022-07-19 18:13:45 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout();
|
2019-03-28 17:04:31 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let defaultProviderConfigOpts;
|
2021-12-02 19:16:46 +01:00
|
|
|
if (process.env.IN_TEST) {
|
2020-10-12 21:05:40 +02:00
|
|
|
defaultProviderConfigOpts = {
|
2022-09-14 16:55:31 +02:00
|
|
|
type: NETWORK_TYPES.RPC,
|
2020-10-12 21:05:40 +02:00
|
|
|
rpcUrl: 'http://localhost:8545',
|
|
|
|
chainId: '0x539',
|
|
|
|
nickname: 'Localhost 8545',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-08 18:22:14 +02:00
|
|
|
} else if (process.env.METAMASK_DEBUG || env === 'test') {
|
2022-04-21 21:09:41 +02:00
|
|
|
defaultProviderConfigOpts = {
|
2022-09-29 05:26:01 +02:00
|
|
|
type: NETWORK_TYPES.GOERLI,
|
|
|
|
chainId: CHAIN_IDS.GOERLI,
|
|
|
|
ticker: TEST_NETWORK_TICKER_MAP.GOERLI,
|
2022-04-21 21:09:41 +02:00
|
|
|
};
|
2019-03-28 17:04:31 +01:00
|
|
|
} else {
|
2022-09-14 16:55:31 +02:00
|
|
|
defaultProviderConfigOpts = {
|
|
|
|
type: NETWORK_TYPES.MAINNET,
|
|
|
|
chainId: CHAIN_IDS.MAINNET,
|
|
|
|
};
|
2019-03-28 17:04:31 +01:00
|
|
|
}
|
2018-05-03 03:03:59 +02:00
|
|
|
|
|
|
|
const defaultProviderConfig = {
|
2018-10-26 10:26:43 +02:00
|
|
|
ticker: 'ETH',
|
2020-10-12 21:05:40 +02:00
|
|
|
...defaultProviderConfigOpts,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-10-26 10:26:43 +02:00
|
|
|
|
2021-06-25 18:24:00 +02:00
|
|
|
const defaultNetworkDetailsState = {
|
|
|
|
EIPS: { 1559: undefined },
|
|
|
|
};
|
|
|
|
|
2021-02-09 00:22:30 +01:00
|
|
|
export const NETWORK_EVENTS = {
|
|
|
|
// Fired after the actively selected network is changed
|
|
|
|
NETWORK_DID_CHANGE: 'networkDidChange',
|
|
|
|
// Fired when the actively selected network *will* change
|
|
|
|
NETWORK_WILL_CHANGE: 'networkWillChange',
|
2021-04-15 19:41:40 +02:00
|
|
|
// Fired when Infura returns an error indicating no support
|
|
|
|
INFURA_IS_BLOCKED: 'infuraIsBlocked',
|
|
|
|
// Fired when not using an Infura network or when Infura returns no error, indicating support
|
|
|
|
INFURA_IS_UNBLOCKED: 'infuraIsUnblocked',
|
2021-02-09 00:22:30 +01:00
|
|
|
};
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default class NetworkController extends EventEmitter {
|
2023-01-23 15:22:42 +01:00
|
|
|
static defaultProviderConfig = defaultProviderConfig;
|
|
|
|
|
2022-12-13 16:39:21 +01:00
|
|
|
/**
|
|
|
|
* Construct a NetworkController.
|
|
|
|
*
|
|
|
|
* @param {object} [options] - NetworkController options.
|
|
|
|
* @param {object} [options.state] - Initial controller state.
|
|
|
|
* @param {string} [options.infuraProjectId] - The Infura project ID.
|
|
|
|
*/
|
|
|
|
constructor({ state = {}, infuraProjectId } = {}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
super();
|
2017-12-22 19:43:02 +01:00
|
|
|
|
2018-05-03 03:03:59 +02:00
|
|
|
// create stores
|
2020-10-06 19:57:02 +02:00
|
|
|
this.providerStore = new ObservableStore(
|
2022-12-13 16:39:21 +01:00
|
|
|
state.provider || { ...defaultProviderConfig },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-01-07 00:31:11 +01:00
|
|
|
this.previousProviderStore = new ObservableStore(
|
|
|
|
this.providerStore.getState(),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
this.networkStore = new ObservableStore('loading');
|
2021-06-25 18:24:00 +02:00
|
|
|
// We need to keep track of a few details about the current network
|
|
|
|
// Ideally we'd merge this.networkStore with this new store, but doing so
|
|
|
|
// will require a decent sized refactor of how we're accessing network
|
|
|
|
// state. Currently this is only used for detecting EIP 1559 support but
|
|
|
|
// can be extended to track other network details.
|
|
|
|
this.networkDetails = new ObservableStore(
|
2022-12-13 16:39:21 +01:00
|
|
|
state.networkDetails || {
|
2021-06-25 18:24:00 +02:00
|
|
|
...defaultNetworkDetailsState,
|
|
|
|
},
|
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
this.store = new ComposedStore({
|
|
|
|
provider: this.providerStore,
|
2021-01-07 00:31:11 +01:00
|
|
|
previousProviderStore: this.previousProviderStore,
|
2020-10-06 19:57:02 +02:00
|
|
|
network: this.networkStore,
|
2021-06-25 18:24:00 +02:00
|
|
|
networkDetails: this.networkDetails,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2018-05-17 05:46:34 +02:00
|
|
|
// provider and block tracker
|
2021-02-04 19:15:23 +01:00
|
|
|
this._provider = null;
|
|
|
|
this._blockTracker = null;
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2018-05-17 05:46:34 +02:00
|
|
|
// provider and block tracker proxies - because the network changes
|
2021-02-04 19:15:23 +01:00
|
|
|
this._providerProxy = null;
|
|
|
|
this._blockTrackerProxy = null;
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2022-12-13 16:39:21 +01:00
|
|
|
if (!infuraProjectId || typeof infuraProjectId !== 'string') {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('Invalid Infura project ID');
|
2020-09-10 18:16:00 +02:00
|
|
|
}
|
2022-12-13 16:39:21 +01:00
|
|
|
this._infuraProjectId = infuraProjectId;
|
2020-09-10 18:16:00 +02:00
|
|
|
|
Expand network controller unit test coverage (#17498)
The network controller has some tests, but they are incomplete and don't
follow our latest best practices for writing unit tests.
This commit greatly increases the amount of test coverage for the API
that we want to retain in NetworkController, in particular the seemingly
myriad paths that the code takes starting from `initializeProvider`,
`resetConnection`, `setRpcTarget`, `setProviderType`,
`rollbackToPreviousProvider`, and `lookupNetwork`.
There were a couple of pieces of logic I noted which don't seem to have
any effect due to being redundant or unreachable, but they also don't
make our lives more difficult, either, so I opted to leave them in.
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
Co-authored-by: Zachary Belford <belfordz66@gmail.com>
2023-02-08 21:10:04 +01:00
|
|
|
this.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, () => {
|
|
|
|
this.lookupNetwork();
|
|
|
|
});
|
2020-09-10 18:16:00 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:16:22 +01:00
|
|
|
/**
|
|
|
|
* Destroy the network controller, stopping any ongoing polling.
|
|
|
|
*
|
|
|
|
* In-progress requests will not be aborted.
|
|
|
|
*/
|
|
|
|
async destroy() {
|
2023-01-11 19:56:33 +01:00
|
|
|
await this._blockTracker?.destroy();
|
2023-01-04 22:16:22 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 18:14:50 +01:00
|
|
|
async initializeProvider() {
|
2023-02-22 18:43:37 +01:00
|
|
|
const { type, rpcUrl, chainId } = this.providerStore.getState();
|
2021-02-04 19:15:23 +01:00
|
|
|
this._configureProvider({ type, rpcUrl, chainId });
|
2022-12-16 17:33:22 +01:00
|
|
|
await this.lookupNetwork();
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// return the proxies so the references will always be good
|
2020-11-03 00:41:28 +01:00
|
|
|
getProviderAndBlockTracker() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const provider = this._providerProxy;
|
|
|
|
const blockTracker = this._blockTrackerProxy;
|
|
|
|
return { provider, blockTracker };
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
2017-05-23 07:56:10 +02:00
|
|
|
|
2021-06-25 18:24:00 +02:00
|
|
|
/**
|
|
|
|
* Method to check if the block header contains fields that indicate EIP 1559
|
|
|
|
* support (baseFeePerGas).
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-06-25 18:24:00 +02:00
|
|
|
* @returns {Promise<boolean>} true if current network supports EIP 1559
|
|
|
|
*/
|
|
|
|
async getEIP1559Compatibility() {
|
|
|
|
const { EIPS } = this.networkDetails.getState();
|
Expand network controller unit test coverage (#17498)
The network controller has some tests, but they are incomplete and don't
follow our latest best practices for writing unit tests.
This commit greatly increases the amount of test coverage for the API
that we want to retain in NetworkController, in particular the seemingly
myriad paths that the code takes starting from `initializeProvider`,
`resetConnection`, `setRpcTarget`, `setProviderType`,
`rollbackToPreviousProvider`, and `lookupNetwork`.
There were a couple of pieces of logic I noted which don't seem to have
any effect due to being redundant or unreachable, but they also don't
make our lives more difficult, either, so I opted to leave them in.
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
Co-authored-by: Zachary Belford <belfordz66@gmail.com>
2023-02-08 21:10:04 +01:00
|
|
|
// NOTE: This isn't necessary anymore because the block cache middleware
|
|
|
|
// already prevents duplicate requests from taking place
|
2021-06-25 18:24:00 +02:00
|
|
|
if (EIPS[1559] !== undefined) {
|
|
|
|
return EIPS[1559];
|
|
|
|
}
|
2022-12-13 20:13:54 +01:00
|
|
|
const latestBlock = await this._getLatestBlock();
|
2021-07-20 19:17:15 +02:00
|
|
|
const supportsEIP1559 =
|
|
|
|
latestBlock && latestBlock.baseFeePerGas !== undefined;
|
2022-12-13 20:13:54 +01:00
|
|
|
this._setNetworkEIPSupport(1559, supportsEIP1559);
|
2021-06-25 18:24:00 +02:00
|
|
|
return supportsEIP1559;
|
|
|
|
}
|
|
|
|
|
2022-12-16 17:33:22 +01:00
|
|
|
async lookupNetwork() {
|
2017-10-23 07:24:50 +02:00
|
|
|
// Prevent firing when provider is not defined.
|
2018-05-17 05:46:34 +02:00
|
|
|
if (!this._provider) {
|
2020-11-03 00:41:28 +01:00
|
|
|
log.warn(
|
|
|
|
'NetworkController - lookupNetwork aborted due to missing provider',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return;
|
2017-10-23 07:24:50 +02:00
|
|
|
}
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2023-02-22 18:43:37 +01:00
|
|
|
const { chainId } = this.providerStore.getState();
|
2020-10-06 19:57:02 +02:00
|
|
|
if (!chainId) {
|
2020-11-03 00:41:28 +01:00
|
|
|
log.warn(
|
|
|
|
'NetworkController - lookupNetwork aborted due to missing chainId',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-12-13 20:13:54 +01:00
|
|
|
this._setNetworkState('loading');
|
|
|
|
this._clearNetworkDetails();
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2020-10-06 19:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ping the RPC endpoint so we can confirm that it works
|
2023-02-22 18:43:37 +01:00
|
|
|
const initialNetwork = this.networkStore.getState();
|
|
|
|
const { type } = this.providerStore.getState();
|
2021-04-15 19:41:40 +02:00
|
|
|
const isInfura = INFURA_PROVIDER_TYPES.includes(type);
|
|
|
|
|
|
|
|
if (isInfura) {
|
|
|
|
this._checkInfuraAvailability(type);
|
|
|
|
} else {
|
|
|
|
this.emit(NETWORK_EVENTS.INFURA_IS_UNBLOCKED);
|
|
|
|
}
|
|
|
|
|
2022-12-16 17:33:22 +01:00
|
|
|
let networkVersion;
|
|
|
|
let networkVersionError;
|
|
|
|
try {
|
|
|
|
networkVersion = await this._getNetworkId();
|
|
|
|
} catch (error) {
|
|
|
|
networkVersionError = error;
|
|
|
|
}
|
2023-02-22 18:43:37 +01:00
|
|
|
if (initialNetwork !== this.networkStore.getState()) {
|
2022-12-16 17:33:22 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2022-12-16 17:33:22 +01:00
|
|
|
if (networkVersionError) {
|
|
|
|
this._setNetworkState('loading');
|
|
|
|
// keep network details in sync with network state
|
|
|
|
this._clearNetworkDetails();
|
|
|
|
} else {
|
|
|
|
this._setNetworkState(networkVersion);
|
|
|
|
// look up EIP-1559 support
|
|
|
|
await this.getEIP1559Compatibility();
|
|
|
|
}
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
setRpcTarget(rpcUrl, chainId, ticker = 'ETH', nickname = '', rpcPrefs) {
|
2021-01-21 00:37:18 +01:00
|
|
|
assert.ok(
|
|
|
|
isPrefixedFormattedHexString(chainId),
|
|
|
|
`Invalid chain ID "${chainId}": invalid hex string.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-01-21 00:37:18 +01:00
|
|
|
assert.ok(
|
|
|
|
isSafeChainId(parseInt(chainId, 16)),
|
|
|
|
`Invalid chain ID "${chainId}": numerical value greater than max safe value.`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-12-13 20:13:54 +01:00
|
|
|
this._setProviderConfig({
|
2022-09-14 16:55:31 +02:00
|
|
|
type: NETWORK_TYPES.RPC,
|
2020-10-06 19:57:02 +02:00
|
|
|
rpcUrl,
|
2018-10-26 10:26:43 +02:00
|
|
|
chainId,
|
|
|
|
ticker,
|
|
|
|
nickname,
|
2019-05-09 19:27:14 +02:00
|
|
|
rpcPrefs,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-01 02:59:53 +02:00
|
|
|
}
|
2017-12-22 19:43:02 +01:00
|
|
|
|
2022-12-13 18:27:48 +01:00
|
|
|
setProviderType(type) {
|
2021-01-21 00:37:18 +01:00
|
|
|
assert.notStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
type,
|
2022-09-14 16:55:31 +02:00
|
|
|
NETWORK_TYPES.RPC,
|
|
|
|
`NetworkController - cannot call "setProviderType" with type "${NETWORK_TYPES.RPC}". Use "setRpcTarget"`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-01-21 00:37:18 +01:00
|
|
|
assert.ok(
|
2020-11-03 00:41:28 +01:00
|
|
|
INFURA_PROVIDER_TYPES.includes(type),
|
2021-01-21 00:37:18 +01:00
|
|
|
`Unknown Infura provider type "${type}".`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-09-14 16:55:31 +02:00
|
|
|
const { chainId, ticker } = BUILT_IN_NETWORKS[type];
|
2022-12-13 20:13:54 +01:00
|
|
|
this._setProviderConfig({
|
2021-06-10 18:24:18 +02:00
|
|
|
type,
|
|
|
|
rpcUrl: '',
|
|
|
|
chainId,
|
2022-04-21 21:09:41 +02:00
|
|
|
ticker: ticker ?? 'ETH',
|
2021-06-10 18:24:18 +02:00
|
|
|
nickname: '',
|
|
|
|
});
|
2018-06-07 01:46:39 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
resetConnection() {
|
2023-02-22 18:43:37 +01:00
|
|
|
this._setProviderConfig(this.providerStore.getState());
|
2021-01-07 00:31:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rollbackToPreviousProvider() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const config = this.previousProviderStore.getState();
|
|
|
|
this.providerStore.updateState(config);
|
|
|
|
this._switchNetwork(config);
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 01:09:38 +02:00
|
|
|
//
|
|
|
|
// Private
|
|
|
|
//
|
|
|
|
|
2022-12-16 17:33:22 +01:00
|
|
|
/**
|
|
|
|
* Get the network ID for the current selected network
|
|
|
|
*
|
|
|
|
* @returns {string} The network ID for the current network.
|
|
|
|
*/
|
|
|
|
async _getNetworkId() {
|
|
|
|
const ethQuery = new EthQuery(this._provider);
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
ethQuery.sendAsync({ method: 'net_version' }, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-13 20:13:54 +01:00
|
|
|
/**
|
|
|
|
* Method to return the latest block for the current network
|
|
|
|
*
|
|
|
|
* @returns {object} Block header
|
|
|
|
*/
|
|
|
|
_getLatestBlock() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const { provider } = this.getProviderAndBlockTracker();
|
|
|
|
const ethQuery = new EthQuery(provider);
|
|
|
|
ethQuery.sendAsync(
|
|
|
|
{ method: 'eth_getBlockByNumber', params: ['latest', false] },
|
|
|
|
(err, block) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(block);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_setNetworkState(network) {
|
|
|
|
this.networkStore.putState(network);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set EIP support indication in the networkDetails store
|
|
|
|
*
|
|
|
|
* @param {number} EIPNumber - The number of the EIP to mark support for
|
|
|
|
* @param {boolean} isSupported - True if the EIP is supported
|
|
|
|
*/
|
|
|
|
_setNetworkEIPSupport(EIPNumber, isSupported) {
|
|
|
|
this.networkDetails.updateState({
|
|
|
|
EIPS: {
|
|
|
|
[EIPNumber]: isSupported,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset EIP support to default (no support)
|
|
|
|
*/
|
|
|
|
_clearNetworkDetails() {
|
|
|
|
this.networkDetails.putState({ ...defaultNetworkDetailsState });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the provider config and switches the network.
|
|
|
|
*
|
|
|
|
* @param config
|
|
|
|
*/
|
|
|
|
_setProviderConfig(config) {
|
2023-02-22 18:43:37 +01:00
|
|
|
this.previousProviderStore.updateState(this.providerStore.getState());
|
2022-12-13 20:13:54 +01:00
|
|
|
this.providerStore.updateState(config);
|
|
|
|
this._switchNetwork(config);
|
|
|
|
}
|
|
|
|
|
2021-04-15 19:41:40 +02:00
|
|
|
async _checkInfuraAvailability(network) {
|
|
|
|
const rpcUrl = `https://${network}.infura.io/v3/${this._infuraProjectId}`;
|
|
|
|
|
|
|
|
let networkChanged = false;
|
|
|
|
this.once(NETWORK_EVENTS.NETWORK_DID_CHANGE, () => {
|
|
|
|
networkChanged = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetchWithTimeout(rpcUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
method: 'eth_blockNumber',
|
|
|
|
params: [],
|
|
|
|
id: 1,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (networkChanged) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
this.emit(NETWORK_EVENTS.INFURA_IS_UNBLOCKED);
|
|
|
|
} else {
|
|
|
|
const responseMessage = await response.json();
|
|
|
|
if (networkChanged) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (responseMessage.error === INFURA_BLOCKED_KEY) {
|
|
|
|
this.emit(NETWORK_EVENTS.INFURA_IS_BLOCKED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
log.warn(`MetaMask - Infura availability check failed`, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_switchNetwork(opts) {
|
2021-06-25 18:24:00 +02:00
|
|
|
// Indicate to subscribers that network is about to change
|
2021-02-09 00:22:30 +01:00
|
|
|
this.emit(NETWORK_EVENTS.NETWORK_WILL_CHANGE);
|
2021-06-25 18:24:00 +02:00
|
|
|
// Set loading state
|
2022-12-13 20:13:54 +01:00
|
|
|
this._setNetworkState('loading');
|
2021-06-25 18:24:00 +02:00
|
|
|
// Reset network details
|
2022-12-13 20:13:54 +01:00
|
|
|
this._clearNetworkDetails();
|
2021-06-25 18:24:00 +02:00
|
|
|
// Configure the provider appropriately
|
2021-02-04 19:15:23 +01:00
|
|
|
this._configureProvider(opts);
|
2021-06-25 18:24:00 +02:00
|
|
|
// Notify subscribers that network has changed
|
2021-02-09 00:22:30 +01:00
|
|
|
this.emit(NETWORK_EVENTS.NETWORK_DID_CHANGE, opts.type);
|
2017-09-30 01:09:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_configureProvider({ type, rpcUrl, chainId }) {
|
2018-05-01 02:59:53 +02:00
|
|
|
// infura type-based endpoints
|
2021-02-04 19:15:23 +01:00
|
|
|
const isInfura = INFURA_PROVIDER_TYPES.includes(type);
|
2018-05-01 02:59:53 +02:00
|
|
|
if (isInfura) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._configureInfuraProvider(type, this._infuraProjectId);
|
2020-11-03 00:41:28 +01:00
|
|
|
// url-based rpc endpoints
|
2022-09-14 16:55:31 +02:00
|
|
|
} else if (type === NETWORK_TYPES.RPC) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._configureStandardProvider(rpcUrl, chainId);
|
2017-12-14 03:57:27 +01:00
|
|
|
} else {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`NetworkController - _configureProvider - unknown type "${type}"`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-12-14 03:57:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_configureInfuraProvider(type, projectId) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.info('NetworkController - configureInfuraProvider', type);
|
2019-09-23 11:54:41 +02:00
|
|
|
const networkClient = createInfuraClient({
|
|
|
|
network: type,
|
2020-09-10 18:16:00 +02:00
|
|
|
projectId,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
this._setNetworkClient(networkClient);
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_configureStandardProvider(rpcUrl, chainId) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.info('NetworkController - configureStandardProvider', rpcUrl);
|
|
|
|
const networkClient = createJsonRpcClient({ rpcUrl, chainId });
|
|
|
|
this._setNetworkClient(networkClient);
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_setNetworkClient({ networkMiddleware, blockTracker }) {
|
2022-12-20 18:28:09 +01:00
|
|
|
const networkProvider = providerFromMiddleware(networkMiddleware);
|
|
|
|
const filterMiddleware = createFilterMiddleware({
|
|
|
|
provider: networkProvider,
|
|
|
|
blockTracker,
|
|
|
|
});
|
|
|
|
const subscriptionManager = createSubscriptionManager({
|
|
|
|
provider: networkProvider,
|
|
|
|
blockTracker,
|
|
|
|
});
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const engine = new JsonRpcEngine();
|
2022-12-20 18:28:09 +01:00
|
|
|
subscriptionManager.events.on('notification', (message) =>
|
|
|
|
engine.emit('notification', message),
|
|
|
|
);
|
|
|
|
engine.push(filterMiddleware);
|
|
|
|
engine.push(subscriptionManager.middleware);
|
2021-02-04 19:15:23 +01:00
|
|
|
engine.push(networkMiddleware);
|
2022-12-20 18:28:09 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const provider = providerFromEngine(engine);
|
2022-12-20 18:28:09 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this._setProviderAndBlockTracker({ provider, blockTracker });
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_setProviderAndBlockTracker({ provider, blockTracker }) {
|
2021-11-16 15:10:49 +01:00
|
|
|
// update or initialize proxies
|
2018-05-17 05:46:34 +02:00
|
|
|
if (this._providerProxy) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._providerProxy.setTarget(provider);
|
2018-05-17 05:46:34 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._providerProxy = createSwappableProxy(provider);
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
|
|
|
if (this._blockTrackerProxy) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this._blockTrackerProxy.setTarget(blockTracker);
|
2018-05-17 05:46:34 +02:00
|
|
|
} else {
|
2020-11-03 00:41:28 +01:00
|
|
|
this._blockTrackerProxy = createEventEmitterProxy(blockTracker, {
|
|
|
|
eventFilter: 'skipInternal',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2017-09-27 23:44:54 +02:00
|
|
|
}
|
2018-05-17 05:46:34 +02:00
|
|
|
// set new provider and blockTracker
|
2021-02-04 19:15:23 +01:00
|
|
|
this._provider = provider;
|
|
|
|
this._blockTracker = blockTracker;
|
2017-09-27 23:44:54 +02:00
|
|
|
}
|
2017-05-18 23:54:02 +02:00
|
|
|
}
|