Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 300
Remove networkId from NetworkController#1633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
2719a27b035596d5825fb1174d1226747ce2d9cbcd6598c9166a422e5be731af8c301ec91fe91c6d1dc543f7cab3dbf14f3700f96aaa8db3cc5c02e986225b258047d1603ef1c840766a8d97a1f71e42f4fe83543c9bba55c5f996e5d271eda5016d98ff807156f3869c9148f3ce582486b0c3db8e954a7458d1a0d52dda76bc0ab3d152038ba585a0c8f0b5File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -119,7 +119,6 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: '1', | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| type: NetworkType.mainnet, | ||
| @@ -445,7 +444,6 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: null, | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| type: NetworkType.mainnet, | ||
| @@ -464,9 +462,8 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: '1544', | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| chainId: toHex(0), | ||
adonesky1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| type: NetworkType.mainnet, | ||
| ticker: NetworksTicker.mainnet, | ||
| }, | ||
| @@ -490,7 +487,6 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: '1', | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| type: NetworkType.mainnet, | ||
| @@ -514,7 +510,6 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: '1', | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| type: NetworkType.mainnet, | ||
| @@ -537,7 +532,6 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: '1', | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| type: NetworkType.mainnet, | ||
| @@ -563,7 +557,6 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: '1', | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| type: NetworkType.mainnet, | ||
| @@ -589,7 +582,6 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: '1', | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| type: NetworkType.mainnet, | ||
| @@ -617,7 +609,6 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: '1', | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| type: NetworkType.mainnet, | ||
| @@ -644,7 +635,6 @@ describe('EnsController', () => { | ||
| provider: getProvider(), | ||
| onNetworkStateChange: (listener) => { | ||
| listener({ | ||
| networkId: '1', | ||
| providerConfig: { | ||
| chainId: toHex(1), | ||
| type: NetworkType.mainnet, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,36 +5,22 @@ import type { | ||
| import { Web3Provider } from '@ethersproject/providers'; | ||
| import type { RestrictedControllerMessenger } from '@metamask/base-controller'; | ||
| import { BaseControllerV2 } from '@metamask/base-controller'; | ||
| import type { ChainId } from '@metamask/controller-utils'; | ||
| import { | ||
| normalizeEnsName, | ||
| isValidHexAddress, | ||
| toChecksumHexAddress, | ||
| NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP, | ||
| CHAIN_ID_TO_ETHERS_NETWORK_NAME_MAP, | ||
| convertHexToDecimal, | ||
| } from '@metamask/controller-utils'; | ||
| import type { NetworkState } from '@metamask/network-controller'; | ||
| import type { Hex } from '@metamask/utils'; | ||
| import { createProjectLogger, hasProperty } from '@metamask/utils'; | ||
| import { createProjectLogger } from '@metamask/utils'; | ||
| import ensNetworkMap from 'ethereum-ens-network-map'; | ||
| import { toASCII } from 'punycode/'; | ||
| const log = createProjectLogger('ens-controller'); | ||
| /** | ||
| * Checks whether the given string is a known network ID. | ||
| * | ||
| * @param networkId - Network id. | ||
| * @returns Boolean indicating if the network ID is recognized. | ||
| */ | ||
| function isKnownNetworkId( | ||
| networkId: string | null, | ||
| ): networkId is keyof typeof NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP { | ||
| return ( | ||
| networkId !== null && | ||
| hasProperty(NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP, networkId) | ||
| ); | ||
| } | ||
| const name = 'EnsController'; | ||
| /** | ||
| @@ -118,9 +104,7 @@ export class EnsController extends BaseControllerV2< | ||
| state?: Partial<EnsControllerState>; | ||
| provider?: ExternalProvider | JsonRpcFetchFunc; | ||
| onNetworkStateChange?: ( | ||
| listener: ( | ||
| networkState: Pick<NetworkState, 'networkId' | 'providerConfig'>, | ||
| ) => void, | ||
| listener: (networkState: Pick<NetworkState, 'providerConfig'>) => void, | ||
| ) => void; | ||
| }) { | ||
| super({ | ||
| @@ -136,15 +120,14 @@ export class EnsController extends BaseControllerV2< | ||
| if (provider && onNetworkStateChange) { | ||
| onNetworkStateChange((networkState) => { | ||
| this.resetState(); | ||
| const currentNetwork = networkState.networkId; | ||
| if ( | ||
| isKnownNetworkId(currentNetwork) && | ||
| this.#getNetworkEnsSupport(currentNetwork) | ||
| ) { | ||
| const currentChainId = networkState.providerConfig.chainId; | ||
| if (this.#getChainEnsSupport(currentChainId)) { | ||
| this.#ethProvider = new Web3Provider(provider, { | ||
| chainId: convertHexToDecimal(networkState.providerConfig.chainId), | ||
| name: NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP[currentNetwork], | ||
| ensAddress: ensNetworkMap[currentNetwork], | ||
| chainId: convertHexToDecimal(currentChainId), | ||
| name: CHAIN_ID_TO_ETHERS_NETWORK_NAME_MAP[ | ||
| currentChainId as ChainId | ||
| ], | ||
| ensAddress: ensNetworkMap[parseInt(currentChainId, 16)], | ||
| }); | ||
| } else { | ||
| this.#ethProvider = null; | ||
| @@ -269,13 +252,13 @@ export class EnsController extends BaseControllerV2< | ||
| } | ||
| /** | ||
| * Check if network supports ENS. | ||
| * Check if the chain supports ENS. | ||
| * | ||
| * @param networkId - Network id. | ||
| * @returns Boolean indicating if the network supports ENS. | ||
| * @param chainId - chain id. | ||
| * @returns Boolean indicating if the chain supports ENS. | ||
| */ | ||
| #getNetworkEnsSupport(networkId: string) { | ||
| return Boolean(ensNetworkMap[networkId]); | ||
| #getChainEnsSupport(chainId: string) { | ||
| return Boolean(ensNetworkMap[parseInt(chainId, 16)]); | ||
jiexi marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /** | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.