Skip to content

Commit

Permalink
Use modular init for accounts controller in Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Cal-L committed Jan 18, 2025
1 parent 84addc5 commit 7635a8a
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions app/core/Engine/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,8 @@ import {
AccountsControllerSelectedAccountChangeEvent,
AccountsControllerAccountAddedEvent,
AccountsControllerAccountRenamedEvent,
} from './controllers/AccountsController/constants';
import { AccountsControllerMessenger } from '@metamask/accounts-controller';
import { createAccountsController } from './controllers/AccountsController/utils';
import { createRemoteFeatureFlagController } from './controllers/RemoteFeatureFlagController';
} from './controllers/accounts-controller/constants';
import { createRemoteFeatureFlagController } from './controllers/remote-feature-flag-controller';
import { captureException } from '@sentry/react-native';
import { lowerCase } from 'lodash';
import {
Expand Down Expand Up @@ -215,6 +213,9 @@ import {
getGlobalNetworkClientId,
} from '../../util/networks/global-network';
import { logEngineCreation } from './utils/logger';
import { initControllers } from './utils';
import { AccountsControllerInit } from './controllers/accounts-controller/utils';
import { Controller, ControllerInitFunction } from './modular-controller.types';

const NON_EMPTY = 'NON_EMPTY';

Expand Down Expand Up @@ -356,26 +357,15 @@ export class Engine {
chainId: getGlobalChainId(networkController),
});

// Create AccountsController
const accountsControllerMessenger: AccountsControllerMessenger =
this.controllerMessenger.getRestricted({
name: 'AccountsController',
allowedEvents: [
'SnapController:stateChange',
'KeyringController:accountRemoved',
'KeyringController:stateChange',
],
allowedActions: [
'KeyringController:getAccounts',
'KeyringController:getKeyringsByType',
'KeyringController:getKeyringForAccount',
],
});
const accountsController = createAccountsController({
messenger: accountsControllerMessenger,
initialState: initialState.AccountsController,
// Modular controller initialization
const controllerInitFunctions = [AccountsControllerInit];
const { controllersByName } = this.#initControllers({
initFunctions: controllerInitFunctions,
initState: initialState as EngineState,
});

const accountsController = controllersByName.AccountsController;

const nftController = new NftController({
chainId: getGlobalChainId(networkController),
useIpfsSubdomains: false,
Expand Down Expand Up @@ -1566,6 +1556,24 @@ export class Engine {
Engine.instance = this;
}

#initControllers({
initFunctions,
initState,
}: {
initFunctions: ControllerInitFunction<Controller>[];
initState: EngineState;
}) {
const initRequest = {
baseControllerMessenger: this.controllerMessenger,
persistedState: initState,
};

return initControllers({
initFunctions,
initRequest,
});
}

// Logs the "Transaction Finalized" event after a transaction was either confirmed, dropped or failed.
_handleTransactionFinalizedEvent = async (
transactionEventPayload: TransactionEventPayload,
Expand Down

0 comments on commit 7635a8a

Please sign in to comment.