From 6ec1d6697ac76239504ab09bc4b97c425ab12edc Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Tue, 7 Jan 2025 10:54:10 +0100 Subject: [PATCH 1/3] Add `hideBalances` to `snap_getPreferences` --- .../src/restricted/getPreferences.test.ts | 10 +++--- .../src/types/methods/get-preferences.ts | 6 +++- .../src/methods/hooks/get-preferences.test.ts | 32 +++++++++++++++++-- .../src/methods/hooks/get-preferences.ts | 4 ++- packages/snaps-simulation/src/options.test.ts | 2 ++ packages/snaps-simulation/src/options.ts | 2 ++ .../src/test-utils/options.ts | 3 ++ .../src/features/simulation/sagas.ts | 6 +++- 8 files changed, 55 insertions(+), 10 deletions(-) diff --git a/packages/snaps-rpc-methods/src/restricted/getPreferences.test.ts b/packages/snaps-rpc-methods/src/restricted/getPreferences.test.ts index 97619c7aed..3966b4aec4 100644 --- a/packages/snaps-rpc-methods/src/restricted/getPreferences.test.ts +++ b/packages/snaps-rpc-methods/src/restricted/getPreferences.test.ts @@ -26,9 +26,11 @@ describe('snap_getPreferences', () => { describe('getImplementation', () => { it('returns the preferences', async () => { const methodHooks = { - getPreferences: jest - .fn() - .mockReturnValue({ locale: 'en', currency: 'usd' }), + getPreferences: jest.fn().mockReturnValue({ + locale: 'en', + currency: 'usd', + hideBalances: false, + }), }; const implementation = getImplementation(methodHooks); @@ -40,7 +42,7 @@ describe('snap_getPreferences', () => { }, method: 'snap_getPreferences', }), - ).toStrictEqual({ locale: 'en', currency: 'usd' }); + ).toStrictEqual({ locale: 'en', currency: 'usd', hideBalances: false }); }); }); }); diff --git a/packages/snaps-sdk/src/types/methods/get-preferences.ts b/packages/snaps-sdk/src/types/methods/get-preferences.ts index c65f0c4687..8d312dc066 100644 --- a/packages/snaps-sdk/src/types/methods/get-preferences.ts +++ b/packages/snaps-sdk/src/types/methods/get-preferences.ts @@ -10,4 +10,8 @@ export type GetPreferencesParams = never; * * It is the user selected preferences from the MetaMask extension. */ -export type GetPreferencesResult = { locale: string; currency: string }; +export type GetPreferencesResult = { + locale: string; + currency: string; + hideBalances: boolean; +}; diff --git a/packages/snaps-simulation/src/methods/hooks/get-preferences.test.ts b/packages/snaps-simulation/src/methods/hooks/get-preferences.test.ts index 69a63f7238..3132a75027 100644 --- a/packages/snaps-simulation/src/methods/hooks/get-preferences.test.ts +++ b/packages/snaps-simulation/src/methods/hooks/get-preferences.test.ts @@ -9,7 +9,11 @@ describe('getGetPreferencesMethodImplementation', () => { }), ); - expect(fn()).toStrictEqual({ currency: 'usd', locale: 'en' }); + expect(fn()).toStrictEqual({ + currency: 'usd', + locale: 'en', + hideBalances: false, + }); }); it('returns the implementation of the `getPreferences` hook for a different locale', async () => { @@ -19,7 +23,11 @@ describe('getGetPreferencesMethodImplementation', () => { }), ); - expect(fn()).toStrictEqual({ currency: 'usd', locale: 'nl' }); + expect(fn()).toStrictEqual({ + currency: 'usd', + locale: 'nl', + hideBalances: false, + }); }); it('returns the implementation of the `getPreferences` hook for a different currency', async () => { @@ -29,6 +37,24 @@ describe('getGetPreferencesMethodImplementation', () => { }), ); - expect(fn()).toStrictEqual({ currency: 'dkk', locale: 'en' }); + expect(fn()).toStrictEqual({ + currency: 'dkk', + locale: 'en', + hideBalances: false, + }); + }); + + it('returns the implementation of the `getPreferences` hook for hidden balances', async () => { + const fn = getGetPreferencesMethodImplementation( + getMockOptions({ + hideBalances: true, + }), + ); + + expect(fn()).toStrictEqual({ + currency: 'usd', + locale: 'en', + hideBalances: true, + }); }); }); diff --git a/packages/snaps-simulation/src/methods/hooks/get-preferences.ts b/packages/snaps-simulation/src/methods/hooks/get-preferences.ts index 0294f6edca..c6ffb4b180 100644 --- a/packages/snaps-simulation/src/methods/hooks/get-preferences.ts +++ b/packages/snaps-simulation/src/methods/hooks/get-preferences.ts @@ -6,13 +6,15 @@ import type { SimulationOptions } from '../../options'; * @param options - The simulation options. * @param options.currency - The currency to use. * @param options.locale - The locale to use. + * @param options.hideBalances - Whether to hide balances. * @returns The implementation of the `getPreferences` hook. */ export function getGetPreferencesMethodImplementation({ currency, locale, + hideBalances, }: SimulationOptions) { return () => { - return { currency, locale }; + return { currency, locale, hideBalances }; }; } diff --git a/packages/snaps-simulation/src/options.test.ts b/packages/snaps-simulation/src/options.test.ts index aa06a1da9b..30e98adf0b 100644 --- a/packages/snaps-simulation/src/options.test.ts +++ b/packages/snaps-simulation/src/options.test.ts @@ -7,6 +7,7 @@ describe('getOptions', () => { expect(options).toMatchInlineSnapshot(` { "currency": "usd", + "hideBalances": false, "locale": "en", "secretRecoveryPhrase": "test test test test test test test test test test test ball", "state": null, @@ -24,6 +25,7 @@ describe('getOptions', () => { expect(options).toMatchInlineSnapshot(` { "currency": "eur", + "hideBalances": false, "locale": "nl", "secretRecoveryPhrase": "test test test test test test test test test test test ball", "state": null, diff --git a/packages/snaps-simulation/src/options.ts b/packages/snaps-simulation/src/options.ts index f485271e42..baf322d7d3 100644 --- a/packages/snaps-simulation/src/options.ts +++ b/packages/snaps-simulation/src/options.ts @@ -1,5 +1,6 @@ import type { Infer } from '@metamask/superstruct'; import { + boolean, create, defaulted, nullable, @@ -21,6 +22,7 @@ const SimulationOptionsStruct = object({ optional(nullable(record(string(), JsonStruct))), null, ), + hideBalances: defaulted(optional(boolean()), false), }); /** diff --git a/packages/snaps-simulation/src/test-utils/options.ts b/packages/snaps-simulation/src/test-utils/options.ts index 54f9dbe927..bd680eb633 100644 --- a/packages/snaps-simulation/src/test-utils/options.ts +++ b/packages/snaps-simulation/src/test-utils/options.ts @@ -10,11 +10,13 @@ import type { SimulationOptions } from '../options'; * @param options.secretRecoveryPhrase - The secret recovery phrase to use. * @param options.state - The state to use. * @param options.unencryptedState - The unencrypted state to use. + * @param options.hideBalances - Whether to hide balances. * @returns The options for the simulation. */ export function getMockOptions({ currency = 'usd', locale = 'en', + hideBalances = false, secretRecoveryPhrase = DEFAULT_SRP, state = null, unencryptedState = null, @@ -25,5 +27,6 @@ export function getMockOptions({ secretRecoveryPhrase, state, unencryptedState, + hideBalances, }; } diff --git a/packages/snaps-simulator/src/features/simulation/sagas.ts b/packages/snaps-simulator/src/features/simulation/sagas.ts index f01c0d68da..24d84dee85 100644 --- a/packages/snaps-simulator/src/features/simulation/sagas.ts +++ b/packages/snaps-simulator/src/features/simulation/sagas.ts @@ -130,7 +130,11 @@ export function* initSaga({ payload }: PayloadAction) { ...sharedHooks, // TODO: Add all the hooks required // TODO: Allow changing this? - getPreferences: () => ({ locale: 'en', currency: 'usd' }), + getPreferences: () => ({ + locale: 'en', + currency: 'usd', + hideBalances: false, + }), getUnlockPromise: async () => Promise.resolve(true), showDialog: async (...args: Parameters) => await runSaga(showDialog, ...args).toPromise(), From 9366e90abf2b657a9f418d95d6a8f84f1a0d0b83 Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Tue, 7 Jan 2025 12:38:38 +0100 Subject: [PATCH 2/3] Add JSDoc to `GetPreferencesResult` --- packages/snaps-sdk/src/types/methods/get-preferences.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/snaps-sdk/src/types/methods/get-preferences.ts b/packages/snaps-sdk/src/types/methods/get-preferences.ts index 8d312dc066..5e798de6af 100644 --- a/packages/snaps-sdk/src/types/methods/get-preferences.ts +++ b/packages/snaps-sdk/src/types/methods/get-preferences.ts @@ -9,6 +9,10 @@ export type GetPreferencesParams = never; * The result returned by the `snap_getPreferences` method. * * It is the user selected preferences from the MetaMask extension. + * + * @property locale - The user's selected locale. + * @property currency - The user's selected currency. + * @property hideBalances - Whether the user has chosen to hide balances. */ export type GetPreferencesResult = { locale: string; From 689d462b9ae9da5b8e774b5556f9034e81ba08eb Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Tue, 7 Jan 2025 12:40:45 +0100 Subject: [PATCH 3/3] use `client` in JSDoc --- packages/snaps-sdk/src/types/methods/get-preferences.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/snaps-sdk/src/types/methods/get-preferences.ts b/packages/snaps-sdk/src/types/methods/get-preferences.ts index 5e798de6af..f0a3e16af2 100644 --- a/packages/snaps-sdk/src/types/methods/get-preferences.ts +++ b/packages/snaps-sdk/src/types/methods/get-preferences.ts @@ -8,7 +8,7 @@ export type GetPreferencesParams = never; /** * The result returned by the `snap_getPreferences` method. * - * It is the user selected preferences from the MetaMask extension. + * It is the user selected preferences from the MetaMask client. * * @property locale - The user's selected locale. * @property currency - The user's selected currency.