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
17 changes: 10 additions & 7 deletions app/core/BackgroundBridge/BackgroundBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export class BackgroundBridge extends EventEmitter {
*/
getNonEvmSupportedMethods(scope) {
return Engine.controllerMessenger.call(
'MultichainRouter:getSupportedMethods',
'MultichainRoutingService:getSupportedMethods',
scope,
);
}
Expand Down Expand Up @@ -766,16 +766,19 @@ export class BackgroundBridge extends EventEmitter {
getNonEvmSupportedMethods: this.getNonEvmSupportedMethods.bind(this),
isNonEvmScopeSupported: Engine.controllerMessenger.call.bind(
Engine.controllerMessenger,
'MultichainRouter:isSupportedScope',
'MultichainRoutingService:isSupportedScope',
),
handleNonEvmRequestForOrigin: (params) =>
Engine.controllerMessenger.call('MultichainRouter:handleRequest', {
...params,
origin: this.origin,
}),
Engine.controllerMessenger.call(
'MultichainRoutingService:handleRequest',
{
...params,
origin: this.origin,
},
),
getNonEvmAccountAddresses: Engine.controllerMessenger.call.bind(
Engine.controllerMessenger,
'MultichainRouter:getSupportedAccounts',
'MultichainRoutingService:getSupportedAccounts',
),
trackSessionCreatedEvent: () => undefined,
}),
Expand Down
16 changes: 8 additions & 8 deletions app/core/Engine/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import {
executionServiceInit,
snapControllerInit,
snapInterfaceControllerInit,
snapsRegistryInit,
snapRegistryControllerInit,
} from './controllers/snaps';
import { RestrictedMethods } from '../Permissions/constants';
///: END:ONLY_INCLUDE_IF
Expand Down Expand Up @@ -171,7 +171,7 @@ import { phishingControllerInit } from './controllers/phishing-controller-init';
import { addressBookControllerInit } from './controllers/address-book-controller-init';
import { analyticsControllerInit } from './controllers/analytics-controller/analytics-controller-init';
import { connectivityControllerInit } from './controllers/connectivity/connectivity-controller-init';
import { multichainRouterInit } from './controllers/multichain-router-init';
import { multichainRoutingServiceInit } from './controllers/multichain-routing-service-init.ts';
import { profileMetricsControllerInit } from './controllers/profile-metrics-controller-init';
import { profileMetricsServiceInit } from './controllers/profile-metrics-service-init';
import { rampsServiceInit } from './controllers/ramps-controller/ramps-service-init';
Expand Down Expand Up @@ -334,7 +334,7 @@ export class Engine {
///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
ExecutionService: executionServiceInit,
CronjobController: cronjobControllerInit,
SnapsRegistry: snapsRegistryInit,
SnapRegistryController: snapRegistryControllerInit,
SnapController: snapControllerInit,
SnapInterfaceController: snapInterfaceControllerInit,
NotificationServicesController: notificationServicesControllerInit,
Expand All @@ -350,7 +350,7 @@ export class Engine {
MultichainAssetsController: multichainAssetsControllerInit,
MultichainAssetsRatesController: multichainAssetsRatesControllerInit,
MultichainBalancesController: multichainBalancesControllerInit,
MultichainRouter: multichainRouterInit,
MultichainRoutingService: multichainRoutingServiceInit,
MultichainTransactionsController: multichainTransactionsControllerInit,
MultichainAccountService: multichainAccountServiceInit,
///: END:ONLY_INCLUDE_IF
Expand Down Expand Up @@ -453,7 +453,7 @@ export class Engine {
const executionService = controllersByName.ExecutionService;
const snapController = controllersByName.SnapController;
const snapInterfaceController = controllersByName.SnapInterfaceController;
const snapsRegistry = controllersByName.SnapsRegistry;
const snapRegistryController = controllersByName.SnapRegistryController;
const webSocketService = controllersByName.WebSocketService;
const notificationServicesController =
controllersByName.NotificationServicesController;
Expand Down Expand Up @@ -538,7 +538,7 @@ export class Engine {
ExecutionService: executionService,
SnapController: snapController,
SnapInterfaceController: snapInterfaceController,
SnapsRegistry: snapsRegistry,
SnapRegistryController: snapRegistryController,
SubjectMetadataController: this.subjectMetadataController,
AuthenticationController: authenticationController,
UserStorageController: userStorageController,
Expand Down Expand Up @@ -1362,7 +1362,7 @@ export default {
NotificationServicesPushController,
SnapController,
SnapInterfaceController,
SnapsRegistry,
SnapRegistryController,
SubjectMetadataController,
UserStorageController,
///: END:ONLY_INCLUDE_IF
Expand Down Expand Up @@ -1434,7 +1434,7 @@ export default {
NotificationServicesPushController.state,
SnapController: SnapController.state,
SnapInterfaceController: SnapInterfaceController.state,
SnapsRegistry: SnapsRegistry.state,
SnapRegistryController: SnapRegistryController.state,
SubjectMetadataController: SubjectMetadataController.state,
UserStorageController: UserStorageController.state,
///: END:ONLY_INCLUDE_IF
Expand Down
2 changes: 1 addition & 1 deletion app/core/Engine/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const BACKGROUND_STATE_CHANGE_EVENT_NAMES = [
'MultichainNetworkController:stateChange',
///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
'SnapController:stateChange',
'SnapsRegistry:stateChange',
'SnapRegistryController:stateChange',
'SubjectMetadataController:stateChange',
'AuthenticationController:stateChange',
'UserStorageController:stateChange',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ function buildTransactionControllerMock(
partialMock?: Partial<TransactionController>,
): TransactionController {
const defaultMocks = {
addTransaction: jest.fn().mockResolvedValue('txId'),
estimateGasFee: jest.fn().mockResolvedValue({ gasLimit: '0x5208' }),
addTransactionBatch: jest.fn().mockResolvedValue(['txId1', 'txId2']),
updateTransaction: jest.fn().mockResolvedValue(undefined),
};

// @ts-expect-error Incomplete mock, just includes properties used by code-under-test
Expand Down Expand Up @@ -197,62 +194,6 @@ describe('BridgeStatusController Init', () => {
});
});

it('correctly sets up addTransactionFn', () => {
// Arrange
const mockTransactionController = buildTransactionControllerMock({
addTransaction: jest.fn().mockResolvedValue('newTxId'),
});
const requestMock = buildInitRequestMock({
getController: jest.fn().mockReturnValue(mockTransactionController),
});

// Act
bridgeStatusControllerInit(requestMock);

// Assert
const constructorOptions =
bridgeStatusControllerClassMock.mock.calls[0][0];
const addTransactionFn = constructorOptions.addTransactionFn;
const mockTxParams = { from: '0xabc', to: '0x123', value: '0x0' };
const mockOrigin = 'test-origin';

addTransactionFn(mockTxParams, {
origin: mockOrigin,
networkClientId: 'mainnet',
});
expect(mockTransactionController.addTransaction).toHaveBeenCalledWith(
mockTxParams,
{ origin: mockOrigin, networkClientId: 'mainnet' },
);
});

it('correctly sets up estimateGasFeeFn', () => {
// Arrange
const mockTransactionController = buildTransactionControllerMock({
estimateGasFee: jest.fn().mockResolvedValue({ gasLimit: '0x7530' }),
});
const requestMock = buildInitRequestMock({
getController: jest.fn().mockReturnValue(mockTransactionController),
});

// Act
bridgeStatusControllerInit(requestMock);

// Assert
const constructorOptions =
bridgeStatusControllerClassMock.mock.calls[0][0];
const estimateGasFeeFn = constructorOptions.estimateGasFeeFn;
const mockTxParams = {
transactionParams: { from: '0xabc', to: '0x123', value: '0x0' },
chainId: '0x1' as const,
};

estimateGasFeeFn(mockTxParams);
expect(mockTransactionController.estimateGasFee).toHaveBeenCalledWith(
mockTxParams,
);
});

it('correctly sets up addTransactionBatchFn', () => {
// Arrange
const mockTransactionController = buildTransactionControllerMock({
Expand Down Expand Up @@ -284,40 +225,6 @@ describe('BridgeStatusController Init', () => {
).toHaveBeenCalledWith(mockTxBatch);
});

it('correctly sets up updateTransactionFn', () => {
// Arrange
const mockTransactionController = buildTransactionControllerMock({
updateTransaction: jest.fn().mockResolvedValue(undefined),
});
const requestMock = buildInitRequestMock({
getController: jest.fn().mockReturnValue(mockTransactionController),
});

// Act
bridgeStatusControllerInit(requestMock);

// Assert
const constructorOptions =
bridgeStatusControllerClassMock.mock.calls[0][0];
const updateTransactionFn = constructorOptions.updateTransactionFn;
const mockTxUpdate = {
id: 'txId',
chainId: '0x1' as const,
networkClientId: 'mainnet',
time: Date.now(),
txParams: { from: '0xabc', to: '0x123', value: '0x0' },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
status: 'confirmed' as any,
};
const mockNote = 'test note';

updateTransactionFn(mockTxUpdate, mockNote);
expect(mockTransactionController.updateTransaction).toHaveBeenCalledWith(
mockTxUpdate,
mockNote,
);
});

it('handles undefined persistedState', () => {
// Arrange
const requestMock = buildInitRequestMock({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,9 @@ export const bridgeStatusControllerInit: ControllerInitFunction<
state: persistedState.BridgeStatusController,
clientId: BridgeClientId.MOBILE,
fetchFn: handleFetch,
addTransactionFn: (
...args: Parameters<typeof transactionController.addTransaction>
) => transactionController.addTransaction(...args),
estimateGasFeeFn: (
...args: Parameters<typeof transactionController.estimateGasFee>
) => transactionController.estimateGasFee(...args),
addTransactionBatchFn: (
...args: Parameters<typeof transactionController.addTransactionBatch>
) => transactionController.addTransactionBatch(...args),
updateTransactionFn: (
...args: Parameters<typeof transactionController.updateTransaction>
) => transactionController.updateTransaction(...args),
traceFn: trace as TraceCallback,
config: {
customBridgeApiBaseUrl: BRIDGE_API_BASE_URL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { GatorPermissionsController } from '@metamask/gator-permissions-controller';
import {
GatorPermissionsController,
type GatorPermissionsControllerState,
} from '@metamask/gator-permissions-controller';
import { buildControllerInitRequestMock } from '../../utils/test-utils';
import type { ControllerInitRequest } from '../../types';
import { isGatorPermissionsFeatureEnabled } from '../../../../util/environment';
import {
getGatorPermissionsControllerMessenger,
GatorPermissionsControllerMessenger,
Expand All @@ -11,7 +13,6 @@ import { ExtendedMessenger } from '../../../ExtendedMessenger';
import { MOCK_ANY_NAMESPACE, MockAnyNamespace } from '@metamask/messenger';

jest.mock('@metamask/gator-permissions-controller');
jest.mock('../../../../util/environment');

function buildInitRequestMock(): jest.Mocked<
ControllerInitRequest<GatorPermissionsControllerMessenger>
Expand Down Expand Up @@ -39,7 +40,6 @@ describe('GatorPermissionsControllerInit', () => {

beforeEach(() => {
jest.resetAllMocks();
jest.mocked(isGatorPermissionsFeatureEnabled).mockReturnValue(true);
process.env.GATOR_PERMISSIONS_PROVIDER_SNAP_ID =
MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID;
});
Expand All @@ -60,53 +60,44 @@ describe('GatorPermissionsControllerInit', () => {
).toBeInstanceOf(GatorPermissionsController);
});

it('initializes with correct messenger and state(gator permissions feature enabled)', () => {
it('initializes with messenger, config including provider snap id, and persisted state', () => {
const requestMock = buildInitRequestMock();
jest.mocked(isGatorPermissionsFeatureEnabled).mockReturnValue(true);
GatorPermissionsControllerInit(requestMock);

expect(GatorPermissionsControllerClassMock).toHaveBeenCalledWith({
messenger: requestMock.controllerMessenger,
state: {
isGatorPermissionsEnabled: true,
gatorPermissionsProviderSnapId: MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID,
...requestMock.persistedState.GatorPermissionsController,
},
});
});
const persisted: GatorPermissionsControllerState = {
grantedPermissions: [],
isFetchingGatorPermissions: false,
pendingRevocations: [],
lastSyncedTimestamp: -1,
};
requestMock.persistedState.GatorPermissionsController = persisted;

it('initializes with correct messenger and state(gator permissions feature disabled)', () => {
const requestMock = buildInitRequestMock();
jest.mocked(isGatorPermissionsFeatureEnabled).mockReturnValue(false);
GatorPermissionsControllerInit(requestMock);

expect(GatorPermissionsControllerClassMock).toHaveBeenCalledWith({
messenger: requestMock.controllerMessenger,
state: {
isGatorPermissionsEnabled: false,
config: {
supportedPermissionTypes: [],
gatorPermissionsProviderSnapId: MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID,
...requestMock.persistedState.GatorPermissionsController,
},
state: persisted,
});
});

it('handles undefined persistedState.GatorPermissionsController', () => {
it('initializes with undefined state when persistedState.GatorPermissionsController is missing', () => {
const requestMock = buildInitRequestMock();
requestMock.persistedState.GatorPermissionsController = undefined;
jest.mocked(isGatorPermissionsFeatureEnabled).mockReturnValue(true);

GatorPermissionsControllerInit(requestMock);

expect(GatorPermissionsControllerClassMock).toHaveBeenCalledWith({
messenger: requestMock.controllerMessenger,
state: {
isGatorPermissionsEnabled: true,
config: {
supportedPermissionTypes: [],
gatorPermissionsProviderSnapId: MOCK_GATOR_PERMISSIONS_PROVIDER_SNAP_ID,
},
state: undefined,
});
});

it('resolves the default when GATOR_PERMISSIONS_PROVIDER_SNAP_ID is not specified', () => {
it('omits gatorPermissionsProviderSnapId from config when env is not set', () => {
const requestMock = buildInitRequestMock();

delete process.env.GATOR_PERMISSIONS_PROVIDER_SNAP_ID;
Expand All @@ -115,22 +106,18 @@ describe('GatorPermissionsControllerInit', () => {

expect(GatorPermissionsControllerClassMock).toHaveBeenCalledWith({
messenger: requestMock.controllerMessenger,
state: {
isGatorPermissionsEnabled: true,
config: {
supportedPermissionTypes: [],
},
state: undefined,
});

const calledWithState =
GatorPermissionsControllerClassMock.mock.calls[0][0].state;

expect(calledWithState).toEqual({
isGatorPermissionsEnabled: true,
});
const calledWithConfig =
GatorPermissionsControllerClassMock.mock.calls[0][0].config;

// GatorPermissionsController requires that the key does not exist if the snap id is not specified
expect(
Object.prototype.hasOwnProperty.call(
calledWithState,
calledWithConfig,
'gatorPermissionsProviderSnapId',
),
).toBe(false);
Expand Down
Loading
Loading