Skip to content

Commit

Permalink
chore(trading): move common types
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Feb 3, 2025
1 parent 27dfa4a commit b9dbbed
Show file tree
Hide file tree
Showing 26 changed files with 127 additions and 114 deletions.
4 changes: 2 additions & 2 deletions packages/suite/src/actions/suite/storageActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MetadataState } from '@suite-common/metadata-types';
import { isDeviceAcquired } from '@suite-common/suite-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import { DefinitionType, TokenManagementAction } from '@suite-common/token-definitions';
import type { TradingTransaction } from '@suite-common/trading';
import type { NetworkSymbol } from '@suite-common/wallet-config';
import { FormDraftPrefixKeyValues } from '@suite-common/wallet-constants';
import { deviceActions, selectDevices } from '@suite-common/wallet-core';
Expand All @@ -18,7 +19,6 @@ import type { PreloadStoreAction } from 'src/support/suite/preloadStore';
import type { AppState, Dispatch, GetState, TrezorDevice } from 'src/types/suite';
import type { Account } from 'src/types/wallet';
import { GraphData } from 'src/types/wallet/graph';
import type { Trade } from 'src/types/wallet/tradingCommonTypes';
import {
serializeCoinjoinAccount,
serializeDevice,
Expand Down Expand Up @@ -209,7 +209,7 @@ export const saveAccounts = async (accounts: Account[]) => {
return db.addItems('accounts', accounts, true);
};

export const saveTradingTrade = async (trade: Trade) => {
export const saveTradingTrade = async (trade: TradingTransaction) => {
if (!(await db.isAccessible())) return;

return db.addItem('tradingTrades', trade, undefined, true);
Expand Down
5 changes: 2 additions & 3 deletions packages/suite/src/actions/wallet/tradingBuyActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import {
FiatCurrencyCode,
} from 'invity-api';

import { invityAPI, regional } from '@suite-common/trading';
import { type TradingFiatCurrenciesProps, invityAPI, regional } from '@suite-common/trading';
import { Account } from '@suite-common/wallet-types';

import * as modalActions from 'src/actions/suite/modalActions';
import { verifyAddress as verifyBuyAddress } from 'src/actions/wallet/trading/tradingCommonActions';
import { Dispatch } from 'src/types/suite';
import { TradingFiatCurrenciesProps } from 'src/types/trading/trading';
import { Account } from 'src/types/wallet';

import { TRADING_BUY, TRADING_COMMON } from './constants';

Expand Down
3 changes: 2 additions & 1 deletion packages/suite/src/actions/wallet/tradingInfoActions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { InfoResponse } from 'invity-api';

import type { TradingPaymentMethodListProps } from '@suite-common/trading';

import { TRADING_INFO } from 'src/actions/wallet/constants';
import { TradingPaymentMethodListProps } from 'src/types/trading/trading';

export type TradingInfoAction =
| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo } from 'react';

import { CryptoId, FiatCurrencyCode } from 'invity-api';

import { getDefaultCountry } from '@suite-common/trading';
import { type TradingPaymentMethodListProps, getDefaultCountry } from '@suite-common/trading';
import { networks } from '@suite-common/wallet-config';

import { BuyInfo } from 'src/actions/wallet/tradingBuyActions';
Expand All @@ -12,7 +12,6 @@ import {
} from 'src/constants/wallet/trading/form';
import { useSelector } from 'src/hooks/suite';
import { useTradingInfo } from 'src/hooks/wallet/trading/useTradingInfo';
import { TradingPaymentMethodListProps } from 'src/types/trading/trading';
import { TradingBuyFormDefaultValuesProps } from 'src/types/trading/tradingForm';
import { Account } from 'src/types/wallet';
import { buildFiatOption } from 'src/utils/wallet/trading/tradingUtils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { isChanged } from '@suite-common/suite-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import {
type TradingExchangeType,
type TradingTransactionExchange,
addIdsToQuotes,
getUnusedAddressFromAccount,
invityAPI,
Expand Down Expand Up @@ -51,7 +52,6 @@ import {
TradingExchangeFormProps,
TradingExchangeStepType,
} from 'src/types/trading/tradingForm';
import { TradeExchange } from 'src/types/wallet/tradingCommonTypes';
import type { CryptoAmountLimitProps } from 'src/utils/suite/validation';
import {
createQuoteLink,
Expand Down Expand Up @@ -123,9 +123,11 @@ export const useTradingExchangeForm = ({
const network = networks[account.symbol];
const trades = useSelector(state => state.wallet.trading.trades);
const trade = trades.find(
trade =>
trade.tradeType === 'exchange' && transactionId && trade.data.orderId === transactionId,
) as TradeExchange | undefined;
(trade): trade is TradingTransactionExchange =>
trade.tradeType === 'exchange' &&
!!transactionId &&
trade.data.orderId === transactionId,
);

const { defaultCurrency, defaultValues } = useTradingExchangeFormDefaultValues(account);
const exchangeDraftKey = 'trading-exchange';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useCallback } from 'react';

import type { TradingTradeMapProps } from '@suite-common/trading';

import { useSelector } from 'src/hooks/suite';
import {
import type {
TradingPaymentMethodListProps,
TradingPaymentMethodProps,
TradingTradeBuySellType,
} from 'src/types/trading/trading';
TradingTradeMapProps,
} from '@suite-common/trading';

import { useSelector } from 'src/hooks/suite';
import { TradingTradeBuySellType } from 'src/types/trading/trading';
import { TradingPaymentMethodHookProps } from 'src/types/trading/tradingForm';

const useTradingPaymentMethod = <
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isChanged } from '@suite-common/suite-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import {
type TradingSellType,
TradingTransactionSell,
addIdsToQuotes,
filterQuotesAccordingTags,
getUnusedAddressFromAccount,
Expand Down Expand Up @@ -54,7 +55,6 @@ import {
TradingSellFormProps,
TradingSellStepType,
} from 'src/types/trading/tradingForm';
import { TradeSell } from 'src/types/wallet/tradingCommonTypes';
import type { AmountLimitProps } from 'src/utils/suite/validation';
import { createQuoteLink, getAmountLimits } from 'src/utils/wallet/trading/sellUtils';
import { getTradingNetworkDecimals } from 'src/utils/wallet/trading/tradingUtils';
Expand Down Expand Up @@ -106,8 +106,9 @@ export const useTradingSellForm = ({
const localCurrencyOption = { value: localCurrency, label: localCurrency.toUpperCase() };
const trades = useSelector(state => state.wallet.trading.trades);
const trade = trades.find(
trade => trade.tradeType === 'sell' && trade.key === transactionId,
) as TradeSell | undefined;
(trade): trade is TradingTransactionSell =>
trade.tradeType === 'sell' && trade.key === transactionId,
);

const [amountLimits, setAmountLimits] = useState<AmountLimitProps | undefined>(undefined);
const [sellStep, setSellStep] = useState<TradingSellStepType>('BANK_ACCOUNT');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useMemo } from 'react';

import { cryptoIdToSymbol, getDefaultCountry } from '@suite-common/trading';
import {
type TradingPaymentMethodListProps,
cryptoIdToSymbol,
getDefaultCountry,
} from '@suite-common/trading';
import { DEFAULT_PAYMENT, DEFAULT_VALUES } from '@suite-common/wallet-constants';
import { FormState, Output } from '@suite-common/wallet-types';

Expand All @@ -11,7 +15,6 @@ import {
} from 'src/constants/wallet/trading/form';
import { useSelector } from 'src/hooks/suite';
import { useTradingBuildAccountGroups } from 'src/hooks/wallet/trading/form/common/useTradingBuildAccountGroups';
import { TradingPaymentMethodListProps } from 'src/types/trading/trading';
import { TradingSellFormDefaultValuesProps } from 'src/types/trading/tradingForm';
import { Account } from 'src/types/wallet';
import {
Expand Down
6 changes: 3 additions & 3 deletions packages/suite/src/hooks/wallet/trading/useTradingDetail.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useContext } from 'react';

import type { TradingType } from '@suite-common/trading';
import type { TradingTransaction, TradingTransactionBuy, TradingType } from '@suite-common/trading';

import { useSelector } from 'src/hooks/suite';
import { useServerEnvironment } from 'src/hooks/wallet/trading/useServerEnviroment';
Expand All @@ -19,9 +19,9 @@ import {
TradingUseDetailOutputProps,
TradingUseDetailProps,
} from 'src/types/trading/tradingDetail';
import { Trade, TradeBuy } from 'src/types/wallet/tradingCommonTypes';

const isBuyTrade = (trade: Trade): trade is TradeBuy => trade.tradeType === 'buy';
const isBuyTrade = (trade: TradingTransaction): trade is TradingTransactionBuy =>
trade.tradeType === 'buy';

const getTypedTrade = <T extends TradingType>({
trades,
Expand Down
12 changes: 8 additions & 4 deletions packages/suite/src/hooks/wallet/trading/useTradingWatchTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@ import {
WatchSellTradeResponse,
} from 'invity-api';

import { type TradingTradeStatusType, type TradingType, invityAPI } from '@suite-common/trading';
import {
type TradingTradeStatusType,
type TradingTransaction,
type TradingType,
invityAPI,
} from '@suite-common/trading';

import { saveTrade as saveBuyTrade } from 'src/actions/wallet/tradingBuyActions';
import { saveTrade as saveExchangeTrade } from 'src/actions/wallet/tradingExchangeActions';
import { saveTrade as saveSellTrade } from 'src/actions/wallet/tradingSellActions';
import { useFormDraft } from 'src/hooks/wallet/useFormDraft';
import { TradingUseWatchTradeProps, TradingWatchTradeProps } from 'src/types/trading/trading';
import { Trade, TradeType } from 'src/types/wallet/tradingCommonTypes';

export const tradeFinalStatuses: Record<TradeType, TradingTradeStatusType[]> = {
export const tradeFinalStatuses: Record<TradingType, TradingTradeStatusType[]> = {
buy: ['SUCCESS', 'ERROR', 'BLOCKED'] satisfies BuyTradeFinalStatus[],
sell: ['SUCCESS', 'ERROR', 'BLOCKED', 'CANCELLED', 'REFUNDED'] satisfies SellTradeFinalStatus[],
exchange: ['SUCCESS', 'ERROR', 'KYC'] satisfies ExchangeTradeFinalStatus[],
};

const shouldRefreshTrade = (trade: Trade | undefined) =>
const shouldRefreshTrade = (trade: TradingTransaction | undefined) =>
trade && trade.data.status && !tradeFinalStatuses[trade.tradeType].includes(trade.data.status);

const tradingWatchTrade = async <T extends TradingType>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
SellFiatTradeQuoteRequest,
} from 'invity-api';

import type { TradingTransactionBuy, TradingTransactionExchange } from '@suite-common/trading';

import { STORAGE } from 'src/actions/suite/constants';
import {
TRADING_BUY,
Expand All @@ -23,7 +25,6 @@ import {
} from 'src/reducers/wallet/__fixtures__/tradingReducerFixtures';
import { accounts } from 'src/reducers/wallet/__fixtures__/transactionConstants';
import { initialState, tradingReducer } from 'src/reducers/wallet/tradingReducer';
import { TradeBuy, TradeExchange } from 'src/types/wallet/tradingCommonTypes';

describe('settings reducer', () => {
it('test initial state', () => {
Expand Down Expand Up @@ -288,7 +289,7 @@ describe('settings reducer', () => {
});

it('SAVE_TRADE', () => {
const tradeBuy: TradeBuy = {
const tradeBuy: TradingTransactionBuy = {
date: 'ddd',
key: 'buy-key',
tradeType: 'buy',
Expand All @@ -313,7 +314,7 @@ describe('settings reducer', () => {
accountType: 'normal',
},
};
const tradeExchange: TradeExchange = {
const tradeExchange: TradingTransactionExchange = {
date: 'ddd',
key: 'exchange-key',
tradeType: 'exchange',
Expand Down
19 changes: 9 additions & 10 deletions packages/suite/src/reducers/wallet/tradingReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import type {
SellFiatTradeQuoteRequest,
} from 'invity-api';

import type { TradingType } from '@suite-common/trading';
import type {
TradingPaymentMethodListProps,
TradingTransaction,
TradingType,
} from '@suite-common/trading';
import type { AccountKey, PrecomposedTransactionFinal } from '@suite-common/wallet-types';
import type { FeeLevel } from '@trezor/connect';

Expand All @@ -26,10 +30,8 @@ import {
import type { BuyInfo } from 'src/actions/wallet/tradingBuyActions';
import type { ExchangeInfo } from 'src/actions/wallet/tradingExchangeActions';
import type { SellInfo } from 'src/actions/wallet/tradingSellActions';
import type { AppState, Action as SuiteAction } from 'src/types/suite';
import { TradingPaymentMethodListProps } from 'src/types/trading/trading';
import type { WalletAction } from 'src/types/wallet';
import type { Trade } from 'src/types/wallet/tradingCommonTypes';
import { AppState } from 'src/reducers/store';
import { Action } from 'src/types/suite';

export interface ComposedTransactionInfo {
composed?: Pick<
Expand Down Expand Up @@ -86,7 +88,7 @@ export interface State {
exchange: Exchange;
sell: Sell;
composedTransactionInfo: ComposedTransactionInfo;
trades: Trade[];
trades: TradingTransaction[];
modalCryptoId: CryptoId | undefined;
modalAccountKey: AccountKey | undefined;
isLoading: boolean;
Expand Down Expand Up @@ -139,10 +141,7 @@ export const initialState: State = {
prefilledFromCryptoId: undefined,
};

export const tradingReducer = (
state: State = initialState,
action: WalletAction | SuiteAction,
): State =>
export const tradingReducer = (state: State = initialState, action: Action): State =>
produce(state, draft => {
switch (action.type) {
case STORAGE.LOAD:
Expand Down
6 changes: 3 additions & 3 deletions packages/suite/src/storage/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AnalyticsState } from '@suite-common/analytics';
import type { MessageState } from '@suite-common/message-system';
import type { DeviceWithEmptyPath, MessageSystem } from '@suite-common/suite-types';
import { SimpleTokenStructure } from '@suite-common/token-definitions';
import type { TradingTransaction } from '@suite-common/trading';
import { NetworkSymbol } from '@suite-common/wallet-config';
import type {
BackendSettings,
Expand All @@ -18,8 +19,7 @@ import type { StorageUpdateMessage } from '@trezor/suite-storage';
import type { SuiteState } from 'src/reducers/suite/suiteReducer';
import type { MetadataState } from 'src/types/suite/metadata';
import type { Account, Discovery, WalletAccountTransaction } from 'src/types/wallet';
import type { CoinjoinAccount, CoinjoinDebugSettings } from 'src/types/wallet/coinjoin';
import type { Trade } from 'src/types/wallet/tradingCommonTypes';
import { CoinjoinAccount, CoinjoinDebugSettings } from 'src/types/wallet/coinjoin';

import { GraphData } from '../types/wallet/graph';

Expand Down Expand Up @@ -105,7 +105,7 @@ export interface SuiteDBSchema extends DBSchema {
};
tradingTrades: {
key: string;
value: Trade;
value: TradingTransaction;
};
metadata: {
key: 'state';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { TradingTransaction } from '@suite-common/trading';
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 (
Expand All @@ -23,7 +22,7 @@ export const migrationCoinmarketToTrading: OnUpgradeFunc<SuiteDBSchema> = async
const newTradesStore = db.createObjectStore(newStoreName, { keyPath: trades.keyPath });

while (tradesCursor) {
const trade = tradesCursor.value as Trade;
const trade = tradesCursor.value as TradingTransaction;

await tradesCursor.delete();
await newTradesStore.add(trade);
Expand Down
Loading

0 comments on commit b9dbbed

Please sign in to comment.