diff --git a/src/background/services/storage/schemaMigrations/migrations/balances_v3.test.ts b/src/background/services/storage/schemaMigrations/migrations/balances_v3.test.ts new file mode 100644 index 00000000..1388b716 --- /dev/null +++ b/src/background/services/storage/schemaMigrations/migrations/balances_v3.test.ts @@ -0,0 +1,75 @@ +import balances_v3, { PreviousSchema } from './balances_v3'; + +describe('background/services/storage/schemaMigrations/migrations/balances_v3', () => { + const previousValue: PreviousSchema = { + balances: { + '1': { + 'address-1-a': { + 'token-address-1-a': { + balance: 1, + }, + }, + 'address-1-b': { + 'token-address-1-b': { + balance: 2, + }, + }, + }, + '137': { + 'address-137-a': { + POL: { + balance: 4, + }, + MATIC: { + balance: 4, + }, + 'token-address-137-a': { + balance: 3, + }, + }, + 'address-137-b': { + POL: { + balance: 5, + }, + }, + }, + }, + }; + + it('removes MATIC from cached balances and leaves other tokens in tact', async () => { + const newBalances = await balances_v3.up(previousValue); + + expect(newBalances).toEqual({ + balances: { + '1': { + 'address-1-a': { + 'token-address-1-a': { + balance: 1, + }, + }, + 'address-1-b': { + 'token-address-1-b': { + balance: 2, + }, + }, + }, + '137': { + 'address-137-a': { + POL: { + balance: 4, + }, + 'token-address-137-a': { + balance: 3, + }, + }, + 'address-137-b': { + POL: { + balance: 5, + }, + }, + }, + }, + version: 3, + }); + }); +}); diff --git a/src/background/services/storage/schemaMigrations/migrations/balances_v3.ts b/src/background/services/storage/schemaMigrations/migrations/balances_v3.ts new file mode 100644 index 00000000..654df7ec --- /dev/null +++ b/src/background/services/storage/schemaMigrations/migrations/balances_v3.ts @@ -0,0 +1,50 @@ +import Joi from 'joi'; + +export type PreviousSchema = { + balances?: { + [networkId: string | number]: { + [accountAddress: string]: { + [tokenAddressOrSymbol: string]: Record; + }; + }; + }; +}; + +const VERSION = 3; +const POLYGON_CHAIN_ID = 137; +const previousSchema = Joi.object(); + +/** + * Polygon changed the name of their native token from MATIC to POL. + * We need to remove MATIC from cache so we don't show it twice. + */ +const up = async (cache: PreviousSchema) => { + const oldBalances = cache.balances; + const oldPolygonBalances = oldBalances?.[POLYGON_CHAIN_ID]; + + if (!oldPolygonBalances) { + return oldBalances; + } + + const newPolygonBalances = Object.fromEntries( + Object.entries(oldPolygonBalances).map(([address, addressBalances]) => { + const { MATIC, ...addressBalancesWithoutMatic } = addressBalances; // eslint-disable-line + + return [address, addressBalancesWithoutMatic]; + }) + ); + + return { + ...cache, + balances: { + ...oldBalances, + [POLYGON_CHAIN_ID]: newPolygonBalances, + }, + version: VERSION, + }; +}; + +export default { + previousSchema, + up, +}; diff --git a/src/background/services/storage/schemaMigrations/schemaMap.ts b/src/background/services/storage/schemaMigrations/schemaMap.ts index 160ce8a7..bed9a15f 100644 --- a/src/background/services/storage/schemaMigrations/schemaMap.ts +++ b/src/background/services/storage/schemaMigrations/schemaMap.ts @@ -16,6 +16,7 @@ import { BALANCES_CACHE_KEY } from '../../balances/models'; import balances_v2 from './migrations/balances_v2'; import network_v3 from './migrations/network_v3'; import network_v4 from './migrations/network_v4'; +import balances_v3 from './migrations/balances_v3'; export type Migration = { previousSchema: Joi.Schema; @@ -100,12 +101,16 @@ export const SCHEMA_MAP = { ], }, [BALANCES_CACHE_KEY]: { - latestVersion: 2, + latestVersion: 3, migrations: [ { version: 2, migration: balances_v2, }, + { + version: 3, + migration: balances_v3, + }, ], }, } as const;