Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add useSafeChainsListValidation, tokenSortConfig and privacyMode #4860

12 changes: 12 additions & 0 deletions packages/preferences-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});

Expand Down Expand Up @@ -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);
});
});

/**
Expand Down
63 changes: 62 additions & 1 deletion packages/preferences-controller/src/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export type EtherscanSupportedChains =
export type EtherscanSupportedHexChainId =
(typeof ETHERSCAN_SUPPORTED_CHAIN_IDS)[EtherscanSupportedChains];

type TokenSortConfig = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit.
do we need to export this type? will it be useful in the downstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think its required to be exported

key: string;
order: 'asc' | 'dsc';
sortCallback: string;
};

/**
* Preferences controller state
*/
Expand Down Expand Up @@ -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 = {
Expand All @@ -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';
Expand Down Expand Up @@ -166,7 +187,7 @@ export type PreferencesControllerMessenger = RestrictedControllerMessenger<
*
* @returns The default PreferencesController state.
*/
export function getDefaultPreferencesState() {
export function getDefaultPreferencesState(): PreferencesState {
return {
featureFlags: {},
identities: {},
Expand Down Expand Up @@ -205,6 +226,13 @@ export function getDefaultPreferencesState() {
useMultiRpcMigration: true,
smartTransactionsOptInStatus: false,
useTransactionSimulations: true,
useSafeChainsListValidation: true,
tokenSortConfig: {
key: 'tokenFiatAmount',
order: 'dsc',
sortCallback: 'stringNumeric',
},
privacyMode: false,
};
}

Expand Down Expand Up @@ -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;
Loading