Skip to content

Commit

Permalink
chore: remove MATIC from balance cache (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w committed Sep 13, 2024
1 parent d6b971d commit dfcf636
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Joi from 'joi';

export type PreviousSchema = {
balances?: {
[networkId: string | number]: {
[accountAddress: string]: {
[tokenAddressOrSymbol: string]: Record<string, unknown>;
};
};
};
};

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,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

0 comments on commit dfcf636

Please sign in to comment.