diff --git a/app/core/Engine.ts b/app/core/Engine.ts index 40ca9707156..e9b61c29d4a 100644 --- a/app/core/Engine.ts +++ b/app/core/Engine.ts @@ -430,6 +430,7 @@ class Engine { TokensController.update({ allTokens: {}, + allIgnoredTokens: {}, ignoredTokens: [], tokens: [], suggestedAssets: [] @@ -446,13 +447,22 @@ class Engine { }); }; - sync = async ({ accounts, preferences, network, transactions, seed, pass, importedAccounts }) => { + sync = async ({ + accounts, + preferences, + network, + transactions, + seed, + pass, + importedAccounts, + tokens: { allTokens, allIgnoredTokens } + }) => { const { KeyringController, PreferencesController, NetworkController, TransactionController, - TokensController + TokensController, } = this.context; // Select same network ? @@ -470,31 +480,9 @@ class Engine { await KeyringController.importAccountWithStrategy('privateKey', [importedAccounts[i]]); } } - // Sync tokens - const allTokens = {}; - Object.keys(preferences.accountTokens).forEach(address => { - const checksummedAddress = toChecksumAddress(address); - allTokens[checksummedAddress] = {}; - Object.keys(preferences.accountTokens[address]).forEach(chainId => { - const network = Object.values(Networks).find( - ({ hexChainId: networkChainId }) => networkChainId === chainId - ); - const networkType = network?.networkType; - // !networkType this will probably happen on custom rpc networks - if (!networkType) return; - allTokens[checksummedAddress][networkType] = - chainId !== `0x1` - ? preferences.accountTokens[address][chainId] - : preferences.accountTokens[address][chainId] - .filter(({ address }) => - contractMap[toChecksumAddress(address)] - ? contractMap[toChecksumAddress(address)].erc20 - : true - ) - .map((token: { address: string; }) => ({ ...token, address: toChecksumAddress(token.address) })); - }); - }); - await TokensController.update({ allTokens }); + + // Restore tokens + await TokensController.update({ allTokens, allIgnoredTokens }); // Restore preferences const updatedPref = { ...preferences, identities: {} }; diff --git a/app/store/migrations.js b/app/store/migrations.js index ad1f6092442..d447a296af9 100644 --- a/app/store/migrations.js +++ b/app/store/migrations.js @@ -154,8 +154,51 @@ export const migrations = { : DefaultPreference.set(METRICS_OPT_IN, DENIED); DefaultPreference.set(ONBOARDING_WIZARD, EXPLORED); + return state; + }, + 7: state => { + const allTokens = state.engine.backgroundState.TokensController.allTokens; + const newAllTokens = {}; + if (allTokens) { + Object.keys(allTokens).forEach(accountAddress => { + Object.keys(allTokens[accountAddress]).forEach(chainId => { + const tokensArray = allTokens[accountAddress][chainId]; + if (newAllTokens[chainId] === undefined) { + newAllTokens[chainId] = { [accountAddress]: tokensArray }; + } else { + newAllTokens[chainId] = { + ...newAllTokens[chainId], + [accountAddress]: tokensArray + }; + } + }); + }); + } + + const ignoredTokens = state.engine.backgroundState.TokensController.ignoredTokens; + const newAllIgnoredTokens = {}; + Object.keys(allTokens).forEach(accountAddress => { + Object.keys(allTokens[accountAddress]).forEach(chainId => { + if (newAllIgnoredTokens[chainId] === undefined) { + newAllIgnoredTokens[chainId] = { + [accountAddress]: ignoredTokens + }; + } else { + newAllIgnoredTokens[chainId] = { + ...newAllIgnoredTokens[chainId], + [accountAddress]: ignoredTokens + }; + } + }); + }); + + state.engine.backgroundState.TokensController = { + allTokens: newAllTokens, + allIgnoredTokens: newAllIgnoredTokens + }; + return state; } }; -export const version = 6; +export const version = 7; diff --git a/package.json b/package.json index 0662e248c24..293982bd810 100644 --- a/package.json +++ b/package.json @@ -78,7 +78,7 @@ "dependencies": { "@exodus/react-native-payments": "https://github.com/wachunei/react-native-payments.git#package-json-hack", "@metamask/contract-metadata": "^1.23.0", - "@metamask/controllers": "^14.2.0", + "@metamask/controllers": "^15.0.2", "@metamask/etherscan-link": "^2.0.0", "@metamask/swaps-controller": "^6.2.0", "@react-native-community/async-storage": "1.12.1", diff --git a/yarn.lock b/yarn.lock index feedc8f4a9f..188ab15608e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1424,6 +1424,11 @@ resolved "https://registry.yarnpkg.com/@metamask/contract-metadata/-/contract-metadata-1.28.0.tgz#76796f5010aa4aa6d28bf6fe36392017cea687cb" integrity sha512-QZj6Y1nmSs9BHufBS1GSuPX5TVFa5gbtMhEo/KRuSdKyY43OdlbBKuyT36/l5p30krlzfMX8bN0MAWqw3g0Bog== +"@metamask/contract-metadata@^1.29.0": + version "1.29.0" + resolved "https://registry.yarnpkg.com/@metamask/contract-metadata/-/contract-metadata-1.29.0.tgz#4ca86a2f03d4dad4350d09216a7fe92f9dd21c8e" + integrity sha512-wxsC0ZCyhPKqThvmsL8+2zVWGWPqofSo8HNtOuOnQM9oGbXX9294imJ3T+A/Lov8fkX4jAWZOeNV0uR80zkNtA== + "@metamask/controllers@^14.2.0": version "14.2.0" resolved "https://registry.yarnpkg.com/@metamask/controllers/-/controllers-14.2.0.tgz#bed161ac3523fd525be79b0e557b132e2e2526e4" @@ -1460,6 +1465,43 @@ web3 "^0.20.7" web3-provider-engine "^16.0.3" +"@metamask/controllers@^15.0.2": + version "15.0.2" + resolved "https://registry.yarnpkg.com/@metamask/controllers/-/controllers-15.0.2.tgz#b4012a8bf28bd2c8dbb3c96f4d934d6cf1bb778c" + integrity sha512-MPxfZ2OOTzEa5lUzDsSkINtlTDU4q4udjpBNM5Dq1yCnLKIuPKzYC5nl/rA88Ssr9Hs6babCtMbnUpbbjQjLOg== + dependencies: + "@ethereumjs/common" "^2.3.1" + "@ethereumjs/tx" "^3.2.1" + "@metamask/contract-metadata" "^1.29.0" + "@types/uuid" "^8.3.0" + abort-controller "^3.0.0" + async-mutex "^0.2.6" + babel-runtime "^6.26.0" + eth-ens-namehash "^2.0.8" + eth-json-rpc-infura "^5.1.0" + eth-keyring-controller "^6.2.1" + eth-method-registry "1.1.0" + eth-phishing-detect "^1.1.14" + eth-query "^2.1.2" + eth-rpc-errors "^4.0.0" + eth-sig-util "^3.0.0" + ethereumjs-util "^7.0.10" + ethereumjs-wallet "^1.0.1" + ethers "^5.4.1" + ethjs-unit "^0.1.6" + ethjs-util "^0.1.6" + human-standard-collectible-abi "^1.0.2" + human-standard-token-abi "^2.0.0" + immer "^8.0.1" + isomorphic-fetch "^3.0.0" + jsonschema "^1.2.4" + nanoid "^3.1.12" + punycode "^2.1.1" + single-call-balance-checker-abi "^1.0.0" + uuid "^8.3.2" + web3 "^0.20.7" + web3-provider-engine "^16.0.3" + "@metamask/etherscan-link@^2.0.0": version "2.1.0" resolved "https://registry.yarnpkg.com/@metamask/etherscan-link/-/etherscan-link-2.1.0.tgz#c0be8e68445b7b83cf85bcc03a56cdf8e256c973"