-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathindex.ts
More file actions
55 lines (48 loc) · 1.86 KB
/
index.ts
File metadata and controls
55 lines (48 loc) · 1.86 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
import { useEffect, useMemo } from 'react';
import { useDispatch } from 'react-redux';
import { Hex, CaipChainId } from '@metamask/utils';
import { setIsGasIncluded7702Supported } from '../../../../../core/redux/slices/bridge';
import { useAsyncResult } from '../../../../hooks/useAsyncResult';
import { isRelaySupported } from '../../../../../util/transactions/transaction-relay';
import {
formatChainIdToHex,
isNonEvmChainId,
} from '@metamask/bridge-controller';
import { useIsHardwareWalletForBridge } from '../useIsHardwareWalletForBridge';
/**
* Hook that determines if 7702 gasless support is available for bridge/swap.
* Should be used at the page level (e.g., BridgeView) to avoid repeated calculations.
*
* Requirements for 7702:
* - Relay must be supported (for 7702 delegation)
* - Source wallet must not be a hardware wallet
*
* @param chainId - The chain ID to check (can be Hex, CAIP, or other format) - only EVM chains are supported
*/
export const useIsGasIncluded7702Supported = (
chainId?: Hex | CaipChainId | string,
) => {
const dispatch = useDispatch();
// Only check gasIncluded for EVM chains
const evmChainId = useMemo(() => {
if (!chainId || isNonEvmChainId(chainId)) {
return undefined;
}
return formatChainIdToHex(chainId);
}, [chainId]);
// Fetch relay support
const { value: isRelaySupportedForChain } = useAsyncResult(async () => {
if (!evmChainId) {
return false;
}
return isRelaySupported(evmChainId as Hex);
}, [evmChainId]);
const isHardwareWallet = useIsHardwareWalletForBridge();
// 7702 is available when ALL conditions are met
const isGasIncluded7702Supported = Boolean(
evmChainId && !!isRelaySupportedForChain && !isHardwareWallet,
);
useEffect(() => {
dispatch(setIsGasIncluded7702Supported(isGasIncluded7702Supported));
}, [isGasIncluded7702Supported, dispatch]);
};