-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: process the migration only if 1 infura rpc exists
- Loading branch information
Showing
4 changed files
with
223 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
|
||
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; | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.