Skip to content

Commit

Permalink
chore: upgrade BridgeController to BaseController V2
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Aug 14, 2024
1 parent ee86bde commit 65c14a6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 32 deletions.
31 changes: 18 additions & 13 deletions app/scripts/controllers/bridge/bridge-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
import nock from 'nock';
import { BRIDGE_API_BASE_URL } from '../../../../shared/constants/bridge';
import BridgeController from './bridge-controller';
import { BridgeControllerMessenger } from './types';
import { DEFAULT_BRIDGE_CONTROLLER_STATE } from './constants';

const EMPTY_INIT_STATE = {
bridgeState: {
bridgeFeatureFlags: {
extensionSupport: false,
},
},
bridgeState: DEFAULT_BRIDGE_CONTROLLER_STATE,
};

const messengerMock = {
call: jest.fn(),
registerActionHandler: jest.fn(),
registerInitialEventPayload: jest.fn(),
publish: jest.fn(),
} as unknown as jest.Mocked<BridgeControllerMessenger>;

describe('BridgeController', function () {
let bridgeController: BridgeController;

beforeAll(function () {
bridgeController = new BridgeController();
bridgeController = new BridgeController({ messenger: messengerMock });
});

it('constructor should setup correctly', function () {
expect(bridgeController.store.getState()).toStrictEqual(EMPTY_INIT_STATE);
expect(bridgeController.state).toStrictEqual(EMPTY_INIT_STATE);
});

it('setBridgeFeatureFlags should fetch and set the bridge feature flags', async function () {
nock(BRIDGE_API_BASE_URL).get('/getAllFeatureFlags').reply(200, {
'extension-support': true,
});
expect(
bridgeController.store.getState().bridgeState.bridgeFeatureFlags,
).toStrictEqual({ extensionSupport: false });
expect(bridgeController.state.bridgeState.bridgeFeatureFlags).toStrictEqual(
{ extensionSupport: false },
);

await bridgeController.setBridgeFeatureFlags();
expect(
bridgeController.store.getState().bridgeState.bridgeFeatureFlags,
).toStrictEqual({ extensionSupport: true });
expect(bridgeController.state.bridgeState.bridgeFeatureFlags).toStrictEqual(
{ extensionSupport: true },
);
});
});
47 changes: 37 additions & 10 deletions app/scripts/controllers/bridge/bridge-controller.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
import { ObservableStore } from '@metamask/obs-store';
import { BaseController, StateMetadata } from '@metamask/base-controller';
import { fetchBridgeFeatureFlags } from '../../../../ui/pages/bridge/bridge.util';
import { DEFAULT_BRIDGE_CONTROLLER_STATE } from './constants';
import {
BRIDGE_CONTROLLER_NAME,
DEFAULT_BRIDGE_CONTROLLER_STATE,
} from './constants';
import { BridgeControllerState, BridgeControllerMessenger } from './types';

export default class BridgeController {
store = new ObservableStore({ bridgeState: DEFAULT_BRIDGE_CONTROLLER_STATE });
const metadata: StateMetadata<{ bridgeState: BridgeControllerState }> = {
bridgeState: {
persist: false,
anonymous: false,
},
};

export default class BridgeController extends BaseController<
typeof BRIDGE_CONTROLLER_NAME,
{ bridgeState: BridgeControllerState },
BridgeControllerMessenger
> {
constructor({ messenger }: { messenger: BridgeControllerMessenger }) {
super({
name: BRIDGE_CONTROLLER_NAME,
metadata,
messenger,
state: { bridgeState: DEFAULT_BRIDGE_CONTROLLER_STATE },
});

this.messagingSystem.registerActionHandler(
`${BRIDGE_CONTROLLER_NAME}:setBridgeFeatureFlags`,
this.setBridgeFeatureFlags.bind(this),
);
}

resetState = () => {
this.store.updateState({
bridgeState: {
this.update((_state) => {
_state.bridgeState = {
...DEFAULT_BRIDGE_CONTROLLER_STATE,
},
};
});
};

setBridgeFeatureFlags = async () => {
const { bridgeState } = this.store.getState();
const { bridgeState } = this.state;
const bridgeFeatureFlags = await fetchBridgeFeatureFlags();
this.store.updateState({
bridgeState: { ...bridgeState, bridgeFeatureFlags },
this.update((_state) => {
_state.bridgeState = { ...bridgeState, bridgeFeatureFlags };
});
};
}
2 changes: 2 additions & 0 deletions app/scripts/controllers/bridge/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { BridgeControllerState, BridgeFeatureFlagsKey } from './types';

export const BRIDGE_CONTROLLER_NAME = 'BridgeController';

export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = {
bridgeFeatureFlags: {
[BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: false,
Expand Down
38 changes: 34 additions & 4 deletions app/scripts/controllers/bridge/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Maps to BridgeController function names
export enum BridgeBackgroundAction {
SET_FEATURE_FLAGS = 'setBridgeFeatureFlags',
}
import {
ControllerStateChangeEvent,
RestrictedControllerMessenger,
} from '@metamask/base-controller';
import BridgeController from './bridge-controller';
import { BRIDGE_CONTROLLER_NAME } from './constants';

export enum BridgeFeatureFlagsKey {
EXTENSION_SUPPORT = 'extensionSupport',
Expand All @@ -14,3 +16,31 @@ export type BridgeFeatureFlags = {
export type BridgeControllerState = {
bridgeFeatureFlags: BridgeFeatureFlags;
};
export enum BridgeBackgroundAction {
SET_FEATURE_FLAGS = 'setBridgeFeatureFlags',
}

type BridgeControllerAction<FunctionName extends keyof BridgeController> = {
type: `${typeof BRIDGE_CONTROLLER_NAME}:${FunctionName}`;
handler: BridgeController[FunctionName];
};

// Maps to BridgeController function names
type BridgeControllerActions =
BridgeControllerAction<BridgeBackgroundAction.SET_FEATURE_FLAGS>;

type BridgeControllerEvents = ControllerStateChangeEvent<
typeof BRIDGE_CONTROLLER_NAME,
BridgeControllerState
>;

/**
* The messenger for the BridgeController.
*/
export type BridgeControllerMessenger = RestrictedControllerMessenger<
typeof BRIDGE_CONTROLLER_NAME,
BridgeControllerActions,
BridgeControllerEvents,
never,
never
>;
20 changes: 15 additions & 5 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ import { isEthAddress } from './lib/multichain/address';
import { decodeTransactionData } from './lib/transaction/decode/util';
import { BridgeBackgroundAction } from './controllers/bridge/types';
import BridgeController from './controllers/bridge/bridge-controller';
import { BRIDGE_CONTROLLER_NAME } from './controllers/bridge/constants';

export const METAMASK_CONTROLLER_EVENTS = {
// Fired after state changes that impact the extension badge (unapproved msg count)
Expand Down Expand Up @@ -1948,7 +1949,16 @@ export default class MetamaskController extends EventEmitter {
},
initState.SwapsController,
);
this.bridgeController = new BridgeController();

const bridgeControllerMessenger = this.controllerMessenger.getRestricted({
name: BRIDGE_CONTROLLER_NAME,
allowedActions: [],
allowedEvents: [],
});
this.bridgeController = new BridgeController({
messenger: bridgeControllerMessenger,
});

this.smartTransactionsController = new SmartTransactionsController(
{
getNetworkClientById: this.networkController.getNetworkClientById.bind(
Expand Down Expand Up @@ -2186,7 +2196,7 @@ export default class MetamaskController extends EventEmitter {
EncryptionPublicKeyController: this.encryptionPublicKeyController,
SignatureController: this.signatureController,
SwapsController: this.swapsController,
BridgeController: this.bridgeController.store,
BridgeController: this.bridgeController,
EnsController: this.ensController,
ApprovalController: this.approvalController,
PPOMController: this.ppomController,
Expand Down Expand Up @@ -3028,7 +3038,6 @@ export default class MetamaskController extends EventEmitter {
appMetadataController,
permissionController,
preferencesController,
bridgeController,
tokensController,
smartTransactionsController,
txController,
Expand Down Expand Up @@ -3682,8 +3691,9 @@ export default class MetamaskController extends EventEmitter {

// Bridge
[BridgeBackgroundAction.SET_FEATURE_FLAGS]:
bridgeController[BridgeBackgroundAction.SET_FEATURE_FLAGS].bind(
bridgeController,
this.controllerMessenger.call.bind(
this.controllerMessenger,
`${BRIDGE_CONTROLLER_NAME}:${BridgeBackgroundAction.SET_FEATURE_FLAGS}`,
),

// Smart Transactions
Expand Down

0 comments on commit 65c14a6

Please sign in to comment.