Skip to content
Draft
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
feat(STX-331): replace legacy STX swaps flags with smart transaction …
…flags from remote config API
  • Loading branch information
rarquevaux committed Dec 9, 2025
commit d93fcd9a7dc4ca6a60cd96c16904ac0ec4c7eb8f
Original file line number Diff line number Diff line change
@@ -1,55 +1,46 @@
import { Messenger } from '@metamask/messenger';
import type {
TransactionControllerConfirmExternalTransactionAction,
TransactionControllerGetNonceLockAction,
TransactionControllerGetTransactionsAction,
TransactionControllerUpdateTransactionAction,
} from '@metamask/transaction-controller';
import {
NetworkControllerGetNetworkClientByIdAction,
NetworkControllerGetStateAction,
NetworkControllerStateChangeEvent,
} from '@metamask/network-controller';
Messenger,
MessengerActions,
MessengerEvents,
} from '@metamask/messenger';
import { SmartTransactionsControllerMessenger } from '@metamask/smart-transactions-controller';
import { MetaMetricsControllerTrackEventAction } from '../../controllers/metametrics-controller';
import { RootMessenger } from '../../lib/messenger';

type MessengerActions =
| NetworkControllerGetNetworkClientByIdAction
| NetworkControllerGetStateAction
| TransactionControllerGetNonceLockAction
| TransactionControllerGetTransactionsAction
| TransactionControllerUpdateTransactionAction
| TransactionControllerConfirmExternalTransactionAction;

type MessengerEvents = NetworkControllerStateChangeEvent;

export type SmartTransactionsControllerMessenger = ReturnType<
typeof getSmartTransactionsControllerMessenger
>;

/**
* Get the messenger for the smart transactions controller. This is scoped to the
* actions and events that the smart transactions controller is allowed to handle.
*
* @param rootMessenger - The root messenger.
* @returns The SmartTransactionsControllerMessenger.
*/
export function getSmartTransactionsControllerMessenger(
messenger: RootMessenger<MessengerActions, MessengerEvents>,
) {
rootMessenger: RootMessenger,
): SmartTransactionsControllerMessenger {
const controllerMessenger = new Messenger<
'SmartTransactionsController',
MessengerActions,
MessengerEvents,
typeof messenger
MessengerActions<SmartTransactionsControllerMessenger>,
MessengerEvents<SmartTransactionsControllerMessenger>,
RootMessenger
>({
namespace: 'SmartTransactionsController',
parent: messenger,
parent: rootMessenger,
});
messenger.delegate({
rootMessenger.delegate({
messenger: controllerMessenger,
actions: [
'ErrorReportingService:captureException',

Check failure on line 32 in app/scripts/controller-init/messengers/smart-transactions-controller-messenger.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

Type '"ErrorReportingService:captureException"' is not assignable to type '"NetworkController:getState" | "NetworkController:getNetworkClientById" | "TransactionController:getTransactions" | "SmartTransactionsController:getState" | "TransactionController:confirmExternalTransaction" | "TransactionController:getNonceLock" | "TransactionController:updateTransaction"'.
'NetworkController:getNetworkClientById',
'NetworkController:getState',
'RemoteFeatureFlagController:getState',

Check failure on line 35 in app/scripts/controller-init/messengers/smart-transactions-controller-messenger.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

Type '"RemoteFeatureFlagController:getState"' is not assignable to type '"NetworkController:getState" | "NetworkController:getNetworkClientById" | "TransactionController:getTransactions" | "SmartTransactionsController:getState" | "TransactionController:confirmExternalTransaction" | "TransactionController:getNonceLock" | "TransactionController:updateTransaction"'.
'TransactionController:getNonceLock',
'TransactionController:confirmExternalTransaction',
'TransactionController:getTransactions',
'TransactionController:updateTransaction',
],
events: ['NetworkController:stateChange'],
events: [
'NetworkController:stateChange',
'RemoteFeatureFlagController:stateChange',

Check failure on line 42 in app/scripts/controller-init/messengers/smart-transactions-controller-messenger.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

Type '"RemoteFeatureFlagController:stateChange"' is not assignable to type '"NetworkController:stateChange" | "SmartTransactionsController:smartTransaction" | "SmartTransactionsController:stateChange" | "SmartTransactionsController:smartTransactionConfirmationDone"'.
],
});
return controllerMessenger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import {
getSmartTransactionsControllerInitMessenger,
SmartTransactionsControllerInitMessenger,
SmartTransactionsControllerMessenger,

Check failure on line 23 in app/scripts/controller-init/smart-transactions/smart-transactions-controller-init.test.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

'"../messengers/smart-transactions-controller-messenger"' has no exported member named 'SmartTransactionsControllerMessenger'. Did you mean 'getSmartTransactionsControllerMessenger'?
} from '../messengers/smart-transactions-controller-messenger';
import { ControllerFlatState } from '../controller-list';
import type {
Expand All @@ -35,10 +35,7 @@
type MockAccountsController = Pick<AccountsController, 'getSelectedAccount'>;
type MockTransactionController = Pick<
TransactionController,
| 'getNonceLock'
| 'confirmExternalTransaction'
| 'getTransactions'
| 'updateTransaction'
'getNonceLock' | 'getTransactions' | 'updateTransaction'
>;

type TestInitRequest = ControllerInitRequest<
Expand Down Expand Up @@ -96,7 +93,6 @@

const transactionController: MockTransactionController = {
getNonceLock: jest.fn().mockResolvedValue({ releaseLock: jest.fn() }),
confirmExternalTransaction: jest.fn(),
getTransactions: jest.fn().mockReturnValue([]),
updateTransaction: jest.fn(),
};
Expand Down Expand Up @@ -324,63 +320,6 @@
expect(listener).toHaveBeenCalledWith(testPayload);
});

describe('getFeatureFlags', () => {
it('returns feature flags from state', () => {
const { fullRequest } = buildInitRequest();
SmartTransactionsControllerInit(fullRequest);

const constructorCall =
smartTransactionsControllerClassMock.mock.calls[0][0];
const { getFeatureFlags } = constructorCall;

const result = getFeatureFlags();

expect(fullRequest.getUIState).toHaveBeenCalled();
expect(result).toHaveProperty('smartTransactions');
expect(result.smartTransactions).toHaveProperty('extensionActive');
expect(result.smartTransactions).toHaveProperty('mobileActive');
expect(result.smartTransactions).toHaveProperty('expectedDeadline');
expect(result.smartTransactions).toHaveProperty('maxDeadline');
expect(result.smartTransactions).toHaveProperty(
'extensionReturnTxHashAsap',
);
});

it('returns default feature flags when getFeatureFlagsByChainId returns null', () => {
// To test the null case, we need to make getStateUI return a state
// that would cause getFeatureFlagsByChainId to return null
const { fullRequest } = buildInitRequest({
getUIState: jest.fn().mockReturnValue({
preferences: {},
selectedNetworkClientId: 'mainnet',
networkConfigurationsByChainId: {
'0x1': {
chainId: '0x1',
rpcEndpoints: [
{
networkClientId: 'mainnet',
url: 'https://mainnet.infura.io/v3/abc',
},
],
},
},
// No swapsState to test null case
}),
});

SmartTransactionsControllerInit(fullRequest);

const constructorCall =
smartTransactionsControllerClassMock.mock.calls[0][0];
const { getFeatureFlags } = constructorCall;

const result = getFeatureFlags();

// When getFeatureFlagsByChainId returns null, the result should be null
expect(result).toBeNull();
});
});

describe('getMetaMetricsProps', () => {
it('returns correct meta metrics properties', async () => {
const { fullRequest } = buildInitRequest();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import {
SmartTransactionsController,
SmartTransactionsControllerMessenger,
ClientId,
} from '@metamask/smart-transactions-controller';
import type { Hex } from '@metamask/utils';
import type { TraceCallback } from '@metamask/controller-utils';
import { getAllowedSmartTransactionsChainIds } from '../../../../shared/constants/smartTransactions';
import { getFeatureFlagsByChainId } from '../../../../shared/modules/selectors';
import { type ProviderConfigState } from '../../../../shared/modules/selectors/networks';
import { type FeatureFlagsMetaMaskState } from '../../../../shared/modules/selectors/feature-flags';
import type { FeatureFlags } from '../../lib/smart-transaction/smart-transactions';
import { ControllerInitFunction, ControllerInitRequest } from '../types';
import {
SmartTransactionsControllerInitMessenger,
SmartTransactionsControllerMessenger,
} from '../messengers/smart-transactions-controller-messenger';
import { SmartTransactionsControllerInitMessenger } from '../messengers/smart-transactions-controller-messenger';
// This import is only used for the type.
// eslint-disable-next-line import/no-restricted-paths
import type { MetaMaskReduxState } from '../../../../ui/store/store';
Expand Down Expand Up @@ -46,7 +40,7 @@
trace,
} = request as SmartTransactionsControllerInitRequest;

const smartTransactionsController = new SmartTransactionsController({

Check failure on line 43 in app/scripts/controller-init/smart-transactions/smart-transactions-controller-init.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

Argument of type '{ supportedChainIds: `0x${string}`[]; clientId: ClientId.Extension; trackMetaMetricsEvent: (event: { event: MetaMetricsEventName; category: MetaMetricsEventCategory; properties?: { ...; } | ... 1 more ... | undefined; sensitiveProperties?: { ...; } | ... 1 more ... | undefined; }, options?: ({ ...; } & Record<...>) ...' is not assignable to parameter of type 'SmartTransactionsControllerOptions'.
supportedChainIds: getAllowedSmartTransactionsChainIds() as Hex[],
clientId: ClientId.Extension,
trackMetaMetricsEvent: initMessenger.call.bind(
Expand All @@ -57,12 +51,6 @@
>[0]['trackMetaMetricsEvent'],
state: persistedState.SmartTransactionsController,
messenger: controllerMessenger,
getFeatureFlags: () => {
const state = { metamask: getUIState() };
return getFeatureFlagsByChainId(
state as unknown as ProviderConfigState & FeatureFlagsMetaMaskState,
) as unknown as FeatureFlags;
},
getMetaMetricsProps: async () => {
const metamask = getUIState();
const { internalAccounts } = metamask;
Expand Down
Loading
Loading