From c685a02a67c413025058fcb16d1fb165378b0d0e Mon Sep 17 00:00:00 2001 From: Nicholas Gambino Date: Tue, 27 Aug 2024 13:37:08 -0700 Subject: [PATCH] Update migration script and add unit test --- app/scripts/migrations/128.test.ts | 145 +++++++++++++++++++++++++++++ app/scripts/migrations/128.ts | 41 ++++---- 2 files changed, 165 insertions(+), 21 deletions(-) create mode 100644 app/scripts/migrations/128.test.ts diff --git a/app/scripts/migrations/128.test.ts b/app/scripts/migrations/128.test.ts new file mode 100644 index 000000000000..dc324b5f09f6 --- /dev/null +++ b/app/scripts/migrations/128.test.ts @@ -0,0 +1,145 @@ +import { migrate, version } from './128'; + +const oldVersion = 127; + +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 }); + }); + + it('Does nothing if `networkConfigurations` is not in the network controller state', async () => { + const oldState = { + NetworkController: { + selectedNetworkClientId: 'mainnet', + }, + }; + + const transformedState = await migrate({ + meta: { version: oldVersion }, + data: oldState, + }); + + expect(transformedState.data).toStrictEqual(oldState); + }); + + it('Updates MATIC ticker to POL in networkConfigurations', async () => { + const oldState = { + NetworkController: { + networkConfigurations: { + '0x89': { + chainId: '0x89', + ticker: 'MATIC', + }, + }, + }, + }; + + const expectedState = { + NetworkController: { + networkConfigurations: { + '0x89': { + chainId: '0x89', + ticker: 'POL', + }, + }, + }, + }; + + const transformedState = await migrate({ + meta: { version: oldVersion }, + data: oldState, + }); + + expect(transformedState.data).toStrictEqual(expectedState); + }); + + it('Does not update tickers for other network configurations', async () => { + const oldState = { + NetworkController: { + networkConfigurations: { + '0x89': { + chainId: '0x89', + ticker: 'MATIC', + }, + '0x1': { + chainId: '0x1', + ticker: 'ETH', + }, + }, + }, + }; + + const expectedState = { + NetworkController: { + networkConfigurations: { + '0x89': { + chainId: '0x89', + ticker: 'POL', + }, + '0x1': { + chainId: '0x1', + ticker: 'ETH', + }, + }, + }, + }; + + const transformedState = await migrate({ + meta: { version: oldVersion }, + data: oldState, + }); + + expect(transformedState.data).toStrictEqual(expectedState); + }); + + it('Does nothing if the ticker is already POL for the 0x89 chainId', async () => { + const oldState = { + NetworkController: { + networkConfigurations: { + '0x89': { + chainId: '0x89', + ticker: 'POL', + }, + }, + }, + }; + + const transformedState = await migrate({ + meta: { version: oldVersion }, + data: oldState, + }); + + expect(transformedState.data).toStrictEqual(oldState); + }); + + it('Does nothing if Polygon ChainId (0x89) is not in networkConfigurations', async () => { + const oldState = { + NetworkController: { + networkConfigurations: { + '0x1': { + chainId: '0x1', + ticker: 'ETH', + }, + '0x2a': { + chainId: '0x2a', + ticker: 'KOVAN', + }, + }, + }, + }; + + const transformedState = await migrate({ + meta: { version: oldVersion }, + data: oldState, + }); + + expect(transformedState.data).toStrictEqual(oldState); + }); +}); diff --git a/app/scripts/migrations/128.ts b/app/scripts/migrations/128.ts index 7cf917e80634..4e49dcd74754 100644 --- a/app/scripts/migrations/128.ts +++ b/app/scripts/migrations/128.ts @@ -10,7 +10,7 @@ type VersionedData = { }; /** - * Explain the purpose of the migration here. + * Migrates MATIC ticker in Network Configuration to POL ticker as per the direction in https://polygon.technology/blog/save-the-date-matic-pol-migration-coming-september-4th-everything-you-need-to-know * * @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist. * @param originalVersionedData.meta - State metadata. @@ -30,27 +30,26 @@ export async function migrate( function transformState(state: Record) { if ( hasProperty(state, 'NetworkController') && - isObject(state.NetworkController) + isObject(state.NetworkController) && + hasProperty(state.NetworkController, 'networkConfigurations') && + isObject(state.NetworkController.networkConfigurations) ) { - // type NetworkConfiguration and NetworkConfigurationId not exported from NetworkConroller.d.ts - // need for reverse lookup. typing to string - const existingNetworkConfigsCopy = state.NetworkController - .networkConfigurations as Record< - string, - NetworkConfiguration & { - id: string; + for (const networkConfiguration of Object.values( + state.NetworkController.networkConfigurations as Record< + string, + { + chainId: string; + ticker: string; + } + >, + )) { + if ( + networkConfiguration.chainId === '0x89' && + networkConfiguration.ticker === 'MATIC' + ) { + networkConfiguration.ticker = 'POL'; } - >; - - Object.values(existingNetworkConfigsCopy).forEach((networkConfig) => { - if (networkConfig.ticker === 'MATIC') { - existingNetworkConfigsCopy[networkConfig.id].ticker = 'POL'; - } - }); - - state.NetworkController.networkConfigurations = existingNetworkConfigsCopy; + } } - const newState = state; - // transform state here - return newState; + return state; }