Skip to content

Commit a423183

Browse files
committed
refactor: move initializeAccountTree to authStateMachine
1 parent 6715cc5 commit a423183

File tree

10 files changed

+63
-71
lines changed

10 files changed

+63
-71
lines changed

app/components/UI/EvmAccountSelectorList/EvmAccountSelectorList.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import Routes from '../../../constants/navigation/Routes';
4040
import { selectAccountSections } from '../../../multichain-accounts/selectors/accountTreeController';
4141

4242
// Internal dependencies.
43-
import { EvmAccountSelectorListProps } from './EvmAccountSelectorList.types';
43+
import { AccountSection, EvmAccountSelectorListProps } from './EvmAccountSelectorList.types';
4444
import styleSheet from './EvmAccountSelectorList.styles';
4545
import { AccountListBottomSheetSelectorsIDs } from '../../../../e2e/selectors/wallet/AccountListBottomSheet.selectors';
4646
import { WalletViewSelectorsIDs } from '../../../../e2e/selectors/wallet/WalletView.selectors';
@@ -52,11 +52,6 @@ import { Skeleton } from '../../../component-library/components/Skeleton';
5252
import { selectInternalAccounts } from '../../../selectors/accountsController';
5353
import { getFormattedAddressFromInternalAccount } from '../../../core/Multichain/utils';
5454

55-
interface AccountSection {
56-
title: string;
57-
data: Account[];
58-
}
59-
6055
/**
6156
* @deprecated This component is deprecated in favor of the CaipAccountSelectorList component.
6257
* Functionally they should be nearly identical except that EvmAccountSelectorList expects

app/components/UI/EvmAccountSelectorList/EvmAccountSelectorList.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ export interface EvmAccountSelectorListProps
6565
*/
6666
privacyMode?: boolean;
6767
}
68+
69+
export interface AccountSection {
70+
title: string;
71+
data: Account[];
72+
}

app/core/Authentication/Authentication.test.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,6 @@ jest.mock('../Engine', () => ({
5757
keyrings: [{ metadata: { id: 'test-keyring-id' } }],
5858
},
5959
},
60-
AccountTreeController: {
61-
init: jest.fn(),
62-
},
63-
AccountsController: {
64-
updateAccounts: jest.fn(),
65-
},
66-
RemoteFeatureFlagController: {
67-
state: {
68-
remoteFeatureFlags: {
69-
enableMultichainAccounts: {
70-
version: '1',
71-
enabled: true,
72-
},
73-
},
74-
},
75-
},
7660
},
7761
}));
7862

app/core/Authentication/Authentication.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ import {
3939
WalletClientType,
4040
} from '../SnapKeyring/MultichainWalletSnapClient';
4141
///: END:ONLY_INCLUDE_IF
42-
import {
43-
assertMultichainAccountsFeatureFlagType,
44-
isMultichainAccountsFeatureEnabled,
45-
MULTI_CHAIN_ACCOUNTS_FEATURE_VERSION_1,
46-
MULTI_CHAIN_ACCOUNTS_FEATURE_VERSION_2,
47-
MultichainAccountsFeatureFlag,
48-
} from '../../selectors/featureFlagController/multichainAccounts/enabledMultichainAccounts';
49-
import { Json } from '@metamask/utils';
5042

5143
/**
5244
* Holds auth data used to determine auth configuration
@@ -233,34 +225,6 @@ class AuthenticationService {
233225
};
234226
};
235227

236-
/**
237-
* Checks if multichain accounts are enabled for state 1.
238-
* @param remoteFeatureFlags - The remote feature flags object containing the multichain accounts feature flag.
239-
* @returns True if the multichain accounts feature is enabled for state 1, false otherwise.
240-
*/
241-
private isMultichainAccountsEnabledForState1 = (remoteFeatureFlags: Json & MultichainAccountsFeatureFlag) => (
242-
[MULTI_CHAIN_ACCOUNTS_FEATURE_VERSION_1, MULTI_CHAIN_ACCOUNTS_FEATURE_VERSION_2].some((featureVersion) =>
243-
isMultichainAccountsFeatureEnabled(remoteFeatureFlags, featureVersion)
244-
)
245-
);
246-
247-
/**
248-
* Initializes the account tree and updates the accounts if multichain accounts is enabled
249-
*/
250-
private initializeAccountTree = async (): Promise<void> => {
251-
const { AccountTreeController, AccountsController, RemoteFeatureFlagController } = Engine.context;
252-
const { enableMultichainAccounts } = RemoteFeatureFlagController.state.remoteFeatureFlags;
253-
if(!assertMultichainAccountsFeatureFlagType(enableMultichainAccounts)) {
254-
return;
255-
}
256-
const isMultichainAccountsEnabled = this.isMultichainAccountsEnabledForState1(enableMultichainAccounts);
257-
258-
if (isMultichainAccountsEnabled) {
259-
AccountTreeController.init();
260-
await AccountsController.updateAccounts();
261-
}
262-
};
263-
264228
/**
265229
* Reset vault will empty password used to clear/reset vault upon errors during login/creation
266230
*/
@@ -483,8 +447,6 @@ class AuthenticationService {
483447
this.retrySolanaDiscoveryIfPending();
484448
///: END:ONLY_INCLUDE_IF
485449

486-
//TODO: Move this logic to the Engine when the account tree state will be persisted
487-
this.initializeAccountTree();
488450
// TODO: Replace "any" with type
489451
// eslint-disable-next-line @typescript-eslint/no-explicit-any
490452
} catch (e: any) {
@@ -539,8 +501,6 @@ class AuthenticationService {
539501
this.retrySolanaDiscoveryIfPending();
540502
///: END:ONLY_INCLUDE_IF
541503

542-
//TODO: Move this logic to the Engine when the account tree state will be persisted
543-
this.initializeAccountTree();
544504
// TODO: Replace "any" with type
545505
// eslint-disable-next-line @typescript-eslint/no-explicit-any
546506
} catch (e: any) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Json } from '@metamask/utils';
2+
import {
3+
assertMultichainAccountsFeatureFlagType,
4+
isMultichainAccountsFeatureEnabled,
5+
MULTI_CHAIN_ACCOUNTS_FEATURE_VERSION_1,
6+
MULTI_CHAIN_ACCOUNTS_FEATURE_VERSION_2,
7+
MultichainAccountsFeatureFlag,
8+
} from '../../selectors/featureFlagController/multichainAccounts/enabledMultichainAccounts';
9+
import Engine from '../../core/Engine';
10+
11+
export class AccountTreeInitService {
12+
initializeAccountTree = async (): Promise<void> => {
13+
const { AccountTreeController, AccountsController, RemoteFeatureFlagController } = Engine.context;
14+
const { enableMultichainAccounts } = RemoteFeatureFlagController.state.remoteFeatureFlags;
15+
if(!assertMultichainAccountsFeatureFlagType(enableMultichainAccounts)) {
16+
return;
17+
}
18+
const isMultichainAccountsEnabled = this.isMultichainAccountsEnabledForState1(enableMultichainAccounts);
19+
20+
if (isMultichainAccountsEnabled) {
21+
AccountTreeController.init();
22+
await AccountsController.updateAccounts();
23+
}
24+
};
25+
26+
private isMultichainAccountsEnabledForState1 = (remoteFeatureFlags: Json & MultichainAccountsFeatureFlag) => (
27+
[MULTI_CHAIN_ACCOUNTS_FEATURE_VERSION_1, MULTI_CHAIN_ACCOUNTS_FEATURE_VERSION_2].some((featureVersion) =>
28+
isMultichainAccountsFeatureEnabled(remoteFeatureFlags, featureVersion)
29+
)
30+
);
31+
}
32+
33+
export default new AccountTreeInitService();

app/multichain-accounts/controllers/account-tree-controller/constants.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

app/multichain-accounts/controllers/account-tree-controller/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { AccountTreeController, AccountTreeControllerMessenger } from '@metamask/account-tree-controller';
22
import type { ControllerInitFunction } from '../../../core/Engine/types';
33

4-
// Export constants
5-
export * from './constants';
6-
74
/**
85
* Initialize the AccountTreeController.
96
*

app/multichain-accounts/messengers/account-tree-controller-messenger/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ export function getAccountTreeControllerMessenger(
1212
return baseControllerMessenger.getRestricted({
1313
name: 'AccountTreeController',
1414
allowedEvents: [
15-
'AccountsController:stateChange',
1615
'AccountsController:accountAdded',
1716
'AccountsController:accountRemoved',
18-
'KeyringController:stateChange',
1917
],
2018
allowedActions: [
2119
'AccountsController:listMultichainAccounts',
22-
'AccountsController:listAccounts',
2320
'SnapController:get',
2421
'KeyringController:getState',
25-
'KeyringController:getAccounts',
2622
],
2723
});
2824
}

app/store/sagas/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from './xmlHttpRequestOverride';
2121
import EngineService from '../../core/EngineService';
2222
import { AppStateEventProcessor } from '../../core/AppStateEventListener';
23+
import AccountTreeInitService from '../../multichain-accounts/AccountTreeInitService';
2324

2425
export function* appLockStateMachine() {
2526
let biometricsListenerTask: Task<void> | undefined;
@@ -50,6 +51,8 @@ export function* authStateMachine() {
5051
yield take(UserActionType.LOGIN);
5152
const appLockStateMachineTask: Task<void> = yield fork(appLockStateMachine);
5253
LockManagerService.startListening();
54+
//TODO: Move this logic to the Engine when the account tree state will be persisted
55+
AccountTreeInitService.initializeAccountTree();
5356
// Listen to app lock behavior.
5457
yield take(UserActionType.LOGOUT);
5558
LockManagerService.stopListening();

app/store/sagas/sagas.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ jest.mock('../../core/AppStateEventListener', () => ({
4444
},
4545
}));
4646

47+
jest.mock('../../core/Engine', () => ({
48+
context: {
49+
AccountTreeController: {
50+
init: jest.fn(),
51+
},
52+
AccountsController: {
53+
updateAccounts: jest.fn(),
54+
},
55+
RemoteFeatureFlagController: {
56+
state: {
57+
remoteFeatureFlags: {
58+
enableMultichainAccounts: {
59+
version: '1',
60+
enabled: true,
61+
},
62+
},
63+
},
64+
},
65+
},
66+
}));
67+
4768
describe('authStateMachine', () => {
4869
beforeEach(() => {
4970
mockNavigate.mockClear();

0 commit comments

Comments
 (0)