diff --git a/packages/preferences-controller/CHANGELOG.md b/packages/preferences-controller/CHANGELOG.md index fc4cfcd8342..4c08c9b5967 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 9d9d4f05bcf..03d9332cc87 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 7f5815e7ccb..e67450caed3 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;