Skip to content

Commit

Permalink
fix: deprecating more networks
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Jun 3, 2024
1 parent 07f1eb4 commit 2f60905
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 132 deletions.
1 change: 0 additions & 1 deletion app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const mainNetworks = {
const testNetworks = {
[CHAIN_IDS.GOERLI]: true,
[CHAIN_IDS.SEPOLIA]: true,
[CHAIN_IDS.LINEA_GOERLI]: true,
[CHAIN_IDS.LINEA_SEPOLIA]: true,
};

Expand Down
2 changes: 0 additions & 2 deletions app/scripts/controllers/preferences.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ describe('preferences controller', () => {
[NETWORK_CONFIGURATION_DATA[addedNonTestNetworks[1]].chainId]: true,
[CHAIN_IDS.GOERLI]: true,
[CHAIN_IDS.SEPOLIA]: true,
[CHAIN_IDS.LINEA_GOERLI]: true,
[CHAIN_IDS.LINEA_SEPOLIA]: true,
});
});
Expand All @@ -390,7 +389,6 @@ describe('preferences controller', () => {
[NETWORK_CONFIGURATION_DATA[addedNonTestNetworks[1]].chainId]: true,
[CHAIN_IDS.GOERLI]: true,
[CHAIN_IDS.SEPOLIA]: true,
[CHAIN_IDS.LINEA_GOERLI]: true,
[CHAIN_IDS.LINEA_SEPOLIA]: true,
});
});
Expand Down
194 changes: 194 additions & 0 deletions app/scripts/migrations/119.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import { NetworkType } from '@metamask/controller-utils';
import {
CHAIN_IDS,
CHAIN_ID_TO_RPC_URL_MAP,
LINEA_SEPOLIA_DISPLAY_NAME,
NETWORK_TYPES,
TEST_NETWORK_TICKER_MAP,
} from '../../../shared/constants/network';
import { migrate, version } from './119';

const oldVersion = 118;

const ethereumProviderConfig = {
chainId: '0x1',
rpcPrefs: {
blockExplorerUrl: 'https://etherscan.io',
},
ticker: 'ETH',
type: 'mainnet',
};

const ethereumNetworksMetadata = {
mainnet: {
EIPS: {
'1559': true,
},
status: 'available',
},
};
const ethereumOldState = {
CurrencyController: {
currencyRates: {
ETH: {
conversionDate: 1708532473.416,
conversionRate: 2918.02,
usdConversionRate: 2918.02,
},
GoerliETH: {
conversionDate: 1708532466.732,
conversionRate: 2918.02,
usdConversionRate: 2918.02,
},
},
currentCurrency: 'usd',
},
NetworkController: {
networkConfigurations: {},
networksMetadata: ethereumNetworksMetadata,
providerConfig: ethereumProviderConfig,
selectedNetworkClientId: 'mainnet',
},
};

const lineaGoerliState = {
NetworkController: {
networkConfigurations: {},
networksMetadata: {
'linea-goerli': {
EIPS: {
'1559': true,
},
status: 'available',
},
},
providerConfig: {
chainId: CHAIN_IDS.LINEA_GOERLI,
rpcPrefs: {},
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.LINEA_GOERLI],
type: NETWORK_TYPES.LINEA_GOERLI,
},
selectedNetworkClientId: NETWORK_TYPES.LINEA_GOERLI,
},
};

describe('migration #119', () => {
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 no preferences state', async () => {
const oldState = {
OtherController: {},
};

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

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

it('Should return state if chainId is not linea-goerli', async () => {
const transformedState = await migrate({
meta: { version: oldVersion },
data: ethereumOldState,
});

expect(transformedState.data).toEqual(ethereumOldState);
});

it('Should return state if there is no NetworkController in state', async () => {
const { NetworkController, ...state } = ethereumOldState;
const transformedState = await migrate({
meta: { version: oldVersion },
data: state,
});

expect(transformedState.data).toEqual(state);
});

it('Should return state if there is no provider in NetworkController', async () => {
const state = {
...ethereumOldState,
NetworkController: {},
};
const transformedState = await migrate({
meta: { version: oldVersion },
data: state,
});

expect(transformedState.data).toEqual(state);
});

it('Should return state if there is no chainId in provider in NetworkController', async () => {
const state = {
...ethereumOldState,
NetworkController: {
providerConfig: {},
},
};
const transformedState = await migrate({
meta: { version: oldVersion },
data: state,
});

expect(transformedState.data).toEqual(state);
});

it('Should return state if chainId is not linea-goerli', async () => {
const transformedState = await migrate({
meta: { version: oldVersion },
data: ethereumOldState,
});

expect(transformedState.data).toEqual(ethereumOldState);
});

it('Should update NetworkController to Linea Sepolia if chainId is on Linea Goerli', async () => {
const expectedNetworkControllerState = {
networkConfigurations: {},
networksMetadata: {
'linea-sepolia': {
EIPS: {
'1559': true,
},
status: 'available',
},
'linea-goerli': {
EIPS: {
'1559': true,
},
status: 'available',
},
},
providerConfig: {
type: NetworkType['linea-sepolia'],
rpcPrefs: {},
chainId: CHAIN_IDS.LINEA_SEPOLIA,
nickname: LINEA_SEPOLIA_DISPLAY_NAME,
rpcUrl: CHAIN_ID_TO_RPC_URL_MAP[CHAIN_IDS.LINEA_SEPOLIA],
providerType: NETWORK_TYPES.LINEA_SEPOLIA,
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.LINEA_SEPOLIA],
id: NETWORK_TYPES.LINEA_SEPOLIA,
},
selectedNetworkClientId: 'linea-sepolia',
};
const transformedState = await migrate({
meta: { version: oldVersion },
data: lineaGoerliState,
});

expect(transformedState.data).toEqual({
NetworkController: expectedNetworkControllerState,
});
});
});
81 changes: 81 additions & 0 deletions app/scripts/migrations/119.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { cloneDeep, isObject } from 'lodash';
import { NetworkType } from '@metamask/controller-utils';
import { hasProperty } from '@metamask/utils';
import { NetworkStatus } from '@metamask/network-controller';
import {
CHAIN_IDS,
CHAIN_ID_TO_RPC_URL_MAP,
NETWORK_TYPES,
TEST_NETWORK_TICKER_MAP,
LINEA_SEPOLIA_DISPLAY_NAME,
} from '../../../shared/constants/network';

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

export const version = 119;

/**
* Migrates the user network to Linea Sepolia if the user is on Linea Goerli network.
*
* @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;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function transformState(state: Record<string, any>) {
const NetworkController = state?.NetworkController || {};
const provider = NetworkController?.providerConfig || {};

if (provider?.chainId !== CHAIN_IDS.LINEA_GOERLI) {
return state;
}
const networkControllerState = state.NetworkController;

if (
hasProperty(state, 'NetworkController') &&
isObject(state.NetworkController) &&
hasProperty(state.NetworkController, 'providerConfig') &&
isObject(state.NetworkController.providerConfig) &&
hasProperty(state.NetworkController.providerConfig, 'chainId') &&
state.NetworkController.providerConfig.chainId === CHAIN_IDS.LINEA_GOERLI
) {
networkControllerState.providerConfig = {
type: NetworkType['linea-sepolia'],
rpcPrefs: {},
chainId: CHAIN_IDS.LINEA_SEPOLIA,
nickname: LINEA_SEPOLIA_DISPLAY_NAME,
rpcUrl: CHAIN_ID_TO_RPC_URL_MAP[CHAIN_IDS.LINEA_SEPOLIA],
providerType: NETWORK_TYPES.LINEA_SEPOLIA,
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.LINEA_SEPOLIA],
id: NETWORK_TYPES.LINEA_SEPOLIA,
};
networkControllerState.selectedNetworkClientId =
NETWORK_TYPES.LINEA_SEPOLIA;
networkControllerState.networksMetadata = {
...networkControllerState.networksMetadata,
'linea-sepolia': {
EIPS: {
'1559': true,
},
status: NetworkStatus.Available,
},
};
}
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 @@ -129,6 +129,7 @@ const migrations = [
require('./116'),
require('./117'),
require('./118'),
require('./119'),
];

export default migrations;
7 changes: 0 additions & 7 deletions shared/constants/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,12 @@ export const NUMBERS_TOKEN_IMAGE_URL = './images/numbers-token.png';
export const INFURA_PROVIDER_TYPES = [
NETWORK_TYPES.MAINNET,
NETWORK_TYPES.SEPOLIA,
NETWORK_TYPES.LINEA_GOERLI,
NETWORK_TYPES.LINEA_SEPOLIA,
NETWORK_TYPES.LINEA_MAINNET,
] as const;

export const TEST_CHAINS = [
CHAIN_IDS.SEPOLIA,
NETWORK_TYPES.LINEA_GOERLI,
CHAIN_IDS.LINEA_SEPOLIA,
CHAIN_IDS.LOCALHOST,
];
Expand Down Expand Up @@ -491,11 +489,6 @@ export const BUILT_IN_NETWORKS = {
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.SEPOLIA],
blockExplorerUrl: `https://${NETWORK_TYPES.SEPOLIA}.etherscan.io`,
},
[NETWORK_TYPES.LINEA_GOERLI]: {
chainId: CHAIN_IDS.LINEA_GOERLI,
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.LINEA_GOERLI],
blockExplorerUrl: 'https://goerli.lineascan.build',
},
[NETWORK_TYPES.LINEA_SEPOLIA]: {
chainId: CHAIN_IDS.LINEA_SEPOLIA,
ticker: TEST_NETWORK_TICKER_MAP[NETWORK_TYPES.LINEA_SEPOLIA],
Expand Down
3 changes: 0 additions & 3 deletions ui/helpers/utils/i18n-helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ export const getMessage = (
};

export function getNetworkLabelKey(network: string): string {
if (network === NETWORK_TYPES.LINEA_GOERLI) {
return 'lineaGoerli';
}
if (network === NETWORK_TYPES.LINEA_SEPOLIA) {
return 'lineaSepolia';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ethErrors } from 'eth-rpc-errors';
import React from 'react';
import { infuraProjectId } from '../../../../../shared/constants/network';
import {
infuraProjectId,
DEPRECATED_NETWORKS,
} from '../../../../../shared/constants/network';
import {
Severity,
TypographyVariant,
Expand Down Expand Up @@ -74,6 +77,20 @@ const MISMATCHED_CHAIN_RECOMMENDATION = {
},
};

const DEPRECATED_CHAIN_ALERT = {
id: 'DEPRECATED_CHAIN_ALERT',
severity: Severity.Warning,
content: {
element: 'span',
children: {
element: 'MetaMaskTranslation',
props: {
translationKey: 'deprecatedNetwork',
},
},
},
};

const MISMATCHED_NETWORK_NAME = {
id: 'MISMATCHED_NETWORK_NAME',
severity: Severity.Warning,
Expand Down Expand Up @@ -172,6 +189,9 @@ async function getAlerts(pendingApproval, data) {
) {
alerts.push(MISMATCHED_NETWORK_RPC);
}
if (DEPRECATED_NETWORKS.includes(pendingApproval.requestData.chainId)) {
alerts.push(DEPRECATED_CHAIN_ALERT);
}
}

if (!data.matchedChain && data.useSafeChainsListValidation) {
Expand Down
Loading

0 comments on commit 2f60905

Please sign in to comment.