1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00

Make initializeProvider and lookupNetwork async (#16881)

* Make `initializeProvider` and `lookupNetwork` async

These two methods were "synchronous" previously, but initiated an
asynchronous operation. They have both been made `async` to bring them
more in-line with the mobile controller API, and to make them easier
to test.

This should include zero functional changes. These methods are still
being invoked without an `await`, to preserve the same behaviour they
had previously.

This relates to https://github.com/MetaMask/controllers/issues/971

* Move 'net_version' query to private function

* Fix error made when resolving conflicts

* Refactor to improve readability
This commit is contained in:
Mark Stacey 2022-12-16 13:03:22 -03:30 committed by GitHub
parent d2f8083d64
commit dbe77289d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -122,11 +122,11 @@ export default class NetworkController extends EventEmitter {
this.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, this.lookupNetwork); this.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, this.lookupNetwork);
} }
initializeProvider(providerParams) { async initializeProvider(providerParams) {
this._baseProviderParams = providerParams; this._baseProviderParams = providerParams;
const { type, rpcUrl, chainId } = this.getProviderConfig(); const { type, rpcUrl, chainId } = this.getProviderConfig();
this._configureProvider({ type, rpcUrl, chainId }); this._configureProvider({ type, rpcUrl, chainId });
this.lookupNetwork(); await this.lookupNetwork();
} }
// return the proxies so the references will always be good // return the proxies so the references will always be good
@ -162,7 +162,7 @@ export default class NetworkController extends EventEmitter {
return this.getNetworkState() === 'loading'; return this.getNetworkState() === 'loading';
} }
lookupNetwork() { async lookupNetwork() {
// Prevent firing when provider is not defined. // Prevent firing when provider is not defined.
if (!this._provider) { if (!this._provider) {
log.warn( log.warn(
@ -183,7 +183,6 @@ export default class NetworkController extends EventEmitter {
} }
// Ping the RPC endpoint so we can confirm that it works // Ping the RPC endpoint so we can confirm that it works
const ethQuery = new EthQuery(this._provider);
const initialNetwork = this.getNetworkState(); const initialNetwork = this.getNetworkState();
const { type } = this.getProviderConfig(); const { type } = this.getProviderConfig();
const isInfura = INFURA_PROVIDER_TYPES.includes(type); const isInfura = INFURA_PROVIDER_TYPES.includes(type);
@ -194,21 +193,26 @@ export default class NetworkController extends EventEmitter {
this.emit(NETWORK_EVENTS.INFURA_IS_UNBLOCKED); this.emit(NETWORK_EVENTS.INFURA_IS_UNBLOCKED);
} }
ethQuery.sendAsync({ method: 'net_version' }, (err, networkVersion) => { let networkVersion;
const currentNetwork = this.getNetworkState(); let networkVersionError;
if (initialNetwork === currentNetwork) { try {
if (err) { networkVersion = await this._getNetworkId();
this._setNetworkState('loading'); } catch (error) {
// keep network details in sync with network state networkVersionError = error;
this._clearNetworkDetails(); }
return; if (initialNetwork !== this.getNetworkState()) {
} return;
}
this._setNetworkState(networkVersion); if (networkVersionError) {
// look up EIP-1559 support this._setNetworkState('loading');
this.getEIP1559Compatibility(); // keep network details in sync with network state
} this._clearNetworkDetails();
}); } else {
this._setNetworkState(networkVersion);
// look up EIP-1559 support
await this.getEIP1559Compatibility();
}
} }
getCurrentChainId() { getCurrentChainId() {
@ -285,6 +289,24 @@ export default class NetworkController extends EventEmitter {
// Private // Private
// //
/**
* 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);
}
});
});
}
/** /**
* Method to return the latest block for the current network * Method to return the latest block for the current network
* *