Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove MATIC from balance cache #46

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading