Skip to content

Commit 3d40573

Browse files
jiexiMajorLift
authored andcommitted
Remove networkId from NetworkController (#1633)
## Explanation Wallets shouldn't be directly concerned about the network ID as this is more of a node concept for gossip. What wallets really care about is chain ID as that is the correct value to use to identify a chain, build transactions, etc. Although these two values usually match (ignoring hex/dec formatting), there are [exceptions](https://medium.com/@pedrouid/chainid-vs-networkid-how-do-they-differ-on-ethereum-eec2ed41635b). We want to remove `networkId` from the NetworkController state to encourage the replacement of networkId with chainId in any usage downstream (extension, mobile, etc). It will still be possible to get networkId using the rpc client to make a call to the `net_version` method for cases that truly still rely on it. ## References * Fixes [mmp-1068](MetaMask/MetaMask-planning#1068) ## Changelog ### `@metamask/controller-utils` - **BREAKING**: `NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP` renamed to `CHAIN_ID_TO_ETHERS_NETWORK_NAME_MAP ` and is now a mapping of `Hex` chain ID to `BuiltInNetworkName` - **REMOVED**: `NetworkId` constant and type ### `@metamask/ens-controller` - **CHANGED**: From Network state, uses `providerConfig.chainId` instead of `networkId` to determine ENS compatability ### `@metamask/network-controller` - **REMOVED**: `NetworkId` type - **REMOVED**: From `NetworkState` removed `networkId` field - **CHANGED**: No longer calls `net_version` to determine network status (only relies on `eth_getBlockByNumber` now) - **CHANGED**: For Built-in Infura Networks, `net_version` is no longer locally resolved by the scaffold middleware ### `@metamask/transaction-controller` - **BREAKING**: Uses `chainId` and `txParams.chainId` to determine if a `TransactionMeta` object belongs to the current chain. No longer considers `networkID` - **BREAKING**: `TransactionMeta.chainId` is now a required field - **REMOVED**: From `RemoteTransactionSourceRequest` removed `currentNetworkId` field - **CHANGED**: From `RemoteTransactionSource` updated `isSupportedNetwork()` params to exclude networkId - **DEPRECATED**: `TransactionMeta.networkID` is deprecated and marked readonly
1 parent ff1d0da commit 3d40573

23 files changed

+3501
-6052
lines changed

packages/assets-controllers/src/NftDetectionController.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ export interface ApiNftCreator {
115115
*
116116
* NftDetection configuration
117117
* @property interval - Polling interval used to fetch new token rates
118-
* @property networkType - Network type ID as per net_version
118+
* @property chainId - Current chain ID
119119
* @property selectedAddress - Vault selected address
120-
* @property tokens - List of tokens associated with the active vault
121120
*/
122121
export interface NftDetectionConfig extends BaseConfig {
123122
interval: number;

packages/assets-controllers/src/TokenListController.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ function buildNetworkControllerStateWithProviderConfig(
519519
return {
520520
selectedNetworkClientId,
521521
providerConfig,
522-
networkId: '1',
523522
networksMetadata: {
524523
[selectedNetworkClientId]: {
525524
EIPS: {},

packages/controller-utils/src/constants.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { NetworkType, NetworksTicker, ChainId, NetworkId } from './types';
1+
import {
2+
NetworkType,
3+
NetworksTicker,
4+
ChainId,
5+
BuiltInNetworkName,
6+
} from './types';
27

38
export const RPC = 'rpc';
49
export const FALL_BACK_VS_CURRENCY = 'ETH';
@@ -125,13 +130,14 @@ export enum ApprovalType {
125130
WatchAsset = 'wallet_watchAsset',
126131
}
127132

128-
export const NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP: Record<
129-
NetworkId,
130-
NetworkType
133+
export const CHAIN_ID_TO_ETHERS_NETWORK_NAME_MAP: Record<
134+
ChainId,
135+
BuiltInNetworkName
131136
> = {
132-
[NetworkId.goerli]: NetworkType.goerli,
133-
[NetworkId.sepolia]: NetworkType.sepolia,
134-
[NetworkId.mainnet]: NetworkType.mainnet,
135-
[NetworkId['linea-goerli']]: NetworkType['linea-goerli'],
136-
[NetworkId['linea-mainnet']]: NetworkType['linea-mainnet'],
137+
[ChainId.goerli]: BuiltInNetworkName.Goerli,
138+
[ChainId.sepolia]: BuiltInNetworkName.Sepolia,
139+
[ChainId.mainnet]: BuiltInNetworkName.Mainnet,
140+
[ChainId['linea-goerli']]: BuiltInNetworkName.LineaGoerli,
141+
[ChainId['linea-mainnet']]: BuiltInNetworkName.LineaMainnet,
142+
[ChainId.aurora]: BuiltInNetworkName.Aurora,
137143
};

packages/controller-utils/src/types.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,6 @@ export const ChainId = {
6161
} as const;
6262
export type ChainId = (typeof ChainId)[keyof typeof ChainId];
6363

64-
/**
65-
* Decimal string network IDs of built-in Infura networks, by name.
66-
*/
67-
export const NetworkId = {
68-
[InfuraNetworkType.mainnet]: '1',
69-
[InfuraNetworkType.goerli]: '5',
70-
[InfuraNetworkType.sepolia]: '11155111',
71-
[InfuraNetworkType['linea-goerli']]: '59140',
72-
[InfuraNetworkType['linea-mainnet']]: '59144',
73-
} as const;
74-
export type NetworkId = (typeof NetworkId)[keyof typeof NetworkId];
75-
7664
export enum NetworksTicker {
7765
mainnet = 'ETH',
7866
goerli = 'GoerliETH',

packages/ens-controller/src/EnsController.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ describe('EnsController', () => {
119119
provider: getProvider(),
120120
onNetworkStateChange: (listener) => {
121121
listener({
122-
networkId: '1',
123122
providerConfig: {
124123
chainId: toHex(1),
125124
type: NetworkType.mainnet,
@@ -445,7 +444,6 @@ describe('EnsController', () => {
445444
provider: getProvider(),
446445
onNetworkStateChange: (listener) => {
447446
listener({
448-
networkId: null,
449447
providerConfig: {
450448
chainId: toHex(1),
451449
type: NetworkType.mainnet,
@@ -464,9 +462,8 @@ describe('EnsController', () => {
464462
provider: getProvider(),
465463
onNetworkStateChange: (listener) => {
466464
listener({
467-
networkId: '1544',
468465
providerConfig: {
469-
chainId: toHex(1),
466+
chainId: toHex(0),
470467
type: NetworkType.mainnet,
471468
ticker: NetworksTicker.mainnet,
472469
},
@@ -490,7 +487,6 @@ describe('EnsController', () => {
490487
provider: getProvider(),
491488
onNetworkStateChange: (listener) => {
492489
listener({
493-
networkId: '1',
494490
providerConfig: {
495491
chainId: toHex(1),
496492
type: NetworkType.mainnet,
@@ -514,7 +510,6 @@ describe('EnsController', () => {
514510
provider: getProvider(),
515511
onNetworkStateChange: (listener) => {
516512
listener({
517-
networkId: '1',
518513
providerConfig: {
519514
chainId: toHex(1),
520515
type: NetworkType.mainnet,
@@ -537,7 +532,6 @@ describe('EnsController', () => {
537532
provider: getProvider(),
538533
onNetworkStateChange: (listener) => {
539534
listener({
540-
networkId: '1',
541535
providerConfig: {
542536
chainId: toHex(1),
543537
type: NetworkType.mainnet,
@@ -563,7 +557,6 @@ describe('EnsController', () => {
563557
provider: getProvider(),
564558
onNetworkStateChange: (listener) => {
565559
listener({
566-
networkId: '1',
567560
providerConfig: {
568561
chainId: toHex(1),
569562
type: NetworkType.mainnet,
@@ -589,7 +582,6 @@ describe('EnsController', () => {
589582
provider: getProvider(),
590583
onNetworkStateChange: (listener) => {
591584
listener({
592-
networkId: '1',
593585
providerConfig: {
594586
chainId: toHex(1),
595587
type: NetworkType.mainnet,
@@ -617,7 +609,6 @@ describe('EnsController', () => {
617609
provider: getProvider(),
618610
onNetworkStateChange: (listener) => {
619611
listener({
620-
networkId: '1',
621612
providerConfig: {
622613
chainId: toHex(1),
623614
type: NetworkType.mainnet,
@@ -644,7 +635,6 @@ describe('EnsController', () => {
644635
provider: getProvider(),
645636
onNetworkStateChange: (listener) => {
646637
listener({
647-
networkId: '1',
648638
providerConfig: {
649639
chainId: toHex(1),
650640
type: NetworkType.mainnet,

packages/ens-controller/src/EnsController.ts

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,22 @@ import type {
55
import { Web3Provider } from '@ethersproject/providers';
66
import type { RestrictedControllerMessenger } from '@metamask/base-controller';
77
import { BaseControllerV2 } from '@metamask/base-controller';
8+
import type { ChainId } from '@metamask/controller-utils';
89
import {
910
normalizeEnsName,
1011
isValidHexAddress,
1112
toChecksumHexAddress,
12-
NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP,
13+
CHAIN_ID_TO_ETHERS_NETWORK_NAME_MAP,
1314
convertHexToDecimal,
1415
} from '@metamask/controller-utils';
1516
import type { NetworkState } from '@metamask/network-controller';
1617
import type { Hex } from '@metamask/utils';
17-
import { createProjectLogger, hasProperty } from '@metamask/utils';
18+
import { createProjectLogger } from '@metamask/utils';
1819
import ensNetworkMap from 'ethereum-ens-network-map';
1920
import { toASCII } from 'punycode/';
2021

2122
const log = createProjectLogger('ens-controller');
2223

23-
/**
24-
* Checks whether the given string is a known network ID.
25-
*
26-
* @param networkId - Network id.
27-
* @returns Boolean indicating if the network ID is recognized.
28-
*/
29-
function isKnownNetworkId(
30-
networkId: string | null,
31-
): networkId is keyof typeof NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP {
32-
return (
33-
networkId !== null &&
34-
hasProperty(NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP, networkId)
35-
);
36-
}
37-
3824
const name = 'EnsController';
3925

4026
/**
@@ -118,9 +104,7 @@ export class EnsController extends BaseControllerV2<
118104
state?: Partial<EnsControllerState>;
119105
provider?: ExternalProvider | JsonRpcFetchFunc;
120106
onNetworkStateChange?: (
121-
listener: (
122-
networkState: Pick<NetworkState, 'networkId' | 'providerConfig'>,
123-
) => void,
107+
listener: (networkState: Pick<NetworkState, 'providerConfig'>) => void,
124108
) => void;
125109
}) {
126110
super({
@@ -136,15 +120,14 @@ export class EnsController extends BaseControllerV2<
136120
if (provider && onNetworkStateChange) {
137121
onNetworkStateChange((networkState) => {
138122
this.resetState();
139-
const currentNetwork = networkState.networkId;
140-
if (
141-
isKnownNetworkId(currentNetwork) &&
142-
this.#getNetworkEnsSupport(currentNetwork)
143-
) {
123+
const currentChainId = networkState.providerConfig.chainId;
124+
if (this.#getChainEnsSupport(currentChainId)) {
144125
this.#ethProvider = new Web3Provider(provider, {
145-
chainId: convertHexToDecimal(networkState.providerConfig.chainId),
146-
name: NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP[currentNetwork],
147-
ensAddress: ensNetworkMap[currentNetwork],
126+
chainId: convertHexToDecimal(currentChainId),
127+
name: CHAIN_ID_TO_ETHERS_NETWORK_NAME_MAP[
128+
currentChainId as ChainId
129+
],
130+
ensAddress: ensNetworkMap[parseInt(currentChainId, 16)],
148131
});
149132
} else {
150133
this.#ethProvider = null;
@@ -269,13 +252,13 @@ export class EnsController extends BaseControllerV2<
269252
}
270253

271254
/**
272-
* Check if network supports ENS.
255+
* Check if the chain supports ENS.
273256
*
274-
* @param networkId - Network id.
275-
* @returns Boolean indicating if the network supports ENS.
257+
* @param chainId - chain id.
258+
* @returns Boolean indicating if the chain supports ENS.
276259
*/
277-
#getNetworkEnsSupport(networkId: string) {
278-
return Boolean(ensNetworkMap[networkId]);
260+
#getChainEnsSupport(chainId: string) {
261+
return Boolean(ensNetworkMap[parseInt(chainId, 16)]);
279262
}
280263

281264
/**

packages/gas-fee-controller/src/GasFeeController.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ const setupNetworkController = async ({
7777
// Call this without awaiting to simulate what the extension or mobile app
7878
// might do
7979
networkController.initializeProvider();
80-
// Ensure that the request for net_version that the network controller makes
81-
// goes through
80+
// Ensure that the request for eth_getBlockByNumber made by the PollingBlockTracker
81+
// inside the NetworkController goes through
8282
await clock.nextAsync();
8383

8484
return networkController;

0 commit comments

Comments
 (0)