diff --git a/packages/suite/src/hooks/wallet/trading/form/common/useTradingAccountKey.ts b/packages/suite/src/hooks/wallet/trading/form/common/useTradingAccountKey.ts index a1a9d3b0644..8ccee3c7f4f 100644 --- a/packages/suite/src/hooks/wallet/trading/form/common/useTradingAccountKey.ts +++ b/packages/suite/src/hooks/wallet/trading/form/common/useTradingAccountKey.ts @@ -1,14 +1,18 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { mapTestnetSymbol } from '@suite-common/trading'; import { selectAccounts, selectSelectedDevice } from '@suite-common/wallet-core'; import { AccountKey, SelectedAccountLoaded } from '@suite-common/wallet-types'; import { isTestnet } from '@suite-common/wallet-utils'; -import { useSelector } from 'src/hooks/suite'; +import { setTradingExchangeAccountKey } from 'src/actions/wallet/tradingExchangeActions'; +import { setTradingSellAccountKey } from 'src/actions/wallet/tradingSellActions'; +import { useDispatch, useSelector } from 'src/hooks/suite'; +import { TradingTradeSellExchangeType } from 'src/types/trading/trading'; import { tradingGetSortedAccounts } from 'src/utils/wallet/trading/tradingUtils'; interface TradingUseAccountKeyProps { + type: TradingTradeSellExchangeType; tradingAccountKey: AccountKey | undefined; selectedAccount: SelectedAccountLoaded; shouldUseTradingAccountKey?: boolean; @@ -20,10 +24,12 @@ interface TradingUseAccountKeyProps { * - selectedAccount is used as initial state if user entries from different page than trade */ export const useTradingAccountKey = ({ + type, tradingAccountKey, selectedAccount, shouldUseTradingAccountKey, }: TradingUseAccountKeyProps): [AccountKey, (state: AccountKey) => void] => { + const dispatch = useDispatch(); const accounts = useSelector(selectAccounts); const device = useSelector(selectSelectedDevice); @@ -50,5 +56,16 @@ export const useTradingAccountKey = ({ return selectedAccount.account.key; }); + // update accountKey in store + useEffect(() => { + if (!shouldUseTradingAccountKey) { + if (type === 'sell') { + dispatch(setTradingSellAccountKey(accountKey)); + } else { + dispatch(setTradingExchangeAccountKey(accountKey)); + } + } + }, [accountKey, dispatch, shouldUseTradingAccountKey, type]); + return [accountKey, setAccountKey]; }; diff --git a/packages/suite/src/hooks/wallet/trading/form/useTradingExchangeForm.ts b/packages/suite/src/hooks/wallet/trading/form/useTradingExchangeForm.ts index 8c880f96893..91cbb02c716 100644 --- a/packages/suite/src/hooks/wallet/trading/form/useTradingExchangeForm.ts +++ b/packages/suite/src/hooks/wallet/trading/form/useTradingExchangeForm.ts @@ -82,6 +82,7 @@ export const useTradingExchangeForm = ({ const { cryptoIdToCoinSymbol } = useTradingInfo(); const isPreviousRouteFromTradeSection = useTradingPreviousRoute(type); const [accountKey, setAccountKey] = useTradingAccountKey({ + type, tradingAccountKey, selectedAccount, shouldUseTradingAccountKey: isPreviousRouteFromTradeSection, diff --git a/packages/suite/src/hooks/wallet/trading/form/useTradingSellForm.ts b/packages/suite/src/hooks/wallet/trading/form/useTradingSellForm.ts index d3e625b6fbf..f2723534bd9 100644 --- a/packages/suite/src/hooks/wallet/trading/form/useTradingSellForm.ts +++ b/packages/suite/src/hooks/wallet/trading/form/useTradingSellForm.ts @@ -80,6 +80,7 @@ export const useTradingSellForm = ({ const { cryptoIdToCoinSymbol } = useTradingInfo(); const isPreviousRouteFromTradeSection = useTradingPreviousRoute(type); const [accountKey, setAccountKey] = useTradingAccountKey({ + type, tradingAccountKey, selectedAccount, shouldUseTradingAccountKey: isPreviousRouteFromTradeSection, diff --git a/packages/suite/src/middlewares/wallet/tradingMiddleware.ts b/packages/suite/src/middlewares/wallet/tradingMiddleware.ts index 94d656cf54b..0a06877216a 100644 --- a/packages/suite/src/middlewares/wallet/tradingMiddleware.ts +++ b/packages/suite/src/middlewares/wallet/tradingMiddleware.ts @@ -1,7 +1,6 @@ import { MiddlewareAPI } from 'redux'; import { invityAPI } from '@suite-common/trading'; -import { ACCOUNTS_MODULE_PREFIX } from '@suite-common/wallet-core'; import { UI } from '@trezor/connect'; import { MODAL, ROUTER } from 'src/actions/suite/constants'; @@ -44,12 +43,10 @@ export const tradingMiddleware = (action: Action): Action => { const state = api.getState(); const { isLoading, lastLoadedTimestamp } = state.wallet.trading; - const { exchangeInfo, tradingAccountKey: tradingExchangeAccountKey } = - state.wallet.trading.exchange; - const { sellInfo, tradingAccountKey: tradingSellAccountKey } = state.wallet.trading.sell; + const { exchangeInfo } = state.wallet.trading.exchange; + const { sellInfo } = state.wallet.trading.sell; const { router, modal } = state; const isRouteChange = action.type === ROUTER.LOCATION_CHANGE; - const isAccountUpdated = action.type === `${ACCOUNTS_MODULE_PREFIX}/updateSelectedAccount`; if (action.type === TRADING_COMMON.LOAD_DATA) { const account = getAccountAccordingToRoute(state); @@ -145,18 +142,6 @@ export const tradingMiddleware = api.dispatch(tradingCommonActions.setTradingModalAccountKey(undefined)); } - // clear the account key in the Sell and Swap section when the route is not trading - if ( - isAccountUpdated && - !isTradingRoute && - (tradingExchangeAccountKey || tradingSellAccountKey) - ) { - api.dispatch(tradingSellActions.setTradingSellAccountKey(action.payload.account?.key)); - api.dispatch( - tradingExchangeActions.setTradingExchangeAccountKey(action.payload.account?.key), - ); - } - next(action); // get the new state after the action has been processed