Skip to content

Commit

Permalink
Make ethProvider private
Browse files Browse the repository at this point in the history
  • Loading branch information
Salah-eddineS committed Apr 20, 2023
1 parent da085eb commit f76e0d5
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 101 deletions.
15 changes: 9 additions & 6 deletions packages/controller-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
NetworkType,
NetworksTicker,
NetworksChainId,
NetworkIds,
NetworkId,
} from './types';

export const RPC = 'rpc';
Expand Down Expand Up @@ -116,8 +116,11 @@ export enum ApprovalType {
ConnectAccounts = 'connect_accounts',
}

export const NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP = {
[NetworkIds.goerli]: NetworkType.goerli,
[NetworkIds.sepolia]: NetworkType.sepolia,
[NetworkIds.mainnet]: NetworkType.mainnet,
} as const;
export const NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP: Record<
NetworkId,
NetworkType
> = {
[NetworkId.goerli]: NetworkType.goerli,
[NetworkId.sepolia]: NetworkType.sepolia,
[NetworkId.mainnet]: NetworkType.mainnet,
};
11 changes: 5 additions & 6 deletions packages/controller-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ export enum NetworksChainId {
rpc = '',
}

export const NetworkIds = {
mainnet: '1',
goerli: '5',
localhost: '1337',
sepolia: '11155111',
} as const;
export enum NetworkId {
mainnet = '1',
goerli = '5',
sepolia = '11155111',
}

export enum NetworksTicker {
mainnet = 'ETH',
Expand Down
1 change: 1 addition & 0 deletions packages/ens-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@ethersproject/providers": "^5.7.0",
"@metamask/base-controller": "workspace:^",
"@metamask/controller-utils": "workspace:^",
"@metamask/utils": "^3.3.1",
"ethereum-ens-network-map": "^1.0.2",
"punycode": "^2.1.1"
},
Expand Down
112 changes: 34 additions & 78 deletions packages/ens-controller/src/EnsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,84 +58,6 @@ describe('EnsController', () => {
});
});

it('should construct the controller given a provider, a network and a onNetworkStateChange', async function () {
const messenger = getMessenger();
const ens = new EnsController({
messenger,
provider: getProvider(),
onNetworkStateChange: (listener) => {
listener({
network: '1',
providerConfig: {
chainId: '1',
},
});
},
});

expect(ens.ethProvider).toBeDefined();
});

it('should return a null eth provider when not given onNetworkStateChange', async function () {
const messenger = getMessenger();
const ens = new EnsController({
messenger,
provider: getProvider(),
});
expect(ens.ethProvider).toBeNull();
});

it('should return a null eth provider when not given a provider', async function () {
const messenger = getMessenger();
const ens = new EnsController({
messenger,

onNetworkStateChange: (listener) => {
listener({
network: '1',
providerConfig: {
chainId: '1',
},
});
},
});
expect(ens.ethProvider).toBeNull();
});

it('should return a null eth provider when network is loading', async function () {
const messenger = getMessenger();
const ens = new EnsController({
messenger,
provider: getProvider(),
onNetworkStateChange: (listener) => {
listener({
network: 'loading',
providerConfig: {
chainId: '1',
},
});
},
});
expect(ens.ethProvider).toBeNull();
});

it('should return a null eth provider when network is not ens supported', async function () {
const messenger = getMessenger();
const ens = new EnsController({
messenger,
provider: getProvider(),
onNetworkStateChange: (listener) => {
listener({
network: '1544',
providerConfig: {
chainId: '1',
},
});
},
});
expect(ens.ethProvider).toBeNull();
});

it('should add a new ENS entry and return true', () => {
const messenger = getMessenger();
const controller = new EnsController({
Expand Down Expand Up @@ -507,6 +429,40 @@ describe('EnsController', () => {
expect(await ens.reverseResolveAddress(address1)).toBeUndefined();
});

it('should return undefined when network is loading', async function () {
const messenger = getMessenger();
const ens = new EnsController({
messenger,
provider: getProvider(),
onNetworkStateChange: (listener) => {
listener({
network: 'loading',
providerConfig: {
chainId: '1',
},
});
},
});
expect(await ens.reverseResolveAddress(address1)).toBeUndefined();
});

it('should return undefined when network is not ens supported', async function () {
const messenger = getMessenger();
const ens = new EnsController({
messenger,
provider: getProvider(),
onNetworkStateChange: (listener) => {
listener({
network: '1544',
providerConfig: {
chainId: '1',
},
});
},
});
expect(await ens.reverseResolveAddress(address1)).toBeUndefined();
});

it('should only resolve an ENS name once', async () => {
const messenger = getMessenger();
const ethProvider = new providersModule.Web3Provider(getProvider());
Expand Down
23 changes: 12 additions & 11 deletions packages/ens-controller/src/EnsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ExternalProvider,
JsonRpcFetchFunc,
} from '@ethersproject/providers';
import { createProjectLogger } from '@metamask/utils';
import {
normalizeEnsName,
isValidHexAddress,
Expand All @@ -18,6 +19,8 @@ import {
import { toASCII } from 'punycode/';
import ensNetworkMap from 'ethereum-ens-network-map';

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

/**
* Checks whether the given string is a known network ID.
*
Expand Down Expand Up @@ -94,7 +97,7 @@ export class EnsController extends BaseControllerV2<
EnsControllerState,
EnsControllerMessenger
> {
ethProvider: Web3Provider | null = null;
#ethProvider: Web3Provider | null = null;

/**
* Creates an EnsController instance.
Expand Down Expand Up @@ -136,22 +139,20 @@ export class EnsController extends BaseControllerV2<

if (provider && onNetworkStateChange) {
onNetworkStateChange((networkState) => {
this.update((currentState) => {
currentState.ensResolutionsByAddress = {};
});
this.resetState();
const currentNetwork =
networkState.network === 'loading' ? null : networkState.network;
if (
isKnownNetworkId(currentNetwork) &&
this.#getNetworkEnsSupport(currentNetwork)
) {
this.ethProvider = new Web3Provider(provider, {
this.#ethProvider = new Web3Provider(provider, {
chainId: convertHexToDecimal(networkState.providerConfig.chainId),
name: NETWORK_ID_TO_ETHERS_NETWORK_NAME_MAP[currentNetwork],
ensAddress: ensNetworkMap[currentNetwork],
});
} else {
this.ethProvider = null;
this.#ethProvider = null;
}
});
}
Expand Down Expand Up @@ -289,7 +290,7 @@ export class EnsController extends BaseControllerV2<
* @returns ens resolution
*/
async reverseResolveAddress(nonChecksummedAddress: string) {
if (!this.ethProvider) {
if (!this.#ethProvider) {
return undefined;
}

Expand All @@ -300,9 +301,9 @@ export class EnsController extends BaseControllerV2<

let domain: string | null;
try {
domain = await this.ethProvider.lookupAddress(address);
domain = await this.#ethProvider.lookupAddress(address);
} catch (error) {
console.debug(error);
log(error);
return undefined;
}

Expand All @@ -312,9 +313,9 @@ export class EnsController extends BaseControllerV2<

let registeredAddress: string | null;
try {
registeredAddress = await this.ethProvider.resolveName(domain);
registeredAddress = await this.#ethProvider.resolveName(domain);
} catch (error) {
console.debug(error);
log(error);
return undefined;
}

Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,7 @@ __metadata:
"@metamask/auto-changelog": ^3.1.0
"@metamask/base-controller": "workspace:^"
"@metamask/controller-utils": "workspace:^"
"@metamask/utils": ^3.3.1
"@types/jest": ^26.0.22
deepmerge: ^4.2.2
ethereum-ens-network-map: ^1.0.2
Expand Down Expand Up @@ -1837,6 +1838,18 @@ __metadata:
languageName: node
linkType: hard

"@metamask/utils@npm:^3.3.1":
version: 3.6.0
resolution: "@metamask/utils@npm:3.6.0"
dependencies:
"@types/debug": ^4.1.7
debug: ^4.3.4
semver: ^7.3.8
superstruct: ^1.0.3
checksum: 1ebc6677bb017e4d09d4af143621fe27194d8ed815234cfd76469c3c734dc1db2ea7b577c01a2096c21c04d8c9c4d721d3035b5353fe2ded3b4737f326755e43
languageName: node
linkType: hard

"@metamask/utils@npm:^5.0.1":
version: 5.0.1
resolution: "@metamask/utils@npm:5.0.1"
Expand Down

0 comments on commit f76e0d5

Please sign in to comment.