Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mobile feature flags and fix tests #5359

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions packages/bridge-controller/src/bridge-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ describe('BridgeController', function () {
},
},
},
'mobile-config': {
refreshRate: 3,
maxRefreshCount: 3,
support: true,
chains: {
'10': {
isActiveSrc: true,
isActiveDest: false,
},
'534352': {
isActiveSrc: true,
isActiveDest: false,
},
'137': {
isActiveSrc: false,
isActiveDest: true,
},
'42161': {
isActiveSrc: false,
isActiveDest: true,
},
},
},
'approval-gas-multiplier': {
'137': 1.1,
'42161': 1.2,
Expand Down Expand Up @@ -127,19 +150,22 @@ describe('BridgeController', function () {
});

it('setBridgeFeatureFlags should fetch and set the bridge feature flags', async function () {
const expectedFeatureFlagsResponse = {
extensionConfig: {
maxRefreshCount: 3,
refreshRate: 3,
support: true,
chains: {
[CHAIN_IDS.OPTIMISM]: { isActiveSrc: true, isActiveDest: false },
[CHAIN_IDS.SCROLL]: { isActiveSrc: true, isActiveDest: false },
[CHAIN_IDS.POLYGON]: { isActiveSrc: false, isActiveDest: true },
[CHAIN_IDS.ARBITRUM]: { isActiveSrc: false, isActiveDest: true },
},
const commonConfig = {
maxRefreshCount: 3,
refreshRate: 3,
support: true,
chains: {
[CHAIN_IDS.OPTIMISM]: { isActiveSrc: true, isActiveDest: false },
[CHAIN_IDS.SCROLL]: { isActiveSrc: true, isActiveDest: false },
[CHAIN_IDS.POLYGON]: { isActiveSrc: false, isActiveDest: true },
[CHAIN_IDS.ARBITRUM]: { isActiveSrc: false, isActiveDest: true },
},
};

const expectedFeatureFlagsResponse = {
extensionConfig: commonConfig,
mobileConfig: commonConfig,
};
expect(bridgeController.state).toStrictEqual(EMPTY_INIT_STATE);

const setIntervalLengthSpy = jest.spyOn(
Expand Down
16 changes: 10 additions & 6 deletions packages/bridge-controller/src/constants/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ export const REFRESH_INTERVAL_MS = 30 * 1000;
export const DEFAULT_MAX_REFRESH_COUNT = 5;

export const BRIDGE_CONTROLLER_NAME = 'BridgeController';

export const DEFAULT_FEATURE_FLAG_CONFIG = {
refreshRate: REFRESH_INTERVAL_MS,
maxRefreshCount: DEFAULT_MAX_REFRESH_COUNT,
support: false,
chains: {},
};

export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = {
bridgeFeatureFlags: {
[BridgeFeatureFlagsKey.EXTENSION_CONFIG]: {
refreshRate: REFRESH_INTERVAL_MS,
maxRefreshCount: DEFAULT_MAX_REFRESH_COUNT,
support: false,
chains: {},
},
[BridgeFeatureFlagsKey.EXTENSION_CONFIG]: DEFAULT_FEATURE_FLAG_CONFIG,
[BridgeFeatureFlagsKey.MOBILE_CONFIG]: DEFAULT_FEATURE_FLAG_CONFIG,
},
quoteRequest: {
walletAddress: undefined,
Expand Down
14 changes: 14 additions & 0 deletions packages/bridge-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export type BridgeToken = {

export enum BridgeFlag {
EXTENSION_CONFIG = 'extension-config',
MOBILE_CONFIG = 'mobile-config',
}
type DecimalChainId = string;
export type GasMultiplierByChainId = Record<DecimalChainId, number>;
Expand All @@ -92,6 +93,12 @@ export type FeatureFlagResponse = {
support: boolean;
chains: Record<number, ChainConfiguration>;
};
[BridgeFlag.MOBILE_CONFIG]: {
refreshRate: number;
maxRefreshCount: number;
support: boolean;
chains: Record<number, ChainConfiguration>;
};
};

export type BridgeAsset = {
Expand Down Expand Up @@ -201,6 +208,7 @@ export type TxData = {
};
export enum BridgeFeatureFlagsKey {
EXTENSION_CONFIG = 'extensionConfig',
MOBILE_CONFIG = 'mobileConfig',
}

export type BridgeFeatureFlags = {
Expand All @@ -210,6 +218,12 @@ export type BridgeFeatureFlags = {
support: boolean;
chains: Record<Hex, ChainConfiguration>;
};
[BridgeFeatureFlagsKey.MOBILE_CONFIG]: {
refreshRate: number;
maxRefreshCount: number;
support: boolean;
chains: Record<Hex, ChainConfiguration>;
};
};
export enum RequestStatus {
LOADING,
Expand Down
171 changes: 90 additions & 81 deletions packages/bridge-controller/src/utils/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,44 @@ import { CHAIN_IDS } from '../constants/chains';

const mockFetchFn = jest.fn();

describe('Bridge utils', () => {
describe('fetch', () => {
describe('fetchBridgeFeatureFlags', () => {
it('should fetch bridge feature flags successfully', async () => {
const mockResponse = {
'extension-config': {
refreshRate: 3,
maxRefreshCount: 1,
support: true,
chains: {
'1': {
isActiveSrc: true,
isActiveDest: true,
},
'10': {
isActiveSrc: true,
isActiveDest: false,
},
'59144': {
isActiveSrc: true,
isActiveDest: true,
},
'120': {
isActiveSrc: true,
isActiveDest: false,
},
'137': {
isActiveSrc: false,
isActiveDest: true,
},
'11111': {
isActiveSrc: false,
isActiveDest: true,
},
const commonResponse = {
refreshRate: 3,
maxRefreshCount: 1,
support: true,
chains: {
'1': {
isActiveSrc: true,
isActiveDest: true,
},
'10': {
isActiveSrc: true,
isActiveDest: false,
},
'59144': {
isActiveSrc: true,
isActiveDest: true,
},
'120': {
isActiveSrc: true,
isActiveDest: false,
},
'137': {
isActiveSrc: false,
isActiveDest: true,
},
'11111': {
isActiveSrc: false,
isActiveDest: true,
},
},
};
const mockResponse = {
'extension-config': commonResponse,
'mobile-config': commonResponse,
};

mockFetchFn.mockResolvedValue(mockResponse);

Expand All @@ -63,59 +65,64 @@ describe('Bridge utils', () => {
},
);

expect(result).toStrictEqual({
extensionConfig: {
maxRefreshCount: 1,
refreshRate: 3,
support: true,
chains: {
[CHAIN_IDS.MAINNET]: {
isActiveSrc: true,
isActiveDest: true,
},
[CHAIN_IDS.OPTIMISM]: {
isActiveSrc: true,
isActiveDest: false,
},
[CHAIN_IDS.LINEA_MAINNET]: {
isActiveSrc: true,
isActiveDest: true,
},
'0x78': {
isActiveSrc: true,
isActiveDest: false,
},
[CHAIN_IDS.POLYGON]: {
isActiveSrc: false,
isActiveDest: true,
},
'0x2b67': {
isActiveSrc: false,
isActiveDest: true,
},
const commonExpected = {
maxRefreshCount: 1,
refreshRate: 3,
support: true,
chains: {
[CHAIN_IDS.MAINNET]: {
isActiveSrc: true,
isActiveDest: true,
},
[CHAIN_IDS.OPTIMISM]: {
isActiveSrc: true,
isActiveDest: false,
},
[CHAIN_IDS.LINEA_MAINNET]: {
isActiveSrc: true,
isActiveDest: true,
},
'0x78': {
isActiveSrc: true,
isActiveDest: false,
},
[CHAIN_IDS.POLYGON]: {
isActiveSrc: false,
isActiveDest: true,
},
'0x2b67': {
isActiveSrc: false,
isActiveDest: true,
},
},
};

expect(result).toStrictEqual({
extensionConfig: commonExpected,
mobileConfig: commonExpected,
});
});

it('should use fallback bridge feature flags if response is unexpected', async () => {
const mockResponse = {
'extension-config': {
refreshRate: 3,
maxRefreshCount: 1,
support: 25,
chains: {
a: {
isActiveSrc: 1,
isActiveDest: 'test',
},
'2': {
isActiveSrc: 'test',
isActiveDest: 2,
},
const commonResponse = {
refreshRate: 3,
maxRefreshCount: 1,
support: 25,
chains: {
a: {
isActiveSrc: 1,
isActiveDest: 'test',
},
'2': {
isActiveSrc: 'test',
isActiveDest: 2,
},
},
};
const mockResponse = {
'extension-config': commonResponse,
'mobile-config': commonResponse,
};

mockFetchFn.mockResolvedValue(mockResponse);

Expand All @@ -131,13 +138,15 @@ describe('Bridge utils', () => {
},
);

const commonExpected = {
maxRefreshCount: 5,
refreshRate: 30000,
support: false,
chains: {},
};
expect(result).toStrictEqual({
extensionConfig: {
maxRefreshCount: 5,
refreshRate: 30000,
support: false,
chains: {},
},
extensionConfig: commonExpected,
mobileConfig: commonExpected,
});
});

Expand Down
Loading
Loading