Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,10 @@ export async function publishHook({
transactionController: TransactionController;
transactionMeta: TransactionMeta;
}) {
const { isSmartTransaction, featureFlags, isHardwareWalletAccount } =
getSmartTransactionCommonParams(flatState, transactionMeta.chainId);
const { isSmartTransaction, featureFlags } = getSmartTransactionCommonParams(
flatState,
transactionMeta.chainId,
);
const sendBundleSupport = await isSendBundleSupported(
transactionMeta.chainId,
);
Expand Down Expand Up @@ -420,8 +422,6 @@ export async function publishHook({
smartTransactionsController,
controllerMessenger: initMessenger,
isSmartTransaction,
isHardwareWallet: isHardwareWalletAccount,
// @ts-expect-error Smart transaction selector return type does not match FeatureFlags type from hook
featureFlags,
});

Expand Down Expand Up @@ -462,8 +462,10 @@ export function publishBatchHook({
);
}

const { isSmartTransaction, featureFlags, isHardwareWalletAccount } =
getSmartTransactionCommonParams(flatState, transactionMeta.chainId);
const { isSmartTransaction, featureFlags } = getSmartTransactionCommonParams(
flatState,
transactionMeta.chainId,
);

if (!isSmartTransaction) {
return undefined;
Expand All @@ -475,8 +477,6 @@ export function publishBatchHook({
smartTransactionsController,
controllerMessenger: hookControllerMessenger,
isSmartTransaction,
isHardwareWallet: isHardwareWalletAccount,
// @ts-expect-error Smart transaction selector return type does not match FeatureFlags type from hook
featureFlags,
transactionMeta,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,46 @@
import { Messenger } from '@metamask/messenger';
import type {
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;

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',
'NetworkController:getNetworkClientById',
'NetworkController:getState',
'RemoteFeatureFlagController:getState',
'TransactionController:getNonceLock',
'TransactionController:getTransactions',
'TransactionController:updateTransaction',
],
events: ['NetworkController:stateChange'],
events: [
'NetworkController:stateChange',
'RemoteFeatureFlagController:stateChange',
],
});
return controllerMessenger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function getSubscriptionServiceMessenger(
'SmartTransactionsController:getState',
'NetworkController:getState',
'SwapsController:getState',
'RemoteFeatureFlagController:getState',
Comment thread
rarquevaux marked this conversation as resolved.
'MetaMetricsController:trackEvent',
'KeyringController:getState',
// Rewards Integration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
SmartTransactionsController,
SmartTransactionsControllerMessenger,
ClientId,
} from '@metamask/smart-transactions-controller';
import {
Expand All @@ -20,7 +21,6 @@ import type {
import {
getSmartTransactionsControllerInitMessenger,
SmartTransactionsControllerInitMessenger,
SmartTransactionsControllerMessenger,
} from '../messengers/smart-transactions-controller-messenger';
import { ControllerFlatState } from '../controller-list';
import type {
Expand All @@ -35,10 +35,7 @@ jest.mock('@metamask/smart-transactions-controller');
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 @@ describe('SmartTransactionsController Init', () => {

const transactionController: MockTransactionController = {
getNonceLock: jest.fn().mockResolvedValue({ releaseLock: jest.fn() }),
confirmExternalTransaction: jest.fn(),
Comment thread
dan437 marked this conversation as resolved.
getTransactions: jest.fn().mockReturnValue([]),
updateTransaction: jest.fn(),
};
Expand Down Expand Up @@ -324,63 +320,6 @@ describe('SmartTransactionsController Init', () => {
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 @@ -57,12 +51,6 @@ export const SmartTransactionsControllerInit: ControllerInitFunction<
>[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