diff --git a/suite-common/trading/src/reducers/__fixtures__/buyTradingReducer.ts b/suite-common/trading/src/reducers/__fixtures__/buyTradingReducer.ts index 624ad12915f..57105c5dd8e 100644 --- a/suite-common/trading/src/reducers/__fixtures__/buyTradingReducer.ts +++ b/suite-common/trading/src/reducers/__fixtures__/buyTradingReducer.ts @@ -1,4 +1,4 @@ -import { BuyTrade, BuyTradeQuoteRequest, CryptoId } from 'invity-api'; +import { BuyTrade, BuyTradeQuoteRequest, CryptoId, FiatCurrenciesProps } from 'invity-api'; import { tradingBuyActions } from '../../actions/buyActions'; import { BuyInfo, TradingBuyState } from '../buyReducer'; @@ -17,11 +17,13 @@ const buyInfo: BuyInfo = { buyInfo: { country: 'cz', providers: [], - defaultAmountsOfFiatCurrencies: new Map([['usd', '1000']]), + defaultAmountsOfFiatCurrencies: { + usd: 150, + } as FiatCurrenciesProps, }, providerInfos: {}, - supportedCryptoCurrencies: new Set(['BTC', 'ETH']) as Set, - supportedFiatCurrencies: new Set(['usd']), + supportedCryptoCurrencies: ['BTC', 'ETH'] as CryptoId[], + supportedFiatCurrencies: ['usd'], }; const quotesRequest: BuyTradeQuoteRequest = { diff --git a/suite-common/trading/src/reducers/buyReducer.ts b/suite-common/trading/src/reducers/buyReducer.ts index 7276beb9e1c..40b8f8c9350 100644 --- a/suite-common/trading/src/reducers/buyReducer.ts +++ b/suite-common/trading/src/reducers/buyReducer.ts @@ -9,16 +9,12 @@ import type { import { createReducerWithExtraDeps } from '@suite-common/redux-utils'; import { tradingBuyActions } from '../actions/buyActions'; -import type { TradingFiatCurrenciesProps } from '../types'; -// TODO: refactor Map and Set https://redux.js.org/style-guide/#do-not-put-non-serializable-values-in-state-or-actions export interface BuyInfo { - buyInfo: Omit & { - defaultAmountsOfFiatCurrencies: TradingFiatCurrenciesProps; - }; + buyInfo: BuyListResponse; providerInfos: { [name: string]: BuyProviderInfo }; - supportedFiatCurrencies: Set; - supportedCryptoCurrencies: Set; + supportedFiatCurrencies: string[]; + supportedCryptoCurrencies: CryptoId[]; } export interface TradingBuyState { diff --git a/suite-common/trading/src/selectors/tradingSelectors.ts b/suite-common/trading/src/selectors/tradingSelectors.ts index e9a45593f34..ca0577fc63d 100644 --- a/suite-common/trading/src/selectors/tradingSelectors.ts +++ b/suite-common/trading/src/selectors/tradingSelectors.ts @@ -1,8 +1,10 @@ +import { BuyListResponse, BuyProviderInfo, CryptoId, FiatCurrencyCode } from 'invity-api'; + import { Account, SelectedAccountStatus } from '@suite-common/wallet-types'; import { AddressDisplayOptions } from '@suite-common/wallet-types/src/settings'; import type { TradingState } from '../reducers/tradingReducer'; -import { InvityServerEnvironment } from '../types'; +import { InvityServerEnvironment, TradingFiatCurrenciesProps } from '../types'; // partial copy of Suite state export type TradingRootState = { @@ -21,6 +23,15 @@ export type TradingRootState = { }; }; +interface BuyInfoSelector { + buyInfo: Omit & { + defaultAmountsOfFiatCurrencies: TradingFiatCurrenciesProps; + }; + providerInfos: { [name: string]: BuyProviderInfo }; + supportedFiatCurrencies: Set; + supportedCryptoCurrencies: Set; +} + export const selectTradingLoadingAndTimestamp = (state: TradingRootState) => ({ isLoading: state.wallet.trading.isLoading, lastLoadedTimestamp: state.wallet.trading.lastLoadedTimestamp, @@ -28,7 +39,34 @@ export const selectTradingLoadingAndTimestamp = (state: TradingRootState) => ({ export const selectTradingInfo = (state: TradingRootState) => state.wallet.trading.info; -export const selectTradingBuy = (state: TradingRootState) => state.wallet.trading.buy; +export const selecteTradingBuyInfo = (state: TradingRootState): BuyInfoSelector | undefined => { + const { buyInfo } = state.wallet.trading.buy; + + const defaultAmountsOfFiatCurrencies: TradingFiatCurrenciesProps = new Map(); + + if (buyInfo?.buyInfo.defaultAmountsOfFiatCurrencies) { + Object.entries(buyInfo.buyInfo.defaultAmountsOfFiatCurrencies).forEach(([key, value]) => { + defaultAmountsOfFiatCurrencies.set(key as FiatCurrencyCode, value.toString()); + }); + } + + if (!buyInfo) return; + + return { + ...buyInfo, + buyInfo: { + ...buyInfo.buyInfo, + defaultAmountsOfFiatCurrencies, + }, + supportedCryptoCurrencies: new Set(buyInfo.supportedCryptoCurrencies), + supportedFiatCurrencies: new Set(buyInfo.supportedFiatCurrencies), + }; +}; + +export const selectTradingBuy = (state: TradingRootState) => ({ + ...state.wallet.trading.buy, + buyInfo: selecteTradingBuyInfo(state), +}); export const selectTradingSelectedAccount = (state: TradingRootState) => state.wallet.selectedAccount; diff --git a/suite-common/trading/src/thunks/buyThunks.ts b/suite-common/trading/src/thunks/buyThunks.ts index 6efa1ce9bcb..d965bfb1e92 100644 --- a/suite-common/trading/src/thunks/buyThunks.ts +++ b/suite-common/trading/src/thunks/buyThunks.ts @@ -5,24 +5,22 @@ import { createThunk } from '@suite-common/redux-utils'; import { invityAPI } from '../invityAPI'; import { BuyInfo } from '../reducers/buyReducer'; import { regional } from '../regional'; -import { TradingFiatCurrenciesProps } from '../types'; const BUY_COMMON_PREFIX = '@trading-buy/thunk'; const loadInfoThunk = createThunk(`${BUY_COMMON_PREFIX}/loadInfo`, async () => { const buyInfo = await invityAPI.getBuyList(); - const defaultAmountsOfFiatCurrencies: TradingFiatCurrenciesProps = new Map(); if (!buyInfo || !buyInfo.providers) { return { buyInfo: { country: regional.UNKNOWN_COUNTRY, providers: [], - defaultAmountsOfFiatCurrencies, + defaultAmountsOfFiatCurrencies: {} as Record, }, providerInfos: {}, - supportedFiatCurrencies: new Set(), - supportedCryptoCurrencies: new Set(), + supportedFiatCurrencies: [], + supportedCryptoCurrencies: [], }; } @@ -30,25 +28,16 @@ const loadInfoThunk = createThunk(`${BUY_COMMON_PREFIX}/loadInfo`, asyn buyInfo.providers.forEach(e => (providerInfos[e.name] = e)); - const tradedFiatCurrencies: string[] = []; - const tradedCoins: CryptoId[] = []; - buyInfo.providers.forEach(p => { - tradedFiatCurrencies.push(...p.tradedFiatCurrencies.map(c => c.toLowerCase())); - tradedCoins.push(...p.tradedCoins); + const supportedFiatCurrencies: string[] = []; + const supportedCryptoCurrencies: CryptoId[] = []; + buyInfo.providers.forEach(provider => { + supportedFiatCurrencies.push(...provider.tradedFiatCurrencies.map(c => c.toLowerCase())); + supportedCryptoCurrencies.push(...provider.tradedCoins); }); - const supportedFiatCurrencies = new Set(tradedFiatCurrencies); - const supportedCryptoCurrencies = new Set(tradedCoins); - - if (buyInfo.defaultAmountsOfFiatCurrencies) { - Object.entries(buyInfo.defaultAmountsOfFiatCurrencies).forEach(([key, value]) => { - defaultAmountsOfFiatCurrencies.set(key as FiatCurrencyCode, value.toString()); - }); - } return { buyInfo: { ...buyInfo, - defaultAmountsOfFiatCurrencies, }, providerInfos, supportedFiatCurrencies,