2021-11-15 20:20:33 +01:00
|
|
|
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);
|
2022-07-31 20:26:40 +02:00
|
|
|
const userHasMadeATestNetTransaction = Object.values(transactions).some(
|
|
|
|
({ chainId }) => TEST_CHAINS.includes(chainId),
|
|
|
|
);
|
2021-11-15 20:20:33 +01:00
|
|
|
const userHasACachedBalanceOnATestnet = TEST_CHAINS.some((chainId) => {
|
|
|
|
const cachedBalancesForChain = Object.values(cachedBalances[chainId] || {});
|
2022-07-31 20:26:40 +02:00
|
|
|
const userHasABalanceGreaterThanZeroOnThisChain =
|
|
|
|
cachedBalancesForChain.some(hexNumberIsGreaterThanZero);
|
2021-11-15 20:20:33 +01:00
|
|
|
return userHasABalanceGreaterThanZeroOnThisChain;
|
|
|
|
});
|
|
|
|
const userHasUsedATestnet =
|
|
|
|
userIsCurrentlyOnATestNet ||
|
|
|
|
userHasMadeATestNetTransaction ||
|
|
|
|
userHasACachedBalanceOnATestnet;
|
|
|
|
|
|
|
|
const newState = {
|
|
|
|
...state,
|
|
|
|
PreferencesController: {
|
|
|
|
...PreferencesController,
|
|
|
|
preferences: {
|
|
|
|
...preferences,
|
|
|
|
showTestNetworks: userHasUsedATestnet,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return newState;
|
|
|
|
}
|