Skip to content

Commit 3928290

Browse files
authored
feat: Add hideBalances to snap_getPreferences (#2978)
This adds a new property to the `snap_getPreferences` return value called `hideBalances` which binds to the property existing in the preference controller called `privacyMode`. Fixes: #2977
1 parent a342b7e commit 3928290

File tree

8 files changed

+60
-11
lines changed

8 files changed

+60
-11
lines changed

packages/snaps-rpc-methods/src/restricted/getPreferences.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ describe('snap_getPreferences', () => {
2626
describe('getImplementation', () => {
2727
it('returns the preferences', async () => {
2828
const methodHooks = {
29-
getPreferences: jest
30-
.fn()
31-
.mockReturnValue({ locale: 'en', currency: 'usd' }),
29+
getPreferences: jest.fn().mockReturnValue({
30+
locale: 'en',
31+
currency: 'usd',
32+
hideBalances: false,
33+
}),
3234
};
3335

3436
const implementation = getImplementation(methodHooks);
@@ -40,7 +42,7 @@ describe('snap_getPreferences', () => {
4042
},
4143
method: 'snap_getPreferences',
4244
}),
43-
).toStrictEqual({ locale: 'en', currency: 'usd' });
45+
).toStrictEqual({ locale: 'en', currency: 'usd', hideBalances: false });
4446
});
4547
});
4648
});

packages/snaps-sdk/src/types/methods/get-preferences.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ export type GetPreferencesParams = never;
88
/**
99
* The result returned by the `snap_getPreferences` method.
1010
*
11-
* It is the user selected preferences from the MetaMask extension.
11+
* It is the user selected preferences from the MetaMask client.
12+
*
13+
* @property locale - The user's selected locale.
14+
* @property currency - The user's selected currency.
15+
* @property hideBalances - Whether the user has chosen to hide balances.
1216
*/
13-
export type GetPreferencesResult = { locale: string; currency: string };
17+
export type GetPreferencesResult = {
18+
locale: string;
19+
currency: string;
20+
hideBalances: boolean;
21+
};

packages/snaps-simulation/src/methods/hooks/get-preferences.test.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ describe('getGetPreferencesMethodImplementation', () => {
99
}),
1010
);
1111

12-
expect(fn()).toStrictEqual({ currency: 'usd', locale: 'en' });
12+
expect(fn()).toStrictEqual({
13+
currency: 'usd',
14+
locale: 'en',
15+
hideBalances: false,
16+
});
1317
});
1418

1519
it('returns the implementation of the `getPreferences` hook for a different locale', async () => {
@@ -19,7 +23,11 @@ describe('getGetPreferencesMethodImplementation', () => {
1923
}),
2024
);
2125

22-
expect(fn()).toStrictEqual({ currency: 'usd', locale: 'nl' });
26+
expect(fn()).toStrictEqual({
27+
currency: 'usd',
28+
locale: 'nl',
29+
hideBalances: false,
30+
});
2331
});
2432

2533
it('returns the implementation of the `getPreferences` hook for a different currency', async () => {
@@ -29,6 +37,24 @@ describe('getGetPreferencesMethodImplementation', () => {
2937
}),
3038
);
3139

32-
expect(fn()).toStrictEqual({ currency: 'dkk', locale: 'en' });
40+
expect(fn()).toStrictEqual({
41+
currency: 'dkk',
42+
locale: 'en',
43+
hideBalances: false,
44+
});
45+
});
46+
47+
it('returns the implementation of the `getPreferences` hook for hidden balances', async () => {
48+
const fn = getGetPreferencesMethodImplementation(
49+
getMockOptions({
50+
hideBalances: true,
51+
}),
52+
);
53+
54+
expect(fn()).toStrictEqual({
55+
currency: 'usd',
56+
locale: 'en',
57+
hideBalances: true,
58+
});
3359
});
3460
});

packages/snaps-simulation/src/methods/hooks/get-preferences.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import type { SimulationOptions } from '../../options';
66
* @param options - The simulation options.
77
* @param options.currency - The currency to use.
88
* @param options.locale - The locale to use.
9+
* @param options.hideBalances - Whether to hide balances.
910
* @returns The implementation of the `getPreferences` hook.
1011
*/
1112
export function getGetPreferencesMethodImplementation({
1213
currency,
1314
locale,
15+
hideBalances,
1416
}: SimulationOptions) {
1517
return () => {
16-
return { currency, locale };
18+
return { currency, locale, hideBalances };
1719
};
1820
}

packages/snaps-simulation/src/options.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe('getOptions', () => {
77
expect(options).toMatchInlineSnapshot(`
88
{
99
"currency": "usd",
10+
"hideBalances": false,
1011
"locale": "en",
1112
"secretRecoveryPhrase": "test test test test test test test test test test test ball",
1213
"state": null,
@@ -24,6 +25,7 @@ describe('getOptions', () => {
2425
expect(options).toMatchInlineSnapshot(`
2526
{
2627
"currency": "eur",
28+
"hideBalances": false,
2729
"locale": "nl",
2830
"secretRecoveryPhrase": "test test test test test test test test test test test ball",
2931
"state": null,

packages/snaps-simulation/src/options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Infer } from '@metamask/superstruct';
22
import {
3+
boolean,
34
create,
45
defaulted,
56
nullable,
@@ -21,6 +22,7 @@ const SimulationOptionsStruct = object({
2122
optional(nullable(record(string(), JsonStruct))),
2223
null,
2324
),
25+
hideBalances: defaulted(optional(boolean()), false),
2426
});
2527

2628
/**

packages/snaps-simulation/src/test-utils/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import type { SimulationOptions } from '../options';
1010
* @param options.secretRecoveryPhrase - The secret recovery phrase to use.
1111
* @param options.state - The state to use.
1212
* @param options.unencryptedState - The unencrypted state to use.
13+
* @param options.hideBalances - Whether to hide balances.
1314
* @returns The options for the simulation.
1415
*/
1516
export function getMockOptions({
1617
currency = 'usd',
1718
locale = 'en',
19+
hideBalances = false,
1820
secretRecoveryPhrase = DEFAULT_SRP,
1921
state = null,
2022
unencryptedState = null,
@@ -25,5 +27,6 @@ export function getMockOptions({
2527
secretRecoveryPhrase,
2628
state,
2729
unencryptedState,
30+
hideBalances,
2831
};
2932
}

packages/snaps-simulator/src/features/simulation/sagas.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ export function* initSaga({ payload }: PayloadAction<string>) {
130130
...sharedHooks,
131131
// TODO: Add all the hooks required
132132
// TODO: Allow changing this?
133-
getPreferences: () => ({ locale: 'en', currency: 'usd' }),
133+
getPreferences: () => ({
134+
locale: 'en',
135+
currency: 'usd',
136+
hideBalances: false,
137+
}),
134138
getUnlockPromise: async () => Promise.resolve(true),
135139
showDialog: async (...args: Parameters<typeof showDialog>) =>
136140
await runSaga(showDialog, ...args).toPromise(),

0 commit comments

Comments
 (0)