From 8a3a4ca0fabc13f3969b9c12e4215f2c10d8f092 Mon Sep 17 00:00:00 2001 From: tomasklim Date: Thu, 5 Sep 2024 16:28:05 +0200 Subject: [PATCH] feat(suite): migration of storage from matic to pol --- packages/suite/src/storage/CHANGELOG.md | 4 + packages/suite/src/storage/index.ts | 2 +- .../suite/src/storage/migrations/index.ts | 189 ++++++++++++++++++ 3 files changed, 194 insertions(+), 1 deletion(-) diff --git a/packages/suite/src/storage/CHANGELOG.md b/packages/suite/src/storage/CHANGELOG.md index 2a0c89e75705..115e8796c57a 100644 --- a/packages/suite/src/storage/CHANGELOG.md +++ b/packages/suite/src/storage/CHANGELOG.md @@ -1,5 +1,9 @@ # Storage changelog +## 47 + +- migrate matic to pol + ## 46 - added `tokenManagement` diff --git a/packages/suite/src/storage/index.ts b/packages/suite/src/storage/index.ts index ef5229041078..05d141618b61 100644 --- a/packages/suite/src/storage/index.ts +++ b/packages/suite/src/storage/index.ts @@ -4,7 +4,7 @@ import { migrate } from './migrations'; import type { SuiteDBSchema } from './definitions'; -const VERSION = 46; // don't forget to add migration and CHANGELOG when changing versions! +const VERSION = 47; // don't forget to add migration and CHANGELOG when changing versions! /** * If the object stores don't already exist then creates them. diff --git a/packages/suite/src/storage/migrations/index.ts b/packages/suite/src/storage/migrations/index.ts index 3604e87667cf..05fe064f6d9f 100644 --- a/packages/suite/src/storage/migrations/index.ts +++ b/packages/suite/src/storage/migrations/index.ts @@ -890,4 +890,193 @@ export const migrate: OnUpgradeFunc = async ( await updateAll(transaction, 'suiteSettings', suiteSettings => suiteSettings); } + + if (oldVersion < 47) { + // migrate matic to pol + + await updateAll(transaction, 'walletSettings', walletSettings => { + // @ts-expect-error + const indexOfMatic = walletSettings.enabledNetworks.indexOf('matic'); + if (indexOfMatic !== -1) { + walletSettings.enabledNetworks[indexOfMatic] = 'pol'; + } + + return walletSettings; + }); + + await updateAll(transaction, 'suiteSettings', suiteSettings => { + if ( + // @ts-expect-error + typeof suiteSettings.evmSettings.confirmExplanationModalClosed?.matic == 'boolean' + ) { + suiteSettings.evmSettings.confirmExplanationModalClosed.pol = + // @ts-expect-error + suiteSettings.evmSettings.confirmExplanationModalClosed.matic; + // @ts-expect-error + delete suiteSettings.evmSettings.confirmExplanationModalClosed.matic; + } + + if ( + // @ts-expect-error + typeof suiteSettings.evmSettings.explanationBannerClosed?.matic == 'boolean' + ) { + suiteSettings.evmSettings.explanationBannerClosed.pol = + // @ts-expect-error + suiteSettings.evmSettings.explanationBannerClosed.matic; + // @ts-expect-error + delete suiteSettings.evmSettings.explanationBannerClosed.matic; + } + + return suiteSettings; + }); + + const backendSettings = transaction.objectStore('backendSettings'); + // @ts-expect-error + const maticBackendSettings = await backendSettings.get('matic'); + if (maticBackendSettings) { + backendSettings.add(maticBackendSettings, 'pol'); + // @ts-expect-error + backendSettings.delete('matic'); + } + + const tokenManagement = transaction.objectStore('tokenManagement'); + const maticTokenManagementShow = await tokenManagement.get('matic-coin-show'); + if (maticTokenManagementShow) { + tokenManagement.add(maticTokenManagementShow, 'pol-coin-show'); + tokenManagement.delete('matic-coin-show'); + } + + const maticTokenManagementHide = await tokenManagement.get('matic-coin-hide'); + if (maticTokenManagementHide) { + tokenManagement.add(maticTokenManagementHide, 'pol-coin-hide'); + tokenManagement.delete('matic-coin-hide'); + } + + await updateAll(transaction, 'discovery', discovery => { + discovery.networks = discovery.networks.filter( + // @ts-expect-error + network => network !== 'matic', + ); + discovery.networks = discovery.networks.map(network => + // @ts-expect-error + network === 'matic' ? 'pol' : network, + ); + + discovery.failed = discovery.failed.map(network => { + // @ts-expect-error + if (network.symbol === 'matic') { + network = { ...network, symbol: 'pol' }; + } + + return network; + }); + + return discovery; + }); + + await updateAll(transaction, 'accounts', account => { + // @ts-expect-error + if (account.symbol === 'matic') { + account = { ...account, symbol: 'pol' }; + } + + return account; + }); + + await updateAll(transaction, 'walletSettings', walletSettings => { + if (walletSettings.lastUsedFeeLevel['matic']) { + walletSettings.lastUsedFeeLevel = { + ...walletSettings.lastUsedFeeLevel, + pol: { ...walletSettings.lastUsedFeeLevel['matic'] }, + // @ts-expect-error + matic: undefined, + }; + } + + return walletSettings; + }); + + await updateAll(transaction, 'txs', tx => { + // @ts-expect-error + if (tx.tx.symbol === 'matic') { + tx.tx = { ...tx.tx, symbol: 'pol' }; + } + + return tx; + }); + + await updateAll(transaction, 'graph', graph => { + // @ts-expect-error + if (graph.account.symbol === 'matic') { + graph.account = { ...graph.account, symbol: 'pol' }; + } + + return graph; + }); + + const historicRates = transaction.objectStore('historicRates'); + const historicRatesKeys = await historicRates.getAllKeys(); + const historicRatesKeysWithMatic = historicRatesKeys.filter(key => key.includes('matic')); + + historicRatesKeysWithMatic.forEach(async key => { + const rate = await historicRates.get(key); + if (rate) { + historicRates.add(rate, key.replace('matic', 'pol')); + } + historicRates.delete(key); + }); + + const sendFormDrafts = transaction.objectStore('sendFormDrafts'); + const sendFormDraftsKeys = await sendFormDrafts.getAllKeys(); + const sendFormDraftsKeysWithMatic = sendFormDraftsKeys.filter(key => key.includes('matic')); + + sendFormDraftsKeysWithMatic.forEach(async key => { + const draft = await sendFormDrafts.get(key); + if (draft) { + sendFormDrafts.add(draft, key.replace('matic', 'pol')); + } + sendFormDrafts.delete(key); + }); + + const formDrafts = transaction.objectStore('formDrafts'); + const formDraftsKeys = await formDrafts.getAllKeys(); + const formDraftsKeysWithMatic = formDraftsKeys.filter(key => key.includes('matic')); + + formDraftsKeysWithMatic.forEach(async key => { + const draft = await formDrafts.get(key); + + if (draft) { + formDrafts.add(draft, key.replace('matic', 'pol')); + } + formDrafts.delete(key); + }); + + await updateAll(transaction, 'formDrafts', draft => { + if (draft.cryptoSelect?.label === 'MATIC') { + draft.cryptoSelect = { + ...draft.cryptoSelect, + label: 'POL', + value: 'polygon-ecosystem-token', + }; + } + + if (draft.receiveCryptoSelect?.label === 'MATIC') { + draft.receiveCryptoSelect = { + ...draft.receiveCryptoSelect, + label: 'POL', + value: 'polygon-ecosystem-token', + }; + } + + if (draft.sendCryptoSelect?.label === 'MATIC') { + draft.sendCryptoSelect = { + ...draft.sendCryptoSelect, + label: 'POL', + value: 'polygon-ecosystem-token', + }; + } + + return draft; + }); + } };