From 955196c58cf835d39d505369f7b38a4e8900d22e Mon Sep 17 00:00:00 2001 From: cryptodev-2s <109512101+cryptodev-2s@users.noreply.github.com> Date: Wed, 30 Oct 2024 14:26:35 +0100 Subject: [PATCH] feat: add useSafeChainsListValidation, tokenSortConfig and privacyMode (#4860) ## Explanation This PR add three new properties to PreferencesController state: - useSafeChainsListValidation: already in use in both clients - extension: https://github.com/MetaMask/metamask-extension/blob/develop/app/scripts/controllers/preferences-controller.ts#L135 - mobile: https://github.com/MetaMask/metamask-mobile/blob/main/patches/%40metamask%2Bpreferences-controller%2B11.0.0.patch#L20 - tokenSortConfig: already in use in both clients - mobile: https://github.com/MetaMask/metamask-mobile/blob/main/patches/%40metamask%2Bpreferences-controller%2B11.0.0.patch#L22 - extension: https://github.com/MetaMask/metamask-extension/blob/develop/app/scripts/controllers/preferences-controller.ts#L117 - privacyMode: WIP to add the new properties on both clients - mobile: https://github.com/MetaMask/metamask-mobile/pull/11961 - extension: https://github.com/MetaMask/metamask-extension/pull/28021 ## References ## Changelog ### `@metamask/preferences-controller` 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 ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes --- packages/preferences-controller/CHANGELOG.md | 12 ++++ .../src/PreferencesController.test.ts | 46 ++++++++++++++ .../src/PreferencesController.ts | 63 ++++++++++++++++++- 3 files changed, 120 insertions(+), 1 deletion(-) 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 diff --git a/packages/preferences-controller/src/PreferencesController.test.ts b/packages/preferences-controller/src/PreferencesController.test.ts index 9d9d4f05bc..03d9332cc8 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, }); }); @@ -427,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); + }); }); /** diff --git a/packages/preferences-controller/src/PreferencesController.ts b/packages/preferences-controller/src/PreferencesController.ts index 7f5815e7cc..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 */ @@ -114,6 +120,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: TokenSortConfig; + /** + * Controls whether balance and assets are hidden or not + */ + privacyMode: boolean; }; const metadata = { @@ -133,6 +151,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'; @@ -166,7 +187,7 @@ export type PreferencesControllerMessenger = RestrictedControllerMessenger< * * @returns The default PreferencesController state. */ -export function getDefaultPreferencesState() { +export function getDefaultPreferencesState(): PreferencesState { return { featureFlags: {}, identities: {}, @@ -205,6 +226,13 @@ export function getDefaultPreferencesState() { useMultiRpcMigration: true, smartTransactionsOptInStatus: false, useTransactionSimulations: true, + useSafeChainsListValidation: true, + tokenSortConfig: { + key: 'tokenFiatAmount', + order: 'dsc', + sortCallback: 'stringNumeric', + }, + privacyMode: false, }; } @@ -524,6 +552,39 @@ 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: TokenSortConfig) { + 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; + }); + } + + /** + * 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;