Skip to content

Commit

Permalink
chore: read bridge chains from feature flags and add selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Jul 25, 2024
1 parent d035314 commit f155cae
Show file tree
Hide file tree
Showing 17 changed files with 554 additions and 66 deletions.
26 changes: 19 additions & 7 deletions app/scripts/controllers/bridge/bridge-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import nock from 'nock';
import { BRIDGE_API_BASE_URL } from '../../../../shared/constants/bridge';
import { CHAIN_IDS } from '../../../../shared/constants/network';
import BridgeController from './bridge-controller';
import { BridgeControllerMessenger } from './types';
import { DEFAULT_BRIDGE_CONTROLLER_STATE } from './constants';
Expand All @@ -22,21 +23,32 @@ describe('BridgeController', function () {
bridgeController = new BridgeController({ messenger: messengerMock });
});

beforeEach(() => {
jest.clearAllMocks();
nock(BRIDGE_API_BASE_URL)
.get('/getAllFeatureFlags')
.reply(200, {
'extension-support': true,
'src-network-allowlist': [10, 534352],
'dest-network-allowlist': [137, 42161],
});
});

it('constructor should setup correctly', function () {
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.state.bridgeState.bridgeFeatureFlags).toStrictEqual(
{ extensionSupport: false },
);
const expectedFeatureFlagsResponse = {
extensionSupport: true,
destNetworkAllowlist: [CHAIN_IDS.POLYGON, CHAIN_IDS.ARBITRUM],
srcNetworkAllowlist: [CHAIN_IDS.OPTIMISM, CHAIN_IDS.SCROLL],
};
expect(bridgeController.state).toStrictEqual(EMPTY_INIT_STATE);

await bridgeController.setBridgeFeatureFlags();
expect(bridgeController.state.bridgeState.bridgeFeatureFlags).toStrictEqual(
{ extensionSupport: true },
expectedFeatureFlagsResponse,
);
});
});
2 changes: 2 additions & 0 deletions app/scripts/controllers/bridge/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ export const BRIDGE_CONTROLLER_NAME = 'BridgeController';
export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = {
bridgeFeatureFlags: {
[BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: false,
[BridgeFeatureFlagsKey.NETWORK_SRC_ALLOWLIST]: [],
[BridgeFeatureFlagsKey.NETWORK_DEST_ALLOWLIST]: [],
},
};
6 changes: 6 additions & 0 deletions app/scripts/controllers/bridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ import {
ControllerStateChangeEvent,
RestrictedControllerMessenger,
} from '@metamask/base-controller';
import { Hex } from '@metamask/utils';
import BridgeController from './bridge-controller';
import { BRIDGE_CONTROLLER_NAME } from './constants';

export enum BridgeFeatureFlagsKey {
EXTENSION_SUPPORT = 'extensionSupport',
NETWORK_SRC_ALLOWLIST = 'srcNetworkAllowlist',
NETWORK_DEST_ALLOWLIST = 'destNetworkAllowlist',
}

export type BridgeFeatureFlags = {
[BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: boolean;
[BridgeFeatureFlagsKey.NETWORK_SRC_ALLOWLIST]: Hex[];
[BridgeFeatureFlagsKey.NETWORK_DEST_ALLOWLIST]: Hex[];
};

export type BridgeControllerState = {
bridgeFeatureFlags: BridgeFeatureFlags;
};

export enum BridgeBackgroundAction {
SET_FEATURE_FLAGS = 'setBridgeFeatureFlags',
}
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/lib/setupSentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export const SENTRY_BACKGROUND_STATE = {
bridgeState: {
bridgeFeatureFlags: {
extensionSupport: false,
destNetworkAllowlist: [],
srcNetworkAllowlist: [],
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/default-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ function defaultFixture(inputChainId = CHAIN_IDS.LOCALHOST) {
BridgeController: {
bridgeState: {
bridgeFeatureFlags: {
destNetworkAllowlist: [],
extensionSupport: false,
srcNetworkAllowlist: [],
},
},
},
Expand Down
30 changes: 19 additions & 11 deletions test/e2e/mock-e2e.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const fs = require('fs');

const { BRIDGE_API_BASE_URL } = require('../../shared/constants/bridge');
const {
BRIDGE_DEV_API_BASE_URL,
BRIDGE_PROD_API_BASE_URL,
} = require('../../shared/constants/bridge');
const {
GAS_API_BASE_URL,
SWAPS_API_V2_BASE_URL,
TOKEN_API_BASE_URL,
} = require('../../shared/constants/swaps');
const {
DEFAULT_FEATURE_FLAGS_RESPONSE: BRIDGE_DEFAULT_FEATURE_FLAGS_RESPONSE,
} = require('./tests/bridge/constants');

const CDN_CONFIG_PATH = 'test/e2e/mock-cdn/cdn-config.txt';
const CDN_STALE_DIFF_PATH = 'test/e2e/mock-cdn/cdn-stale-diff.txt';
Expand Down Expand Up @@ -294,16 +300,18 @@ async function setupMocking(
};
});

await server
.forGet(`${BRIDGE_API_BASE_URL}/getAllFeatureFlags`)
.thenCallback(() => {
return {
statusCode: 200,
json: {
'extension-support': false,
},
};
});
[
`${BRIDGE_DEV_API_BASE_URL}/getAllFeatureFlags`,
`${BRIDGE_PROD_API_BASE_URL}/getAllFeatureFlags`,
].forEach(
async (url) =>
await server.forGet(url).thenCallback(() => {
return {
statusCode: 200,
json: BRIDGE_DEFAULT_FEATURE_FLAGS_RESPONSE,
};
}),
);

await server
.forGet(`https://token.api.cx.metamask.io/tokens/${chainId}`)
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/tests/bridge/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { FeatureFlagResponse } from '../../../../ui/pages/bridge/bridge.util';

export const DEFAULT_FEATURE_FLAGS_RESPONSE: FeatureFlagResponse = {
'extension-support': false,
'src-network-allowlist': [1, 42161, 59144],
'dest-network-allowlist': [1, 42161, 59144],
};

export const LOCATOR = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,21 @@
},
"AuthenticationController": { "isSignedIn": "boolean" },
"BridgeController": {
"bridgeState": { "bridgeFeatureFlags": { "extensionSupport": "boolean" } }
"bridgeState": {
"bridgeFeatureFlags": {
"extensionSupport": "boolean",
"srcNetworkAllowlist": {
"0": "string",
"1": "string",
"2": "string"
},
"destNetworkAllowlist": {
"0": "string",
"1": "string",
"2": "string"
}
}
}
},
"CronjobController": { "jobs": "object" },
"CurrencyController": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,21 @@
"swapsStxMaxFeeMultiplier": 2,
"swapsFeatureFlags": {}
},
"bridgeState": { "bridgeFeatureFlags": { "extensionSupport": "boolean" } },
"bridgeState": {
"bridgeFeatureFlags": {
"extensionSupport": "boolean",
"srcNetworkAllowlist": {
"0": "string",
"1": "string",
"2": "string"
},
"destNetworkAllowlist": {
"0": "string",
"1": "string",
"2": "string"
}
}
},
"ensEntries": "object",
"ensResolutionsByAddress": "object",
"pendingApprovals": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@
"BridgeController": {
"bridgeState": {
"bridgeFeatureFlags": {
"extensionSupport": "boolean"
"destNetworkAllowlist": {},
"extensionSupport": "boolean",
"srcNetworkAllowlist": {}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@
"BridgeController": {
"bridgeState": {
"bridgeFeatureFlags": {
"extensionSupport": "boolean"
"extensionSupport": "boolean",
"srcNetworkAllowlist": {},
"destNetworkAllowlist": {}
}
}
},
Expand Down
8 changes: 7 additions & 1 deletion test/jest/mock-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,19 +668,25 @@ export const createSwapsMockStore = () => {
};
};

export const createBridgeMockStore = (featureFlagOverrides = {}) => {
export const createBridgeMockStore = (
featureFlagOverrides = {},
bridgeSliceOverrides = {},
) => {
const swapsStore = createSwapsMockStore();
return {
...swapsStore,
bridge: {
toChain: null,
...bridgeSliceOverrides,
},
metamask: {
...swapsStore.metamask,
bridgeState: {
...(swapsStore.metamask.bridgeState ?? {}),
bridgeFeatureFlags: {
extensionSupport: false,
srcNetworkAllowlist: [],
destNetworkAllowlist: [],
...featureFlagOverrides,
},
},
Expand Down
6 changes: 4 additions & 2 deletions ui/ducks/bridge/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createSlice } from '@reduxjs/toolkit';
import { ProviderConfig } from '@metamask/network-controller';

import { swapsSlice } from '../swaps/swaps';

// Only states that are not in swaps slice
type BridgeState = {
toChain: string | null;
export type BridgeState = {
toChain: ProviderConfig | null;
};

const initialState: BridgeState = {
Expand Down
Loading

0 comments on commit f155cae

Please sign in to comment.