From ae9b12d80afcd9b254ea2589964187cc5ad2f460 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Wed, 10 May 2023 16:46:12 -0230 Subject: [PATCH] Fix ENS controller network change handler (#1362) The network change handler for the ENS controller has been broken since the PR #1170 due to a conflict with #1196, which was merged around the same time. It referenced the `network` property of the network state that we have removed. The change handler has been updated to use `networkId` instead. Additionally, the `NetworkState` type has been imported so that we're less likely to make this mistake again. --- packages/ens-controller/package.json | 4 +++ .../ens-controller/src/EnsController.test.ts | 32 ++++++++++++------- packages/ens-controller/src/EnsController.ts | 13 +++----- packages/ens-controller/tsconfig.build.json | 3 +- packages/ens-controller/tsconfig.json | 3 +- yarn.lock | 3 ++ 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/packages/ens-controller/package.json b/packages/ens-controller/package.json index 8640282faf3..e19494ffa94 100644 --- a/packages/ens-controller/package.json +++ b/packages/ens-controller/package.json @@ -32,6 +32,7 @@ "@ethersproject/providers": "^5.7.0", "@metamask/base-controller": "workspace:^", "@metamask/controller-utils": "workspace:^", + "@metamask/network-controller": "workspace:^", "@metamask/utils": "^5.0.2", "ethereum-ens-network-map": "^1.0.2", "punycode": "^2.1.1" @@ -46,6 +47,9 @@ "typedoc-plugin-missing-exports": "^0.22.6", "typescript": "~4.6.3" }, + "peerDependencies": { + "@metamask/network-controller": "workspace:^" + }, "engines": { "node": ">=16.0.0" }, diff --git a/packages/ens-controller/src/EnsController.test.ts b/packages/ens-controller/src/EnsController.test.ts index 31331794e83..c1cb6616ec4 100644 --- a/packages/ens-controller/src/EnsController.test.ts +++ b/packages/ens-controller/src/EnsController.test.ts @@ -1,5 +1,5 @@ import { ControllerMessenger } from '@metamask/base-controller'; -import { toChecksumHexAddress } from '@metamask/controller-utils'; +import { NetworkType, toChecksumHexAddress } from '@metamask/controller-utils'; import * as providersModule from '@ethersproject/providers'; import { EnsController } from './EnsController'; @@ -113,9 +113,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: '1', + networkId: '1', providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, @@ -436,9 +437,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: 'loading', + networkId: null, providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, @@ -453,9 +455,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: '1544', + networkId: '1544', providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, @@ -477,9 +480,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: '1', + networkId: '1', providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, @@ -503,9 +507,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: '1', + networkId: '1', providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, @@ -524,9 +529,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: '1', + networkId: '1', providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, @@ -548,9 +554,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: '1', + networkId: '1', providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, @@ -572,9 +579,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: '1', + networkId: '1', providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, @@ -598,9 +606,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: '1', + networkId: '1', providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, @@ -623,9 +632,10 @@ describe('EnsController', () => { provider: getProvider(), onNetworkStateChange: (listener) => { listener({ - network: '1', + networkId: '1', providerConfig: { chainId: '1', + type: NetworkType.mainnet, }, }); }, diff --git a/packages/ens-controller/src/EnsController.ts b/packages/ens-controller/src/EnsController.ts index da4d9b768c9..e8b3a2f3ed9 100644 --- a/packages/ens-controller/src/EnsController.ts +++ b/packages/ens-controller/src/EnsController.ts @@ -15,6 +15,7 @@ import { NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP, convertHexToDecimal, } from '@metamask/controller-utils'; +import type { NetworkState } from '@metamask/network-controller'; import { toASCII } from 'punycode/'; import ensNetworkMap from 'ethereum-ens-network-map'; @@ -118,12 +119,9 @@ export class EnsController extends BaseControllerV2< state?: Partial; provider?: ExternalProvider | JsonRpcFetchFunc; onNetworkStateChange?: ( - listener: (networkState: { - network: string; - providerConfig: { - chainId: string; - }; - }) => void, + listener: ( + networkState: Pick, + ) => void, ) => void; }) { super({ @@ -139,8 +137,7 @@ export class EnsController extends BaseControllerV2< if (provider && onNetworkStateChange) { onNetworkStateChange((networkState) => { this.resetState(); - const currentNetwork = - networkState.network === 'loading' ? null : networkState.network; + const currentNetwork = networkState.networkId; if ( isKnownNetworkId(currentNetwork) && this.#getNetworkEnsSupport(currentNetwork) diff --git a/packages/ens-controller/tsconfig.build.json b/packages/ens-controller/tsconfig.build.json index bbfe057a207..ac0df4920c6 100644 --- a/packages/ens-controller/tsconfig.build.json +++ b/packages/ens-controller/tsconfig.build.json @@ -7,7 +7,8 @@ }, "references": [ { "path": "../base-controller/tsconfig.build.json" }, - { "path": "../controller-utils/tsconfig.build.json" } + { "path": "../controller-utils/tsconfig.build.json" }, + { "path": "../network-controller/tsconfig.build.json" } ], "include": ["../../types", "./src"] } diff --git a/packages/ens-controller/tsconfig.json b/packages/ens-controller/tsconfig.json index 7ee9852347a..4bbb0be81b1 100644 --- a/packages/ens-controller/tsconfig.json +++ b/packages/ens-controller/tsconfig.json @@ -5,7 +5,8 @@ }, "references": [ { "path": "../base-controller" }, - { "path": "../controller-utils" } + { "path": "../controller-utils" }, + { "path": "../network-controller" } ], "include": ["../../types", "./src"] } diff --git a/yarn.lock b/yarn.lock index cbf09e18116..28c71cc6688 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1499,6 +1499,7 @@ __metadata: "@metamask/auto-changelog": ^3.1.0 "@metamask/base-controller": "workspace:^" "@metamask/controller-utils": "workspace:^" + "@metamask/network-controller": "workspace:^" "@metamask/utils": ^5.0.2 "@types/jest": ^27.4.1 deepmerge: ^4.2.2 @@ -1509,6 +1510,8 @@ __metadata: typedoc: ^0.22.15 typedoc-plugin-missing-exports: ^0.22.6 typescript: ~4.6.3 + peerDependencies: + "@metamask/network-controller": "workspace:^" languageName: unknown linkType: soft