From ff2ee9816e522117359750ba102290abf3f2e26d Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Tue, 3 Dec 2024 04:08:00 -0500 Subject: [PATCH] Fix `any` types in `EngineService` --- app/core/Engine/Engine.test.ts | 5 ++--- app/core/Engine/Engine.ts | 7 +++++-- app/core/EngineService/EngineService.ts | 26 ++++++++++--------------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/app/core/Engine/Engine.test.ts b/app/core/Engine/Engine.test.ts index ef191cce1f32..59661ad2e920 100644 --- a/app/core/Engine/Engine.test.ts +++ b/app/core/Engine/Engine.test.ts @@ -5,13 +5,12 @@ import { zeroAddress } from 'ethereumjs-util'; import { createMockAccountsControllerState } from '../../util/test/accountsControllerTestUtils'; import { mockNetworkState } from '../../util/test/network'; import MetaMetrics from '../Analytics/MetaMetrics'; -import { store } from '../../store'; +import { ReduxState, store } from '../../store'; import { MetaMetricsEvents } from '../Analytics'; import { NetworkState } from '@metamask/network-controller'; import { Hex } from '@metamask/utils'; import { MarketDataDetails } from '../../components/UI/Tokens'; import { TransactionMeta } from '@metamask/transaction-controller'; -import { RootState } from '../../reducers'; import { MetricsEventBuilder } from '../Analytics/MetricsEventBuilder'; jest.unmock('./Engine'); @@ -369,7 +368,7 @@ describe('Transaction event handlers', () => { beforeEach(() => { engine = Engine.init({}); jest.spyOn(MetaMetrics.getInstance(), 'trackEvent').mockImplementation(); - jest.spyOn(store, 'getState').mockReturnValue({} as RootState); + jest.spyOn(store, 'getState').mockReturnValue({} as ReduxState); }); afterEach(() => { diff --git a/app/core/Engine/Engine.ts b/app/core/Engine/Engine.ts index fc946c01115e..91ec951e1685 100644 --- a/app/core/Engine/Engine.ts +++ b/app/core/Engine/Engine.ts @@ -268,7 +268,7 @@ export class Engine { // eslint-disable-next-line @typescript-eslint/default-param-last constructor( initialState: Partial = {}, - initialKeyringState?: KeyringControllerState | null, + initialKeyringState?: Partial | null, ) { this.controllerMessenger = new ExtendedControllerMessenger(); @@ -2179,7 +2179,10 @@ export default { instance = null; }, - init(state: Partial | undefined, keyringState = null) { + init( + state: Partial | undefined, + keyringState: Partial | null = null, + ) { instance = Engine.instance || new Engine(state, keyringState); Object.freeze(instance); return instance; diff --git a/app/core/EngineService/EngineService.ts b/app/core/EngineService/EngineService.ts index c653e496c29b..432d174d175e 100644 --- a/app/core/EngineService/EngineService.ts +++ b/app/core/EngineService/EngineService.ts @@ -1,7 +1,7 @@ import UntypedEngine from '../Engine'; -import AppConstants from '../AppConstants'; +import { Engine as TypedEngine } from '../Engine/Engine'; import { getVaultFromBackup } from '../BackupVault'; -import { store as importedStore } from '../../store'; +import { store as importedStore, ReduxStore } from '../../store'; import Logger from '../../util/Logger'; import { NO_VAULT_IN_BACKUP_ERROR, @@ -28,9 +28,7 @@ class EngineService { * @param store - Redux store */ - // TODO: Replace "any" with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - initalizeEngine = (store: any) => { + initalizeEngine = (store: ReduxStore) => { trace({ name: TraceName.EngineInitialization, op: TraceOperation.EngineInitialization, @@ -39,17 +37,14 @@ class EngineService { }); const reduxState = store.getState?.(); const state = reduxState?.engine?.backgroundState || {}; - // TODO: Replace "any" with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const Engine = UntypedEngine as any; + const Engine = UntypedEngine; Engine.init(state); - this.updateControllers(store, Engine); + // `Engine.init()` call mutates `typeof UntypedEngine` to `TypedEngine` + this.updateControllers(store, Engine as unknown as TypedEngine); endTrace({ name: TraceName.EngineInitialization }); }; - // TODO: Replace "any" with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - private updateControllers = (store: any, engine: any) => { + private updateControllers = (store: ReduxStore, engine: TypedEngine) => { if (!engine.context) { Logger.error( new Error( @@ -96,9 +91,7 @@ class EngineService { const keyringState = await getVaultFromBackup(); const reduxState = importedStore.getState?.(); const state = reduxState?.engine?.backgroundState || {}; - // TODO: Replace "any" with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const Engine = UntypedEngine as any; + const Engine = UntypedEngine; // This ensures we create an entirely new engine await Engine.destroyEngine(); this.engineInitialized = false; @@ -107,7 +100,8 @@ class EngineService { keyrings: [], vault: keyringState.vault, }; - const instance = Engine.init(state, newKeyringState); + // `Engine.init()` call mutates `typeof UntypedEngine` to `Engine` + const instance = Engine.init(state, newKeyringState) as unknown as TypedEngine; if (instance) { this.updateControllers(importedStore, instance); // this is a hack to give the engine time to reinitialize