1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-04 23:14:56 +01:00
metamask-extension/app/scripts/migrations/067.js
Dan J Miller 0c08cb6bf8 Add migration to set showTestNetworks to true if there is evidence of testnet use (#12675)
* Add migration to set showTestNetworks to true if there is evidence the user has used a test network

* Add migration to index file

* Remove console.log

* Clean up conditional structure in migration 67
2021-11-15 17:04:48 -07:00

67 lines
2.0 KiB
JavaScript

import { cloneDeep } from 'lodash';
import BigNumber from 'bignumber.js';
import { TEST_CHAINS } from '../../../shared/constants/network';
const hexNumberIsGreaterThanZero = (hexNumber) =>
new BigNumber(hexNumber || '0x0', 16).gt(0);
const version = 67;
/**
* Sets the showTestNetworks property to true if it was false or undefined, and there is evidence
* that the user has used a test net
*/
export default {
version,
async migrate(originalVersionedData) {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
const state = versionedData.data;
const newState = transformState(state);
versionedData.data = newState;
return versionedData;
},
};
function transformState(state) {
const PreferencesController = state?.PreferencesController || {};
const preferences = PreferencesController.preferences || {};
if (preferences.showTestNetworks) {
return state;
}
const transactions = state?.TransactionController?.transactions || {};
const provider = state.NetworkController?.provider || {};
const cachedBalances = state.CachedBalancesController?.cachedBalances || {};
const userIsCurrentlyOnATestNet = TEST_CHAINS.includes(provider?.chainId);
const userHasMadeATestNetTransaction = Object.values(
transactions,
).some(({ chainId }) => TEST_CHAINS.includes(chainId));
const userHasACachedBalanceOnATestnet = TEST_CHAINS.some((chainId) => {
const cachedBalancesForChain = Object.values(cachedBalances[chainId] || {});
const userHasABalanceGreaterThanZeroOnThisChain = cachedBalancesForChain.some(
hexNumberIsGreaterThanZero,
);
return userHasABalanceGreaterThanZeroOnThisChain;
});
const userHasUsedATestnet =
userIsCurrentlyOnATestNet ||
userHasMadeATestNetTransaction ||
userHasACachedBalanceOnATestnet;
const newState = {
...state,
PreferencesController: {
...PreferencesController,
preferences: {
...preferences,
showTestNetworks: userHasUsedATestnet,
},
},
};
return newState;
}