Skip to content
Open
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
3 changes: 3 additions & 0 deletions app/scripts/constants/sentry-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ export const SENTRY_BACKGROUND_STATE = {
lastFetchedBlockNumbers: false,
methodData: false,
},
TransactionPayController: {
transactionData: false,
},
TxController: {
transactions: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const TransactionControllerInit: ControllerInitFunction<
getGasFeeEstimates: (...args) =>
gasFeeController().fetchGasFeeEstimates(...args),
getNetworkClientRegistry: (...args) =>
// @ts-expect-error - NetworkController registry type mismatch between peer dependencies
networkController().getNetworkClientRegistry(...args),
getNetworkState: () => networkController().state,
// @ts-expect-error Controller type does not support undefined return value
Expand Down
3 changes: 3 additions & 0 deletions app/scripts/controller-init/controller-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { PPOMController } from '@metamask/ppom-validator';
import { SmartTransactionsController } from '@metamask/smart-transactions-controller';
import { TransactionController } from '@metamask/transaction-controller';
import { TransactionPayController } from '@metamask/transaction-pay-controller';
import { AccountsController } from '@metamask/accounts-controller';
import {
AccountTrackerController,
Expand Down Expand Up @@ -182,6 +183,7 @@ export type Controller =
| TokenListController
| TokensController
| TransactionController
| TransactionPayController
| InstitutionalSnapController
| UserOperationController
| UserStorageController
Expand Down Expand Up @@ -261,6 +263,7 @@ export type ControllerFlatState = AccountOrderController['state'] &
TokenListController['state'] &
TokensController['state'] &
TransactionController['state'] &
TransactionPayController['state'] &
UserOperationController['state'] &
UserStorageController['state'] &
TokenRatesController['state'] &
Expand Down
13 changes: 13 additions & 0 deletions app/scripts/controller-init/messengers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import {
getTransactionControllerMessenger,
getTransactionControllerInitMessenger,
} from './transaction-controller-messenger';
import {
getTransactionPayControllerMessenger,
getTransactionPayControllerInitMessenger,
} from './transaction-pay-controller-messenger';
import {
getBackendWebSocketServiceMessenger,
getBackendWebSocketServiceInitMessenger,
Expand Down Expand Up @@ -392,6 +396,11 @@ export {
getTokensControllerMessenger,
getTokensControllerInitMessenger,
} from './tokens-controller-messenger';
export type { TransactionPayControllerInitMessenger } from './transaction-pay-controller-messenger';
export {
getTransactionPayControllerMessenger,
getTransactionPayControllerInitMessenger,
} from './transaction-pay-controller-messenger';
export type {
UserOperationControllerMessenger,
UserOperationControllerInitMessenger,
Expand Down Expand Up @@ -696,6 +705,10 @@ export const CONTROLLER_MESSENGERS = {
getMessenger: getTransactionControllerMessenger,
getInitMessenger: getTransactionControllerInitMessenger,
},
TransactionPayController: {
getMessenger: getTransactionPayControllerMessenger,
getInitMessenger: getTransactionPayControllerInitMessenger,
},
UserOperationController: {
getMessenger: getUserOperationControllerMessenger,
getInitMessenger: getUserOperationControllerInitMessenger,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Messenger } from '@metamask/messenger';
import { getRootMessenger } from '../../lib/messenger';
import {
getTransactionPayControllerInitMessenger,
getTransactionPayControllerMessenger,
} from './transaction-pay-controller-messenger';

describe('getTransactionPayControllerMessenger', () => {
it('returns a restricted messenger', () => {
const messenger = getRootMessenger<never, never>();
const transactionPayControllerMessenger =
getTransactionPayControllerMessenger(messenger);

expect(transactionPayControllerMessenger).toBeInstanceOf(Messenger);
});
});

describe('getTransactionPayControllerInitMessenger', () => {
it('returns a restricted messenger', () => {
const messenger = getRootMessenger<never, never>();
const transactionPayControllerInitMessenger =
getTransactionPayControllerInitMessenger(messenger);

expect(transactionPayControllerInitMessenger).toBeInstanceOf(Messenger);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
Messenger,
MessengerActions,
MessengerEvents,
} from '@metamask/messenger';
import type { TransactionPayControllerMessenger } from '@metamask/transaction-pay-controller';
import type { DelegationControllerSignDelegationAction } from '@metamask/delegation-controller';
import type { KeyringControllerSignEip7702AuthorizationAction } from '@metamask/keyring-controller';
import type { RootMessenger } from '../../lib/messenger';

export function getTransactionPayControllerMessenger(
messenger: RootMessenger,
): TransactionPayControllerMessenger {
const controllerMessenger = new Messenger<
'TransactionPayController',
MessengerActions<TransactionPayControllerMessenger>,
MessengerEvents<TransactionPayControllerMessenger>,
typeof messenger
>({
namespace: 'TransactionPayController',
parent: messenger,
});

messenger.delegate({
messenger: controllerMessenger,
actions: [
'AccountTrackerController:getState',
'BridgeController:fetchQuotes',
'BridgeStatusController:submitTx',
'CurrencyRateController:getState',
'GasFeeController:getState',
'NetworkController:findNetworkClientIdByChainId',
'NetworkController:getNetworkClientById',
'RemoteFeatureFlagController:getState',
'TokenBalancesController:getState',
'TokenListController:getState',
'TokenRatesController:getState',
'TokensController:getState',
'TransactionController:getGasFeeTokens',
'TransactionController:getState',
'TransactionController:updateTransaction',
],
events: [
'BridgeStatusController:stateChange',
'TransactionController:stateChange',
'TransactionController:unapprovedTransactionAdded',
],
});

return controllerMessenger;
}

type InitMessengerActions =
| DelegationControllerSignDelegationAction
| KeyringControllerSignEip7702AuthorizationAction;

type InitMessengerEvents = never;

export type TransactionPayControllerInitMessenger = ReturnType<
typeof getTransactionPayControllerInitMessenger
>;

export function getTransactionPayControllerInitMessenger(
messenger: RootMessenger<InitMessengerActions, InitMessengerEvents>,
) {
const controllerInitMessenger = new Messenger<
'TransactionPayControllerInit',
InitMessengerActions,
InitMessengerEvents,
typeof messenger
>({
namespace: 'TransactionPayControllerInit',
parent: messenger,
});

messenger.delegate({
messenger: controllerInitMessenger,
actions: [
'DelegationController:signDelegation',
'KeyringController:signEip7702Authorization',
],
events: [],
});

return controllerInitMessenger;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
TransactionPayController,
TransactionPayControllerMessenger,
} from '@metamask/transaction-pay-controller';
import { getRootMessenger } from '../lib/messenger';
import { ControllerInitRequest } from './types';
import { buildControllerInitRequestMock } from './test/utils';
import {
getTransactionPayControllerMessenger,
getTransactionPayControllerInitMessenger,
TransactionPayControllerInitMessenger,
} from './messengers';
import { TransactionPayControllerInit } from './transaction-pay-controller-init';

jest.mock('@metamask/transaction-pay-controller');

function getInitRequestMock(): jest.Mocked<
ControllerInitRequest<
TransactionPayControllerMessenger,
TransactionPayControllerInitMessenger
>
> {
const baseMessenger = getRootMessenger<never, never>();

const requestMock = {
...buildControllerInitRequestMock(),
controllerMessenger: getTransactionPayControllerMessenger(baseMessenger),
initMessenger: getTransactionPayControllerInitMessenger(baseMessenger),
};

return requestMock;
}

describe('TransactionPayControllerInit', () => {
it('initializes the controller', () => {
const { controller } = TransactionPayControllerInit(getInitRequestMock());
expect(controller).toBeInstanceOf(TransactionPayController);
});

it('passes the proper arguments to the controller', () => {
TransactionPayControllerInit(getInitRequestMock());

const controllerMock = jest.mocked(TransactionPayController);
expect(controllerMock).toHaveBeenCalledWith({
getDelegationTransaction: expect.any(Function),
getStrategy: expect.any(Function),
messenger: expect.any(Object),
state: undefined,
});
});
});
31 changes: 31 additions & 0 deletions app/scripts/controller-init/transaction-pay-controller-init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
TransactionPayController,
TransactionPayControllerMessenger,
TransactionPayStrategy,
} from '@metamask/transaction-pay-controller';
import type { TransactionMeta } from '@metamask/transaction-controller';
import type { ControllerInitFunction } from './types';
import type { TransactionPayControllerInitMessenger } from './messengers';

export const TransactionPayControllerInit: ControllerInitFunction<
TransactionPayController,
TransactionPayControllerMessenger,
TransactionPayControllerInitMessenger
> = (request) => {
const { controllerMessenger, persistedState } = request;

const controller = new TransactionPayController({
getDelegationTransaction: async () => {
throw new Error('Delegation transaction not supported');
},
getStrategy,
messenger: controllerMessenger,
state: persistedState.TransactionPayController,
});

return { controller };
};

function getStrategy(_transaction: TransactionMeta): TransactionPayStrategy {
return TransactionPayStrategy.Relay;
}
1 change: 1 addition & 0 deletions app/scripts/controller-init/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type ControllersToInitialize =
| 'SnapInterfaceController'
| 'PPOMController'
| 'TransactionController'
| 'TransactionPayController'
| 'UserStorageController';

type InitFunction<Name extends ControllersToInitialize> =
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ import {
TokenRatesControllerInit,
} from './controller-init/assets';
import { TransactionControllerInit } from './controller-init/confirmations/transaction-controller-init';
import { TransactionPayControllerInit } from './controller-init/transaction-pay-controller-init';
import { PPOMControllerInit } from './controller-init/confirmations/ppom-controller-init';
import { SmartTransactionsControllerInit } from './controller-init/smart-transactions/smart-transactions-controller-init';
import { initControllers } from './controller-init/utils';
Expand Down Expand Up @@ -609,6 +610,7 @@ export default class MetamaskController extends EventEmitter {
PhishingController: PhishingControllerInit,
AccountTrackerController: AccountTrackerControllerInit,
TransactionController: TransactionControllerInit,
TransactionPayController: TransactionPayControllerInit,
SmartTransactionsController: SmartTransactionsControllerInit,
SwapsController: SwapsControllerInit,
BridgeController: BridgeControllerInit,
Expand Down
Loading
Loading