Skip to content

Commit 22f11ed

Browse files
committed
fix: refacto
1 parent e6f8648 commit 22f11ed

File tree

3 files changed

+89
-88
lines changed

3 files changed

+89
-88
lines changed

packages/network-enablement-controller/src/NetworkEnablementController.test.ts

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ import type { CaipChainId, CaipNamespace, Hex } from '@metamask/utils';
1616
import { useFakeTimers } from 'sinon';
1717

1818
import { POPULAR_NETWORKS } from './constants';
19-
import {
20-
NetworkEnablementController,
21-
initNativeAssetIdentifiers,
22-
} from './NetworkEnablementController';
19+
import { NetworkEnablementController } from './NetworkEnablementController';
2320
import type {
2421
NetworkEnablementControllerMessenger,
2522
NativeAssetIdentifiersMap,
@@ -130,47 +127,6 @@ const setupController = ({
130127
};
131128
};
132129

133-
describe('initNativeAssetIdentifiers', () => {
134-
it('builds native asset identifiers from network configurations', () => {
135-
const networks = [
136-
{ chainId: 'eip155:1' as const, nativeCurrency: 'ETH' },
137-
{ chainId: 'eip155:56' as const, nativeCurrency: 'BNB' },
138-
{
139-
chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp' as const,
140-
nativeCurrency: 'SOL',
141-
},
142-
];
143-
144-
const result = initNativeAssetIdentifiers(networks);
145-
146-
expect(result).toStrictEqual({
147-
'eip155:1': 'eip155:1/slip44:60',
148-
'eip155:56': 'eip155:56/slip44:714',
149-
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp':
150-
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501',
151-
});
152-
});
153-
154-
it('skips networks with unknown symbols', () => {
155-
const networks = [
156-
{ chainId: 'eip155:1' as const, nativeCurrency: 'ETH' },
157-
{ chainId: 'eip155:999' as const, nativeCurrency: 'UNKNOWN_XYZ' },
158-
];
159-
160-
const result = initNativeAssetIdentifiers(networks);
161-
162-
expect(result).toStrictEqual({
163-
'eip155:1': 'eip155:1/slip44:60',
164-
});
165-
expect(result['eip155:999']).toBeUndefined();
166-
});
167-
168-
it('returns empty map for empty input', () => {
169-
const result = initNativeAssetIdentifiers([]);
170-
expect(result).toStrictEqual({});
171-
});
172-
});
173-
174130
describe('NetworkEnablementController', () => {
175131
let clock: sinon.SinonFakeTimers;
176132

@@ -1054,6 +1010,56 @@ describe('NetworkEnablementController', () => {
10541010
});
10551011
});
10561012

1013+
describe('initNativeAssetIdentifiers', () => {
1014+
it('populates nativeAssetIdentifiers from network configurations', () => {
1015+
const { controller } = setupController();
1016+
1017+
const networks = [
1018+
{ chainId: 'eip155:1' as const, nativeCurrency: 'ETH' },
1019+
{ chainId: 'eip155:56' as const, nativeCurrency: 'BNB' },
1020+
{
1021+
chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp' as const,
1022+
nativeCurrency: 'SOL',
1023+
},
1024+
];
1025+
1026+
controller.initNativeAssetIdentifiers(networks);
1027+
1028+
expect(controller.state.nativeAssetIdentifiers).toStrictEqual({
1029+
'eip155:1': 'eip155:1/slip44:60',
1030+
'eip155:56': 'eip155:56/slip44:714',
1031+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp':
1032+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501',
1033+
});
1034+
});
1035+
1036+
it('skips networks with unknown symbols', () => {
1037+
const { controller } = setupController();
1038+
1039+
const networks = [
1040+
{ chainId: 'eip155:1' as const, nativeCurrency: 'ETH' },
1041+
{ chainId: 'eip155:999' as const, nativeCurrency: 'UNKNOWN_XYZ' },
1042+
];
1043+
1044+
controller.initNativeAssetIdentifiers(networks);
1045+
1046+
expect(controller.state.nativeAssetIdentifiers['eip155:1']).toBe(
1047+
'eip155:1/slip44:60',
1048+
);
1049+
expect(
1050+
controller.state.nativeAssetIdentifiers['eip155:999'],
1051+
).toBeUndefined();
1052+
});
1053+
1054+
it('does not modify state for empty input', () => {
1055+
const { controller } = setupController();
1056+
1057+
controller.initNativeAssetIdentifiers([]);
1058+
1059+
expect(controller.state.nativeAssetIdentifiers).toStrictEqual({});
1060+
});
1061+
});
1062+
10571063
describe('enableAllPopularNetworks', () => {
10581064
it('enables all popular networks that exist in controller configurations and Solana mainnet', () => {
10591065
const { controller, messenger } = setupController();

packages/network-enablement-controller/src/NetworkEnablementController.ts

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -147,45 +147,6 @@ export type NetworkConfig = {
147147
nativeCurrency: string;
148148
};
149149

150-
/**
151-
* Initializes the native asset identifiers map from network configurations.
152-
* This function should be called from the client during controller initialization
153-
* to populate the nativeAssetIdentifiers state based on actual network configurations.
154-
*
155-
* @param networks - Array of network configurations with chainId and nativeCurrency
156-
* @returns A map of CAIP-2 chain IDs to their native asset identifiers
157-
* @example
158-
* ```typescript
159-
* const evmNetworks = Object.values(networkControllerState.networkConfigurationsByChainId)
160-
* .map(config => ({
161-
* chainId: toEvmCaipChainId(config.chainId),
162-
* nativeCurrency: config.nativeCurrency,
163-
* }));
164-
*
165-
* const multichainNetworks = Object.values(multichainState.multichainNetworkConfigurationsByChainId)
166-
* .map(config => ({
167-
* chainId: config.chainId,
168-
* nativeCurrency: config.nativeCurrency,
169-
* }));
170-
*
171-
* const nativeAssetIdentifiers = initNativeAssetIdentifiers([...evmNetworks, ...multichainNetworks]);
172-
* ```
173-
*/
174-
export function initNativeAssetIdentifiers(
175-
networks: NetworkConfig[],
176-
): NativeAssetIdentifiersMap {
177-
const result: NativeAssetIdentifiersMap = {};
178-
179-
for (const { chainId, nativeCurrency } of networks) {
180-
const slip44CoinType = Slip44Service.getSlip44BySymbol(nativeCurrency);
181-
if (slip44CoinType !== undefined) {
182-
result[chainId] = buildNativeAssetIdentifier(chainId, slip44CoinType);
183-
}
184-
}
185-
186-
return result;
187-
}
188-
189150
/**
190151
* Gets the default state for the NetworkEnablementController.
191152
*
@@ -555,6 +516,43 @@ export class NetworkEnablementController extends BaseController<
555516
});
556517
}
557518

519+
/**
520+
* Initializes the native asset identifiers from network configurations.
521+
* This method should be called from the client during controller initialization
522+
* to populate the nativeAssetIdentifiers state based on actual network configurations.
523+
*
524+
* @param networks - Array of network configurations with chainId and nativeCurrency
525+
* @example
526+
* ```typescript
527+
* const evmNetworks = Object.values(networkControllerState.networkConfigurationsByChainId)
528+
* .map(config => ({
529+
* chainId: toEvmCaipChainId(config.chainId),
530+
* nativeCurrency: config.nativeCurrency,
531+
* }));
532+
*
533+
* const multichainNetworks = Object.values(multichainState.multichainNetworkConfigurationsByChainId)
534+
* .map(config => ({
535+
* chainId: config.chainId,
536+
* nativeCurrency: config.nativeCurrency,
537+
* }));
538+
*
539+
* controller.initNativeAssetIdentifiers([...evmNetworks, ...multichainNetworks]);
540+
* ```
541+
*/
542+
initNativeAssetIdentifiers(networks: NetworkConfig[]): void {
543+
this.update((state) => {
544+
for (const { chainId, nativeCurrency } of networks) {
545+
const slip44CoinType = Slip44Service.getSlip44BySymbol(nativeCurrency);
546+
if (slip44CoinType !== undefined) {
547+
state.nativeAssetIdentifiers[chainId] = buildNativeAssetIdentifier(
548+
chainId,
549+
slip44CoinType,
550+
);
551+
}
552+
}
553+
});
554+
}
555+
558556
/**
559557
* Disables a network for the user.
560558
*

packages/network-enablement-controller/src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
export {
2-
NetworkEnablementController,
3-
initNativeAssetIdentifiers,
4-
} from './NetworkEnablementController';
1+
export { NetworkEnablementController } from './NetworkEnablementController';
52

63
export type {
74
NetworkEnablementControllerState,

0 commit comments

Comments
 (0)