-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathuseFetchSupportedRoutes.ts
More file actions
127 lines (111 loc) · 3.14 KB
/
Copy pathuseFetchSupportedRoutes.ts
File metadata and controls
127 lines (111 loc) · 3.14 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
import { useEffect, useState } from 'react';
import config from 'config';
import { getTokenDetails } from 'telemetry';
import { maybeLogSdkError } from 'utils/errors';
import { ReadOnlyWallet } from 'utils/wallet/ReadOnlyWallet';
import type { Chain } from '@wormhole-foundation/sdk';
import type { Token } from 'config/tokens';
import type { WalletData } from 'store/wallet';
type HookReturn = {
supportedRoutes: string[];
isFetching: boolean;
};
interface UseFetchSupportedRoutesArgs {
fromChain: Chain | undefined;
toChain: Chain | undefined;
sourceToken: Token | undefined;
destToken: Token | undefined;
toNativeToken: number;
receivingWallet: WalletData | undefined;
}
const useFetchSupportedRoutes = ({
fromChain,
toChain,
sourceToken,
destToken,
toNativeToken,
receivingWallet,
}: UseFetchSupportedRoutesArgs): HookReturn => {
const [routes, setRoutes] = useState<string[]>([]);
const [isFetching, setIsFetching] = useState<boolean>(false);
useEffect(() => {
if (!fromChain || !toChain || !sourceToken || !destToken) {
setRoutes([]);
setIsFetching(false);
return;
}
let isActive = true;
const getSupportedRoutes = async () => {
setIsFetching(true);
let _routes: string[] = [];
await config.routes.forEach(async (name, route) => {
// Disable manual routes when the receiving wallet is a ReadOnlyWallet
// because the receiving wallet can't sign/complete the transaction
if (
!route.AUTOMATIC_DEPOSIT &&
receivingWallet?.name === ReadOnlyWallet.NAME
) {
return;
}
let supported = false;
try {
supported = await route.isRouteSupported(
sourceToken,
destToken,
fromChain,
toChain,
);
if (supported && config.isRouteSupportedHandler) {
supported = await config.isRouteSupportedHandler({
route: name,
fromChain,
toChain,
fromToken: getTokenDetails(sourceToken),
toToken: getTokenDetails(destToken),
});
}
} catch (e) {
maybeLogSdkError(
e,
`Error when checking route (${name}) is supported`,
);
}
if (supported) {
_routes.push(name);
}
});
// HAX - We don't want users to use the token bridge route when the TBTC route is available
// or they might receive frankenstein TBTC
if (_routes.includes('ManualTBTC')) {
_routes = _routes.filter(
(route) =>
![
'ManualTokenBridge',
'AutomaticTokenBridge',
'TokenBridgeExecutorRoute',
].includes(route),
);
}
if (isActive) {
setIsFetching(false);
setRoutes(_routes);
}
};
getSupportedRoutes();
return () => {
isActive = false;
};
}, [
sourceToken,
destToken,
fromChain,
toChain,
toNativeToken,
receivingWallet,
]);
return {
supportedRoutes: routes,
isFetching,
};
};
export default useFetchSupportedRoutes;