diff --git a/packages/suite/src/actions/wallet/__tests__/formDraftActions.test.ts b/packages/suite/src/actions/wallet/__tests__/formDraftActions.test.ts index 501f20050eb..bb85f078e6b 100644 --- a/packages/suite/src/actions/wallet/__tests__/formDraftActions.test.ts +++ b/packages/suite/src/actions/wallet/__tests__/formDraftActions.test.ts @@ -29,29 +29,29 @@ describe('Form draft actions', () => { const initialState = getInitialState(); const store = initStore(initialState); const expectedFormDraftState = { - 'coinmarket-buy/form': { input: 'value' }, + 'trading-buy/form': { input: 'value' }, }; - store.dispatch(formDraftActions.saveDraft('coinmarket-buy')('form', { input: 'value' })); + store.dispatch(formDraftActions.saveDraft('trading-buy')('form', { input: 'value' })); const formDraftState = store.getState().wallet.formDrafts; expect(formDraftState).toEqual(expectedFormDraftState); }); it('gets draft', () => { - const initialState = getInitialState({ 'coinmarket-buy/form': { input: 'value' } }); + const initialState = getInitialState({ 'trading-buy/form': { input: 'value' } }); const store = initStore(initialState); const expectedDraft = { input: 'value' }; - const draft = store.dispatch(formDraftActions.getDraft('coinmarket-buy')('form')); + const draft = store.dispatch(formDraftActions.getDraft('trading-buy')('form')); expect(draft).toEqual(expectedDraft); }); it('removes draft', () => { - const initialState = getInitialState({ 'coinmarket-buy/form': { input: 'value' } }); + const initialState = getInitialState({ 'trading-buy/form': { input: 'value' } }); const store = initStore(initialState); const expectedDraft = {}; - store.dispatch(formDraftActions.removeDraft('coinmarket-buy')('form')); + store.dispatch(formDraftActions.removeDraft('trading-buy')('form')); expect(store.getState().wallet.formDrafts).toEqual(expectedDraft); }); }); diff --git a/packages/suite/src/hooks/wallet/trading/form/useTradingBuyForm.tsx b/packages/suite/src/hooks/wallet/trading/form/useTradingBuyForm.tsx index 93c030e763e..c75f5c2768b 100644 --- a/packages/suite/src/hooks/wallet/trading/form/useTradingBuyForm.tsx +++ b/packages/suite/src/hooks/wallet/trading/form/useTradingBuyForm.tsx @@ -82,8 +82,7 @@ export const useTradingBuyForm = ({ suggestedFiatCurrency, } = useTradingBuyFormDefaultValues(account.symbol, buyInfo); const buyDraftKey = account.key; - const { saveDraft, getDraft, removeDraft } = - useFormDraft('coinmarket-buy'); + const { saveDraft, getDraft, removeDraft } = useFormDraft('trading-buy'); const draft = getDraft(buyDraftKey); const draftUpdated: TradingBuyFormProps | null = draft ? { diff --git a/packages/suite/src/hooks/wallet/trading/form/useTradingExchangeForm.ts b/packages/suite/src/hooks/wallet/trading/form/useTradingExchangeForm.ts index 80d5a0b06db..8e796320b9f 100644 --- a/packages/suite/src/hooks/wallet/trading/form/useTradingExchangeForm.ts +++ b/packages/suite/src/hooks/wallet/trading/form/useTradingExchangeForm.ts @@ -123,7 +123,7 @@ export const useTradingExchangeForm = ({ ) as TradeExchange | undefined; const { defaultCurrency, defaultValues } = useTradingExchangeFormDefaultValues(account); - const exchangeDraftKey = 'coinmarket-exchange'; + const exchangeDraftKey = 'trading-exchange'; const { getDraft, saveDraft, removeDraft } = useFormDraft(exchangeDraftKey); const draft = getDraft(exchangeDraftKey); diff --git a/packages/suite/src/hooks/wallet/trading/form/useTradingSellForm.ts b/packages/suite/src/hooks/wallet/trading/form/useTradingSellForm.ts index f577c5ead8c..bf09dcde579 100644 --- a/packages/suite/src/hooks/wallet/trading/form/useTradingSellForm.ts +++ b/packages/suite/src/hooks/wallet/trading/form/useTradingSellForm.ts @@ -114,7 +114,7 @@ export const useTradingSellForm = ({ const { defaultValues, defaultCountry, defaultCurrency, defaultPaymentMethod } = useTradingSellFormDefaultValues(account, sellInfo); - const sellDraftKey = 'coinmarket-sell'; + const sellDraftKey = 'trading-sell'; const { saveDraft, getDraft, removeDraft } = useFormDraft(sellDraftKey); const draft = getDraft(sellDraftKey); const getDraftUpdated = (): TradingSellFormProps | null => { diff --git a/packages/suite/src/hooks/wallet/trading/useTradingWatchTrade.ts b/packages/suite/src/hooks/wallet/trading/useTradingWatchTrade.ts index b79fe10c2cd..57eb1d7f50f 100644 --- a/packages/suite/src/hooks/wallet/trading/useTradingWatchTrade.ts +++ b/packages/suite/src/hooks/wallet/trading/useTradingWatchTrade.ts @@ -116,7 +116,7 @@ export const useTradingWatchTrade = ({ cancelRefresh(); }); - const { removeDraft } = useFormDraft(`coinmarket-${trade?.tradeType ?? 'buy'}`); + const { removeDraft } = useFormDraft(`trading-${trade?.tradeType ?? 'buy'}`); useEffect(() => { if (!trade || !account) return; diff --git a/packages/suite/src/storage/CHANGELOG.md b/packages/suite/src/storage/CHANGELOG.md index 76b81cc880c..c7722721077 100644 --- a/packages/suite/src/storage/CHANGELOG.md +++ b/packages/suite/src/storage/CHANGELOG.md @@ -1,8 +1,13 @@ # Storage changelog +## 53 + +- Renamed coinmarketTrades to tradingTrades + ## 52 - Deprecated Vertcoin (VTC), Bitcoin Gold (BTG), Namecoin (NMC), DigiByte (DGB), and Dash (DASH) networks. Removed related transactions, accounts, and settings. +- Renamed coinmarketTrades to tradingTrades ## 51 diff --git a/packages/suite/src/storage/definitions.ts b/packages/suite/src/storage/definitions.ts index 260b1198e10..3b17176e804 100644 --- a/packages/suite/src/storage/definitions.ts +++ b/packages/suite/src/storage/definitions.ts @@ -103,7 +103,7 @@ export interface SuiteDBSchema extends DBSchema { deviceState: string; }; }; - coinmarketTrades: { + tradingTrades: { key: string; value: Trade; }; diff --git a/packages/suite/src/storage/index.ts b/packages/suite/src/storage/index.ts index ffb721a2109..a16f675968f 100644 --- a/packages/suite/src/storage/index.ts +++ b/packages/suite/src/storage/index.ts @@ -5,7 +5,7 @@ import { reloadApp } from 'src/utils/suite/reload'; import { migrate } from './migrations'; import type { SuiteDBSchema } from './definitions'; -const VERSION = 52; // don't forget to add migration and CHANGELOG when changing versions! +const VERSION = 53; // 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 88cb45c88bb..05faeca8e56 100644 --- a/packages/suite/src/storage/migrations/index.ts +++ b/packages/suite/src/storage/migrations/index.ts @@ -22,6 +22,7 @@ import { PartialRecord } from '@trezor/type-utils'; import type { CustomBackend, BlockbookUrl } from 'src/types/wallet/backend'; import type { State } from 'src/reducers/wallet/settingsReducer'; import { migrationOfBnbNetwork } from 'src/storage/migrations/networks/bnb'; +import { migrationCoinmarketToTrading } from 'src/storage/migrations/trading/migrationCoinmarketToTrading'; import { updateAll } from './utils'; import type { DBWalletAccountTransaction, SuiteDBSchema } from '../definitions'; @@ -129,6 +130,7 @@ export const migrate: OnUpgradeFunc = async ( } if (oldVersion < 17) { + // @ts-expect-error coinmarketTrades doesn't exists anymore db.createObjectStore('coinmarketTrades', { keyPath: 'key' }); } @@ -1226,4 +1228,8 @@ export const migrate: OnUpgradeFunc = async ( await backendSettings.delete(network as NetworkSymbol); // Delete backend settings for each deprecated network } } + + if (oldVersion < 53) { + await migrationCoinmarketToTrading(db, oldVersion, newVersion, transaction); + } }; diff --git a/packages/suite/src/storage/migrations/networks/bnb.ts b/packages/suite/src/storage/migrations/networks/bnb.ts index c3d39615dfb..5977fbd5e7a 100644 --- a/packages/suite/src/storage/migrations/networks/bnb.ts +++ b/packages/suite/src/storage/migrations/networks/bnb.ts @@ -192,8 +192,8 @@ export const migrationOfBnbNetwork: OnUpgradeFunc = async ( formDrafts.delete(key); }); - await updateAll(transaction, 'coinmarketTrades', trade => { - // @ts-expect-error + // @ts-expect-error -coinmarketTrades doesn't exists anymore + await updateAll(transaction, 'coinmarketTrades', (trade: Trade) => { if (trade.account.symbol === 'bnb') { trade.account.symbol = 'bsc'; } diff --git a/packages/suite/src/storage/migrations/trading/migrationCoinmarketToTrading.ts b/packages/suite/src/storage/migrations/trading/migrationCoinmarketToTrading.ts new file mode 100644 index 00000000000..c176d57da35 --- /dev/null +++ b/packages/suite/src/storage/migrations/trading/migrationCoinmarketToTrading.ts @@ -0,0 +1,52 @@ +import type { OnUpgradeFunc } from '@trezor/suite-storage'; + +import { Trade } from 'src/types/wallet/tradingCommonTypes'; + +import type { SuiteDBSchema } from '../../definitions'; + +export const migrationCoinmarketToTrading: OnUpgradeFunc = async ( + db, + _oldVersion, + _newVersion, + transaction, +) => { + const oldStoreName = 'coinmarketTrades'; + const newStoreName = 'tradingTrades'; + + // 1. Migration coinmarketTrades to tradingTrades + // @ts-expect-error - old type + if (db.objectStoreNames.contains(oldStoreName)) { + // @ts-expect-error - old type + const trades = transaction.objectStore(oldStoreName); + let tradesCursor = await trades.openCursor(); + + const newTradesStore = db.createObjectStore(newStoreName, { keyPath: trades.keyPath }); + + while (tradesCursor) { + const trade = tradesCursor.value as Trade; + + await tradesCursor.delete(); + await newTradesStore.add(trade); + + tradesCursor = await tradesCursor.continue(); + } + + // @ts-expect-error - old type + db.deleteObjectStore(oldStoreName); + } + + // 2. Update draft keys to trading + const formDrafts = transaction.objectStore('formDrafts'); + const formDraftsKeys = (await formDrafts.getAllKeys()).filter(key => + key.includes('coinmarket'), + ); + + formDraftsKeys.forEach(async key => { + const draft = await formDrafts.get(key); + + if (draft) { + formDrafts.add(draft, key.replace('coinmarket', 'trading')); + formDrafts.delete(key); + } + }); +}; diff --git a/packages/suite/src/support/suite/preloadStore.ts b/packages/suite/src/support/suite/preloadStore.ts index c0ab7d7f378..dfc1cdf0a37 100644 --- a/packages/suite/src/support/suite/preloadStore.ts +++ b/packages/suite/src/support/suite/preloadStore.ts @@ -30,7 +30,7 @@ export const preloadStore = async () => { const accounts = await db.getItemsExtended('accounts'); const discovery = await db.getItemsExtended('discovery'); const walletSettings = await db.getItemByPK('walletSettings', 'wallet'); - const tradingTrades = await db.getItemsExtended('coinmarketTrades'); + const tradingTrades = await db.getItemsExtended('tradingTrades'); const historicRates = await db.getItemsWithKeys('historicRates'); const graph = await db.getItemsExtended('graph'); const analytics = await db.getItemByPK('analytics', 'suite'); diff --git a/suite-common/wallet-constants/src/formDraft.ts b/suite-common/wallet-constants/src/formDraft.ts index 2b71d6336ef..a14efeb4c98 100644 --- a/suite-common/wallet-constants/src/formDraft.ts +++ b/suite-common/wallet-constants/src/formDraft.ts @@ -1,7 +1,7 @@ export const FormDraftPrefixKeyValues = [ - 'coinmarket-buy', - 'coinmarket-sell', - 'coinmarket-exchange', + 'trading-buy', + 'trading-sell', + 'trading-exchange', 'stake-eth', 'unstake-eth', ] as const;