Skip to content

Commit

Permalink
chore(trading): update indexed db to trading
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Jan 29, 2025
1 parent 83f851f commit 574824a
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ export const useTradingBuyForm = ({
suggestedFiatCurrency,
} = useTradingBuyFormDefaultValues(account.symbol, buyInfo);
const buyDraftKey = account.key;
const { saveDraft, getDraft, removeDraft } =
useFormDraft<TradingBuyFormProps>('coinmarket-buy');
const { saveDraft, getDraft, removeDraft } = useFormDraft<TradingBuyFormProps>('trading-buy');
const draft = getDraft(buyDraftKey);
const draftUpdated: TradingBuyFormProps | null = draft
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TradingExchangeFormProps>(exchangeDraftKey);
const draft = getDraft(exchangeDraftKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TradingSellFormProps>(sellDraftKey);
const draft = getDraft(sellDraftKey);
const getDraftUpdated = (): TradingSellFormProps | null => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const useTradingWatchTrade = <T extends TradingType>({
cancelRefresh();
});

const { removeDraft } = useFormDraft(`coinmarket-${trade?.tradeType ?? 'buy'}`);
const { removeDraft } = useFormDraft(`trading-${trade?.tradeType ?? 'buy'}`);

useEffect(() => {
if (!trade || !account) return;
Expand Down
5 changes: 5 additions & 0 deletions packages/suite/src/storage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/storage/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export interface SuiteDBSchema extends DBSchema {
deviceState: string;
};
};
coinmarketTrades: {
tradingTrades: {
key: string;
value: Trade;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions packages/suite/src/storage/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -129,6 +130,7 @@ export const migrate: OnUpgradeFunc<SuiteDBSchema> = async (
}

if (oldVersion < 17) {
// @ts-expect-error coinmarketTrades doesn't exists anymore
db.createObjectStore('coinmarketTrades', { keyPath: 'key' });
}

Expand Down Expand Up @@ -1226,4 +1228,8 @@ export const migrate: OnUpgradeFunc<SuiteDBSchema> = async (
await backendSettings.delete(network as NetworkSymbol); // Delete backend settings for each deprecated network
}
}

if (oldVersion < 53) {
await migrationCoinmarketToTrading(db, oldVersion, newVersion, transaction);
}
};
4 changes: 2 additions & 2 deletions packages/suite/src/storage/migrations/networks/bnb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ export const migrationOfBnbNetwork: OnUpgradeFunc<SuiteDBSchema> = 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';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SuiteDBSchema> = 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);
}
});
};
2 changes: 1 addition & 1 deletion packages/suite/src/support/suite/preloadStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions suite-common/wallet-constants/src/formDraft.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 574824a

Please sign in to comment.