-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuse-swap-canister-liquidity-position.ts
66 lines (61 loc) · 1.98 KB
/
use-swap-canister-liquidity-position.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
import {
liquidityPositionActions,
selectLiquidityPositionState,
useAppDispatch,
useAppSelector,
} from '@/store';
import { Pair } from '@psychedelic/sonic-js';
import { useCallback } from 'react';
import { useSwapCanisterController } from '.';
/**
* Example of creating a custom hook for balances usage
*/
export const useSwapCanisterLiquidityPosition = (): {
lpList?: Pair.Balances;
isLoading: boolean;
updateLpList: (principalId?: string) => void;
lpOf: string | undefined;
} => {
// Use the SwapCanisterController hook
const controller = useSwapCanisterController();
// Use states from store
const { lpList, isLoading, lpOf } = useAppSelector(
selectLiquidityPositionState
);
const dispatch = useAppDispatch();
/**
* Creating a function to get liquidity position list and add it on redux store
*/
const updateLpList = useCallback(
(principalId?: string) => {
if (controller) {
// If no principalId is provided, let's get the agent principal to store
if (!principalId) {
controller
.getAgentPrincipal()
.then((agentPrincipal) =>
dispatch(
liquidityPositionActions.setLpOf(agentPrincipal.toString())
)
)
.catch((error) => alert(error));
} else {
dispatch(liquidityPositionActions.setLpOf(principalId));
}
// Get balances from controller and manage app loading states
dispatch(liquidityPositionActions.setIsLoading(true));
controller
.getLPBalances(principalId)
.then((response) =>
dispatch(liquidityPositionActions.setLpList(response))
)
.catch((error) => alert(error))
.finally(() =>
dispatch(liquidityPositionActions.setIsLoading(false))
);
}
},
[controller, dispatch]
);
return { lpList, isLoading, updateLpList, lpOf };
};