-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathuseLsChangeNetwork.ts
48 lines (38 loc) · 1.53 KB
/
useLsChangeNetwork.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
import useSwitchNetwork from '@webb-tools/tangle-shared-ui/hooks/useSwitchNetwork';
import { useWebbUI } from '@webb-tools/webb-ui-components';
import { useCallback } from 'react';
import { LsNetworkId } from '../../../constants/liquidStaking/types';
import { NETWORK_FEATURE_MAP } from '../../../constants/networks';
import { useLsStore } from '../../../data/liquidStaking/useLsStore';
import { NetworkFeature } from '../../../types';
import getLsTangleNetwork from '../../../utils/liquidStaking/getLsTangleNetwork';
const useLsChangeNetwork = () => {
const { lsNetworkId, setSelectedNetworkId } = useLsStore();
const { switchNetwork } = useSwitchNetwork();
const { notificationApi } = useWebbUI();
const tryChangeNetwork = useCallback(
async (newLsNetworkId: LsNetworkId) => {
// No need to change network if it's already selected.
if (lsNetworkId === newLsNetworkId) {
return;
}
const tangleNetwork = getLsTangleNetwork(newLsNetworkId);
const supportsLiquidStaking = NETWORK_FEATURE_MAP[
tangleNetwork.id
].includes(NetworkFeature.LsPools);
if (!supportsLiquidStaking) {
notificationApi({
message: 'Network does not support liquid staking yet',
variant: 'error',
});
return;
}
if (await switchNetwork(tangleNetwork, false)) {
setSelectedNetworkId(newLsNetworkId);
}
},
[notificationApi, lsNetworkId, setSelectedNetworkId, switchNetwork],
);
return tryChangeNetwork;
};
export default useLsChangeNetwork;