Skip to content

Commit

Permalink
Update migration script and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gambinish committed Aug 27, 2024
1 parent 94e304d commit c685a02
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 21 deletions.
145 changes: 145 additions & 0 deletions app/scripts/migrations/128.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
41 changes: 20 additions & 21 deletions app/scripts/migrations/128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,27 +30,26 @@ export async function migrate(
function transformState(state: Record<string, unknown>) {
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;
}

0 comments on commit c685a02

Please sign in to comment.