Skip to content

Commit

Permalink
fix(trading): stuck account
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Feb 7, 2025
1 parent cecf169 commit 3a8d2da
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);

Expand All @@ -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];
};
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const useTradingExchangeForm = ({
const { cryptoIdToCoinSymbol } = useTradingInfo();
const isPreviousRouteFromTradeSection = useTradingPreviousRoute(type);
const [accountKey, setAccountKey] = useTradingAccountKey({
type,
tradingAccountKey,
selectedAccount,
shouldUseTradingAccountKey: isPreviousRouteFromTradeSection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const useTradingSellForm = ({
const { cryptoIdToCoinSymbol } = useTradingInfo();
const isPreviousRouteFromTradeSection = useTradingPreviousRoute(type);
const [accountKey, setAccountKey] = useTradingAccountKey({
type,
tradingAccountKey,
selectedAccount,
shouldUseTradingAccountKey: isPreviousRouteFromTradeSection,
Expand Down
19 changes: 2 additions & 17 deletions packages/suite/src/middlewares/wallet/tradingMiddleware.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3a8d2da

Please sign in to comment.