Skip to content

Commit

Permalink
Remove providerConfig from NetworkController
Browse files Browse the repository at this point in the history
Historically, the `providerConfig` property in NetworkController has
been used to track the currently selected network as well as provide
access to information about that network. The selected network is now
tracked via `selectedNetworkClientId`, and information about that
network can be retrieved by looking at the `networkConfigurations`
property or the `configuration` property on the NetworkClient interface.
This means that we no longer need `providerConfig` and we can remove
this redundant state.
  • Loading branch information
mcmire committed May 6, 2024
1 parent 9687bfc commit 730b264
Show file tree
Hide file tree
Showing 20 changed files with 3,090 additions and 4,443 deletions.
6 changes: 3 additions & 3 deletions packages/assets-controllers/src/AccountTrackerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { Provider } from '@metamask/eth-query';
import type {
NetworkClientId,
NetworkController,
NetworkState,
} from '@metamask/network-controller';
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import type { Hex } from '@metamask/utils';
import { assert } from '@metamask/utils';
import { Mutex } from 'async-mutex';
import { cloneDeep } from 'lodash';
Expand Down Expand Up @@ -120,7 +120,7 @@ export class AccountTrackerController extends StaticIntervalPollingControllerV1<

private readonly getMultiAccountBalancesEnabled: () => PreferencesState['isMultiAccountBalancesEnabled'];

private readonly getCurrentChainId: () => NetworkState['providerConfig']['chainId'];
private readonly getCurrentChainId: () => Hex;

private readonly getNetworkClientById: NetworkController['getNetworkClientById'];

Expand Down Expand Up @@ -152,7 +152,7 @@ export class AccountTrackerController extends StaticIntervalPollingControllerV1<
getIdentities: () => PreferencesState['identities'];
getSelectedAddress: () => PreferencesState['selectedAddress'];
getMultiAccountBalancesEnabled: () => PreferencesState['isMultiAccountBalancesEnabled'];
getCurrentChainId: () => NetworkState['providerConfig']['chainId'];
getCurrentChainId: () => Hex;
getNetworkClientById: NetworkController['getNetworkClientById'];
},
config?: Partial<AccountTrackerConfig>,
Expand Down
11 changes: 8 additions & 3 deletions packages/assets-controllers/src/AssetsContractController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,15 @@ export class AssetsContractController extends BaseControllerV1<
this.configure({ ipfsGateway });
});

onNetworkDidChange((networkState) => {
if (this.config.chainId !== networkState.providerConfig.chainId) {
onNetworkDidChange(({ selectedNetworkClientId }) => {
const selectedNetworkClient = getNetworkClientById(
selectedNetworkClientId,
);
const { chainId } = selectedNetworkClient.configuration;

if (this.config.chainId !== chainId) {
this.configure({
chainId: networkState.providerConfig.chainId,
chainId: selectedNetworkClient.configuration.chainId,
});
}
});
Expand Down
Loading

0 comments on commit 730b264

Please sign in to comment.