From 9e62f92881f55b9bc51986212349ba49d3f956bd Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Mon, 28 Oct 2024 21:21:31 +0100 Subject: [PATCH 1/5] feat: add useSafeChainsListValidation, tokenSortConfig and privacyMode to PreferencesController state --- .../src/PreferencesController.test.ts | 7 +++ .../src/PreferencesController.ts | 58 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/packages/preferences-controller/src/PreferencesController.test.ts b/packages/preferences-controller/src/PreferencesController.test.ts index 9d9d4f05bc..6d524fa6ee 100644 --- a/packages/preferences-controller/src/PreferencesController.test.ts +++ b/packages/preferences-controller/src/PreferencesController.test.ts @@ -36,6 +36,13 @@ describe('PreferencesController', () => { return acc; }, {} as { [chainId in EtherscanSupportedHexChainId]: boolean }), smartTransactionsOptInStatus: false, + useSafeChainsListValidation: true, + tokenSortConfig: { + key: 'tokenFiatAmount', + order: 'dsc', + sortCallback: 'stringNumeric', + }, + privacyMode: false, }); }); diff --git a/packages/preferences-controller/src/PreferencesController.ts b/packages/preferences-controller/src/PreferencesController.ts index 7f5815e7cc..0f8dd7fbf1 100644 --- a/packages/preferences-controller/src/PreferencesController.ts +++ b/packages/preferences-controller/src/PreferencesController.ts @@ -114,6 +114,18 @@ export type PreferencesState = { * Controls whether Multi rpc modal is displayed or not */ useMultiRpcMigration: boolean; + /** + * Controls whether to use the safe chains list validation + */ + useSafeChainsListValidation: boolean; + /** + * Controls which order tokens are sorted in + */ + tokenSortConfig: Record; + /** + * Controls whether balance and assets are hidden or not + */ + privacyMode: boolean; }; const metadata = { @@ -133,6 +145,9 @@ const metadata = { smartTransactionsOptInStatus: { persist: true, anonymous: false }, useTransactionSimulations: { persist: true, anonymous: true }, useMultiRpcMigration: { persist: true, anonymous: true }, + useSafeChainsListValidation: { persist: true, anonymous: true }, + tokenSortConfig: { persist: true, anonymous: true }, + privacyMode: { persist: true, anonymous: true }, }; const name = 'PreferencesController'; @@ -205,6 +220,13 @@ export function getDefaultPreferencesState() { useMultiRpcMigration: true, smartTransactionsOptInStatus: false, useTransactionSimulations: true, + useSafeChainsListValidation: true, + tokenSortConfig: { + key: 'tokenFiatAmount', + order: 'dsc', + sortCallback: 'stringNumeric', + }, + privacyMode: false, }; } @@ -524,6 +546,42 @@ export class PreferencesController extends BaseController< state.useTransactionSimulations = useTransactionSimulations; }); } + + /** + * A setter to update the user's preferred token sorting order. + * + * @param tokenSortConfig - a configuration representing the sort order of tokens. + */ + setTokenSortConfig(tokenSortConfig: Record) { + this.update((state) => { + state.tokenSortConfig = tokenSortConfig; + }); + } + + /** + * A setter for the user preferences to enable/disable safe chains list validation. + * + * @param useSafeChainsListValidation - true to enable safe chains list validation, false to disable it. + */ + setUseSafeChainsListValidation(useSafeChainsListValidation: boolean) { + this.update((state) => { + state.useSafeChainsListValidation = useSafeChainsListValidation; + if (!useSafeChainsListValidation) { + state.useSafeChainsListValidation = false; + } + }); + } + + /** + * A setter for the user preferences to enable/disable privacy mode. + * + * @param privacyMode - true to enable privacy mode, false to disable it. + */ + setPrivacyMode(privacyMode: boolean) { + this.update((state) => { + state.privacyMode = privacyMode; + }); + } } export default PreferencesController; From 62b07af4da57218435c7674cb662a7b4c1e653b0 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Mon, 28 Oct 2024 21:31:59 +0100 Subject: [PATCH 2/5] fix: add changelog --- packages/preferences-controller/CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/preferences-controller/CHANGELOG.md b/packages/preferences-controller/CHANGELOG.md index fc4cfcd834..4c08c9b596 100644 --- a/packages/preferences-controller/CHANGELOG.md +++ b/packages/preferences-controller/CHANGELOG.md @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `useSafeChainsListValidation` preference ([#4860](https://github.com/MetaMask/core/pull/4860)) + - Add `useSafeChainsListValidation` property to the `PreferencesController` state (default: `true`) + - Add `setUseSafeChainsListValidation` method to set this property +- Add `tokenSortConfig` preference ([#4860](https://github.com/MetaMask/core/pull/4860)) + - Add `tokenSortConfig` property to the `PreferencesController` state (default value: `{ key: 'tokenFiatAmount', order: 'dsc', sortCallback: 'stringNumeric' }`) + - Add `setTokenSortConfig` method to set this property +- Add `privacyMode` preference ([#4860](https://github.com/MetaMask/core/pull/4860)) + - Add `privacyMode` property to the `PreferencesController` state (default value: `false`) + - Add `setPrivacyMode` method to set this property + ## [13.1.0] ### Changed From a5ce0b330c4217e022652d944724c4eaa0763b40 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Mon, 28 Oct 2024 21:40:36 +0100 Subject: [PATCH 3/5] fix: add unit tests --- .../src/PreferencesController.test.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/packages/preferences-controller/src/PreferencesController.test.ts b/packages/preferences-controller/src/PreferencesController.test.ts index 6d524fa6ee..03d9332cc8 100644 --- a/packages/preferences-controller/src/PreferencesController.test.ts +++ b/packages/preferences-controller/src/PreferencesController.test.ts @@ -434,6 +434,45 @@ describe('PreferencesController', () => { controller.setUseTransactionSimulations(false); expect(controller.state.useTransactionSimulations).toBe(false); }); + + it('should set useSafeChainsListValidation', () => { + const controller = setupPreferencesController({ + options: { + state: { + useSafeChainsListValidation: false, + }, + }, + }); + expect(controller.state.useSafeChainsListValidation).toBe(false); + controller.setUseSafeChainsListValidation(true); + expect(controller.state.useSafeChainsListValidation).toBe(true); + }); + + it('should set tokenSortConfig', () => { + const controller = setupPreferencesController(); + expect(controller.state.tokenSortConfig).toStrictEqual({ + key: 'tokenFiatAmount', + order: 'dsc', + sortCallback: 'stringNumeric', + }); + controller.setTokenSortConfig({ + key: 'someToken', + order: 'asc', + sortCallback: 'stringNumeric', + }); + expect(controller.state.tokenSortConfig).toStrictEqual({ + key: 'someToken', + order: 'asc', + sortCallback: 'stringNumeric', + }); + }); + + it('should set privacyMode', () => { + const controller = setupPreferencesController(); + expect(controller.state.privacyMode).toBe(false); + controller.setPrivacyMode(true); + expect(controller.state.privacyMode).toBe(true); + }); }); /** From 3b83cd00c2da1f3929a64a940585296cfd23e4e0 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Mon, 28 Oct 2024 21:49:43 +0100 Subject: [PATCH 4/5] fix: setter issue --- packages/preferences-controller/src/PreferencesController.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/preferences-controller/src/PreferencesController.ts b/packages/preferences-controller/src/PreferencesController.ts index 0f8dd7fbf1..0f65f25d82 100644 --- a/packages/preferences-controller/src/PreferencesController.ts +++ b/packages/preferences-controller/src/PreferencesController.ts @@ -566,9 +566,6 @@ export class PreferencesController extends BaseController< setUseSafeChainsListValidation(useSafeChainsListValidation: boolean) { this.update((state) => { state.useSafeChainsListValidation = useSafeChainsListValidation; - if (!useSafeChainsListValidation) { - state.useSafeChainsListValidation = false; - } }); } From 18d35e17abd4da54fd3d4c120ed3891ca0f327a3 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Tue, 29 Oct 2024 17:16:21 +0100 Subject: [PATCH 5/5] fix: add TokenSortConfig type --- .../src/PreferencesController.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/preferences-controller/src/PreferencesController.ts b/packages/preferences-controller/src/PreferencesController.ts index 0f65f25d82..e67450caed 100644 --- a/packages/preferences-controller/src/PreferencesController.ts +++ b/packages/preferences-controller/src/PreferencesController.ts @@ -44,6 +44,12 @@ export type EtherscanSupportedChains = export type EtherscanSupportedHexChainId = (typeof ETHERSCAN_SUPPORTED_CHAIN_IDS)[EtherscanSupportedChains]; +type TokenSortConfig = { + key: string; + order: 'asc' | 'dsc'; + sortCallback: string; +}; + /** * Preferences controller state */ @@ -121,7 +127,7 @@ export type PreferencesState = { /** * Controls which order tokens are sorted in */ - tokenSortConfig: Record; + tokenSortConfig: TokenSortConfig; /** * Controls whether balance and assets are hidden or not */ @@ -181,7 +187,7 @@ export type PreferencesControllerMessenger = RestrictedControllerMessenger< * * @returns The default PreferencesController state. */ -export function getDefaultPreferencesState() { +export function getDefaultPreferencesState(): PreferencesState { return { featureFlags: {}, identities: {}, @@ -552,7 +558,7 @@ export class PreferencesController extends BaseController< * * @param tokenSortConfig - a configuration representing the sort order of tokens. */ - setTokenSortConfig(tokenSortConfig: Record) { + setTokenSortConfig(tokenSortConfig: TokenSortConfig) { this.update((state) => { state.tokenSortConfig = tokenSortConfig; });