-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathusePredictDeposit.ts
More file actions
113 lines (103 loc) · 3.81 KB
/
usePredictDeposit.ts
File metadata and controls
113 lines (103 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { useNavigation } from '@react-navigation/native';
import { useCallback, useContext } from 'react';
import { useSelector } from 'react-redux';
import { ToastContext } from '../../../../component-library/components/Toast';
import Logger from '../../../../util/Logger';
import { useAppThemeFromContext } from '../../../../util/theme';
import { ConfirmationLoader } from '../../../Views/confirmations/components/confirm/confirm-component';
import { useConfirmNavigation } from '../../../Views/confirmations/hooks/useConfirmNavigation';
import Routes from '../../../../constants/navigation/Routes';
import { PREDICT_CONSTANTS } from '../constants/errors';
import { selectPredictPendingDepositByAddress } from '../selectors/predictController';
import {
createDepositErrorToast,
ensureError,
} from '../utils/predictErrorHandler';
import { usePredictTrading } from './usePredictTrading';
import { getEvmAccountFromSelectedAccountGroup } from '../utils/accounts';
import { selectSelectedAccountGroupId } from '../../../../selectors/multichainAccounts/accountTreeController';
import { PlaceOrderParams } from '../types';
interface PredictDepositAnalyticsParams {
amountUsd?: number;
analyticsProperties?: PlaceOrderParams['analyticsProperties'];
}
export const usePredictDeposit = () => {
const { navigateToConfirmation } = useConfirmNavigation();
const theme = useAppThemeFromContext();
const { toastRef } = useContext(ToastContext);
const navigation = useNavigation();
// Subscribe to account group changes so the hook re-renders when the user switches accounts
useSelector(selectSelectedAccountGroupId);
const evmAccount = getEvmAccountFromSelectedAccountGroup();
const selectedInternalAccountAddress = evmAccount?.address ?? '0x0';
const { deposit: depositWithConfirmation } = usePredictTrading();
const depositBatchId = useSelector(
selectPredictPendingDepositByAddress({
address: selectedInternalAccountAddress,
}),
);
const deposit = useCallback(
async (params?: PredictDepositAnalyticsParams) => {
try {
navigateToConfirmation({
loader: ConfirmationLoader.CustomAmount,
stack: Routes.PREDICT.ROOT,
});
depositWithConfirmation({}).catch((err) => {
console.error('Failed to initialize deposit:', err);
// Log error with deposit initialization context
Logger.error(ensureError(err), {
tags: {
feature: PREDICT_CONSTANTS.FEATURE_NAME,
component: 'usePredictDeposit',
},
context: {
name: 'usePredictDeposit',
data: {
method: 'deposit',
action: 'deposit_initialization',
operation: 'financial_operations',
},
},
});
navigation.goBack();
toastRef?.current?.showToast(
createDepositErrorToast(theme, () => deposit(params)),
);
});
} catch (err) {
console.error('Failed to proceed with deposit:', err);
navigation.goBack();
toastRef?.current?.showToast(
createDepositErrorToast(theme, () => deposit(params)),
);
// Log error with deposit navigation context
Logger.error(ensureError(err), {
tags: {
feature: PREDICT_CONSTANTS.FEATURE_NAME,
component: 'usePredictDeposit',
},
context: {
name: 'usePredictDeposit',
data: {
method: 'deposit',
action: 'deposit_navigation',
operation: 'financial_operations',
},
},
});
}
},
[
depositWithConfirmation,
navigateToConfirmation,
navigation,
theme,
toastRef,
],
);
return {
deposit,
isDepositPending: !!depositBatchId,
};
};