Skip to content

Commit

Permalink
chore(trading): move common trading utils to suite-common
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Jan 31, 2025
1 parent 24e7357 commit 9b5e65b
Show file tree
Hide file tree
Showing 47 changed files with 757 additions and 446 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CryptoId } from 'invity-api';

import { notificationsActions } from '@suite-common/toast-notifications';
import type { TradingType } from '@suite-common/trading';
import { type TradingType, getUnusedAddressFromAccount } from '@suite-common/trading';
import {
confirmAddressOnDeviceThunk,
selectSelectedDevice,
Expand All @@ -26,7 +26,6 @@ import { ComposedTransactionInfo } from 'src/reducers/wallet/tradingReducer';
import { Dispatch, GetState } from 'src/types/suite';
import { Account } from 'src/types/wallet';
import { submitRequestForm as envSubmitRequestForm } from 'src/utils/suite/env';
import { getUnusedAddressFromAccount } from 'src/utils/wallet/trading/tradingUtils';

export type TradingCommonAction =
| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback } from 'react';

import { cryptoIdToSymbol } from '@suite-common/trading';
import {
getDisplaySymbol,
getNetwork,
Expand All @@ -17,7 +18,6 @@ import {
import { useSelector } from 'src/hooks/suite';
import { useTradingInfo } from 'src/hooks/wallet/trading/useTradingInfo';
import { selectAccountIncludingChosenInTrading } from 'src/reducers/wallet/selectedAccountReducer';
import { cryptoIdToSymbol } from 'src/utils/wallet/trading/tradingUtils';

import { ConfirmActionModal } from './DeviceContextModal/ConfirmActionModal';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useState } from 'react';

import { mapTestnetSymbol } from '@suite-common/trading';
import { selectAccounts, selectSelectedDevice } from '@suite-common/wallet-core';
import { Account, SelectedAccountLoaded } from '@suite-common/wallet-types';
import { isTestnet } from '@suite-common/wallet-utils';

import { useSelector } from 'src/hooks/suite';
import { mapTestnetSymbol, tradingGetSortedAccounts } from 'src/utils/wallet/trading/tradingUtils';
import { tradingGetSortedAccounts } from 'src/utils/wallet/trading/tradingUtils';

interface TradingUseAccountProps {
tradingAccount: Account | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useEffect } from 'react';

import { FiatCurrencyCode } from 'invity-api';

import { cryptoIdToSymbol, mapTestnetSymbol } from '@suite-common/trading';
import { NetworkSymbol, networks } from '@suite-common/wallet-config';
import { selectFiatRatesByFiatRateKey, updateFiatRatesThunk } from '@suite-common/wallet-core';
import { FiatRatesResult, Rate, Timestamp, TokenAddress } from '@suite-common/wallet-types';
Expand All @@ -11,11 +12,7 @@ import { useDispatch, useSelector } from 'src/hooks/suite';
import { useBitcoinAmountUnit } from 'src/hooks/wallet/useBitcoinAmountUnit';
import { selectLocalCurrency } from 'src/reducers/wallet/settingsReducer';
import { TradingAccountOptionsGroupOptionProps } from 'src/types/trading/trading';
import {
cryptoIdToSymbol,
getTradingNetworkDecimals,
mapTestnetSymbol,
} from 'src/utils/wallet/trading/tradingUtils';
import { getTradingNetworkDecimals } from 'src/utils/wallet/trading/tradingUtils';

interface TradingBalanceProps {
sendCryptoSelect?: TradingAccountOptionsGroupOptionProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useDebounce } from 'react-use';
import { FiatCurrencyCode } from 'invity-api';

import { isChanged } from '@suite-common/suite-utils';
import { cryptoIdToSymbol } from '@suite-common/trading';
import { selectAccounts, selectSelectedDevice } from '@suite-common/wallet-core';
import {
amountToSmallestUnit,
Expand Down Expand Up @@ -38,7 +39,6 @@ import {
} from 'src/types/trading/tradingForm';
import { tradingGetExchangeReceiveCryptoId } from 'src/utils/wallet/trading/exchangeUtils';
import {
cryptoIdToSymbol,
getAddressAndTokenFromAccountOptionsGroupProps,
getTradingNetworkDecimals,
tradingGetSortedAccounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import useDebounce from 'react-use/lib/useDebounce';

import { isChanged } from '@suite-common/suite-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import { type TradingBuyType, invityAPI } from '@suite-common/trading';
import {
type TradingBuyType,
addIdsToQuotes,
cryptoIdToNetwork,
filterQuotesAccordingTags,
invityAPI,
tradingGetSuccessQuotes,
} from '@suite-common/trading';
import { networks } from '@suite-common/wallet-config';
import { formatAmount } from '@suite-common/wallet-utils';
import { isDesktop } from '@trezor/env-utils';
Expand Down Expand Up @@ -37,13 +44,7 @@ import { UseTradingFormProps } from 'src/types/trading/trading';
import { TradingBuyFormContextProps, TradingBuyFormProps } from 'src/types/trading/tradingForm';
import type { AmountLimitProps } from 'src/utils/suite/validation';
import { createQuoteLink, createTxLink, getAmountLimits } from 'src/utils/wallet/trading/buyUtils';
import {
addIdsToQuotes,
cryptoIdToNetwork,
filterQuotesAccordingTags,
getTradingNetworkDecimals,
tradingGetSuccessQuotes,
} from 'src/utils/wallet/trading/tradingUtils';
import { getTradingNetworkDecimals } from 'src/utils/wallet/trading/tradingUtils';

import { useTradingInitializer } from './common/useTradingInitializer';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMemo } from 'react';

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

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

import { BuyInfo } from 'src/actions/wallet/tradingBuyActions';
Expand All @@ -14,7 +15,7 @@ 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, getDefaultCountry } from 'src/utils/wallet/trading/tradingUtils';
import { buildFiatOption } from 'src/utils/wallet/trading/tradingUtils';

export const useTradingBuyFormDefaultValues = (
accountSymbol: Account['symbol'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import useDebounce from 'react-use/lib/useDebounce';

import { isChanged } from '@suite-common/suite-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import { type TradingExchangeType, invityAPI } from '@suite-common/trading';
import {
type TradingExchangeType,
addIdsToQuotes,
getUnusedAddressFromAccount,
invityAPI,
tradingGetSuccessQuotes,
} from '@suite-common/trading';
import { networks } from '@suite-common/wallet-config';
import { Account } from '@suite-common/wallet-types';
import { amountToSmallestUnit, formatAmount, toFiatCurrency } from '@suite-common/wallet-utils';
Expand Down Expand Up @@ -52,12 +58,7 @@ import {
getSuccessQuotesOrdered,
tradingGetExchangeReceiveCryptoId,
} from 'src/utils/wallet/trading/exchangeUtils';
import {
addIdsToQuotes,
getTradingNetworkDecimals,
getUnusedAddressFromAccount,
tradingGetSuccessQuotes,
} from 'src/utils/wallet/trading/tradingUtils';
import { getTradingNetworkDecimals } from 'src/utils/wallet/trading/tradingUtils';

import { useTradingInitializer } from './common/useTradingInitializer';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from 'react';

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

Expand Down Expand Up @@ -28,7 +29,6 @@ import { Account } from 'src/types/wallet';
import { tradingGetExchangeReceiveCryptoId } from 'src/utils/wallet/trading/exchangeUtils';
import {
buildFiatOption,
cryptoIdToSymbol,
getAddressAndTokenFromAccountOptionsGroupProps,
} from 'src/utils/wallet/trading/tradingUtils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useCallback } from 'react';

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

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

Expand All @@ -14,7 +15,7 @@ const useTradingPaymentMethod = <
>(): TradingPaymentMethodHookProps<T> => {
const paymentMethods = useSelector(state => state.wallet.trading.info.paymentMethods);

const getPaymentMethods = (quotes: TradingTradeDetailMapProps[T][]) => {
const getPaymentMethods = (quotes: TradingTradeMapProps[T][]) => {
const newPaymentMethods: TradingPaymentMethodListProps[] = [];

quotes.forEach(quote => {
Expand All @@ -33,7 +34,7 @@ const useTradingPaymentMethod = <

const getQuotesByPaymentMethod = useCallback(
(
quotes: TradingTradeDetailMapProps[T][] | undefined,
quotes: TradingTradeMapProps[T][] | undefined,
currentPaymentMethod: TradingPaymentMethodProps,
) => {
if (!quotes) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import useDebounce from 'react-use/lib/useDebounce';

import { isChanged } from '@suite-common/suite-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import { type TradingSellType, invityAPI } from '@suite-common/trading';
import {
type TradingSellType,
addIdsToQuotes,
filterQuotesAccordingTags,
getUnusedAddressFromAccount,
invityAPI,
tradingGetSuccessQuotes,
} from '@suite-common/trading';
import { networks } from '@suite-common/wallet-config';
import { amountToSmallestUnit, formatAmount } from '@suite-common/wallet-utils';
import { EventType, analytics } from '@trezor/suite-analytics';
Expand Down Expand Up @@ -49,13 +56,7 @@ import {
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 {
addIdsToQuotes,
filterQuotesAccordingTags,
getTradingNetworkDecimals,
getUnusedAddressFromAccount,
tradingGetSuccessQuotes,
} from 'src/utils/wallet/trading/tradingUtils';
import { getTradingNetworkDecimals } from 'src/utils/wallet/trading/tradingUtils';

import { useTradingInitializer } from './common/useTradingInitializer';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from 'react';

import { 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 @@ -15,9 +16,7 @@ import { TradingSellFormDefaultValuesProps } from 'src/types/trading/tradingForm
import { Account } from 'src/types/wallet';
import {
buildFiatOption,
cryptoIdToSymbol,
getAddressAndTokenFromAccountOptionsGroupProps,
getDefaultCountry,
} from 'src/utils/wallet/trading/tradingUtils';

export const useTradingSellFormDefaultValues = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { useEffect, useMemo, useState } from 'react';
import { useForm } from 'react-hook-form';

import { TrezorDevice } from '@suite-common/suite-types';
import {
cryptoIdToSymbol,
getUnusedAddressFromAccount,
parseCryptoId,
} from '@suite-common/trading';
import { selectSelectedDevice } from '@suite-common/wallet-core';
import { Account } from '@suite-common/wallet-types';
import { filterReceiveAccounts } from '@suite-common/wallet-utils';
Expand All @@ -19,11 +24,6 @@ import {
TradingVerifyFormAccountOptionProps,
TradingVerifyFormProps,
} from 'src/types/trading/tradingVerify';
import {
cryptoIdToSymbol,
getUnusedAddressFromAccount,
parseCryptoId,
} from 'src/utils/wallet/trading/tradingUtils';

const getSelectAccountOptions = (
suiteReceiveAccounts: Account[] | undefined,
Expand Down
12 changes: 6 additions & 6 deletions packages/suite/src/hooks/wallet/trading/useTradingInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { useCallback } from 'react';

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

import {
cryptoIdToNetwork,
isCryptoIdForNativeToken,
parseCryptoId,
testnetToProdCryptoId,
} from '@suite-common/trading';
import {
NetworkSymbolExtended,
getDisplaySymbol,
Expand All @@ -16,12 +22,6 @@ import {
TradingCryptoSelectOptionProps,
TradingInfoProps,
} from 'src/types/trading/trading';
import {
cryptoIdToNetwork,
isCryptoIdForNativeToken,
parseCryptoId,
testnetToProdCryptoId,
} from 'src/utils/wallet/trading/tradingUtils';

const supportedAddressValidatorSymbols = new Set(
addressValidator.getCurrencies().map(c => c.symbol),
Expand Down
8 changes: 0 additions & 8 deletions packages/suite/src/types/trading/trading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
BuyTrade,
CryptoId,
ExchangeProviderInfo,
ExchangeTrade,
FiatCurrencyCode,
SellFiatTrade,
SellProviderInfo,
Expand Down Expand Up @@ -78,13 +77,6 @@ export type TradingTradeMapProps = {

export type TradingTradeDetailBuySellType = BuyTrade | SellFiatTrade;

export type TradingTradeDetailMapProps = {
buy: BuyTrade;
sell: SellFiatTrade;
exchange: ExchangeTrade;
};
export type TradingTradeBuySellDetailMapProps = Omit<TradingTradeDetailMapProps, 'exchange'>;

export type TradingTradeInfoMapProps = {
buy: BuyInfo;
sell: SellInfo;
Expand Down
8 changes: 4 additions & 4 deletions packages/suite/src/types/trading/tradingForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
TradingExchangeType,
TradingPaymentMethodType,
TradingSellType,
TradingTradeMapProps,
TradingTradeType,
TradingType,
} from '@suite-common/trading';
Expand Down Expand Up @@ -59,7 +60,6 @@ import {
TradingGetProvidersInfoProps,
TradingPaymentMethodListProps,
TradingPaymentMethodProps,
TradingTradeDetailMapProps,
TradingTradeSellExchangeType,
} from 'src/types/trading/trading';
import type { Account } from 'src/types/wallet';
Expand Down Expand Up @@ -279,11 +279,11 @@ export type TradingFormContextValues<T extends TradingType> = TradingFormMapProp

export type TradingPaymentMethodHookProps<T extends TradingType> = {
paymentMethods: TradingPaymentMethodListProps[];
getPaymentMethods: (quotes: TradingTradeDetailMapProps[T][]) => TradingPaymentMethodListProps[];
getPaymentMethods: (quotes: TradingTradeMapProps[T][]) => TradingPaymentMethodListProps[];
getQuotesByPaymentMethod: (
quotes: TradingTradeDetailMapProps[T][] | undefined,
quotes: TradingTradeMapProps[T][] | undefined,
currentPaymentMethod: TradingPaymentMethodProps,
) => TradingTradeDetailMapProps[T][] | undefined;
) => TradingTradeMapProps[T][] | undefined;
};

export interface TradingFormInputDefaultProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ExchangeInfo } from 'src/actions/wallet/tradingExchangeActions';
const litecoin = 'litecoin' as CryptoId;
const bitcoin = 'bitcoin' as CryptoId;
const ethereum = 'ethereum' as CryptoId;
const cronoseth = 'ethereum__0xa0b73e1ff0b80914ab6fe0444e65848c4c34450b' as CryptoId;
const cronoseth = 'ethereum--0xa0b73e1ff0b80914ab6fe0444e65848c4c34450b' as CryptoId;

const tickers1 = [ethereum, 'monero', bitcoin] as CryptoId[];
const tickers2 = [ethereum, bitcoin] as CryptoId[];
Expand Down
Loading

0 comments on commit 9b5e65b

Please sign in to comment.