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

feat: Update Polygon from MATIC to POL #26671

Merged
merged 30 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
00ea151
Add pol-token.svg
gambinish Aug 27, 2024
f9467b4
Update POL network configurations
gambinish Aug 27, 2024
05b2fb7
Update polygon network to support collision for both MATIC and POL
gambinish Aug 27, 2024
7f7e379
Add migration for 128, rename MATIC to POL
gambinish Aug 27, 2024
94e304d
Update network typing on migration
gambinish Aug 27, 2024
c685a02
Update migration script and add unit test
gambinish Aug 27, 2024
32691fd
Update unit tests
gambinish Aug 27, 2024
4f7ef65
Cleanup
gambinish Aug 27, 2024
3e10e3e
Update snapshot
gambinish Aug 27, 2024
66b6d8f
Lint
gambinish Aug 27, 2024
3cc4371
Merge branch 'develop' into feat/mmassets-362_update-polygon-native-t…
gambinish Aug 27, 2024
cb19219
Update migration to handle legacy NetworkController providerConfig, c…
gambinish Aug 28, 2024
fe7b9ef
Correct migration version to 28
gambinish Aug 28, 2024
d29c5e9
Merge branch 'develop' into feat/mmassets-362_update-polygon-native-t…
gambinish Aug 28, 2024
d75c312
Remap references from matic-token.png and matic-token.svg to new pol-…
gambinish Aug 28, 2024
46688c0
Renumber migration 128 to 121.1 for incremental backport over 121
gambinish Aug 28, 2024
e9f7eaa
Type return type in migration script as void
gambinish Aug 29, 2024
b5cd488
Dont return newState, just mutate old state
gambinish Aug 29, 2024
d909341
Ensure that imageUrl is migrated for MATIC
gambinish Aug 29, 2024
d8e5ccc
Correct error in mocks to only update imageUrl within rpcPrefs, not e…
gambinish Aug 29, 2024
5b20b9b
Extra check to only update image path on string match
gambinish Aug 29, 2024
2adad06
Final cleanup
gambinish Aug 29, 2024
a1092f9
Add additional unit test for edge case
gambinish Aug 29, 2024
578e1a5
Merge branch 'develop' into feat/mmassets-362_update-polygon-native-t…
gambinish Aug 29, 2024
ed5ad56
Bump migration number to 121.2
gambinish Aug 29, 2024
30f88bc
Cleanup migration file
gambinish Aug 29, 2024
2bcf315
Merge develop, address conflict in migrations file
gambinish Aug 29, 2024
e8491d8
Clone deep migration test inputs
gambinish Aug 29, 2024
ff94e03
Merge branch 'develop' into feat/mmassets-362_update-polygon-native-t…
gambinish Aug 29, 2024
1866d55
Merge branch 'develop' into feat/mmassets-362_update-polygon-native-t…
gambinish Aug 29, 2024
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
Binary file removed app/images/matic-token.png
Binary file not shown.
File renamed without changes
274 changes: 274 additions & 0 deletions app/scripts/migrations/121.2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
import { cloneDeep } from 'lodash';
import { migrate, version } from './121.2';

const oldVersion = 121.1;

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` or `providerConfig` are not in the network controller state', async () => {
const oldState = {
NetworkController: {
selectedNetworkClientId: 'mainnet',
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

expect(transformedState.data).toStrictEqual(oldState);
});

it('Updates MATIC ticker to POL and updates imageURL in networkConfigurations', async () => {
const oldState = {
NetworkController: {
networkConfigurations: {
'0x89': {
chainId: '0x89',
ticker: 'MATIC',
rpcPrefs: {
imageUrl: './images/matic-token.svg',
},
},
},
},
};

const expectedState = {
NetworkController: {
networkConfigurations: {
'0x89': {
chainId: '0x89',
ticker: 'POL',
rpcPrefs: {
imageUrl: './images/pol-token.svg',
},
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

expect(transformedState.data).toStrictEqual(expectedState);
});

it('Does not update ticker to POL if ticker is not MATIC, but still updates imageURL in networkConfigurations', async () => {
const oldState = {
NetworkController: {
networkConfigurations: {
'0x89': {
chainId: '0x89',
ticker: 'NOT_MATIC',
rpcPrefs: {
imageUrl: './images/matic-token.svg',
},
},
},
},
};

const expectedState = {
NetworkController: {
networkConfigurations: {
'0x89': {
chainId: '0x89',
ticker: 'NOT_MATIC',
rpcPrefs: {
imageUrl: './images/pol-token.svg',
},
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

expect(transformedState.data).toStrictEqual(expectedState);
});

it('Does not update tickers for other network configurations, updates only ticker and imageURL for chain 0x89', async () => {
const oldState = {
NetworkController: {
networkConfigurations: {
'0x89': {
chainId: '0x89',
ticker: 'MATIC',
rpcPrefs: {
imageUrl: './images/matic-token.svg',
},
},
'0x1': {
chainId: '0x1',
ticker: 'ETH',
rpcPrefs: {
imageUrl: './images/eth-token.svg',
},
},
},
},
};

const expectedState = {
NetworkController: {
networkConfigurations: {
'0x89': {
chainId: '0x89',
ticker: 'POL',
rpcPrefs: {
imageUrl: './images/pol-token.svg',
},
},
'0x1': {
chainId: '0x1',
ticker: 'ETH',
rpcPrefs: {
imageUrl: './images/eth-token.svg',
},
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

expect(transformedState.data).toStrictEqual(expectedState);
});

it('Does nothing if Polygon ChainId (0x89) is not in networkConfigurations', async () => {
const oldState = {
NetworkController: {
networkConfigurations: {
'0x1': {
chainId: '0x1',
ticker: 'ETH',
rpcPrefs: {
imageUrl: './images/eth-token.svg',
},
},
'0x2a': {
chainId: '0x2a',
ticker: 'KOVAN',
rpcPrefs: {
imageUrl: './images/kovan-token.svg',
},
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

expect(transformedState.data).toStrictEqual(oldState);
});

it('Updates Polygon ChainId (0x89) in ProviderConfig if exists, and ticker is set to MATIC, and updates imageUrl', async () => {
const oldState = {
NetworkController: {
providerConfig: {
chainId: '0x89',
ticker: 'MATIC',
rpcPrefs: {
imageUrl: './images/matic-token.svg',
},
},
},
};

const expectedState = {
NetworkController: {
providerConfig: {
chainId: '0x89',
ticker: 'POL',
rpcPrefs: {
imageUrl: './images/pol-token.svg',
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

expect(transformedState.data).toStrictEqual(expectedState);
});

it('Does nothing if Polygon ChainId (0x89) is not in providerConfig', async () => {
const oldState = {
NetworkController: {
providerConfig: {
chainId: '0x1',
ticker: 'ETH',
rpcPrefs: {
imageUrl: './images/eth-token.svg',
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

expect(transformedState.data).toStrictEqual(oldState);
});

it('Does not update ticker if Polygon ChainId (0x89) is in providerConfig, but ticker is not MATIC, but still updates imageUrl', async () => {
const oldState = {
NetworkController: {
providerConfig: {
chainId: '0x89',
ticker: 'NOT_MATIC',
rpcPrefs: {
imageUrl: './images/matic-token.svg',
},
},
},
};

const expectedState = {
NetworkController: {
providerConfig: {
chainId: '0x89',
ticker: 'NOT_MATIC',
rpcPrefs: {
imageUrl: './images/pol-token.svg',
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

expect(transformedState.data).toStrictEqual(expectedState);
});
});
93 changes: 93 additions & 0 deletions app/scripts/migrations/121.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { hasProperty, isObject } from '@metamask/utils';
import { cloneDeep } from 'lodash';
import { CHAIN_IDS } from '../../../shared/constants/network';

export const version = 121.2;

type VersionedData = {
meta: { version: number };
data: Record<string, unknown>;
};

/**
* 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.
* @param originalVersionedData.meta.version - The current state version.
* @param originalVersionedData.data - The persisted MetaMask state, keyed by controller.
* @returns Updated versioned MetaMask extension state.
*/
export async function migrate(
originalVersionedData: VersionedData,
): Promise<VersionedData> {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
transformState(versionedData.data);
return versionedData;
}

function transformState(state: Record<string, unknown>): void {
const networkControllerState = state.NetworkController;
if (
hasProperty(state, 'NetworkController') &&
isObject(networkControllerState) &&
hasProperty(networkControllerState, 'networkConfigurations') &&
isObject(networkControllerState.networkConfigurations)
) {
for (const networkConfiguration of Object.values(
networkControllerState.networkConfigurations,
)) {
if (
isObject(networkConfiguration) &&
networkConfiguration.chainId === CHAIN_IDS.POLYGON
) {
// update image path regardless of ticker
if (
hasProperty(networkConfiguration, 'rpcPrefs') &&
isObject(networkConfiguration.rpcPrefs) &&
hasProperty(networkConfiguration.rpcPrefs, 'imageUrl') &&
networkConfiguration.rpcPrefs.imageUrl === './images/matic-token.svg'
) {
networkConfiguration.rpcPrefs.imageUrl = './images/pol-token.svg';
}
// update ticker only if MATIC
if (
hasProperty(networkConfiguration, 'ticker') &&
networkConfiguration.ticker === 'MATIC'
) {
networkConfiguration.ticker = 'POL';
}
}
}
}

// handle legacy NetworkController versions (with providerConfig)
if (
hasProperty(state, 'NetworkController') &&
isObject(networkControllerState) &&
hasProperty(networkControllerState, 'providerConfig') &&
isObject(networkControllerState.providerConfig) &&
hasProperty(networkControllerState.providerConfig, 'chainId') &&
networkControllerState.providerConfig.chainId === CHAIN_IDS.POLYGON
) {
// update image path regardless of ticker
if (
hasProperty(networkControllerState.providerConfig, 'rpcPrefs') &&
isObject(networkControllerState.providerConfig.rpcPrefs) &&
hasProperty(networkControllerState.providerConfig.rpcPrefs, 'imageUrl') &&
networkControllerState.providerConfig.rpcPrefs.imageUrl ===
'./images/matic-token.svg'
) {
networkControllerState.providerConfig.rpcPrefs.imageUrl =
'./images/pol-token.svg';
}
// update ticker only if MATIC
if (
hasProperty(networkControllerState.providerConfig, 'ticker') &&
networkControllerState.providerConfig.ticker === 'MATIC'
) {
networkControllerState.providerConfig.ticker = 'POL';
}
}
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const migrations = [
require('./120.6'),
require('./121'),
require('./121.1'),
require('./121.2'),
require('./122'),
require('./123'),
require('./124'),
Expand Down
Loading