Skip to content

Commit

Permalink
refactor: reorganize bridge controller
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Aug 14, 2024
1 parent 2707dc3 commit ee86bde
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 50 deletions.
43 changes: 0 additions & 43 deletions app/scripts/controllers/bridge.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
23 changes: 23 additions & 0 deletions app/scripts/controllers/bridge/bridge-controller.ts
Original file line number Diff line number Diff line change
@@ -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 },
});
};
}
7 changes: 7 additions & 0 deletions app/scripts/controllers/bridge/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BridgeControllerState, BridgeFeatureFlagsKey } from './types';

export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = {
bridgeFeatureFlags: {
[BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: false,
},
};
16 changes: 16 additions & 0 deletions app/scripts/controllers/bridge/types.ts
Original file line number Diff line number Diff line change
@@ -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;
};
3 changes: 2 additions & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ui/ducks/bridge/actions.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion ui/ducks/bridge/bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion ui/pages/bridge/bridge.util.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ee86bde

Please sign in to comment.