Skip to content

Commit

Permalink
fix: process the migration only if 1 infura rpc exists
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Feb 14, 2025
1 parent 188d7d6 commit b0b8de3
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 120 deletions.
87 changes: 87 additions & 0 deletions app/scripts/migrations/144.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { migrate, version } from './144';

const oldVersion = 143;

const DEFAULT_CURRENCY = 'usd';
const VALID_CURRENCY = 'eur';
const INVALID_CURRENCY = 'INVALID_CURRENCY';

describe(`migration #${version}`, () => {
it('updates the version metadata', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.meta).toStrictEqual({ version });
});

describe(`migration #${version}`, () => {
it('does nothing if CurrencyController is missing', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual({});
});

it('does nothing if CurrencyController is not an object', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
CurrencyController: 'invalidData',
},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldStorage.data);
});

it('sets currentCurrency to "USD" if it is missing', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
CurrencyController: {},
},
};
const expectedData = {
CurrencyController: {
currentCurrency: DEFAULT_CURRENCY,
},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(expectedData);
});

it('sets currentCurrency to "USD" if it is invalid', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
CurrencyController: {
currentCurrency: INVALID_CURRENCY,
},
},
};
const expectedData = {
CurrencyController: {
currentCurrency: DEFAULT_CURRENCY,
},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(expectedData);
});

it('does nothing if currentCurrency is valid', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
CurrencyController: {
currentCurrency: VALID_CURRENCY,
},
},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.data).toStrictEqual(oldStorage.data);
});
});
});
71 changes: 71 additions & 0 deletions app/scripts/migrations/144.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { hasProperty } from '@metamask/utils';
import { cloneDeep, isObject } from 'lodash';
import availableCurrencies from '../../../ui/helpers/constants/available-conversions.json';

Check failure on line 3 in app/scripts/migrations/144.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Unexpected path "../../../ui/helpers/constants/available-conversions.json" imported in restricted zone. Should not import from UI in background, use shared directory instead

type VersionedData = {
meta: { version: number };
data: Record<string, unknown>;
};

type CurrencyController = {
currentCurrency?: string;
};

export const version = 144;
const DEFAULT_CURRENCY = 'usd';

/**
* This migration ensures that the `currentCurrency` in `CurrencyController`
* is set to a valid available currency. If it's missing or invalid, it defaults to "USD".
*
* @param originalVersionedData - The original MetaMask extension state.
* @returns Updated versioned MetaMask extension state.
*/
export async function migrate(
originalVersionedData: VersionedData,
): Promise<VersionedData> {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
transformState(versionedData.data);
return versionedData;
}

function transformState(state: Record<string, unknown>) {
if (!hasProperty(state, 'CurrencyController')) {
global.sentry?.captureException?.(
new Error(`Migration ${version}: Missing CurrencyController in state`),
);
return;
}

const currencyController = state.CurrencyController as CurrencyController;

if (!isObject(currencyController)) {
global.sentry?.captureException?.(
new Error(
`Migration ${version}: Invalid CurrencyController state type '${typeof currencyController}'`,
),
);
return;
}

const currentCurrency = currencyController.currentCurrency;

Check failure on line 52 in app/scripts/migrations/144.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Use object destructuring

if (!currentCurrency) {
global.sentry?.captureException?.(
new Error(
`Migration ${version}: Missing currentCurrency in CurrencyController`,
),
);
currencyController.currentCurrency = DEFAULT_CURRENCY;
return;
}

const isValidCurrency = availableCurrencies.some(
(currency) => currency.code === currentCurrency,
);

if (!isValidCurrency) {
currencyController.currentCurrency = DEFAULT_CURRENCY;
}
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const migrations = [
require('./141'),
require('./142'),
require('./143'),
require('./144'),
];

export default migrations;
Loading

0 comments on commit b0b8de3

Please sign in to comment.