Skip to content

Commit

Permalink
Replace NetworksChainId constant with ChainId
Browse files Browse the repository at this point in the history
The constant `NetworksChainId` was ambiguous in whether it referred to
network or chain Ids, and it didn't match our enum naming conventions.
It was also undocumented, and unclear in which networks it was intended
to contain.

It has been replaced with a `ChainId` constant with documentation and a
clear scope (built-in networks). A `BuiltInNetworks` enum has been
added as well to make that more clear.

Relates to #1209
  • Loading branch information
Gudahtt committed May 9, 2023
1 parent 9e26a16 commit d4daa70
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 147 deletions.
10 changes: 3 additions & 7 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ERC1155,
OPENSEA_API_URL,
ERC721,
NetworksChainId,
ChainId,
NetworkType,
} from '@metamask/controller-utils';
import { Network } from '@ethersproject/providers';
Expand Down Expand Up @@ -799,15 +799,11 @@ describe('NftController', () => {
changeNetwork(SEPOLIA);

expect(
nftController.state.allNfts[selectedAddress]?.[
NetworksChainId[GOERLI.type]
],
nftController.state.allNfts[selectedAddress]?.[ChainId[GOERLI.type]],
).toBeUndefined();

expect(
nftController.state.allNfts[selectedAddress][
NetworksChainId[SEPOLIA.type]
][0],
nftController.state.allNfts[selectedAddress][ChainId[SEPOLIA.type]][0],
).toStrictEqual({
address: '0x01',
description: 'description',
Expand Down
28 changes: 12 additions & 16 deletions packages/assets-controllers/src/TokenDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NetworkState,
ProviderConfig,
} from '@metamask/network-controller';
import { NetworksChainId, NetworkType } from '@metamask/controller-utils';
import { ChainId, NetworkType } from '@metamask/controller-utils';
import { PreferencesController } from '@metamask/preferences-controller';
import { ControllerMessenger } from '@metamask/base-controller';
import {
Expand Down Expand Up @@ -108,7 +108,7 @@ const setupTokenListController = (
});

const tokenList = new TokenListController({
chainId: NetworksChainId.mainnet,
chainId: ChainId.mainnet,
preventPollingOnNetworkRestart: false,
messenger: tokenListMessenger,
});
Expand Down Expand Up @@ -137,21 +137,17 @@ describe('TokenDetectionController', () => {
});
};
const mainnet = {
chainId: NetworksChainId.mainnet,
chainId: ChainId.mainnet,
type: NetworkType.mainnet,
};

beforeEach(async () => {
nock(TOKEN_END_POINT_API)
.get(`/tokens/${NetworksChainId.mainnet}`)
.get(`/tokens/${ChainId.mainnet}`)
.reply(200, sampleTokenList)
.get(
`/token/${NetworksChainId.mainnet}?address=${tokenAFromList.address}`,
)
.get(`/token/${ChainId.mainnet}?address=${tokenAFromList.address}`)
.reply(200, tokenAFromList)
.get(
`/token/${NetworksChainId.mainnet}?address=${tokenBFromList.address}`,
)
.get(`/token/${ChainId.mainnet}?address=${tokenBFromList.address}`)
.reply(200, tokenBFromList)
.persist();

Expand Down Expand Up @@ -211,7 +207,7 @@ describe('TokenDetectionController', () => {
interval: DEFAULT_INTERVAL,
selectedAddress: '',
disabled: true,
chainId: NetworksChainId.mainnet,
chainId: ChainId.mainnet,
isDetectionEnabledForNetwork: true,
isDetectionEnabledFromPreferences: true,
});
Expand Down Expand Up @@ -245,7 +241,7 @@ describe('TokenDetectionController', () => {
expect(
isTokenDetectionSupportedForNetwork(tokenDetection.config.chainId),
).toStrictEqual(true);
tokenDetection.configure({ chainId: NetworksChainId.goerli });
tokenDetection.configure({ chainId: ChainId.goerli });
expect(
isTokenDetectionSupportedForNetwork(tokenDetection.config.chainId),
).toStrictEqual(false);
Expand All @@ -254,7 +250,7 @@ describe('TokenDetectionController', () => {
it('should not autodetect while not on supported networks', async () => {
tokenDetection.configure({
selectedAddress: '0x1',
chainId: NetworksChainId.goerli,
chainId: ChainId.goerli,
isDetectionEnabledForNetwork: false,
});

Expand All @@ -278,7 +274,7 @@ describe('TokenDetectionController', () => {

it('should detect tokens correctly on the Aurora network', async () => {
const auroraMainnet = {
chainId: NetworksChainId.aurora,
chainId: ChainId.aurora,
type: NetworkType.mainnet,
};
preferences.update({ selectedAddress: '0x1' });
Expand Down Expand Up @@ -440,7 +436,7 @@ describe('TokenDetectionController', () => {
isDetectionEnabledForNetwork: true,
isDetectionEnabledFromPreferences: true,
selectedAddress: '0x1',
chainId: NetworksChainId.mainnet,
chainId: ChainId.mainnet,
},
);

Expand Down Expand Up @@ -556,7 +552,7 @@ describe('TokenDetectionController', () => {

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await networkStateChangeListener!({
providerConfig: { chainId: NetworksChainId.mainnet },
providerConfig: { chainId: ChainId.mainnet },
});

expect(getBalancesInSingleCallMock.called).toBe(true);
Expand Down
Loading

0 comments on commit d4daa70

Please sign in to comment.