Skip to content

Commit 82b5674

Browse files
sherpaPSXvytick
authored andcommitted
refactor(trading): move currencies to the common & invity-api update
1 parent 4744bcc commit 82b5674

41 files changed

Lines changed: 359 additions & 293 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/suite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
"@testing-library/user-event": "^14.6.1",
151151
"@trezor/eslint": "workspace:*",
152152
"@types/file-saver": "^2.0.6",
153-
"@types/invity-api": "^1.1.14",
153+
"@types/invity-api": "^1.1.15",
154154
"@types/pako": "^2.0.4",
155155
"@types/pdfmake": "^0.3.1",
156156
"@types/react": "19.1.6",

packages/suite/src/hooks/wallet/trading/form/common/useTradingFormActions.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type TradingExchangeFormProps,
1616
type TradingSellFormProps,
1717
isCountrySubdivisionEmpty,
18+
mapFiatCurrencyCodeToBaseCurrencyCode,
1819
tradingExchangeActions,
1920
} from '@suite-common/trading';
2021
import {
@@ -90,7 +91,7 @@ export const useTradingFormActions = <T extends TradingSellExchangeFormProps>({
9091
const tradingFiatValues = useTradingFiatValues({
9192
cryptoId: sendCryptoSelect?.id,
9293
amount: sendCryptoAccount?.balance,
93-
fiatCurrency: getValues().outputs?.[0]?.currency?.value as FiatCurrencyCode,
94+
fiatCurrency: getValues().outputs?.[0]?.currency?.value || undefined,
9495
});
9596

9697
const { getAssetDecimals } = useTradingAssetDecimals();
@@ -123,7 +124,10 @@ export const useTradingFormActions = <T extends TradingSellExchangeFormProps>({
123124

124125
if (!tradingFiatValues) return;
125126

126-
const rate = await tradingFiatValues.fiatRatesUpdater(value);
127+
const mappedBaseCurrencyCode = mapFiatCurrencyCodeToBaseCurrencyCode(value);
128+
if (!mappedBaseCurrencyCode) return;
129+
130+
const rate = await tradingFiatValues.fiatRatesUpdater(mappedBaseCurrencyCode);
127131
const amount = getValues(TRADING_FORM_OUTPUT_AMOUNT);
128132
const formattedAmount = new BigNumber(
129133
shouldSendInSats ? convertAmountSubunitsToUnits(amount, networkDecimals) : amount,
@@ -191,10 +195,16 @@ export const useTradingFormActions = <T extends TradingSellExchangeFormProps>({
191195
setAccountOnChange(account);
192196
changeFeeLevel('normal'); // reset fee level
193197

194-
await tradingFiatValues?.fiatRatesUpdater(
195-
getValues(TRADING_FORM_OUTPUT_CURRENCY)?.value as FiatCurrencyCode,
196-
selected.contractAddress as TokenAddress,
198+
const mappedBaseCurrencyCode = mapFiatCurrencyCodeToBaseCurrencyCode(
199+
getValues(TRADING_FORM_OUTPUT_CURRENCY)?.value,
197200
);
201+
202+
if (mappedBaseCurrencyCode) {
203+
await tradingFiatValues?.fiatRatesUpdater(
204+
mappedBaseCurrencyCode,
205+
selected.contractAddress as TokenAddress,
206+
);
207+
}
198208
};
199209

200210
const setRatioAmount = (divisor: number) => {

packages/suite/src/hooks/wallet/trading/form/useTradingBuyForm.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useRef } from 'react';
22
import { useForm, useWatch } from 'react-hook-form';
33

4-
import type { BuyTrade, BuyTradeResponse, FiatCurrencyCode } from 'invity-api';
4+
import type { BuyTrade, BuyTradeResponse } from 'invity-api';
55
import useDebounce from 'react-use/lib/useDebounce';
66

77
import { events } from '@suite/analytics';
@@ -17,6 +17,7 @@ import {
1717
buyThunks,
1818
getTradingQuotesByPaymentMethod,
1919
isCountrySubdivisionEmpty,
20+
mapFiatCurrencyCodeToBaseCurrencyCode,
2021
selectTradingBuy,
2122
selectTradingPaymentMethods,
2223
selectTradingVerifiedAddress,
@@ -90,12 +91,12 @@ export const useTradingBuyForm = ({
9091
? {
9192
cryptoId: selectedQuote.receiveCurrency,
9293
amount: selectedQuote.receiveAmount?.toString(),
93-
fiatCurrency: selectedQuote.fiatCurrency as FiatCurrencyCode | undefined,
94+
fiatCurrency: mapFiatCurrencyCodeToBaseCurrencyCode(selectedQuote.fiatCurrency),
9495
}
9596
: {
9697
cryptoId: quotesRequest?.receiveCurrency,
9798
amount: quotesRequest?.cryptoStringAmount,
98-
fiatCurrency: quotesRequest?.fiatCurrency as FiatCurrencyCode | undefined,
99+
fiatCurrency: mapFiatCurrencyCodeToBaseCurrencyCode(quotesRequest?.fiatCurrency),
99100
};
100101
useTradingFiatValues(fiatTradingValuesParams);
101102

packages/suite/src/hooks/wallet/trading/form/useTradingBuyFormDefaultValues.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
import { useMemo } from 'react';
22

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

55
import {
66
TRADING_DEFAULT_PAYMENT_METHOD,
77
type TradingBuyInfoSelector,
88
TradingCountryCode,
99
type TradingPaymentMethodListProps,
10-
enabledTradingCurrencies,
10+
buildTradingFiatOption,
1111
getDefaultCountry,
1212
getDefaultCountrySubdivision,
13+
getSupportedFiatCurrencyWithFallback,
1314
regional,
1415
useTradingAssets,
1516
} from '@suite-common/trading';
1617
import { selectBaseCurrency } from '@suite-common/wallet-core';
17-
import { isArrayMember, typedObjectValues } from '@trezor/utils';
1818

1919
import { useSelector } from 'src/hooks/suite';
2020
import { selectTorState } from 'src/selectors/suite/suiteSelectors';
2121
import { TradingBuyFormDefaultValuesProps } from 'src/types/trading/tradingForm';
22-
import { buildTradingFiatOption } from 'src/utils/wallet/trading/tradingUtils';
2322

2423
export const useTradingBuyFormDefaultValues = (
2524
cryptoId: CryptoId | undefined,
@@ -51,16 +50,10 @@ export const useTradingBuyFormDefaultValues = (
5150
);
5251

5352
const baseCurrencyCode = useSelector(selectBaseCurrency);
54-
const isEnabledTradingCurrency = isArrayMember(
55-
baseCurrencyCode,
56-
typedObjectValues(enabledTradingCurrencies),
57-
);
58-
const suggestedFiatCurrency = (
59-
isEnabledTradingCurrency ? baseCurrencyCode : 'usd'
60-
) as FiatCurrencyCode;
53+
const suggestedFiatCurrency = getSupportedFiatCurrencyWithFallback(baseCurrencyCode);
6154
const defaultCurrency = useMemo(
62-
() => buildTradingFiatOption(isEnabledTradingCurrency ? baseCurrencyCode : 'usd'),
63-
[isEnabledTradingCurrency, baseCurrencyCode],
55+
() => buildTradingFiatOption(suggestedFiatCurrency),
56+
[suggestedFiatCurrency],
6457
);
6558
const defaultValues = useMemo(
6659
() => ({

packages/suite/src/hooks/wallet/trading/form/useTradingBuyFormRedirectValues.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { BuyTradeQuoteRequest, FiatCurrencyCode } from 'invity-api';
1+
import { BuyTradeQuoteRequest } from 'invity-api';
22

33
import {
44
TradingBuyFormProps,
55
TradingCountryCode,
6+
buildTradingFiatOption,
67
getDefaultCountry,
78
getDefaultCountrySubdivision,
9+
getSupportedFiatCurrencyWithFallback,
810
useTradingAssets,
911
} from '@suite-common/trading';
1012

11-
import { buildTradingFiatOption } from 'src/utils/wallet/trading/tradingUtils';
12-
1313
export const useTradingBuyFormRedirectValues = (
1414
isFromRedirect: boolean,
1515
quotesRequest: BuyTradeQuoteRequest | undefined,
@@ -21,7 +21,9 @@ export const useTradingBuyFormRedirectValues = (
2121
return {
2222
amountInCrypto: quotesRequest.wantCrypto,
2323
cryptoSelect: createAssetOptionFromCryptoId(quotesRequest.receiveCurrency),
24-
currencySelect: buildTradingFiatOption(quotesRequest.fiatCurrency as FiatCurrencyCode),
24+
currencySelect: buildTradingFiatOption(
25+
getSupportedFiatCurrencyWithFallback(quotesRequest.fiatCurrency),
26+
),
2527
countrySelect: getDefaultCountry(quotesRequest.country as TradingCountryCode),
2628
countrySubdivisionSelect: getDefaultCountrySubdivision(quotesRequest.subdivision),
2729

packages/suite/src/hooks/wallet/trading/form/useTradingExchangeForm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
22
import { useForm, useWatch } from 'react-hook-form';
33
import { useDebounce } from 'react-use';
44

5-
import type { DexApprovalType, ExchangeTrade, FiatCurrencyCode } from 'invity-api';
5+
import type { DexApprovalType, ExchangeTrade } from 'invity-api';
66

77
import { events } from '@suite/analytics';
88
import { TranslationKey, useTranslation } from '@suite/intl';
@@ -192,7 +192,7 @@ export const useTradingExchangeForm = ({
192192
useTradingFiatValues({
193193
cryptoId: receiveCryptoSelect?.id,
194194
amount: selectedQuote?.receiveStringAmount,
195-
fiatCurrency: output?.currency?.value as FiatCurrencyCode | undefined,
195+
fiatCurrency: output?.currency?.value || undefined,
196196
});
197197

198198
const formIsValid = Object.keys(formState.errors).length === 0;

packages/suite/src/hooks/wallet/trading/form/useTradingExchangeFormDefaultValues.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ import {
1515
TradingExchangeKycFilter,
1616
TradingExchangeRateFilter,
1717
TradingExchangeRateType,
18-
enabledTradingCurrencies,
18+
buildTradingBaseCurrencyOptionFromFiat,
19+
buildTradingFiatOption,
20+
getSupportedFiatCurrencyWithFallback,
1921
} from '@suite-common/trading';
2022
import { DEFAULT_PAYMENT, DEFAULT_VALUES } from '@suite-common/wallet-constants';
2123
import { selectBaseCurrency } from '@suite-common/wallet-core';
2224
import { AccountKey, FormState, Output } from '@suite-common/wallet-types';
23-
import { isArrayMember, typedObjectValues } from '@trezor/utils';
2425

2526
import { useSelector } from 'src/hooks/suite';
26-
import {
27-
buildTradingFiatOption,
28-
resolveAddressAndToken,
29-
} from 'src/utils/wallet/trading/tradingUtils';
27+
import { resolveAddressAndToken } from 'src/utils/wallet/trading/tradingUtils';
3028

3129
import { useTradingDefaultSellAsset } from './common/useTradingDefaultSellAsset';
3230

@@ -37,11 +35,7 @@ export const useTradingExchangeFormDefaultValues = (accountKey: AccountKey, cryp
3735
() =>
3836
// Here, we are using BaseCurrency as a way how to determine the users preferred Sell/Buy currency,
3937
// however, they may not be available (or it is 'btc'). In that case, we fall back to 'usd'
40-
buildTradingFiatOption(
41-
isArrayMember(baseCurrencyCode, typedObjectValues(enabledTradingCurrencies))
42-
? baseCurrencyCode
43-
: 'usd',
44-
),
38+
buildTradingFiatOption(getSupportedFiatCurrencyWithFallback(baseCurrencyCode)),
4539
[baseCurrencyCode],
4640
);
4741
const { account, defaultAsset } = useTradingDefaultSellAsset({ accountKey, cryptoId });
@@ -50,7 +44,7 @@ export const useTradingExchangeFormDefaultValues = (accountKey: AccountKey, cryp
5044
const defaultPayment: Output = useMemo(
5145
() => ({
5246
...DEFAULT_PAYMENT,
53-
currency: defaultCurrency,
47+
currency: buildTradingBaseCurrencyOptionFromFiat(defaultCurrency.value),
5448
address,
5549
token,
5650
}),

packages/suite/src/hooks/wallet/trading/form/useTradingSellFormDefaultValues.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@ import {
66
TRADING_DEFAULT_PAYMENT_METHOD,
77
TradingCountryCode,
88
type TradingPaymentMethodListProps,
9-
enabledTradingCurrencies,
9+
buildTradingBaseCurrencyOptionFromFiat,
10+
buildTradingFiatOption,
1011
getDefaultCountry,
1112
getDefaultCountrySubdivision,
13+
getSupportedFiatCurrencyWithFallback,
1214
regional,
1315
} from '@suite-common/trading';
1416
import { DEFAULT_PAYMENT, DEFAULT_VALUES } from '@suite-common/wallet-constants';
1517
import { selectBaseCurrency } from '@suite-common/wallet-core';
1618
import { AccountKey, FormState, Output } from '@suite-common/wallet-types';
17-
import { isArrayMember, typedObjectValues } from '@trezor/utils';
1819

1920
import { useSelector } from 'src/hooks/suite';
2021
import { selectTorState } from 'src/selectors/suite/suiteSelectors';
2122
import { TradingSellFormDefaultValuesProps } from 'src/types/trading/tradingForm';
22-
import {
23-
buildTradingFiatOption,
24-
resolveAddressAndToken,
25-
} from 'src/utils/wallet/trading/tradingUtils';
23+
import { resolveAddressAndToken } from 'src/utils/wallet/trading/tradingUtils';
2624

2725
import { useTradingDefaultSellAsset } from './common/useTradingDefaultSellAsset';
2826

@@ -58,18 +56,13 @@ export const useTradingSellFormDefaultValues = (
5856
);
5957
const baseCurrencyCode = useSelector(selectBaseCurrency);
6058
const defaultCurrency = useMemo(
61-
() =>
62-
buildTradingFiatOption(
63-
isArrayMember(baseCurrencyCode, typedObjectValues(enabledTradingCurrencies))
64-
? baseCurrencyCode
65-
: 'usd',
66-
),
59+
() => buildTradingFiatOption(getSupportedFiatCurrencyWithFallback(baseCurrencyCode)),
6760
[baseCurrencyCode],
6861
);
6962
const defaultPayment: Output = useMemo(
7063
() => ({
7164
...DEFAULT_PAYMENT,
72-
currency: defaultCurrency,
65+
currency: buildTradingBaseCurrencyOptionFromFiat(defaultCurrency.value),
7366
address,
7467
token,
7568
}),

packages/suite/src/hooks/wallet/trading/form/useTradingSellFormRedirectValues.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { useCallback, useMemo } from 'react';
22

3-
import { FiatCurrencyCode, SellFiatTradeQuoteRequest } from 'invity-api';
3+
import { SellFiatTradeQuoteRequest } from 'invity-api';
44

55
import {
66
TradingAssetOption,
77
TradingAssetSellOption,
88
TradingCountryCode,
99
type TradingSellFormProps,
10+
buildTradingBaseCurrencyOptionFromFiat,
1011
getDefaultCountry,
1112
getDefaultCountrySubdivision,
1213
selectTradingComposedTransactionInfo,
@@ -18,10 +19,7 @@ import { getContractAddressForNetworkSymbol } from '@suite-common/wallet-utils';
1819
import { useCurrentRef } from '@trezor/react-utils';
1920

2021
import { useSelector } from 'src/hooks/suite';
21-
import {
22-
buildTradingFiatOption,
23-
resolveAddressAndToken,
24-
} from 'src/utils/wallet/trading/tradingUtils';
22+
import { resolveAddressAndToken } from 'src/utils/wallet/trading/tradingUtils';
2523

2624
export const useTradingSellFormRedirectValues = (
2725
isFromRedirect: boolean,
@@ -95,9 +93,7 @@ export const useTradingSellFormRedirectValues = (
9593
{
9694
...DEFAULT_PAYMENT,
9795
fiat: quotesRequest.fiatStringAmount as string,
98-
currency: buildTradingFiatOption(
99-
quotesRequest.fiatCurrency as FiatCurrencyCode,
100-
),
96+
currency: buildTradingBaseCurrencyOptionFromFiat(quotesRequest.fiatCurrency),
10197
amount: quotesRequest.cryptoStringAmount as string,
10298
address,
10399
token,

packages/suite/src/utils/wallet/trading/__tests__/tradingUtils.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { buildTradingFiatOption } from '@suite-common/trading';
2+
13
import { FIXTURE_ACCOUNT_OPTIONS } from 'src/utils/wallet/trading/__fixtures__/tradingUtils';
24
import {
3-
buildTradingFiatOption,
45
getCountryLabelParts,
56
getTradeTypeByRoute,
67
resolveAddressAndToken,

0 commit comments

Comments
 (0)