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
  • Loading branch information
dan437 committed Aug 12, 2024
1 parent 4aa12c5 commit bfa863b
Show file tree
Hide file tree
Showing 6 changed files with 83 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]);
});
});
});
17 changes: 17 additions & 0 deletions app/constants/smartTransactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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[] => {

Check failure on line 13 in app/constants/smartTransactions.ts

View workflow job for this annotation

GitHub Actions / scripts (lint)

Prefer default export on a file with single export

Check failure on line 13 in app/constants/smartTransactions.ts

View workflow job for this annotation

GitHub Actions / scripts (lint)

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return 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(() => {

Check failure on line 6 in app/util/environment.test.ts

View workflow job for this annotation

GitHub Actions / scripts (lint)

Delete `·`
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);
});
});
5 changes: 5 additions & 0 deletions app/util/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const isProduction = (): boolean => {

Check failure on line 1 in app/util/environment.ts

View workflow job for this annotation

GitHub Actions / scripts (lint)

Prefer default export on a file with single export

Check failure on line 1 in app/util/environment.ts

View workflow job for this annotation

GitHub Actions / scripts (lint)

Unexpected block statement surrounding arrow body; parenthesize the returned value and move it immediately after the `=>`
// 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.
return {...process.env}?.NODE_ENV === 'production';

Check failure on line 4 in app/util/environment.ts

View workflow job for this annotation

GitHub Actions / scripts (lint)

Replace `...process.env` with `·...process.env·`
};

0 comments on commit bfa863b

Please sign in to comment.