Skip to content

Commit 2bee8b4

Browse files
authored
refactor: remove unused testnet config for swaps (MetaMask#24244)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** In MM mobile, we define an array of allowed testnets for swaps. This is consumed only on DEV env and is used by some utilities like isSwapsAllowed. To complete the legacy migration we will have to take care of those utilities (either remove or migrate) but before that, this PR removes the dependency to testnets and reduce the tech debt of the swaps module. <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: null ## **Related issues** Fixes: https://consensyssoftware.atlassian.net/browse/SWAPS-3594 ## **Manual testing steps** ```gherkin This PR does not introduce any business logic change, just run a basic regression test to ensure swaps functionality is not affected. ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Removes testnet-specific swaps configuration and dev-only paths, simplifying chain gating and feature flag handling for Swaps. > > - Drops `SWAPS_TESTNET_CHAIN_ID` and `allowedTestnetChainIds`; removes `__DEV__` and `ONLY_MAINNET` branching in `isSwapsAllowed` > - Updates allowed chain lists in `UI/Swaps/utils` and `Engine/constants` to include only supported mainnets and multichain scopes > - Simplifies `getFeatureFlagChainId` to identity and adjusts liveness/feature flag mapping in `reducers/swaps` > - Cleans up imports and removes unused constants > - Updates unit tests to reflect removal of testnet logic and identity `getFeatureFlagChainId` > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit fba3425. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 76a0e9e commit 2bee8b4

6 files changed

Lines changed: 13 additions & 119 deletions

File tree

app/components/UI/Swaps/utils/index.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@ import BigNumber from 'bignumber.js';
33
import { swapsUtils } from '@metamask/swaps-controller';
44
import { strings } from '../../../../../locales/i18n';
55
import AppConstants from '../../../../core/AppConstants';
6-
import { NETWORKS_CHAIN_ID } from '../../../../constants/network';
76
import { SolScope, BtcScope, TrxScope } from '@metamask/keyring-api';
87
import { CHAIN_IDS } from '@metamask/transaction-controller';
9-
import {
10-
NATIVE_SWAPS_TOKEN_ADDRESS,
11-
SWAPS_TESTNET_CHAIN_ID,
12-
} from '../../../../constants/bridge';
138
import { isSwapsNativeAsset } from '../../../../util/bridge';
149

1510
const allowedChainIds = [
@@ -24,25 +19,12 @@ const allowedChainIds = [
2419
CHAIN_IDS.BASE,
2520
CHAIN_IDS.SEI,
2621
CHAIN_IDS.MONAD,
27-
SWAPS_TESTNET_CHAIN_ID,
2822
];
2923

30-
export const allowedTestnetChainIds = [
31-
NETWORKS_CHAIN_ID.GOERLI,
32-
NETWORKS_CHAIN_ID.SEPOLIA,
33-
];
34-
35-
if (__DEV__) {
36-
allowedChainIds.push(...allowedTestnetChainIds);
37-
}
38-
3924
export function isSwapsAllowed(chainId) {
4025
if (!AppConstants.SWAPS.ACTIVE) {
4126
return false;
4227
}
43-
if (!AppConstants.SWAPS.ONLY_MAINNET) {
44-
allowedChainIds.push(SWAPS_TESTNET_CHAIN_ID);
45-
}
4628

4729
if (
4830
chainId === SolScope.Mainnet ||

app/components/UI/Swaps/utils/index.test.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
isSwapsAllowed,
55
} from './index';
66
import { CHAIN_IDS } from '@metamask/transaction-controller';
7-
import { SWAPS_TESTNET_CHAIN_ID } from '../../../../constants/bridge';
87

98
// Mock AppConstants
109
const mockSwapsConstantsGetter = jest.fn(() => ({
@@ -221,24 +220,4 @@ describe('isSwapsAllowed', () => {
221220
const unsupportedChainId = '0x9999';
222221
expect(isSwapsAllowed(unsupportedChainId)).toBe(false);
223222
});
224-
225-
describe('testnet chain IDs', () => {
226-
it('should return true for testnet chain IDs in development when ONLY_MAINNET is true', () => {
227-
global.__DEV__ = true;
228-
mockSwapsConstantsGetter.mockReturnValue({
229-
...mockSwapsConstantsGetter(),
230-
ONLY_MAINNET: true,
231-
});
232-
expect(isSwapsAllowed(SWAPS_TESTNET_CHAIN_ID)).toBe(true);
233-
});
234-
235-
it('should return true for testnet chain IDs when ONLY_MAINNET is false', () => {
236-
global.__DEV__ = false;
237-
mockSwapsConstantsGetter.mockReturnValue({
238-
...mockSwapsConstantsGetter(),
239-
ONLY_MAINNET: false,
240-
});
241-
expect(isSwapsAllowed(SWAPS_TESTNET_CHAIN_ID)).toBe(true);
242-
});
243-
});
244223
});

app/constants/bridge.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ import {
66
BRIDGE_PROD_API_BASE_URL,
77
} from '@metamask/bridge-controller';
88

9-
/**
10-
* Swaps testnet chain ID (1337 in decimal)
11-
* Used for testing swaps functionality on local/test networks
12-
*/
13-
export const SWAPS_TESTNET_CHAIN_ID: Hex = '0x539';
14-
159
/**
1610
* Native token address (zero address)
1711
* Used to represent native tokens (ETH, BNB, MATIC, etc.) across all EVM chains

app/core/Engine/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { CHAIN_IDS } from '@metamask/transaction-controller';
2-
import { SWAPS_TESTNET_CHAIN_ID } from '../../constants/bridge';
32

43
/**
54
* Messageable modules that are part of the Engine's context, but are not defined with state.
@@ -88,7 +87,6 @@ export const BACKGROUND_STATE_CHANGE_EVENT_NAMES = [
8887
export const swapsSupportedChainIds = [
8988
CHAIN_IDS.MAINNET,
9089
CHAIN_IDS.BSC,
91-
SWAPS_TESTNET_CHAIN_ID,
9290
CHAIN_IDS.POLYGON,
9391
CHAIN_IDS.AVALANCHE,
9492
CHAIN_IDS.ARBITRUM,

app/reducers/swaps/index.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,23 @@ import { createSelector } from 'reselect';
22
import { isMainnetByChainId } from '../../util/networks';
33
import { safeToChecksumAddress, areAddressesEqual } from '../../util/address';
44
import { lte } from '../../util/lodash';
5-
import {
6-
selectChainId,
7-
selectEvmChainId,
8-
} from '../../selectors/networkController';
5+
import { selectEvmChainId } from '../../selectors/networkController';
96
import {
107
selectAllTokens,
118
selectTokens,
129
} from '../../selectors/tokensController';
1310
import { selectTokenList } from '../../selectors/tokenListController';
1411
import { selectContractBalances } from '../../selectors/tokenBalancesController';
15-
import { getChainFeatureFlags, getSwapsLiveness } from './utils';
16-
import { allowedTestnetChainIds } from '../../components/UI/Swaps/utils';
17-
import { NETWORKS_CHAIN_ID } from '../../constants/network';
12+
import { getSwapsLiveness } from './utils';
1813
import { selectSelectedInternalAccountAddress } from '../../selectors/accountsController';
1914
import { CHAIN_ID_TO_NAME_MAP } from '@metamask/swaps-controller/dist/constants';
2015
import { invert, omit } from 'lodash';
2116
import { createDeepEqualSelector } from '../../selectors/util';
2217
import { toHex } from '@metamask/controller-utils';
23-
import { SolScope } from '@metamask/keyring-api';
24-
25-
// If we are in dev and on a testnet, just use mainnet feature flags,
26-
// since we don't have feature flags for testnets in the API
27-
export const getFeatureFlagChainId = (chainId) =>
28-
typeof __DEV__ !== 'undefined' &&
29-
__DEV__ &&
30-
allowedTestnetChainIds.includes(chainId)
31-
? NETWORKS_CHAIN_ID.MAINNET
32-
: chainId;
18+
19+
// Identity function, will be removed when legacy swaps is removed,
20+
// but keep it for now to keep changes atomic.
21+
export const getFeatureFlagChainId = (chainId) => chainId;
3322

3423
// * Constants
3524
export const SWAPS_SET_LIVENESS = 'SWAPS_SET_LIVENESS';

app/reducers/swaps/swaps.test.ts

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Device from '../../util/device';
44
import { NetworkClientType } from '@metamask/network-controller';
55
// eslint-disable-next-line import/no-namespace
66
import * as tokensControllerSelectors from '../../selectors/tokensController';
7-
import { NETWORKS_CHAIN_ID } from '../../constants/network';
87
import { FeatureFlags } from '@metamask/swaps-controller/dist/types';
98

109
// Type definitions for the swaps reducer
@@ -42,15 +41,11 @@ interface SwapsState {
4241
}
4342

4443
jest.mock('../../selectors/tokensController');
45-
jest.mock('../../components/UI/Swaps/utils', () => ({
46-
allowedTestnetChainIds: ['0xaa36a7'], // Sepolia testnet
47-
}));
4844
jest.mock('@metamask/swaps-controller/dist/constants', () => ({
4945
CHAIN_ID_TO_NAME_MAP: {
5046
'0x1': 'ethereum',
5147
'0x38': 'bsc',
5248
'0x89': 'polygon',
53-
'0xaa36a7': 'sepolia',
5449
},
5550
}));
5651

@@ -111,16 +106,6 @@ const DEFAULT_FEATURE_FLAGS = {
111106
} as unknown as FeatureFlags;
112107

113108
describe('swaps reducer', () => {
114-
const withGlobalDev = (devValue: boolean, testFn: () => void) => {
115-
const originalDev = (global as { __DEV__?: boolean }).__DEV__;
116-
(global as { __DEV__?: boolean }).__DEV__ = devValue;
117-
try {
118-
testFn();
119-
} finally {
120-
(global as { __DEV__?: boolean }).__DEV__ = originalDev;
121-
}
122-
};
123-
124109
it('should return initial state', () => {
125110
const state = reducer(undefined, emptyAction);
126111
expect(state).toEqual(initialState);
@@ -299,30 +284,6 @@ describe('swaps reducer', () => {
299284
).toEqual(DEFAULT_FEATURE_FLAGS.bsc);
300285
});
301286

302-
it('should handle testnet chain IDs in dev mode', () => {
303-
withGlobalDev(true, () => {
304-
const initalState = reducer(undefined, emptyAction);
305-
const action: SetLivenessAction = {
306-
type: SWAPS_SET_LIVENESS,
307-
payload: {
308-
featureFlags: DEFAULT_FEATURE_FLAGS,
309-
chainId: '0xaa36a7', // Sepolia testnet
310-
},
311-
};
312-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
313-
const liveState = reducer(initalState as any, action) as SwapsState;
314-
315-
// Should use mainnet feature flags for testnet
316-
expect(liveState['0x1'].featureFlags).toEqual(
317-
DEFAULT_FEATURE_FLAGS.ethereum,
318-
);
319-
// Should also set the testnet chain with mainnet flags
320-
expect(
321-
(liveState['0xaa36a7'] as { featureFlags?: unknown }).featureFlags,
322-
).toEqual(DEFAULT_FEATURE_FLAGS.ethereum);
323-
});
324-
});
325-
326287
it('should preserve existing state when updating feature flags', () => {
327288
const existingState = {
328289
...initialState,
@@ -404,25 +365,16 @@ describe('swaps reducer', () => {
404365
});
405366

406367
describe('getFeatureFlagChainId', () => {
407-
it('should return mainnet chain ID for testnets in dev mode', () => {
408-
withGlobalDev(true, () => {
409-
const result = getFeatureFlagChainId('0xaa36a7'); // Sepolia
410-
expect(result).toBe(NETWORKS_CHAIN_ID.MAINNET);
411-
});
412-
});
368+
it('returns the same chain ID without modification', () => {
369+
const result = getFeatureFlagChainId('0x1');
413370

414-
it('should return original chain ID for non-testnets', () => {
415-
withGlobalDev(true, () => {
416-
const result = getFeatureFlagChainId('0x38'); // BSC
417-
expect(result).toBe('0x38');
418-
});
371+
expect(result).toBe('0x1');
419372
});
420373

421-
it('should return original chain ID when not in dev mode', () => {
422-
withGlobalDev(false, () => {
423-
const result = getFeatureFlagChainId('0xaa36a7'); // Sepolia
424-
expect(result).toBe('0xaa36a7');
425-
});
374+
it('returns the same chain ID for any input', () => {
375+
const result = getFeatureFlagChainId('0x38');
376+
377+
expect(result).toBe('0x38');
426378
});
427379
});
428380

0 commit comments

Comments
 (0)