-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathindex.ts
More file actions
154 lines (140 loc) · 4.78 KB
/
index.ts
File metadata and controls
154 lines (140 loc) · 4.78 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { useCallback, useMemo } from 'react';
import Engine from '../../../../../core/Engine';
import {
formatAddressToCaipReference,
type GenericQuoteRequest,
} from '@metamask/bridge-controller';
import { useSelector } from 'react-redux';
import {
selectSourceAmount,
selectSourceToken,
selectDestToken,
selectSelectedDestChainId,
selectSlippage,
selectDestAddress,
} from '../../../../../core/redux/slices/bridge';
import {
selectGasIncludedQuoteParams,
selectSourceWalletAddress,
} from '../../../../../selectors/bridge';
import { getDecimalChainId } from '../../../../../util/networks';
import { calcTokenValue } from '../../../../../util/transactions';
import { debounce } from 'lodash';
import { useUnifiedSwapBridgeContext } from '../useUnifiedSwapBridgeContext';
import useIsInsufficientBalance from '../useInsufficientBalance';
import { useLatestBalance } from '../useLatestBalance';
import { BigNumber } from 'ethers';
import { useInsufficientNativeReserveError } from '../useInsufficientNativeReserveError';
export const DEBOUNCE_WAIT = 300;
interface UseBridgeQuoteRequestOptions {
latestSourceAtomicBalance?: BigNumber;
}
/**
* Hook for handling bridge quote request updates
* @returns {Function} A debounced function to update quote parameters
*/
export const useBridgeQuoteRequest = (
options: UseBridgeQuoteRequestOptions = {},
) => {
const sourceAmount = useSelector(selectSourceAmount);
const sourceToken = useSelector(selectSourceToken);
const destToken = useSelector(selectDestToken);
const destChainId = useSelector(selectSelectedDestChainId);
const slippage = useSelector(selectSlippage);
const walletAddress = useSelector(selectSourceWalletAddress);
const destAddress = useSelector(selectDestAddress);
const context = useUnifiedSwapBridgeContext();
const { latestSourceAtomicBalance } = options;
const hasLatestSourceBalanceOverride = 'latestSourceAtomicBalance' in options;
const latestSourceBalance = useLatestBalance(
hasLatestSourceBalanceOverride
? {}
: {
address: sourceToken?.address,
decimals: sourceToken?.decimals,
chainId: sourceToken?.chainId,
balance: sourceToken?.balance,
},
);
const sourceAtomicBalance = hasLatestSourceBalanceOverride
? latestSourceAtomicBalance
: latestSourceBalance?.atomicBalance;
// Use simple balance check (ignoring gas fees) for quote requests to avoid circular dependencies.
// The full balance check with gas fees is used separately within the BridgeView to block user from executing
// the swap in insufficient balance.
// This prevents the infinite loop: quote request → gas data changes → insufficientBal changes → new quote request
const insufficientBalance = useIsInsufficientBalance({
amount: sourceAmount,
token: sourceToken,
latestAtomicBalance: sourceAtomicBalance,
ignoreGasFees: true,
});
const { gasIncluded, gasIncluded7702 } = useSelector(
selectGasIncludedQuoteParams,
);
const insufficientNativeReserveError = useInsufficientNativeReserveError({
amount: sourceAmount,
token: sourceToken,
latestAtomicBalance: sourceAtomicBalance,
walletAddress,
});
const insufficientBal =
insufficientBalance || Boolean(insufficientNativeReserveError);
/**
* Updates quote parameters in the bridge controller
*/
const updateQuoteParams = useCallback(async () => {
if (
!sourceToken ||
!destToken ||
sourceAmount === undefined ||
!destChainId ||
!walletAddress
) {
return;
}
const normalizedSourceAmount =
sourceAmount && sourceToken?.decimals
? calcTokenValue(
sourceAmount === '.' ? '0' : sourceAmount || '0',
sourceToken.decimals,
).toFixed(0)
: '0';
const params: GenericQuoteRequest = {
srcChainId: getDecimalChainId(sourceToken.chainId),
srcTokenAddress: formatAddressToCaipReference(sourceToken.address),
destChainId: getDecimalChainId(destChainId),
destTokenAddress: formatAddressToCaipReference(destToken.address),
srcTokenAmount: normalizedSourceAmount,
slippage: slippage ? Number(slippage) : undefined,
walletAddress,
destWalletAddress: destAddress ?? walletAddress,
gasIncluded,
gasIncluded7702,
insufficientBal,
};
await Engine.context.BridgeController.updateBridgeQuoteRequestParams(
params,
context,
0,
1,
);
}, [
sourceToken,
destToken,
sourceAmount,
destChainId,
slippage,
walletAddress,
destAddress,
context,
gasIncluded,
gasIncluded7702,
insufficientBal,
]);
// Create a stable debounced function that persists across renders
return useMemo(
() => debounce(updateQuoteParams, DEBOUNCE_WAIT),
[updateQuoteParams],
);
};