Skip to content

Commit

Permalink
refactor(trading): unserialized buyInfo data
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Feb 11, 2025
1 parent 7f4d77f commit 661d9e0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<CryptoId>,
supportedFiatCurrencies: new Set(['usd']),
supportedCryptoCurrencies: ['BTC', 'ETH'] as CryptoId[],
supportedFiatCurrencies: ['usd'],
};

const quotesRequest: BuyTradeQuoteRequest = {
Expand Down
10 changes: 3 additions & 7 deletions suite-common/trading/src/reducers/buyReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuyListResponse, 'defaultAmountsOfFiatCurrencies'> & {
defaultAmountsOfFiatCurrencies: TradingFiatCurrenciesProps;
};
buyInfo: BuyListResponse;
providerInfos: { [name: string]: BuyProviderInfo };
supportedFiatCurrencies: Set<string>;
supportedCryptoCurrencies: Set<CryptoId>;
supportedFiatCurrencies: string[];
supportedCryptoCurrencies: CryptoId[];
}

export interface TradingBuyState {
Expand Down
42 changes: 40 additions & 2 deletions suite-common/trading/src/selectors/tradingSelectors.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -21,14 +23,50 @@ export type TradingRootState = {
};
};

interface BuyInfoSelector {
buyInfo: Omit<BuyListResponse, 'defaultAmountsOfFiatCurrencies'> & {
defaultAmountsOfFiatCurrencies: TradingFiatCurrenciesProps;
};
providerInfos: { [name: string]: BuyProviderInfo };
supportedFiatCurrencies: Set<string>;
supportedCryptoCurrencies: Set<CryptoId>;
}

export const selectTradingLoadingAndTimestamp = (state: TradingRootState) => ({
isLoading: state.wallet.trading.isLoading,
lastLoadedTimestamp: state.wallet.trading.lastLoadedTimestamp,
});

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;
Expand Down
27 changes: 8 additions & 19 deletions suite-common/trading/src/thunks/buyThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,39 @@ 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<BuyInfo>(`${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<FiatCurrencyCode, number>,
},
providerInfos: {},
supportedFiatCurrencies: new Set(),
supportedCryptoCurrencies: new Set(),
supportedFiatCurrencies: [],
supportedCryptoCurrencies: [],
};
}

const providerInfos: { [name: string]: BuyProviderInfo } = {};

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,
Expand Down

0 comments on commit 661d9e0

Please sign in to comment.