From 3e6e2e58683c789fa9f9f7ed12a60dc0812e72e2 Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Mon, 2 Dec 2024 03:50:44 -0500 Subject: [PATCH] Move `metamask-controllers` types to `shared` directory --- .../controllers/metametrics-controller.ts | 1 - app/scripts/lib/PatchStore.ts | 20 ++++++------ app/scripts/lib/state-utils.ts | 8 ++--- .../types/metamask.ts | 32 +++++++++---------- ui/ducks/metamask/constants.ts | 2 +- 5 files changed, 30 insertions(+), 33 deletions(-) rename app/scripts/metamask-controller-stores.ts => shared/types/metamask.ts (80%) diff --git a/app/scripts/controllers/metametrics-controller.ts b/app/scripts/controllers/metametrics-controller.ts index 244a63ef13c7..947ad4369be4 100644 --- a/app/scripts/controllers/metametrics-controller.ts +++ b/app/scripts/controllers/metametrics-controller.ts @@ -32,7 +32,6 @@ import { ControllerStateChangeEvent, RestrictedControllerMessenger, } from '@metamask/base-controller'; -import type { MemStoreControllersComposedState } from '../metamask-controller-stores'; import { ENVIRONMENT_TYPE_BACKGROUND } from '../../../shared/constants/app'; import { METAMETRICS_ANONYMOUS_ID, diff --git a/app/scripts/lib/PatchStore.ts b/app/scripts/lib/PatchStore.ts index 70af25d63235..ee69efc954b4 100644 --- a/app/scripts/lib/PatchStore.ts +++ b/app/scripts/lib/PatchStore.ts @@ -1,7 +1,7 @@ import { createProjectLogger, getKnownPropertyNames } from '@metamask/utils'; import { Patch } from 'immer'; import { v4 as uuid } from 'uuid'; -import { MemStoreControllersComposedState } from '../metamask-controller-stores'; +import { MemStoreControllersComposedState as BackgroundState } from '../../../shared/types/metamask'; import ComposableObservableStore from './ComposableObservableStore'; import { sanitizeUIState } from './state-utils'; @@ -16,8 +16,8 @@ export class PatchStore { private listener: (request: { controllerKey: string; - oldState: MemStoreControllersComposedState; - newState: MemStoreControllersComposedState; + oldState: BackgroundState; + newState: BackgroundState; }) => void; constructor(observableStore: ComposableObservableStore) { @@ -52,8 +52,8 @@ export class PatchStore { newState, }: { controllerKey: string; - oldState: MemStoreControllersComposedState; - newState: MemStoreControllersComposedState; + oldState: BackgroundState; + newState: BackgroundState; }) { const sanitizedNewState = sanitizeUIState(newState); const patches = this._generatePatches(oldState, sanitizedNewState); @@ -81,12 +81,12 @@ export class PatchStore { } private _generatePatches( - oldState: MemStoreControllersComposedState, - newState: MemStoreControllersComposedState, + oldState: BackgroundState, + newState: BackgroundState, ): Patch[] { - return getKnownPropertyNames( - newState, - ).reduce((patches, controllerName) => { + return getKnownPropertyNames(newState).reduce< + Patch[] + >((patches, controllerName) => { Object.keys(oldState[controllerName]).forEach((key) => { const oldData = oldState[controllerName][key]; const newData = newState[controllerName][key]; diff --git a/app/scripts/lib/state-utils.ts b/app/scripts/lib/state-utils.ts index 4c317f6b0127..cbcb81779e99 100644 --- a/app/scripts/lib/state-utils.ts +++ b/app/scripts/lib/state-utils.ts @@ -1,12 +1,10 @@ import { SnapControllerState } from '@metamask/snaps-controllers'; import { isSnapId, Snap } from '@metamask/snaps-utils'; -import { MemStoreControllersComposedState } from '../metamask-controller-stores'; +import { MemStoreControllersComposedState as BackgroundState } from '../../../shared/types/metamask'; const REMOVE_KEYS = ['snapStates', 'unencryptedSnapStates', 'vault'] as const; -export function sanitizeUIState( - state: MemStoreControllersComposedState, -): MemStoreControllersComposedState { +export function sanitizeUIState(state: BackgroundState): BackgroundState { const newState = { ...state }; for (const key of REMOVE_KEYS) { @@ -22,7 +20,7 @@ export function sanitizeUIState( return newState; } -function sanitizeSnapData(state: MemStoreControllersComposedState) { +function sanitizeSnapData(state: BackgroundState) { const snapsData: SnapControllerState['snaps'] | undefined = state.SnapController.snaps; diff --git a/app/scripts/metamask-controller-stores.ts b/shared/types/metamask.ts similarity index 80% rename from app/scripts/metamask-controller-stores.ts rename to shared/types/metamask.ts index 70d2f6bfa0a0..adbf4fd9e995 100644 --- a/app/scripts/metamask-controller-stores.ts +++ b/shared/types/metamask.ts @@ -58,23 +58,23 @@ import type { NotificationServicesPushController, } from '@metamask/notification-services-controller'; -import { BridgeStatusControllerState } from '../../shared/types/bridge-status'; +import { BridgeStatusControllerState } from './bridge-status'; -import type { BalancesController as MultichainBalancesControllerState } from './lib/accounts/BalancesController'; -import type { NetworkOrderControllerState } from './controllers/network-order'; -import type { AccountOrderControllerState } from './controllers/account-order'; -import type { PreferencesControllerState } from './controllers/preferences-controller'; -import type { AppStateControllerState } from './controllers/app-state-controller'; -import type { AlertControllerState } from './controllers/alert-controller'; -import type { OnboardingControllerState } from './controllers/onboarding'; -import type { EncryptionPublicKeyControllerState } from './controllers/encryption-public-key'; -import type { AppMetadataControllerState } from './controllers/app-metadata'; -import type { DecryptMessageControllerState } from './controllers/decrypt-message'; -import type { SwapsControllerState } from './controllers/swaps/swaps.types'; -import type { BridgeControllerState } from './controllers/bridge/types'; -import { MetaMetricsDataDeletionState } from './controllers/metametrics-data-deletion/metametrics-data-deletion'; -import { MetaMetricsControllerState } from './controllers/metametrics-controller'; -import { AccountTrackerControllerState } from './controllers/account-tracker-controller'; +import type { BalancesController as MultichainBalancesControllerState } from '../../app/scripts/lib/accounts/BalancesController'; +import type { NetworkOrderControllerState } from '../../app/scripts/controllers/network-order'; +import type { AccountOrderControllerState } from '../../app/scripts/controllers/account-order'; +import type { PreferencesControllerState } from '../../app/scripts/controllers/preferences-controller'; +import type { AppStateControllerState } from '../../app/scripts/controllers/app-state-controller'; +import type { AlertControllerState } from '../../app/scripts/controllers/alert-controller'; +import type { OnboardingControllerState } from '../../app/scripts/controllers/onboarding'; +import type { EncryptionPublicKeyControllerState } from '../../app/scripts/controllers/encryption-public-key'; +import type { AppMetadataControllerState } from '../../app/scripts/controllers/app-metadata'; +import type { DecryptMessageControllerState } from '../../app/scripts/controllers/decrypt-message'; +import type { SwapsControllerState } from '../../app/scripts/controllers/swaps/swaps.types'; +import type { BridgeControllerState } from '../../app/scripts/controllers/bridge/types'; +import { MetaMetricsDataDeletionState } from '../../app/scripts/controllers/metametrics-data-deletion/metametrics-data-deletion'; +import { MetaMetricsControllerState } from '../../app/scripts/controllers/metametrics-controller'; +import { AccountTrackerControllerState } from '../../app/scripts/controllers/account-tracker-controller'; export type ResetOnRestartStoresComposedState = { AccountTracker: AccountTrackerControllerState; diff --git a/ui/ducks/metamask/constants.ts b/ui/ducks/metamask/constants.ts index 0fbe51efbe26..e7759555156c 100644 --- a/ui/ducks/metamask/constants.ts +++ b/ui/ducks/metamask/constants.ts @@ -1,6 +1,6 @@ import { NameType } from '@metamask/name-controller'; import { getDefaultPreferencesControllerState } from '../../../app/scripts/controllers/preferences-controller'; -import type { BackgroundStateProxy, MemStoreControllersComposedState } from '../../../app/scripts/metamask-controller-stores'; +import type { BackgroundStateProxy, MemStoreControllersComposedState } from '../../../shared/types/metamask'; import { DEFAULT_AUTO_LOCK_TIME_LIMIT, ThemeType,