Skip to content

Commit

Permalink
fix: fix conflict rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Jun 6, 2024
1 parent 014510b commit 089e0c1
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 223 deletions.
188 changes: 18 additions & 170 deletions app/scripts/migrations/120.test.ts
Original file line number Diff line number Diff line change
@@ -1,194 +1,42 @@
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 './120';

const oldVersion = 119;

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 #120', () => {
afterEach(() => jest.resetAllMocks());

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({
it('deletes the deprecated token rates controller fields', async () => {
const oldStorage = {
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,
data: {
TokenRatesController: {
contractExchangeRates: {
'0x8290333ceF9e6D528dD5618Fb97a76f268f3EDD4': 0.00001142055192565137,
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2': 1.000125,
},
status: 'available',
},
'linea-goerli': {
EIPS: {
'1559': true,
contractExchangeRatesByChainId: {
'0x1': {
ETH: {
'0x8290333ceF9e6D528dD5618Fb97a76f268f3EDD4': 0.00001142055192565137,
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2': 1.000125,
},
},
},
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,
});
const newStorage = await migrate(oldStorage);
expect(newStorage.data.TokenRatesController).toStrictEqual({});
});
});
62 changes: 9 additions & 53 deletions app/scripts/migrations/120.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { cloneDeep, isObject } from 'lodash';
import { NetworkType } from '@metamask/controller-utils';
import { cloneDeep } from 'lodash';
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;
export const version = 120;

/**
* Migrates the user network to Linea Sepolia if the user is on Linea Goerli network.
* This migration removes the deprecated fields `contractExchangeRates`
* and `contractExchangeRatesByChainId` from the TokenRatesController.
*
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist.
* @param originalVersionedData.meta - State metadata.
Expand All @@ -34,48 +26,12 @@ export async function migrate(
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,
},
};
if (hasProperty(state, 'TokenRatesController')) {
delete state.TokenRatesController.contractExchangeRates;
delete state.TokenRatesController.contractExchangeRatesByChainId;
}
return {
...state,
NetworkController: networkControllerState,
};
return state;
}
Loading

0 comments on commit 089e0c1

Please sign in to comment.