Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions packages/config-registry-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add `ConfigRegistryControllerStateChangedEvent` (`ConfigRegistryController:stateChanged`) to the controller's events ([#9595](https://github.com/MetaMask/core/pull/9595))
- Add `ConfigRegistryController.getNetworkConfigByCaip2ChainId` method to retrieve a network config by its CAIP-2 chain ID ([#9597](https://github.com/MetaMask/core/pull/9597))
- The method returns the network config if found, or `undefined` if not found.
- The method is also accessible via the controller's messenger as `ConfigRegistryController:getNetworkConfigByCaip2ChainId`.

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@

import type { ConfigRegistryController } from './ConfigRegistryController.js';

/**
* Get the network configuration for a given CAIP-2 chain ID.
*
* @param caip2ChainId - The CAIP-2 chain ID (e.g., "eip155:1").
* @returns The network configuration if found, otherwise undefined.
*/
export type ConfigRegistryControllerGetNetworkConfigByCaip2ChainIdAction = {
type: `ConfigRegistryController:getNetworkConfigByCaip2ChainId`;
handler: ConfigRegistryController['getNetworkConfigByCaip2ChainId'];
};

/**
* Stop all polling.
*/
Expand All @@ -22,5 +33,6 @@ export type ConfigRegistryControllerStartPollingAction = {
* Union of all ConfigRegistryController action types.
*/
export type ConfigRegistryControllerMethodActions =
| ConfigRegistryControllerGetNetworkConfigByCaip2ChainIdAction
| ConfigRegistryControllerStopPollingAction
| ConfigRegistryControllerStartPollingAction;
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,46 @@ describe('ConfigRegistryController', () => {
});
});

describe('getNetworkConfigByCaip2ChainId', () => {
it('returns the correct network config for a given CAIP-2 chainId', async () => {
const mockNetworkConfig = createMockNetworkConfig({
chainId: 'eip155:1',
name: 'Ethereum Mainnet',
});
await withController(
{
options: {
state: {
configs: {
networks: {
'eip155:1': createMockNetworkConfig({
chainId: 'eip155:1',
name: 'Ethereum Mainnet',
}),
},
},
},
},
},
async ({ controller }) => {
const networkConfig =
controller.getNetworkConfigByCaip2ChainId('eip155:1');

expect(networkConfig).toStrictEqual(mockNetworkConfig);
},
);
});

it('returns undefined for a non-existent CAIP-2 chainId', async () => {
await withController(async ({ controller }) => {
const networkConfig =
controller.getNetworkConfigByCaip2ChainId('eip155:9999');

expect(networkConfig).toBeUndefined();
});
});
});

describe('feature flag', () => {
it('uses API when feature flag is enabled', async () => {
await withController(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ const stateMetadata = {
*/
const DEFAULT_FALLBACK_CONFIG: Record<string, RegistryNetworkConfig> = {};

const MESSENGER_EXPOSED_METHODS = ['startPolling', 'stopPolling'] as const;
const MESSENGER_EXPOSED_METHODS = [
'startPolling',
'stopPolling',
'getNetworkConfigByCaip2ChainId',
] as const;

/**
* Published when the state of {@link ConfigRegistryController} changes.
Expand Down Expand Up @@ -205,6 +209,18 @@ export class ConfigRegistryController extends StaticIntervalPollingController<nu
this.#setupEventListeners();
}

/**
* Get the network configuration for a given CAIP-2 chain ID.
*
* @param caip2ChainId - The CAIP-2 chain ID (e.g., "eip155:1").
* @returns The network configuration if found, otherwise undefined.
*/
getNetworkConfigByCaip2ChainId(
caip2ChainId: string,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance we can do slightly better than string here as a type ? I often saw ${string}:${string}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The state is typed Record<string, _> so the argument is matching the state key type, but we can change both

): RegistryNetworkConfig | undefined {
return this.state.configs.networks[caip2ChainId];
}

async _executePoll(_input: null): Promise<void> {
try {
const result = await this.messenger.call(
Expand Down