forked from wormhole-foundation/wormhole-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseFetchSupportedRoutes.ts
121 lines (101 loc) · 2.95 KB
/
useFetchSupportedRoutes.ts
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
import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import type { RootState } from 'store';
import config from 'config';
import { getTokenDetails } from 'telemetry';
import { useGetTokens } from './useGetTokens';
import { maybeLogSdkError } from 'utils/errors';
import { ReadOnlyWallet } from 'utils/wallet/ReadOnlyWallet';
type HookReturn = {
supportedRoutes: string[];
isFetching: boolean;
};
const useFetchSupportedRoutes = (): HookReturn => {
const [routes, setRoutes] = useState<string[]>([]);
const [isFetching, setIsFetching] = useState<boolean>(false);
const { fromChain, toChain, amount } = useSelector(
(state: RootState) => state.transferInput,
);
const { sourceToken, destToken } = useGetTokens();
const { toNativeToken } = useSelector((state: RootState) => state.relay);
const receivingWallet = useSelector(
(state: RootState) => state.wallet.receiving,
);
useEffect(() => {
if (!fromChain || !toChain || !sourceToken || !destToken) {
setRoutes([]);
setIsFetching(false);
return;
}
let isActive = true;
const getSupportedRoutes = async () => {
setIsFetching(true);
const _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`,
);
}
// HAX - Enable Mayan routes (except SHUTTLE) for all assets
// TODO token refactor
if (
route.rc.name.includes('Mayan') &&
route.rc.name !== 'MayanSwapSHUTTLE'
) {
supported = true;
}
if (supported) {
_routes.push(name);
}
});
if (isActive) {
setIsFetching(false);
setRoutes(_routes);
}
};
getSupportedRoutes();
return () => {
isActive = false;
};
}, [
sourceToken,
destToken,
amount,
fromChain,
toChain,
toNativeToken,
receivingWallet,
]);
return {
supportedRoutes: routes,
isFetching,
};
};
export default useFetchSupportedRoutes;