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
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const RegistryNetworkConfigSchema = type({
blockExplorerUrls: BlockExplorerUrlsSchema,
config: ChainConfigSchema,
contracts: optional(NetworkContractsSchema),
supportsEtherscanApi: optional(boolean()),
});

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/preferences-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 90,
branches: 91.66,
functions: 100,
lines: 100,
statements: 100,
Expand Down
1 change: 1 addition & 0 deletions packages/preferences-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
},
"dependencies": {
"@metamask/base-controller": "^9.1.0",
"@metamask/config-registry-controller": "^2.0.1",
"@metamask/messenger": "^2.0.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,28 @@ describe('PreferencesController', () => {
expect(controller.state.showIncomingTransactions['0x1']).toBe(false);
});

it('should set showIncomingTransactions to true when the chain supports Etherscan API (via config-registry)', () => {
const {
controller,
rootMessenger,
mockConfigRegistryControllerGetNetworkConfigByCaip2ChainId,
} = setupPreferencesController();
mockConfigRegistryControllerGetNetworkConfigByCaip2ChainId.mockReturnValue({
supportsEtherscanApi: true,
});

rootMessenger.call(
'PreferencesController:setEnableNetworkIncomingTransactions',
'0x9999',
true,
);

expect(controller.state.showIncomingTransactions['0x9999']).toBe(true);
expect(
mockConfigRegistryControllerGetNetworkConfigByCaip2ChainId,
).toHaveBeenCalledWith('eip155:0x9999');
});

it('should set smartTransactionsOptInStatus', () => {
const { controller, rootMessenger } = setupPreferencesController();
rootMessenger.call(
Expand Down Expand Up @@ -528,7 +550,20 @@ function setupPreferencesController({
}: {
options?: Partial<ConstructorParameters<typeof PreferencesController>[0]>;
messenger?: RootMessenger;
} = {}): { controller: PreferencesController; rootMessenger: RootMessenger } {
} = {}): {
controller: PreferencesController;
rootMessenger: RootMessenger;
mockConfigRegistryControllerGetNetworkConfigByCaip2ChainId: jest.Mock;
} {
const mockConfigRegistryControllerGetNetworkConfigByCaip2ChainId = jest
.fn()
.mockReturnValue({});

messenger.registerActionHandler(
'ConfigRegistryController:getNetworkConfigByCaip2ChainId',
mockConfigRegistryControllerGetNetworkConfigByCaip2ChainId,
);

const preferencesControllerMessenger = new Messenger<
'PreferencesController',
AllPreferencesControllerActions,
Expand All @@ -541,13 +576,17 @@ function setupPreferencesController({

messenger.delegate({
messenger: preferencesControllerMessenger,
actions: [],
actions: ['ConfigRegistryController:getNetworkConfigByCaip2ChainId'],
});

const controller = new PreferencesController({
messenger: preferencesControllerMessenger,
...options,
});

return { controller, rootMessenger: messenger };
return {
controller,
rootMessenger: messenger,
mockConfigRegistryControllerGetNetworkConfigByCaip2ChainId,
};
}
29 changes: 23 additions & 6 deletions packages/preferences-controller/src/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
ControllerStateChangeEvent,
ControllerGetStateAction,
} from '@metamask/base-controller';
import type { ConfigRegistryControllerGetNetworkConfigByCaip2ChainIdAction } from '@metamask/config-registry-controller';
import type { Messenger } from '@metamask/messenger';

import { ETHERSCAN_SUPPORTED_CHAIN_IDS } from './constants.js';
import type { PreferencesControllerMethodActions } from './PreferencesController-method-action-types.js';
import {
Hex,
KnownCaipNamespace,
parseCaipChainId,

Check failure on line 14 in packages/preferences-controller/src/PreferencesController.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint) (24.x)

'parseCaipChainId' is defined but never used
toCaipChainId,
} from '@metamask/utils';

/**
* A type union of the name for each chain that is supported by Etherscan or
Expand Down Expand Up @@ -59,9 +66,7 @@
/**
* Controls whether incoming transactions are enabled, per-chain (for Etherscan-supported chains)
*/
showIncomingTransactions: {
[chainId in EtherscanSupportedHexChainId]: boolean;
};
showIncomingTransactions: Record<Hex, boolean>;
/**
* Controls whether test networks are shown in the wallet
*/
Expand Down Expand Up @@ -267,11 +272,14 @@
| PreferencesControllerGetStateAction
| PreferencesControllerMethodActions;

export type AllowedActions =
ConfigRegistryControllerGetNetworkConfigByCaip2ChainIdAction;

export type PreferencesControllerEvents = PreferencesControllerStateChangeEvent;

export type PreferencesControllerMessenger = Messenger<
typeof name,
PreferencesControllerActions,
PreferencesControllerActions | AllowedActions,
PreferencesControllerEvents
>;

Expand Down Expand Up @@ -487,10 +495,19 @@
* @param isIncomingTransactionNetworkEnable - true to enable incoming transactions
*/
setEnableNetworkIncomingTransactions(
chainId: EtherscanSupportedHexChainId,
chainId: Hex,
isIncomingTransactionNetworkEnable: boolean,
): void {
if (Object.values(ETHERSCAN_SUPPORTED_CHAIN_IDS).includes(chainId)) {
const isEtherscanSupportedChain =
this.messenger.call(
'ConfigRegistryController:getNetworkConfigByCaip2ChainId',
toCaipChainId(KnownCaipNamespace.Eip155, chainId),
)?.supportsEtherscanApi ??
Object.values(ETHERSCAN_SUPPORTED_CHAIN_IDS).includes(
chainId as EtherscanSupportedHexChainId,
);

if (isEtherscanSupportedChain) {
this.update((state) => {
state.showIncomingTransactions = {
...this.state.showIncomingTransactions,
Expand Down
3 changes: 3 additions & 0 deletions packages/preferences-controller/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
{
"path": "../base-controller/tsconfig.build.json"
},
{
"path": "../config-registry-controller/tsconfig.build.json"
},
{
"path": "../messenger/tsconfig.build.json"
}
Expand Down
3 changes: 3 additions & 0 deletions packages/preferences-controller/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
{
"path": "../base-controller"
},
{
"path": "../config-registry-controller"
},
{
"path": "../messenger"
}
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8551,6 +8551,7 @@ __metadata:
dependencies:
"@metamask/auto-changelog": "npm:^6.1.0"
"@metamask/base-controller": "npm:^9.1.0"
"@metamask/config-registry-controller": "npm:^2.0.1"
"@metamask/messenger": "npm:^2.0.0"
"@metamask/utils": "npm:^11.11.0"
"@ts-bridge/cli": "npm:^0.6.4"
Expand Down
Loading