1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 04:37:13 +01:00
metamask-extension/app/scripts/migrations/052.js

108 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-02-26 16:40:25 +01:00
import { cloneDeep } from 'lodash';
import { CHAIN_IDS, NETWORK_TYPES } from '../../../shared/constants/network';
2021-02-26 16:40:25 +01:00
const version = 52;
/**
* Migrate tokens in Preferences to be keyed by chainId instead of
* providerType. To prevent breaking user's MetaMask and selected
* tokens, this migration copies the RPC entry into *every* custom RPC
* chainId.
*/
export default {
version,
async migrate(originalVersionedData) {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
const state = versionedData.data;
versionedData.data = transformState(state);
return versionedData;
},
};
function transformState(state = {}) {
if (state.PreferencesController) {
2022-07-31 20:26:40 +02:00
const { accountTokens, accountHiddenTokens, frequentRpcListDetail } =
state.PreferencesController;
2021-02-26 16:40:25 +01:00
const newAccountTokens = {};
const newAccountHiddenTokens = {};
if (accountTokens && Object.keys(accountTokens).length > 0) {
for (const address of Object.keys(accountTokens)) {
newAccountTokens[address] = {};
if (accountTokens[address][NETWORK_TYPES.RPC]) {
2021-02-26 16:40:25 +01:00
frequentRpcListDetail.forEach((detail) => {
newAccountTokens[address][detail.chainId] =
accountTokens[address][NETWORK_TYPES.RPC];
2021-02-26 16:40:25 +01:00
});
}
for (const providerType of Object.keys(accountTokens[address])) {
switch (providerType) {
case NETWORK_TYPES.MAINNET:
newAccountTokens[address][CHAIN_IDS.MAINNET] =
accountTokens[address][NETWORK_TYPES.MAINNET];
2021-02-26 16:40:25 +01:00
break;
case 'ropsten':
newAccountTokens[address]['0x3'] = accountTokens[address].ropsten;
2021-02-26 16:40:25 +01:00
break;
case 'rinkeby':
newAccountTokens[address]['0x4'] = accountTokens[address].rinkeby;
2021-02-26 16:40:25 +01:00
break;
case NETWORK_TYPES.GOERLI:
newAccountTokens[address][CHAIN_IDS.GOERLI] =
accountTokens[address][NETWORK_TYPES.GOERLI];
2021-02-26 16:40:25 +01:00
break;
case 'kovan':
newAccountTokens[address]['0x2a'] = accountTokens[address].kovan;
2021-02-26 16:40:25 +01:00
break;
default:
break;
}
}
}
state.PreferencesController.accountTokens = newAccountTokens;
}
if (accountHiddenTokens && Object.keys(accountHiddenTokens).length > 0) {
for (const address of Object.keys(accountHiddenTokens)) {
newAccountHiddenTokens[address] = {};
if (accountHiddenTokens[address][NETWORK_TYPES.RPC]) {
2021-02-26 16:40:25 +01:00
frequentRpcListDetail.forEach((detail) => {
newAccountHiddenTokens[address][detail.chainId] =
accountHiddenTokens[address][NETWORK_TYPES.RPC];
2021-02-26 16:40:25 +01:00
});
}
for (const providerType of Object.keys(accountHiddenTokens[address])) {
switch (providerType) {
case NETWORK_TYPES.MAINNET:
newAccountHiddenTokens[address][CHAIN_IDS.MAINNET] =
accountHiddenTokens[address][NETWORK_TYPES.MAINNET];
2021-02-26 16:40:25 +01:00
break;
case 'ropsten':
newAccountHiddenTokens[address]['0x3'] =
accountHiddenTokens[address].ropsten;
2021-02-26 16:40:25 +01:00
break;
case 'rinkeby':
newAccountHiddenTokens[address]['0x4'] =
accountHiddenTokens[address].rinkeby;
2021-02-26 16:40:25 +01:00
break;
case NETWORK_TYPES.GOERLI:
newAccountHiddenTokens[address][CHAIN_IDS.GOERLI] =
accountHiddenTokens[address][NETWORK_TYPES.GOERLI];
2021-02-26 16:40:25 +01:00
break;
case 'kovan':
newAccountHiddenTokens[address]['0x2a'] =
accountHiddenTokens[address].kovan;
2021-02-26 16:40:25 +01:00
break;
default:
break;
}
}
}
state.PreferencesController.accountHiddenTokens = newAccountHiddenTokens;
}
}
return state;
}