Skip to content

Commit

Permalink
refactor(trading): modalAccount replaced only by saved key of an account
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Feb 3, 2025
1 parent a710615 commit f154f61
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const SAVE_TRADE = '@trading-common/save_trade';
export const LOAD_DATA = '@trading-common/load_data';
export const SET_LOADING = '@trading-common/set_loading';
export const SET_MODAL_CRYPTO_CURRENCY = '@trading-common/set_modal_crypto_currency';
export const SET_MODAL_ACCOUNT = '@trading-common/set_modal_account';
export const SET_MODAL_ACCOUNT_KEY = '@trading-common/set_modal_account_key';
export const SET_TRADING_ACTIVE_SECTION = '@trading-common/set_trading_active_section';
export const SET_TRADING_FROM_PREFILLED_CRYPTO_ID =
'@trading-common/set_trading_from_prefilled_crypto_id';
20 changes: 11 additions & 9 deletions packages/suite/src/actions/wallet/trading/tradingCommonActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
selectSelectedDevice,
toggleRememberDevice,
} from '@suite-common/wallet-core';
import { AddressDisplayOptions, Output } from '@suite-common/wallet-types/src';
import { AccountKey, AddressDisplayOptions, Output } from '@suite-common/wallet-types/src';
import {
amountToSmallestUnit,
formatAmount,
Expand Down Expand Up @@ -45,8 +45,8 @@ export type TradingCommonAction =
modalCryptoId: CryptoId | undefined;
}
| {
type: typeof TRADING_COMMON.SET_MODAL_ACCOUNT;
modalAccount: Account | undefined;
type: typeof TRADING_COMMON.SET_MODAL_ACCOUNT_KEY;
modalAccountKey: AccountKey | undefined;
}
| {
type: typeof TRADING_COMMON.SET_TRADING_ACTIVE_SECTION;
Expand All @@ -63,13 +63,15 @@ type FormState = {
};

/**
* Set modalAccount to retrieve the correct account in modals.
* Used in ConfirmAddressModal and TransactionReviewModalContent through selectAccountIncludingChosenInTrading.
* Set modalAccountKey to retrieve the correct account key in modals.
* Used in ConfirmAddressModal and TransactionReviewModalContent through selectAccountKeyIncludingChosenInTrading.
* Unset in middleware after modal is closed.
*/
export const setTradingModalAccount = (modalAccount: Account | undefined): TradingCommonAction => ({
type: TRADING_COMMON.SET_MODAL_ACCOUNT,
modalAccount,
export const setTradingModalAccountKey = (
modalAccountKey: AccountKey | undefined,
): TradingCommonAction => ({
type: TRADING_COMMON.SET_MODAL_ACCOUNT_KEY,
modalAccountKey,
});

export const setActiveSection = (activeSection: TradingType): TradingCommonAction => ({
Expand All @@ -92,7 +94,7 @@ export const verifyAddress =
path = path ?? accountAddress.path;
if (!path || !address) return;

dispatch(setTradingModalAccount(account));
dispatch(setTradingModalAccountKey(account.key));

const addressDisplayType = selectAddressDisplayType(getState());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ export const useTradingExchangeForm = ({
};

const sendTransaction = async () => {
dispatch(tradingCommonActions.setTradingModalAccount(account));
dispatch(tradingCommonActions.setTradingModalAccountKey(account.key));

if (selectedQuote?.isDex) {
sendDexTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export const useTradingSellForm = ({
};

const sendTransaction = async () => {
dispatch(tradingCommonActions.setTradingModalAccount(account));
dispatch(tradingCommonActions.setTradingModalAccountKey(account.key));

const selectedTrade = trade?.data || selectedQuote;
// destinationAddress may be set by useTradingWatchTrade hook to the trade object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,12 @@ describe('tradingMiddleware', () => {
expect(setInvityServersEnvironmentMock).toHaveBeenCalledTimes(0);
});

it('Test of cleaning modalAccount property after receive modal is closed', () => {
it('Test of cleaning modalAccountKey property after receive modal is closed', () => {
const store = initStore(
getInitialState({
trading: {
...initialState,
modalAccount: accounts[0],
modalAccountKey: accounts[0].key,
lastLoadedTimestamp: Date.now(),
},
}),
Expand All @@ -260,15 +260,15 @@ describe('tradingMiddleware', () => {
type: UI.CLOSE_UI_WINDOW,
});

expect(store.getState().wallet.trading.modalAccount).toEqual(undefined);
expect(store.getState().wallet.trading.modalAccountKey).toEqual(undefined);
});

it('Test of cleaning modalAccount property after send modal is closed', () => {
it('Test of cleaning modalAccountKey property after send modal is closed', () => {
const store = initStore(
getInitialState({
trading: {
...initialState,
modalAccount: accounts[0],
modalAccountKey: accounts[0].key,
lastLoadedTimestamp: Date.now(),
},
}),
Expand All @@ -294,7 +294,7 @@ describe('tradingMiddleware', () => {
type: MODAL.CLOSE,
});

expect(store.getState().wallet.trading.modalAccount).toEqual(undefined);
expect(store.getState().wallet.trading.modalAccountKey).toEqual(undefined);
});

it('Test of setting activeSection after changing route', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/middlewares/wallet/tradingMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const tradingMiddleware =
// clear modal account on close button requests
// it is necessary to clear the state because it could affect the next modal state
if (isTradingRoute && (isReceiveModal || isSendModal)) {
api.dispatch(tradingCommonActions.setTradingModalAccount(undefined));
api.dispatch(tradingCommonActions.setTradingModalAccountKey(undefined));
}

next(action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ describe('settings reducer', () => {
it('TRADING_COMMON.SET_MODAL_ACCOUNT', () => {
expect(
tradingReducer(undefined, {
type: TRADING_COMMON.SET_MODAL_ACCOUNT,
modalAccount: accounts[0],
type: TRADING_COMMON.SET_MODAL_ACCOUNT_KEY,
modalAccountKey: accounts[0].key,
}),
).toEqual({
...initialState,
modalAccount: accounts[0],
modalAccountKey: accounts[0].key,
});

expect(
tradingReducer(undefined, {
type: TRADING_COMMON.SET_MODAL_ACCOUNT,
modalAccount: undefined,
type: TRADING_COMMON.SET_MODAL_ACCOUNT_KEY,
modalAccountKey: undefined,
}),
).toEqual({
...initialState,
modalAccount: undefined,
modalAccountKey: undefined,
});
});

Expand Down
10 changes: 5 additions & 5 deletions packages/suite/src/reducers/wallet/selectedAccountReducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { accountsActions } from '@suite-common/wallet-core';
import { AccountsRootState, accountsActions, selectAccountByKey } from '@suite-common/wallet-core';
import type { SelectedAccountStatus } from '@suite-common/wallet-types';

import { State as TradingState } from 'src/reducers/wallet/tradingReducer';
Expand All @@ -10,7 +10,7 @@ export type SelectedAccountRootState = {
wallet: {
selectedAccount: SelectedAccountStatus;
};
};
} & AccountsRootState;

export type SelectedAccountRootStateWithTrading = SelectedAccountRootState & {
wallet: {
Expand Down Expand Up @@ -54,10 +54,10 @@ export const selectIsSelectedAccountLoaded = (state: SelectedAccountRootState) =
export const selectAccountIncludingChosenInTrading = (
state: SelectedAccountRootStateWithTrading,
) => {
const { modalAccount } = state.wallet.trading;
const { modalAccountKey } = state.wallet.trading;

if (modalAccount) {
return modalAccount;
if (modalAccountKey) {
return selectAccountByKey(state, modalAccountKey) ?? undefined;
}

const selectedAccount = selectSelectedAccount(state);
Expand Down
8 changes: 4 additions & 4 deletions packages/suite/src/reducers/wallet/tradingReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export interface State {
composedTransactionInfo: ComposedTransactionInfo;
trades: Trade[];
modalCryptoId: CryptoId | undefined;
modalAccount: Account | undefined;
modalAccountKey: AccountKey | undefined;
isLoading: boolean;
lastLoadedTimestamp: number;
activeSection?: TradingType;
Expand Down Expand Up @@ -144,7 +144,7 @@ export const initialState: State = {
composedTransactionInfo: {},
trades: [],
isLoading: false,
modalAccount: undefined,
modalAccountKey: undefined,
modalCryptoId: undefined,
lastLoadedTimestamp: 0,
activeSection: 'buy',
Expand Down Expand Up @@ -265,8 +265,8 @@ export const tradingReducer = (
draft.isLoading = action.isLoading;
draft.lastLoadedTimestamp = action.lastLoadedTimestamp;
break;
case TRADING_COMMON.SET_MODAL_ACCOUNT:
draft.modalAccount = action.modalAccount;
case TRADING_COMMON.SET_MODAL_ACCOUNT_KEY:
draft.modalAccountKey = action.modalAccountKey;
break;
case TRADING_COMMON.SET_MODAL_CRYPTO_CURRENCY:
draft.modalCryptoId = action.modalCryptoId;
Expand Down

0 comments on commit f154f61

Please sign in to comment.