Skip to content

Commit

Permalink
Fix any types in EngineService
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Dec 3, 2024
1 parent e33fade commit ff2ee98
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
5 changes: 2 additions & 3 deletions app/core/Engine/Engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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(() => {
Expand Down
7 changes: 5 additions & 2 deletions app/core/Engine/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class Engine {
// eslint-disable-next-line @typescript-eslint/default-param-last
constructor(
initialState: Partial<EngineState> = {},
initialKeyringState?: KeyringControllerState | null,
initialKeyringState?: Partial<KeyringControllerState> | null,
) {
this.controllerMessenger = new ExtendedControllerMessenger();

Expand Down Expand Up @@ -2179,7 +2179,10 @@ export default {
instance = null;
},

init(state: Partial<EngineState> | undefined, keyringState = null) {
init(
state: Partial<EngineState> | undefined,
keyringState: Partial<KeyringControllerState> | null = null,
) {
instance = Engine.instance || new Engine(state, keyringState);
Object.freeze(instance);
return instance;
Expand Down
26 changes: 10 additions & 16 deletions app/core/EngineService/EngineService.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit ff2ee98

Please sign in to comment.