Skip to content

Commit cf40af1

Browse files
committed
refactor(trading): move getRandomAccountDescriptor to suite-common
Extract getRandomAccountDescriptor into @suite-common/trading as the single source and have both web (TRADING_FALLBACK_API_KEY) and native consume it, replacing the duplicated native-only helper.
1 parent ba6e17b commit cf40af1

12 files changed

Lines changed: 35 additions & 27 deletions

File tree

suite-common/trading/src/constants.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { type SellFiatFlowType } from 'invity-api';
22

33
import { type NetworkSymbol, type NetworkType } from '@suite-common/wallet-config';
4-
import { getWeakRandomId } from '@trezor/utils';
4+
5+
import { getRandomAccountDescriptor } from './utils/apiKeyUtils';
56

67
export const TRADING_PREFIX = '@trading';
78
export const TRADING_EXTENDED_PREFIX = `${TRADING_PREFIX}-extended`;
@@ -74,7 +75,7 @@ export const SLIPPAGE_MIN = '0.01';
7475
export const SLIPPAGE_MAX = '50';
7576
export const SLIPPAGE_PRESETS = ['0.1', '0.5', '1', '3'];
7677
export const INVITY_API_RELOAD_DATA_AFTER_MS = 10 * 60 * 1000; // 10 minutes
77-
export const TRADING_FALLBACK_API_KEY = getWeakRandomId(20);
78+
export const TRADING_FALLBACK_API_KEY = getRandomAccountDescriptor();
7879
export const INVITY_API_RELOAD_QUOTES_AFTER_SECONDS = 30;
7980

8081
export const CRYPTO_PLATFORM_SEPARATOR = '--';

suite-common/trading/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export * from './hooks/useTradingDetail';
3636
export type * from './types/tradingDetail';
3737
export type * from './types';
3838
export * from './utils';
39+
export * from './utils/apiKeyUtils';
3940
export * from './utils/tradingAccountUtils';
4041
export * from './utils/buy/buyUtils';
4142
export * from './utils/receiveAccountUtils';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getRandomAccountDescriptor } from '../apiKeyUtils';
2+
3+
describe('getRandomAccountDescriptor', () => {
4+
it('should return 20 characters', () => {
5+
expect(getRandomAccountDescriptor().length).toBe(20);
6+
});
7+
8+
it('should return different string on every call', () => {
9+
expect(getRandomAccountDescriptor()).not.toBe(getRandomAccountDescriptor());
10+
});
11+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { getWeakRandomId } from '@trezor/utils';
2+
3+
export const getRandomAccountDescriptor = () => getWeakRandomId(20);

suite-native/module-trading/src/hooks/buy/__tests__/useBuyData.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import { tradingSlice } from '@suite-native/trading-state';
1616

1717
import { useBuyData } from '../useBuyData';
1818

19-
jest.mock('@suite-native/trading-quote-utils', () => ({
20-
...jest.requireActual('@suite-native/trading-quote-utils'),
19+
jest.mock('@suite-common/trading', () => ({
20+
...jest.requireActual('@suite-common/trading'),
2121
getRandomAccountDescriptor: () => 'random_string',
2222
}));
2323

suite-native/module-trading/src/hooks/buy/useBuyData.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { useEffect } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
33

4-
import { selectTradingBuyLoadingTimestampAndStatus, tradingThunks } from '@suite-common/trading';
5-
import { getRandomAccountDescriptor } from '@suite-native/trading-quote-utils';
4+
import {
5+
getRandomAccountDescriptor,
6+
selectTradingBuyLoadingTimestampAndStatus,
7+
tradingThunks,
8+
} from '@suite-common/trading';
69
import { selectBuySelectedReceiveAccount } from '@suite-native/trading-state';
710

811
export const useBuyData = (reloadRequestOrdinal: number) => {

suite-native/module-trading/src/hooks/exchange/__tests__/useExchangeData.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { getBtcAccount, getInitializedTradingState } from '@suite-native/trading
66
import { createTradingLightStore } from '../../../__tests__/tradingTestUtils';
77
import { useExchangeData } from '../useExchangeData';
88

9-
jest.mock('@suite-native/trading-quote-utils', () => ({
10-
...jest.requireActual('@suite-native/trading-quote-utils'),
9+
jest.mock('@suite-common/trading', () => ({
10+
...jest.requireActual('@suite-common/trading'),
1111
getRandomAccountDescriptor: () => 'random_string',
1212
}));
1313

suite-native/module-trading/src/hooks/exchange/useExchangeData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { useEffect } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
33

44
import {
5+
getRandomAccountDescriptor,
56
selectTradingExchangeLoadingTimestampAndStatus,
67
tradingThunks,
78
} from '@suite-common/trading';
8-
import { getRandomAccountDescriptor } from '@suite-native/trading-quote-utils';
99
import { selectExchangeSelectedSendAccount } from '@suite-native/trading-state';
1010

1111
export const useExchangeData = (reloadRequestOrdinal: number) => {

suite-native/module-trading/src/hooks/sell/__tests__/useSellData.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { getBtcAccount, getInitializedTradingState } from '@suite-native/trading
66
import { createTradingLightStore } from '../../../__tests__/tradingTestUtils';
77
import { useSellData } from '../useSellData';
88

9-
jest.mock('@suite-native/trading-quote-utils', () => ({
10-
...jest.requireActual('@suite-native/trading-quote-utils'),
9+
jest.mock('@suite-common/trading', () => ({
10+
...jest.requireActual('@suite-common/trading'),
1111
getRandomAccountDescriptor: () => 'random_string',
1212
}));
1313

suite-native/module-trading/src/hooks/sell/useSellData.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { useEffect } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
33

4-
import { selectTradingSellLoadingTimestampAndStatus, tradingThunks } from '@suite-common/trading';
5-
import { getRandomAccountDescriptor } from '@suite-native/trading-quote-utils';
4+
import {
5+
getRandomAccountDescriptor,
6+
selectTradingSellLoadingTimestampAndStatus,
7+
tradingThunks,
8+
} from '@suite-common/trading';
69
import { selectSellSelectedSendAccount } from '@suite-native/trading-state';
710

811
export const useSellData = (reloadRequestOrdinal: number) => {

0 commit comments

Comments
 (0)