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 14 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
21 changes: 21 additions & 0 deletions app/images/pol-token.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
208 changes: 208 additions & 0 deletions app/scripts/migrations/128.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
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);
});

it('Updates Polygon ChainId (0x89) in ProviderConfig if exists, and ticker is set to MATIC', async () => {
const oldState = {
NetworkController: {
providerConfig: {
chainId: '0x89',
ticker: 'MATIC',
},
},
};

const expectedState = {
NetworkController: {
providerConfig: {
chainId: '0x89',
ticker: 'POL',
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: 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',
},
},
};

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

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

it('Does nothing if Polygon ChainId (0x89) is in providerConfig, but ticker is not MATIC', async () => {
const oldState = {
NetworkController: {
providerConfig: {
chainId: '0x89',
ticker: 'NOT_MATIC',
},
},
};

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

expect(transformedState.data).toStrictEqual(oldState);
});
});
66 changes: 66 additions & 0 deletions app/scripts/migrations/128.ts
gambinish marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { hasProperty, isObject } from '@metamask/utils';
import { cloneDeep } from 'lodash';
import { CHAIN_IDS } from '../../../shared/constants/network';

export const version = 128;

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>) {
gambinish marked this conversation as resolved.
Show resolved Hide resolved
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 &&
networkConfiguration.ticker === 'MATIC'
) {
networkConfiguration.ticker = 'POL';
}
}
}

if (
hasProperty(state, 'NetworkController') &&
isObject(networkControllerState) &&
hasProperty(networkControllerState, 'providerConfig') &&
isObject(networkControllerState.providerConfig) &&
hasProperty(networkControllerState.providerConfig, 'chainId') &&
networkControllerState.providerConfig.chainId === CHAIN_IDS.POLYGON &&
networkControllerState.providerConfig.ticker === 'MATIC'
) {
networkControllerState.providerConfig.ticker = 'POL';
}
return {
...state,
NetworkController: networkControllerState,
};
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const migrations = [
require('./125'),
require('./126'),
require('./127'),
require('./128'),
];

export default migrations;
14 changes: 8 additions & 6 deletions shared/constants/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export const CURRENCY_SYMBOLS = {
HARMONY: 'ONE',
PALM: 'PALM',
MATIC: 'MATIC',
POL: 'POL',
TEST_ETH: 'TESTETH',
USDC: 'USDC',
USDT: 'USDT',
Expand Down Expand Up @@ -368,6 +369,7 @@ const CHAINLIST_CURRENCY_SYMBOLS_MAP = {
export const CHAINLIST_CURRENCY_SYMBOLS_MAP_NETWORK_COLLISION = {
WETHIO: 'ZYN',
CHZ: 'CHZ',
MATIC: 'POL',
};

export const ETH_TOKEN_IMAGE_URL = './images/eth_logo.svg';
Expand All @@ -376,7 +378,7 @@ export const LINEA_SEPOLIA_TOKEN_IMAGE_URL = './images/linea-logo-testnet.png';
export const LINEA_MAINNET_TOKEN_IMAGE_URL = './images/linea-logo-mainnet.svg';
export const TEST_ETH_TOKEN_IMAGE_URL = './images/black-eth-logo.svg';
export const BNB_TOKEN_IMAGE_URL = './images/bnb.svg';
export const MATIC_TOKEN_IMAGE_URL = './images/matic-token.svg';
gambinish marked this conversation as resolved.
Show resolved Hide resolved
export const POL_TOKEN_IMAGE_URL = './images/pol-token.svg';
export const AVAX_TOKEN_IMAGE_URL = './images/avax-token.svg';
export const AETH_TOKEN_IMAGE_URL = './images/arbitrum.svg';
export const FTM_TOKEN_IMAGE_URL = './images/fantom-opera.svg';
Expand Down Expand Up @@ -563,7 +565,7 @@ export const CHAIN_ID_TO_CURRENCY_SYMBOL_MAP = {
[CHAINLIST_CHAIN_IDS_MAP.MAINNET]: CHAINLIST_CURRENCY_SYMBOLS_MAP.ETH,
[CHAINLIST_CHAIN_IDS_MAP.OPBNB]: CHAINLIST_CURRENCY_SYMBOLS_MAP.OPBNB,
[CHAINLIST_CHAIN_IDS_MAP.OPTIMISM]: CHAINLIST_CURRENCY_SYMBOLS_MAP.OPTIMISM,
[CHAINLIST_CHAIN_IDS_MAP.POLYGON]: CHAINLIST_CURRENCY_SYMBOLS_MAP.MATIC,
[CHAINLIST_CHAIN_IDS_MAP.POLYGON]: CHAINLIST_CURRENCY_SYMBOLS_MAP.POL,
[CHAINLIST_CHAIN_IDS_MAP.ZKSYNC_ERA]:
CHAINLIST_CURRENCY_SYMBOLS_MAP.ZKSYNC_ERA,
[CHAINLIST_CHAIN_IDS_MAP.GOERLI]:
Expand Down Expand Up @@ -713,7 +715,7 @@ export const CHAIN_ID_TO_NETWORK_IMAGE_URL_MAP = {
[CHAIN_IDS.LINEA_MAINNET]: LINEA_MAINNET_TOKEN_IMAGE_URL,
[CHAIN_IDS.AVALANCHE]: AVAX_TOKEN_IMAGE_URL,
[CHAIN_IDS.BSC]: BNB_TOKEN_IMAGE_URL,
[CHAIN_IDS.POLYGON]: MATIC_TOKEN_IMAGE_URL,
[CHAIN_IDS.POLYGON]: POL_TOKEN_IMAGE_URL,
[CHAIN_IDS.ARBITRUM]: AETH_TOKEN_IMAGE_URL,
[CHAIN_IDS.FANTOM]: FTM_TOKEN_IMAGE_URL,
[CHAIN_IDS.HARMONY]: HARMONY_ONE_TOKEN_IMAGE_URL,
Expand Down Expand Up @@ -790,7 +792,7 @@ export const CHAIN_ID_TOKEN_IMAGE_MAP = {
[CHAIN_IDS.MAINNET]: ETH_TOKEN_IMAGE_URL,
[CHAIN_IDS.TEST_ETH]: TEST_ETH_TOKEN_IMAGE_URL,
[CHAIN_IDS.BSC]: BNB_TOKEN_IMAGE_URL,
[CHAIN_IDS.POLYGON]: MATIC_TOKEN_IMAGE_URL,
[CHAIN_IDS.POLYGON]: POL_TOKEN_IMAGE_URL,
[CHAIN_IDS.AVALANCHE]: AVAX_TOKEN_IMAGE_URL,
[CHAIN_IDS.OPTIMISM]: ETH_TOKEN_IMAGE_URL,
[CHAIN_IDS.CELO]: CELO_TOKEN_IMAGE_URL,
Expand Down Expand Up @@ -965,10 +967,10 @@ export const FEATURED_RPCS: RPCDefinition[] = [
chainId: CHAIN_IDS.POLYGON,
nickname: `${POLYGON_DISPLAY_NAME} ${capitalize(NETWORK_TYPES.MAINNET)}`,
rpcUrl: `https://polygon-mainnet.infura.io/v3/${infuraProjectId}`,
ticker: CURRENCY_SYMBOLS.MATIC,
ticker: CURRENCY_SYMBOLS.POL,
rpcPrefs: {
blockExplorerUrl: 'https://polygonscan.com/',
imageUrl: MATIC_TOKEN_IMAGE_URL,
imageUrl: POL_TOKEN_IMAGE_URL,
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions shared/constants/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ETH_TOKEN_IMAGE_URL,
TEST_ETH_TOKEN_IMAGE_URL,
BNB_TOKEN_IMAGE_URL,
MATIC_TOKEN_IMAGE_URL,
POL_TOKEN_IMAGE_URL,
AVAX_TOKEN_IMAGE_URL,
CURRENCY_SYMBOLS,
CHAIN_IDS,
Expand Down Expand Up @@ -74,7 +74,7 @@ export const MATIC_SWAPS_TOKEN_OBJECT: SwapsTokenObject = {
name: 'Matic',
address: DEFAULT_TOKEN_ADDRESS,
decimals: 18,
iconUrl: MATIC_TOKEN_IMAGE_URL,
iconUrl: POL_TOKEN_IMAGE_URL,
} as const;

export const AVAX_SWAPS_TOKEN_OBJECT: SwapsTokenObject = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports[`NetworkListItem renders properly 1`] = `
<img
alt="Polygon logo"
class="mm-avatar-network__network-image"
src="./images/matic-token.svg"
src="./images/pol-token.svg"
/>
</div>
<div
Expand Down
Loading