Skip to content

Commit

Permalink
feat: add useSafeChainsListValidation, tokenSortConfig and privacyMode (
Browse files Browse the repository at this point in the history
#4860)

## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

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: MetaMask/metamask-mobile#11961
- extension: MetaMask/metamask-extension#28021


## References

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/preferences-controller`
Added
- Add `useSafeChainsListValidation` preference
([#4860](#4860))
- Add `useSafeChainsListValidation` property to the
`PreferencesController` state (default: `true`)
  - Add `setUseSafeChainsListValidation` method to set this property
- Add `tokenSortConfig` preference
([#4860](#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](#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
  • Loading branch information
cryptodev-2s authored Oct 30, 2024
1 parent 018261d commit 955196c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
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
46 changes: 46 additions & 0 deletions packages/preferences-controller/src/PreferencesController.test.ts
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 = {
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;

0 comments on commit 955196c

Please sign in to comment.