Skip to content

Commit

Permalink
Enable STX only for ETH mainnet in production and for ETH mainnet and…
Browse files Browse the repository at this point in the history
… Sepolia in non-prod

Resolve linting
  • Loading branch information
dan437 committed Aug 12, 2024
1 parent 4aa12c5 commit ad4db97
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 deletions.
33 changes: 33 additions & 0 deletions app/constants/smartTransactions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NETWORKS_CHAIN_ID } from './network';
import { isProduction } from '../util/environment';
import { getAllowedSmartTransactionsChainIds } from './smartTransactions';

jest.mock('../util/environment', () => ({
isProduction: jest.fn(() => false), // Initially mock isProduction to return false
}));

// Cast isProduction to jest.Mock to inform TypeScript about the mock type
const mockIsProduction = isProduction as jest.Mock;

describe('smartTransactions', () => {
describe('getAllowedSmartTransactionsChainIds', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('returns the correct chain IDs for development environment', () => {
mockIsProduction.mockReturnValue(false);
const allowedChainIds = getAllowedSmartTransactionsChainIds();
expect(allowedChainIds).toStrictEqual([
NETWORKS_CHAIN_ID.MAINNET,
NETWORKS_CHAIN_ID.SEPOLIA,
]);
});

it('returns the correct chain IDs for production environment', () => {
mockIsProduction.mockReturnValue(true);
const allowedChainIds = getAllowedSmartTransactionsChainIds();
expect(allowedChainIds).toStrictEqual([NETWORKS_CHAIN_ID.MAINNET]);
});
});
});
18 changes: 18 additions & 0 deletions app/constants/smartTransactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable import/prefer-default-export */

import { isProduction } from '../util/environment';
import { NETWORKS_CHAIN_ID } from './network';

const ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS_DEVELOPMENT: string[] = [
NETWORKS_CHAIN_ID.MAINNET,
NETWORKS_CHAIN_ID.SEPOLIA,
];

const ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS_PRODUCTION: string[] = [
NETWORKS_CHAIN_ID.MAINNET,
];

export const getAllowedSmartTransactionsChainIds = (): string[] =>
isProduction()
? ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS_PRODUCTION
: ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS_DEVELOPMENT;
8 changes: 2 additions & 6 deletions app/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ import {
networkIdWillUpdate,
} from '../core/redux/slices/inpageProvider';
import SmartTransactionsController from '@metamask/smart-transactions-controller';
import { NETWORKS_CHAIN_ID } from '../../app/constants/network';
import { getAllowedSmartTransactionsChainIds } from '../../app/constants/smartTransactions';
import { selectShouldUseSmartTransaction } from '../selectors/smartTransactionsController';
import { selectSwapsChainFeatureFlags } from '../reducers/swaps';
import { SmartTransactionStatuses } from '@metamask/smart-transactions-controller/dist/types';
Expand Down Expand Up @@ -1248,11 +1248,7 @@ class Engine {
trackMetaMetricsEvent: smartTransactionsControllerTrackMetaMetricsEvent,
},
{
supportedChainIds: [
NETWORKS_CHAIN_ID.MAINNET,
NETWORKS_CHAIN_ID.GOERLI,
NETWORKS_CHAIN_ID.SEPOLIA,
],
supportedChainIds: getAllowedSmartTransactionsChainIds(),
},
initialState.SmartTransactionsController,
);
Expand Down
8 changes: 2 additions & 6 deletions app/selectors/smartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import {
SmartTransactionStatuses,
} from '@metamask/smart-transactions-controller/dist/types';
import { selectSelectedInternalAccountChecksummedAddress } from './accountsController';
import { getAllowedSmartTransactionsChainIds } from '../../app/constants/smartTransactions';

export const ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS = [
NETWORKS_CHAIN_ID.MAINNET,
NETWORKS_CHAIN_ID.GOERLI,
NETWORKS_CHAIN_ID.SEPOLIA,
];
export const selectSmartTransactionsEnabled = (state: RootState) => {
const selectedAddress =
selectSelectedInternalAccountChecksummedAddress(state);
Expand All @@ -25,7 +21,7 @@ export const selectSmartTransactionsEnabled = (state: RootState) => {
const providerConfigRpcUrl = selectProviderConfig(state).rpcUrl;

const isAllowedNetwork =
ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS.includes(chainId);
getAllowedSmartTransactionsChainIds().includes(chainId);

// E.g. if a user has a Mainnet Flashbots RPC, we do not want to bypass it
// Only want to bypass on default mainnet RPC
Expand Down
24 changes: 24 additions & 0 deletions app/util/environment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { isProduction } from './environment';

const originalNodeEnvironment = process.env.NODE_ENV;

describe('isProduction', () => {
afterAll(() => {
process.env.NODE_ENV = originalNodeEnvironment;
});

it('returns true when NODE_ENV is "production"', () => {
process.env.NODE_ENV = 'production';
expect(isProduction()).toBe(true);
});

it('returns false when NODE_ENV is "development"', () => {
process.env.NODE_ENV = 'development';
expect(isProduction()).toBe(false);
});

it('returns false when NODE_ENV is "test"', () => {
process.env.NODE_ENV = 'test';
expect(isProduction()).toBe(false);
});
});
6 changes: 6 additions & 0 deletions app/util/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable import/prefer-default-export */

export const isProduction = (): boolean =>
// TODO: process.env.NODE_ENV === 'production' doesn't work with tests yet. Once we make it work,
// we can remove the following line and use the code above instead.
({ ...process.env }?.NODE_ENV === 'production');

0 comments on commit ad4db97

Please sign in to comment.