Skip to content

Commit d66c8a8

Browse files
authored
Merge branch 'main' into fix/remove-supported-chains-check-ppom
2 parents 3e9ea7b + 599a4b5 commit d66c8a8

File tree

7 files changed

+299
-120
lines changed

7 files changed

+299
-120
lines changed

app/scripts/migrations/144.test.ts

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { migrate, version } from './144';
2+
3+
const oldVersion = 143;
4+
5+
const DEFAULT_CURRENCY = 'usd';
6+
const VALID_CURRENCY = 'eur';
7+
const INVALID_CURRENCY = 'INVALID_CURRENCY';
8+
9+
describe(`migration #${version}`, () => {
10+
it('updates the version metadata', async () => {
11+
const oldStorage = {
12+
meta: { version: oldVersion },
13+
data: {},
14+
};
15+
const newStorage = await migrate(oldStorage);
16+
expect(newStorage.meta).toStrictEqual({ version });
17+
});
18+
19+
describe(`migration #${version}`, () => {
20+
it('does nothing if CurrencyController is missing', async () => {
21+
const oldStorage = {
22+
meta: { version: oldVersion },
23+
data: {},
24+
};
25+
const newStorage = await migrate(oldStorage);
26+
expect(newStorage.data).toStrictEqual({});
27+
});
28+
29+
it('does nothing if CurrencyController is not an object', async () => {
30+
const oldStorage = {
31+
meta: { version: oldVersion },
32+
data: {
33+
CurrencyController: 'invalidData',
34+
},
35+
};
36+
const newStorage = await migrate(oldStorage);
37+
expect(newStorage.data).toStrictEqual(oldStorage.data);
38+
});
39+
40+
it('sets currentCurrency to "USD" if it is missing', async () => {
41+
const oldStorage = {
42+
meta: { version: oldVersion },
43+
data: {
44+
CurrencyController: {},
45+
},
46+
};
47+
const expectedData = {
48+
CurrencyController: {
49+
currentCurrency: DEFAULT_CURRENCY,
50+
},
51+
};
52+
const newStorage = await migrate(oldStorage);
53+
expect(newStorage.data).toStrictEqual(expectedData);
54+
});
55+
56+
it('sets currentCurrency to "USD" if it is invalid', async () => {
57+
const oldStorage = {
58+
meta: { version: oldVersion },
59+
data: {
60+
CurrencyController: {
61+
currentCurrency: INVALID_CURRENCY,
62+
},
63+
},
64+
};
65+
const expectedData = {
66+
CurrencyController: {
67+
currentCurrency: DEFAULT_CURRENCY,
68+
},
69+
};
70+
const newStorage = await migrate(oldStorage);
71+
expect(newStorage.data).toStrictEqual(expectedData);
72+
});
73+
74+
it('does nothing if currentCurrency is valid', async () => {
75+
const oldStorage = {
76+
meta: { version: oldVersion },
77+
data: {
78+
CurrencyController: {
79+
currentCurrency: VALID_CURRENCY,
80+
},
81+
},
82+
};
83+
const newStorage = await migrate(oldStorage);
84+
expect(newStorage.data).toStrictEqual(oldStorage.data);
85+
});
86+
});
87+
});

app/scripts/migrations/144.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { hasProperty } from '@metamask/utils';
2+
import { cloneDeep, isObject } from 'lodash';
3+
import { PRICE_API_CURRENCIES } from '../../../shared/constants/price-api-currencies';
4+
5+
type VersionedData = {
6+
meta: { version: number };
7+
data: Record<string, unknown>;
8+
};
9+
10+
type CurrencyController = {
11+
currentCurrency?: string;
12+
};
13+
14+
export const version = 144;
15+
const DEFAULT_CURRENCY = 'usd';
16+
17+
/**
18+
* This migration ensures that the `currentCurrency` in `CurrencyController`
19+
* is set to a valid available currency. If it's missing or invalid, it defaults to "USD".
20+
*
21+
* @param originalVersionedData - The original MetaMask extension state.
22+
* @returns Updated versioned MetaMask extension state.
23+
*/
24+
export async function migrate(
25+
originalVersionedData: VersionedData,
26+
): Promise<VersionedData> {
27+
const versionedData = cloneDeep(originalVersionedData);
28+
versionedData.meta.version = version;
29+
transformState(versionedData.data);
30+
return versionedData;
31+
}
32+
33+
function transformState(state: Record<string, unknown>) {
34+
if (!hasProperty(state, 'CurrencyController')) {
35+
global.sentry?.captureException?.(
36+
new Error(`Migration ${version}: Missing CurrencyController in state`),
37+
);
38+
return;
39+
}
40+
41+
const currencyController = state.CurrencyController as CurrencyController;
42+
43+
if (!isObject(currencyController)) {
44+
global.sentry?.captureException?.(
45+
new Error(
46+
`Migration ${version}: Invalid CurrencyController state type '${typeof currencyController}'`,
47+
),
48+
);
49+
return;
50+
}
51+
52+
const { currentCurrency } = currencyController;
53+
54+
if (!currentCurrency) {
55+
global.sentry?.captureException?.(
56+
new Error(
57+
`Migration ${version}: Missing currentCurrency in CurrencyController, defaulting to ${DEFAULT_CURRENCY}`,
58+
),
59+
);
60+
currencyController.currentCurrency = DEFAULT_CURRENCY;
61+
return;
62+
}
63+
64+
const isValidCurrency = PRICE_API_CURRENCIES.some(
65+
(currency) => currency === currentCurrency,
66+
);
67+
68+
if (!isValidCurrency) {
69+
currencyController.currentCurrency = DEFAULT_CURRENCY;
70+
}
71+
}

app/scripts/migrations/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ const migrations = [
168168
require('./141'),
169169
require('./142'),
170170
require('./143'),
171+
require('./144'),
171172
];
172173

173174
export default migrations;
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export const PRICE_API_CURRENCIES = [
2+
'aud',
3+
'hkd',
4+
'sgd',
5+
'idr',
6+
'inr',
7+
'nzd',
8+
'php',
9+
'btc',
10+
'cad',
11+
'eur',
12+
'gbp',
13+
'jpy',
14+
'ltc',
15+
'rub',
16+
'uah',
17+
'usd',
18+
'xlm',
19+
'xrp',
20+
'sek',
21+
'aed',
22+
'ars',
23+
'bch',
24+
'bnb',
25+
'brl',
26+
'clp',
27+
'cny',
28+
'czk',
29+
'dkk',
30+
'chf',
31+
'dot',
32+
'eos',
33+
'eth',
34+
'gel',
35+
'huf',
36+
'ils',
37+
'krw',
38+
'mxn',
39+
'myr',
40+
'ngn',
41+
'nok',
42+
'pln',
43+
'thb',
44+
'try',
45+
'zar',
46+
];

0 commit comments

Comments
 (0)