-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove MATIC from balance cache (#46)
- Loading branch information
Showing
3 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/background/services/storage/schemaMigrations/migrations/balances_v3.test.ts
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,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, | ||
}); | ||
}); | ||
}); |
50 changes: 50 additions & 0 deletions
50
src/background/services/storage/schemaMigrations/migrations/balances_v3.ts
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,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, | ||
}; |
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