From ee86bdea7bcf238d4c0a76f545c4e0e631250414 Mon Sep 17 00:00:00 2001 From: Micaela Estabillo Date: Wed, 24 Jul 2024 15:56:37 -0700 Subject: [PATCH] refactor: reorganize bridge controller --- app/scripts/controllers/bridge.ts | 43 ------------------- .../bridge-controller.test.ts} | 4 +- .../controllers/bridge/bridge-controller.ts | 23 ++++++++++ app/scripts/controllers/bridge/constants.ts | 7 +++ app/scripts/controllers/bridge/types.ts | 16 +++++++ app/scripts/metamask-controller.js | 3 +- ui/ducks/bridge/actions.ts | 2 +- ui/ducks/bridge/bridge.test.ts | 2 +- ui/pages/bridge/bridge.util.ts | 2 +- ui/selectors/selectors.js | 2 +- 10 files changed, 54 insertions(+), 50 deletions(-) delete mode 100644 app/scripts/controllers/bridge.ts rename app/scripts/controllers/{bridge.test.ts => bridge/bridge-controller.test.ts} (88%) create mode 100644 app/scripts/controllers/bridge/bridge-controller.ts create mode 100644 app/scripts/controllers/bridge/constants.ts create mode 100644 app/scripts/controllers/bridge/types.ts diff --git a/app/scripts/controllers/bridge.ts b/app/scripts/controllers/bridge.ts deleted file mode 100644 index 459631531bd9..000000000000 --- a/app/scripts/controllers/bridge.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { ObservableStore } from '@metamask/obs-store'; -import { fetchBridgeFeatureFlags } from '../../../ui/pages/bridge/bridge.util'; - -// Maps to BridgeController function names -export enum BridgeBackgroundAction { - SET_FEATURE_FLAGS = 'setBridgeFeatureFlags', -} - -export enum BridgeFeatureFlagsKey { - EXTENSION_SUPPORT = 'extensionSupport', -} - -export type BridgeFeatureFlags = { - [BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: boolean; -}; - -const initialState = { - bridgeState: { - bridgeFeatureFlags: { - [BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: false, - }, - }, -}; - -export default class BridgeController { - store = new ObservableStore(initialState); - - resetState = () => { - this.store.updateState({ - bridgeState: { - ...initialState.bridgeState, - }, - }); - }; - - setBridgeFeatureFlags = async () => { - const { bridgeState } = this.store.getState(); - const bridgeFeatureFlags = await fetchBridgeFeatureFlags(); - this.store.updateState({ - bridgeState: { ...bridgeState, bridgeFeatureFlags }, - }); - }; -} diff --git a/app/scripts/controllers/bridge.test.ts b/app/scripts/controllers/bridge/bridge-controller.test.ts similarity index 88% rename from app/scripts/controllers/bridge.test.ts rename to app/scripts/controllers/bridge/bridge-controller.test.ts index f82902f4cc7b..57165a07a5d7 100644 --- a/app/scripts/controllers/bridge.test.ts +++ b/app/scripts/controllers/bridge/bridge-controller.test.ts @@ -1,6 +1,6 @@ import nock from 'nock'; -import { BRIDGE_API_BASE_URL } from '../../../shared/constants/bridge'; -import BridgeController from './bridge'; +import { BRIDGE_API_BASE_URL } from '../../../../shared/constants/bridge'; +import BridgeController from './bridge-controller'; const EMPTY_INIT_STATE = { bridgeState: { diff --git a/app/scripts/controllers/bridge/bridge-controller.ts b/app/scripts/controllers/bridge/bridge-controller.ts new file mode 100644 index 000000000000..cf74516423a4 --- /dev/null +++ b/app/scripts/controllers/bridge/bridge-controller.ts @@ -0,0 +1,23 @@ +import { ObservableStore } from '@metamask/obs-store'; +import { fetchBridgeFeatureFlags } from '../../../../ui/pages/bridge/bridge.util'; +import { DEFAULT_BRIDGE_CONTROLLER_STATE } from './constants'; + +export default class BridgeController { + store = new ObservableStore({ bridgeState: DEFAULT_BRIDGE_CONTROLLER_STATE }); + + resetState = () => { + this.store.updateState({ + bridgeState: { + ...DEFAULT_BRIDGE_CONTROLLER_STATE, + }, + }); + }; + + setBridgeFeatureFlags = async () => { + const { bridgeState } = this.store.getState(); + const bridgeFeatureFlags = await fetchBridgeFeatureFlags(); + this.store.updateState({ + bridgeState: { ...bridgeState, bridgeFeatureFlags }, + }); + }; +} diff --git a/app/scripts/controllers/bridge/constants.ts b/app/scripts/controllers/bridge/constants.ts new file mode 100644 index 000000000000..d79f1c2c2640 --- /dev/null +++ b/app/scripts/controllers/bridge/constants.ts @@ -0,0 +1,7 @@ +import { BridgeControllerState, BridgeFeatureFlagsKey } from './types'; + +export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = { + bridgeFeatureFlags: { + [BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: false, + }, +}; diff --git a/app/scripts/controllers/bridge/types.ts b/app/scripts/controllers/bridge/types.ts new file mode 100644 index 000000000000..9534ae42d28c --- /dev/null +++ b/app/scripts/controllers/bridge/types.ts @@ -0,0 +1,16 @@ +// Maps to BridgeController function names +export enum BridgeBackgroundAction { + SET_FEATURE_FLAGS = 'setBridgeFeatureFlags', +} + +export enum BridgeFeatureFlagsKey { + EXTENSION_SUPPORT = 'extensionSupport', +} + +export type BridgeFeatureFlags = { + [BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: boolean; +}; + +export type BridgeControllerState = { + bridgeFeatureFlags: BridgeFeatureFlags; +}; diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index c7f54bdbbbfb..fab912dda9a9 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -331,7 +331,8 @@ import { updateSecurityAlertResponse } from './lib/ppom/ppom-util'; import createEvmMethodsToNonEvmAccountReqFilterMiddleware from './lib/createEvmMethodsToNonEvmAccountReqFilterMiddleware'; import { isEthAddress } from './lib/multichain/address'; import { decodeTransactionData } from './lib/transaction/decode/util'; -import BridgeController, { BridgeBackgroundAction } from './controllers/bridge'; +import { BridgeBackgroundAction } from './controllers/bridge/types'; +import BridgeController from './controllers/bridge/bridge-controller'; export const METAMASK_CONTROLLER_EVENTS = { // Fired after state changes that impact the extension badge (unapproved msg count) diff --git a/ui/ducks/bridge/actions.ts b/ui/ducks/bridge/actions.ts index d30e197e9cf0..456c9ed36679 100644 --- a/ui/ducks/bridge/actions.ts +++ b/ui/ducks/bridge/actions.ts @@ -1,4 +1,4 @@ -import { BridgeBackgroundAction } from '../../../app/scripts/controllers/bridge'; +import { BridgeBackgroundAction } from '../../../app/scripts/controllers/bridge/types'; import { forceUpdateMetamaskState } from '../../store/actions'; import { submitRequestToBackground } from '../../store/background-connection'; import { MetaMaskReduxDispatch } from '../../store/store'; diff --git a/ui/ducks/bridge/bridge.test.ts b/ui/ducks/bridge/bridge.test.ts index 5a380cf7507f..f47d3c4f64b2 100644 --- a/ui/ducks/bridge/bridge.test.ts +++ b/ui/ducks/bridge/bridge.test.ts @@ -3,7 +3,7 @@ import thunk from 'redux-thunk'; import { createBridgeMockStore } from '../../../test/jest/mock-store'; import { CHAIN_IDS } from '../../../shared/constants/network'; import { setBackgroundConnection } from '../../store/background-connection'; -import { BridgeBackgroundAction } from '../../../app/scripts/controllers/bridge'; +import { BridgeBackgroundAction } from '../../../app/scripts/controllers/bridge/types'; import bridgeReducer from './bridge'; import { setBridgeFeatureFlags, setToChain } from './actions'; diff --git a/ui/pages/bridge/bridge.util.ts b/ui/pages/bridge/bridge.util.ts index 9baff4191e8d..0c3e54d1dc79 100644 --- a/ui/pages/bridge/bridge.util.ts +++ b/ui/pages/bridge/bridge.util.ts @@ -1,7 +1,7 @@ import { BridgeFeatureFlagsKey, BridgeFeatureFlags, -} from '../../../app/scripts/controllers/bridge'; +} from '../../../app/scripts/controllers/bridge/types'; import { BRIDGE_API_BASE_URL, BRIDGE_CLIENT_ID, diff --git a/ui/selectors/selectors.js b/ui/selectors/selectors.js index 2eb0edfdc169..539e2418a1e7 100644 --- a/ui/selectors/selectors.js +++ b/ui/selectors/selectors.js @@ -106,7 +106,7 @@ import { import { PRIVACY_POLICY_DATE } from '../helpers/constants/privacy-policy'; import { ENVIRONMENT_TYPE_POPUP } from '../../shared/constants/app'; import { MultichainNativeAssets } from '../../shared/constants/multichain/assets'; -import { BridgeFeatureFlagsKey } from '../../app/scripts/controllers/bridge'; +import { BridgeFeatureFlagsKey } from '../../app/scripts/controllers/bridge/types'; import { getAllUnapprovedTransactions, getCurrentNetworkTransactions,