-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathliquidity-position.tsx
81 lines (75 loc) · 2.61 KB
/
liquidity-position.tsx
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
import {
useSwapCanisterLiquidityPosition,
useSwapCanisterLists,
} from '@/hooks';
import { selectPlugState, useAppSelector } from '@/store';
import { Liquidity, toBigNumber } from '@psychedelic/sonic-js';
import { useEffect, useState } from 'react';
/**
* Liquidity Position Section React Component
* Example of a component that show liquidity positions
*/
export const LiquidityPositionSection = () => {
const { principal } = useAppSelector(selectPlugState);
const { tokenList } = useSwapCanisterLists();
const { lpOf, lpList, isLoading, updateLpList } =
useSwapCanisterLiquidityPosition();
// Create a state to manage the principal id shown and searched
const [findingPrincipalId, setFindingPrincipalId] = useState<string>();
// If the principal from our Plug state changes we update the LP list
useEffect(() => {
if (principal) {
updateLpList(principal.toString());
}
}, [principal]);
// Update the shown principal id if the searched from store changes
useEffect(() => {
setFindingPrincipalId(lpOf);
}, [lpOf]);
const handleGetLp = () => {
updateLpList(findingPrincipalId);
};
return (
<section>
<h1>Liquidity Position</h1>
<div>
Principal Id:
<input
value={findingPrincipalId}
onChange={(e) => setFindingPrincipalId(e.currentTarget.value)}
/>
<button onClick={handleGetLp}>Get LPs</button>
</div>
<div className="card-list">
{/** Render the LP list if is searching for lpOf from store */}
{lpOf &&
(isLoading || !tokenList || !lpList ? (
// Render loading if is loading
<span>Loading...</span>
) : // Render the LP list
Object.keys(lpList).length > 0 ? (
Object.entries(lpList).map(([pairId, lp]) => {
// Split pair id to get token ids
const [token0, token1] = pairId.split(':');
return (
<div className="token-card" key={pairId}>
<h2>
{tokenList[token0].symbol}/{tokenList[token1].symbol}
</h2>
<span className="data-row">
<label>Wallet: </label>
{toBigNumber(lp)
.applyDecimals(Liquidity.PAIR_DECIMALS)
.toString()}
</span>
</div>
);
})
) : (
<div>No Liquidity Positions</div>
))}
</div>
</section>
);
};