Skip to content

Commit 2a761d4

Browse files
adderpositivetomasklim
authored andcommitted
fix(trading): cleaning of modal account key
1 parent 41d8bdd commit 2a761d4

File tree

2 files changed

+32
-94
lines changed

2 files changed

+32
-94
lines changed

packages/suite/src/middlewares/wallet/__tests__/tradingMiddleware.test.ts

+24-64
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { invityAPI } from '@suite-common/trading';
2-
import { UI } from '@trezor/connect';
32

43
import { MODAL, ROUTER } from 'src/actions/suite/constants';
54
import { TRADING_COMMON } from 'src/actions/wallet/constants';
@@ -42,6 +41,18 @@ const TRADING_EXCHANGE_ROUTE = {
4241
url: '/accounts/coinmarket/exchange#/btc/0/normal',
4342
};
4443

44+
const DEFAULT_ROUTE = {
45+
loaded: false,
46+
url: '/',
47+
pathname: '/',
48+
app: 'unknown',
49+
route: undefined,
50+
params: undefined,
51+
settingsBackRoute: {
52+
name: 'suite-index',
53+
},
54+
} as RouterState;
55+
4556
type TradingState = ReturnType<typeof tradingReducer>;
4657
type SelectedAccountState = ReturnType<typeof selectedAccountReducer>;
4758
type SuiteState = ReturnType<typeof suiteReducer>;
@@ -54,7 +65,7 @@ interface Args {
5465
modal?: ModalState;
5566
}
5667

57-
const getInitialState = ({ trading, selectedAccount }: Args = {}) => ({
68+
const getInitialState = ({ trading, selectedAccount, router }: Args = {}) => ({
5869
wallet: {
5970
trading:
6071
trading ||
@@ -85,20 +96,7 @@ const getInitialState = ({ trading, selectedAccount }: Args = {}) => ({
8596
} as any,
8697
{ type: 'foo' } as any,
8798
),
88-
router: routerReducer(
89-
{
90-
loaded: false,
91-
url: '/',
92-
pathname: '/',
93-
app: 'unknown',
94-
route: undefined,
95-
params: undefined,
96-
settingsBackRoute: {
97-
name: 'suite-index',
98-
},
99-
} as RouterState,
100-
{} as Action,
101-
),
99+
router: router ?? routerReducer(DEFAULT_ROUTE, {} as Action),
102100
modal: modalReducer({ context: MODAL.CONTEXT_NONE }, {} as Action),
103101
});
104102

@@ -229,71 +227,30 @@ describe('tradingMiddleware', () => {
229227
expect(setInvityServersEnvironmentMock).toHaveBeenCalledTimes(0);
230228
});
231229

232-
it('Test of cleaning modalAccountKey property after receive modal is closed', () => {
230+
it('should clean modalAccountKey after leaving trading', () => {
233231
const store = initStore(
234232
getInitialState({
235233
trading: {
236234
...initialState,
237235
modalAccountKey: accounts[0].key,
238236
lastLoadedTimestamp: Date.now(),
239237
},
238+
router: routerReducer(TRADING_EXCHANGE_ROUTE as RouterState, {} as Action),
240239
}),
241240
);
242241

243-
// go to trading
242+
// go away from trading
244243
store.dispatch({
245244
type: ROUTER.LOCATION_CHANGE,
246-
payload: TRADING_EXCHANGE_ROUTE,
247-
});
248-
249-
// open modal
250-
store.dispatch({
251-
type: UI.REQUEST_BUTTON,
252245
payload: {
253-
context: MODAL.CONTEXT_DEVICE,
254-
code: 'ButtonRequest_Address',
255-
},
256-
});
257-
258-
// close modal
259-
store.dispatch({
260-
type: UI.CLOSE_UI_WINDOW,
261-
});
262-
263-
expect(store.getState().wallet.trading.modalAccountKey).toEqual(undefined);
264-
});
265-
266-
it('Test of cleaning modalAccountKey property after send modal is closed', () => {
267-
const store = initStore(
268-
getInitialState({
269-
trading: {
270-
...initialState,
271-
modalAccountKey: accounts[0].key,
272-
lastLoadedTimestamp: Date.now(),
246+
...DEFAULT_ROUTE,
247+
route: {
248+
...DEFAULT_ROUTE.route,
249+
name: 'suite-start',
273250
},
274-
}),
275-
);
276-
277-
// go to trading
278-
store.dispatch({
279-
type: ROUTER.LOCATION_CHANGE,
280-
payload: TRADING_EXCHANGE_ROUTE,
281-
});
282-
283-
// open modal
284-
store.dispatch({
285-
type: UI.REQUEST_BUTTON,
286-
payload: {
287-
context: MODAL.CONTEXT_DEVICE,
288-
code: 'ButtonRequest_SignTx',
289251
},
290252
});
291253

292-
// close modal
293-
store.dispatch({
294-
type: MODAL.CLOSE,
295-
});
296-
297254
expect(store.getState().wallet.trading.modalAccountKey).toEqual(undefined);
298255
});
299256

@@ -303,6 +260,9 @@ describe('tradingMiddleware', () => {
303260
trading: {
304261
...initialState,
305262
},
263+
router: {
264+
...getInitialState().router,
265+
},
306266
}),
307267
);
308268

packages/suite/src/middlewares/wallet/tradingMiddleware.ts

+8-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { MiddlewareAPI } from 'redux';
22

33
import { INVITY_API_RELOAD_DATA_AFTER_MS, invityAPI } from '@suite-common/trading';
4-
import { UI } from '@trezor/connect';
54

6-
import { MODAL, ROUTER } from 'src/actions/suite/constants';
5+
import { ROUTER } from 'src/actions/suite/constants';
76
import { TRADING_COMMON, TRADING_EXCHANGE, TRADING_SELL } from 'src/actions/wallet/constants';
87
import * as tradingCommonActions from 'src/actions/wallet/trading/tradingCommonActions';
98
import * as tradingBuyActions from 'src/actions/wallet/tradingBuyActions';
@@ -41,10 +40,9 @@ export const tradingMiddleware =
4140
(next: Dispatch) =>
4241
(action: Action): Action => {
4342
const state = api.getState();
44-
const { isLoading, lastLoadedTimestamp } = state.wallet.trading;
43+
const { isLoading, lastLoadedTimestamp, modalAccountKey } = state.wallet.trading;
4544
const { exchangeInfo } = state.wallet.trading.exchange;
4645
const { sellInfo } = state.wallet.trading.sell;
47-
const { router, modal } = state;
4846
const isRouteChange = action.type === ROUTER.LOCATION_CHANGE;
4947

5048
if (action.type === TRADING_COMMON.LOAD_DATA) {
@@ -115,43 +113,23 @@ export const tradingMiddleware =
115113
}
116114
}
117115

118-
const isTradingRoute = !!router.route?.name.includes('wallet-trading');
119-
const isDeviceContext = modal.context === MODAL.CONTEXT_DEVICE;
120-
const isUserContext = modal.context === MODAL.CONTEXT_USER;
121-
122-
const isCloseUiWindowEvent = action.type === UI.CLOSE_UI_WINDOW;
123-
const isReceiveModal =
124-
isCloseUiWindowEvent && isDeviceContext && modal.windowType === 'ButtonRequest_Address';
125-
126-
/*
127-
isCloseEvent
128-
- happens only one time when the whole flow of sending the transaction is closed
129-
- isCloseUiWindowEvent can not be used, it is called multiple times during flow because of
130-
changing context from CONTEXT_DEVICE (sign transaction) to CONTEXT_USER (summary)
131-
*/
132-
const isCloseEvent = action.type === MODAL.CLOSE;
133-
const isOtherFlow = isDeviceContext && modal.windowType === `ButtonRequest_Other`; // passphrase request, etc.
134-
const isSigningFlow = isDeviceContext && modal.windowType === 'ButtonRequest_SignTx';
135-
const isSummaryFlow = isUserContext && modal.payload.type === 'review-transaction';
136-
const isSendModal = isCloseEvent && (isOtherFlow || isSigningFlow || isSummaryFlow);
137-
138-
// clear modal account on close button requests
139-
// it is necessary to clear the state because it could affect the next modal state
140-
if (isTradingRoute && (isReceiveModal || isSendModal)) {
141-
api.dispatch(tradingCommonActions.setTradingModalAccountKey(undefined));
142-
}
143-
144116
next(action);
145117

146118
// get the new state after the action has been processed
147119
const newState = api.getState();
148120

149121
if (isRouteChange) {
150122
const routeName = newState.router.route?.name;
123+
const isTradingRoute = !!routeName?.includes('wallet-trading');
151124
const isBuy = routeName === 'wallet-trading-buy';
152125
const isSell = routeName === 'wallet-trading-sell';
153126
const isExchange = routeName === 'wallet-trading-exchange';
154127

128+
// it is necessary to clear the state because it could affect the other modal state
129+
if (!isTradingRoute && modalAccountKey) {
130+
api.dispatch(tradingCommonActions.setTradingModalAccountKey(undefined));
131+
}
132+
155133
if (isBuy) {
156134
api.dispatch(tradingCommonActions.setActiveSection('buy'));
157135
api.dispatch(tradingBuyActions.saveTransactionDetailId(undefined));

0 commit comments

Comments
 (0)