Skip to content
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

Group network configurations by chain #4286

Merged
merged 68 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
5afcc9d
Group network configurations by chain ID
mcmire May 7, 2024
a7facb3
WIP
mcmire Jun 12, 2024
c5f4534
Revert "WIP"
mcmire Jun 12, 2024
648004d
Fix tests
mcmire Jun 13, 2024
f5f5232
Update JSDoc for nativeTokenName
mcmire Jun 13, 2024
c2a4e02
Remove extra empty line
mcmire Jun 13, 2024
cc15c05
Really fix tests
mcmire Jun 13, 2024
89345a5
Drop type assertions for reduce
mcmire Jun 13, 2024
a600e92
Add @link's to @see's
mcmire Jun 13, 2024
fb10024
Fix lint
mcmire Jun 13, 2024
e7ad54b
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jun 13, 2024
0ee2e0a
Fix tests again
mcmire Jun 13, 2024
a86489f
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jun 18, 2024
98d4c2a
Rename nativeTokenName to nativeCurrency
mcmire Jun 18, 2024
467b702
Tweak controller-utils CHANGELOG
mcmire Jun 24, 2024
9d419df
Use the type parameter for `reduce`
mcmire Jun 25, 2024
21115a6
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 1, 2024
00e0282
Export RpcEndpointType
mcmire Jul 1, 2024
4be6a34
Add changelog entry for RpcEndpointType export
mcmire Jul 1, 2024
e2b5c23
Rename defaultRpcEndpointUrl to defaultRpcEndpointIndex
mcmire Jul 2, 2024
f2bcf43
blockExplorerUrl -> blockExplorerUrls; add defaultBlockExplorerUrlIndex
mcmire Jul 2, 2024
1c4ca62
Make 'name' on an RPC endpoint optional
mcmire Jul 2, 2024
28c6556
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 2, 2024
3109357
Adjust JSDoc for nativeCurrency to be more accurate
mcmire Jul 5, 2024
7fa3ee0
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 10, 2024
1093077
Extract validation logic
mcmire Jul 10, 2024
14f44a9
Extract creating network configurations and network clients
mcmire Jul 10, 2024
cb28629
Add ability to specify replacement selected RPC endpoint
mcmire Jul 12, 2024
5ba43a2
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 13, 2024
1d11bf3
Remove TODOs
mcmire Jul 15, 2024
b6d6afb
Remove another TODO
mcmire Jul 15, 2024
e108ea3
Update typo in changelog
mcmire Jul 16, 2024
7d11153
Clarify some other things in the changelog
mcmire Jul 16, 2024
c875acf
Fix lint violations?
mcmire Jul 16, 2024
668ac2e
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 16, 2024
dbcd850
Fix bug with detection of removing selectedNetworkClientId
mcmire Jul 17, 2024
ee06f30
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 17, 2024
8fd0716
Fix tests for SelectedNetworkController
mcmire Jul 17, 2024
a1fe96f
Fix transaction controller tests
mcmire Jul 17, 2024
4331bd9
Ensure we do not test updating selected network configuration in most…
mcmire Jul 19, 2024
607ccd3
No need to sort chain IDs when creating network client registry
mcmire Jul 19, 2024
a5dffe8
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 19, 2024
32634ea
Fix lint violations
mcmire Jul 19, 2024
f4de92d
Fix TokenDetectionController test
mcmire Jul 19, 2024
4f103f5
Export network fields types
mcmire Jul 22, 2024
9f4ba9e
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 22, 2024
7d36993
Fix tests
mcmire Jul 22, 2024
8c8a944
Fix yarn.lock
mcmire Jul 22, 2024
276db03
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 22, 2024
64d2d0d
Update changelog
mcmire Jul 22, 2024
380328b
Remove debugging code
mcmire Jul 23, 2024
f25fb8c
Move errorMessagePrefix into #validateNetworkFields
mcmire Jul 23, 2024
ab27c88
Add TODO for moving Partialize to @metamask/utils
mcmire Jul 23, 2024
e8f1374
Merge branch 'main' into group-network-configurations-by-chain
mcmire Jul 26, 2024
5f1c6ab
Merge branch 'main' into group-network-configurations-by-chain
mikesposito Jul 29, 2024
a97ee13
Merge branch 'main' into group-network-configurations-by-chain
mikesposito Aug 14, 2024
49a6984
fix: ts errors
mikesposito Aug 14, 2024
e998273
Revert "remove eslint-disable"
mikesposito Aug 14, 2024
451515b
fix: lint
mikesposito Aug 14, 2024
500a9f1
Merge branch 'main' into group-network-configurations-by-chain
mikesposito Aug 26, 2024
1a9335f
update selected network controller
bergeron Aug 22, 2024
b59ee0c
Fix https://github.com/MetaMask/core/pull/4286#discussion_r1732085702
bergeron Aug 27, 2024
02e7ff9
fix for `Cannot perform 'get' on a proxy that has been revoked`
bergeron Aug 29, 2024
8aafb34
Merge branch 'main' into group-network-configurations-by-chain
mikesposito Sep 2, 2024
21bb4d4
fix: `SelectedNetworkController` network stateChange listener (#4638)
mikesposito Sep 2, 2024
2fb9309
Merge branch 'main' into group-network-configurations-by-chain
mikesposito Sep 2, 2024
b897c9c
Merge branch 'main' into group-network-configurations-by-chain
mikesposito Sep 3, 2024
b259b01
Merge branch 'main' into group-network-configurations-by-chain
bergeron Sep 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { InternalAccount } from '@metamask/keyring-api';
import {
type NetworkClientId,
type NetworkClientConfiguration,
defaultState as defaultnetworkControllerState,
getDefaultNetworkControllerState,
} from '@metamask/network-controller';
import {
buildCustomNetworkClientConfiguration,
Expand Down Expand Up @@ -618,7 +618,7 @@ async function withController<ReturnValue>(
);

const mockNetworkState = jest.fn().mockReturnValue({
...defaultnetworkControllerState,
...getDefaultNetworkControllerState(),
chainId: initialChainId,
});
messenger.registerActionHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ async function setupAssetContractControllers({
const networkController = new NetworkController({
infuraProjectId,
messenger,
trackMetaMetricsEvent: jest.fn(),
});
if (useNetworkControllerProvider) {
await networkController.initializeProvider();
Expand Down
4 changes: 2 additions & 2 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
NetworkControllerGetNetworkClientByIdAction,
NetworkControllerNetworkDidChangeEvent,
} from '@metamask/network-controller';
import { defaultState as defaultNetworkState } from '@metamask/network-controller';
import { getDefaultNetworkControllerState } from '@metamask/network-controller';
import type { PreferencesControllerStateChangeEvent } from '@metamask/preferences-controller';
import {
getDefaultPreferencesState,
Expand Down Expand Up @@ -262,7 +262,7 @@ function setupController({
selectedNetworkClientId: NetworkClientId;
}) => {
messenger.publish('NetworkController:networkDidChange', {
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
InfuraNetworkType,
} from '@metamask/controller-utils';
import {
getDefaultNetworkControllerState,
NetworkClientType,
defaultState as defaultNetworkState,
} from '@metamask/network-controller';
import type {
NetworkClient,
Expand Down Expand Up @@ -1653,7 +1653,7 @@ async function withController<ReturnValue>(
messenger.registerActionHandler(
'NetworkController:getState',
jest.fn<NetworkState, []>().mockReturnValue({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
...mockNetworkState,
}),
);
Expand Down
55 changes: 30 additions & 25 deletions packages/assets-controllers/src/TokenDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ChainId,
NetworkType,
convertHexToDecimal,
BUILT_IN_NETWORKS,
InfuraNetworkType,
} from '@metamask/controller-utils';
import type { InternalAccount } from '@metamask/keyring-api';
import type { KeyringControllerState } from '@metamask/keyring-controller';
Expand All @@ -14,9 +14,13 @@ import type {
NetworkController,
NetworkClientId,
} from '@metamask/network-controller';
import { defaultState as defaultNetworkState } from '@metamask/network-controller';
import { getDefaultNetworkControllerState } from '@metamask/network-controller';
import type { AutoManagedNetworkClient } from '@metamask/network-controller/src/create-auto-managed-network-client';
import type { CustomNetworkClientConfiguration } from '@metamask/network-controller/src/types';
import {
buildCustomRpcEndpoint,
buildInfuraNetworkConfiguration,
} from '@metamask/network-controller/tests/helpers';
import {
getDefaultPreferencesState,
type PreferencesState,
Expand Down Expand Up @@ -110,22 +114,23 @@ const sampleTokenB = {
};

const mockNetworkConfigurations: Record<string, NetworkConfiguration> = {
[NetworkType.mainnet]: {
...BUILT_IN_NETWORKS[NetworkType.mainnet],
rpcUrl: 'https://mainnet.infura.io/v3/fakekey',
},
[NetworkType.goerli]: {
...BUILT_IN_NETWORKS[NetworkType.goerli],
rpcUrl: 'https://goerli.infura.io/v3/fakekey',
},
[InfuraNetworkType.mainnet]: buildInfuraNetworkConfiguration(
InfuraNetworkType.mainnet,
),
[InfuraNetworkType.goerli]: buildInfuraNetworkConfiguration(
InfuraNetworkType.goerli,
),
polygon: {
blockExplorerUrl: 'https://polygonscan.com/',
chainId: '0x89',
nickname: 'Polygon Mainnet',
rpcUrl: `https://polygon-mainnet.infura.io/v3/fakekey`,
ticker: 'MATIC',
rpcPrefs: {
blockExplorerUrl: 'https://polygonscan.com/',
},
defaultRpcEndpointUrl: 'https://polygon-mainnet.infura.io/v3/fakekey',
name: 'Polygon Mainnet',
nativeCurrency: 'MATIC',
rpcEndpoints: [
buildCustomRpcEndpoint({
url: 'https://polygon-mainnet.infura.io/v3/fakekey',
}),
],
},
};

Expand Down Expand Up @@ -306,7 +311,7 @@ describe('TokenDetectionController', () => {
},
async ({ controller, mockNetworkState }) => {
mockNetworkState({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: NetworkType.goerli,
});
await controller.start();
Expand Down Expand Up @@ -393,7 +398,7 @@ describe('TokenDetectionController', () => {
callActionSpy,
}) => {
mockNetworkState({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'polygon',
});
mockGetNetworkClientById(
Expand Down Expand Up @@ -1400,7 +1405,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'polygon',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1461,7 +1466,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'goerli',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1512,7 +1517,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'mainnet',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1565,7 +1570,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'polygon',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1619,7 +1624,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'polygon',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1955,7 +1960,7 @@ describe('TokenDetectionController', () => {
callActionSpy,
}) => {
mockNetworkState({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: NetworkType.goerli,
});
triggerPreferencesStateChange({
Expand Down Expand Up @@ -2372,7 +2377,7 @@ async function withController<ReturnValue>(
const mockNetworkState = jest.fn<NetworkState, []>();
controllerMessenger.registerActionHandler(
'NetworkController:getState',
mockNetworkState.mockReturnValue({ ...defaultNetworkState }),
mockNetworkState.mockReturnValue({ ...getDefaultNetworkControllerState() }),
);
const mockTokensState = jest.fn<TokensControllerState, []>();
controllerMessenger.registerActionHandler(
Expand Down
10 changes: 5 additions & 5 deletions packages/assets-controllers/src/TokenListController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ describe('TokenListController', () => {
);
onNetworkStateChangeCallback({
selectedNetworkClientId,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand Down Expand Up @@ -996,7 +996,7 @@ describe('TokenListController', () => {
'NetworkController:stateChange',
{
selectedNetworkClientId: InfuraNetworkType.goerli,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand All @@ -1017,7 +1017,7 @@ describe('TokenListController', () => {
'NetworkController:stateChange',
{
selectedNetworkClientId: selectedCustomNetworkClientId,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand Down Expand Up @@ -1097,7 +1097,7 @@ describe('TokenListController', () => {
'NetworkController:stateChange',
{
selectedNetworkClientId: InfuraNetworkType.mainnet,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand Down Expand Up @@ -1147,7 +1147,7 @@ describe('TokenListController', () => {
'NetworkController:stateChange',
{
selectedNetworkClientId: selectedCustomNetworkClientId,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand Down
44 changes: 21 additions & 23 deletions packages/assets-controllers/src/TokenRatesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
NetworkClientId,
NetworkState,
} from '@metamask/network-controller';
import { defaultState as defaultNetworkState } from '@metamask/network-controller';
import { getDefaultNetworkControllerState } from '@metamask/network-controller';
import type { NetworkClientConfiguration } from '@metamask/network-controller/src/types';
import type { Hex } from '@metamask/utils';
import { add0x } from '@metamask/utils';
Expand Down Expand Up @@ -48,8 +48,6 @@ const defaultSelectedAccount = createMockInternalAccount({
});
const mockTokenAddress = '0x0000000000000000000000000000000000000010';

const defaultSelectedNetworkClientId = 'AAAA-BBBB-CCCC-DDDD';

type MainControllerMessenger = ControllerMessenger<
AllowedActions | AddApprovalRequest,
AllowedEvents
Expand Down Expand Up @@ -638,8 +636,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).toHaveBeenCalledTimes(1);
Expand All @@ -666,8 +664,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -720,8 +718,8 @@ describe('TokenRatesController', () => {
await controller.start();
jest.spyOn(controller, 'updateExchangeRates').mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
Expand Down Expand Up @@ -774,8 +772,8 @@ describe('TokenRatesController', () => {
await controller.start();
jest.spyOn(controller, 'updateExchangeRates').mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
Expand All @@ -802,8 +800,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).not.toHaveBeenCalled();
Expand Down Expand Up @@ -831,8 +829,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).not.toHaveBeenCalled();
Expand All @@ -858,8 +856,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).not.toHaveBeenCalled();
Expand Down Expand Up @@ -911,8 +909,8 @@ describe('TokenRatesController', () => {
async ({ controller, triggerNetworkStateChange }) => {
jest.spyOn(controller, 'updateExchangeRates').mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
Expand Down Expand Up @@ -964,8 +962,8 @@ describe('TokenRatesController', () => {
async ({ controller, triggerNetworkStateChange }) => {
jest.spyOn(controller, 'updateExchangeRates').mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
Expand Down Expand Up @@ -2327,7 +2325,7 @@ async function withController<ReturnValue>(
controllerMessenger.registerActionHandler(
'NetworkController:getState',
networkStateMock.mockReturnValue({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
...mockNetworkState,
}),
);
Expand Down Expand Up @@ -2445,7 +2443,7 @@ async function callUpdateExchangeRatesMethod({
// As with many BaseControllerV1-based controllers, runtime config
// modification is allowed by the API but not supported in practice.
triggerNetworkStateChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId,
});
}
Expand Down
Loading
Loading