-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddLiquidityDrawer.tsx
More file actions
183 lines (163 loc) · 6.25 KB
/
Copy pathAddLiquidityDrawer.tsx
File metadata and controls
183 lines (163 loc) · 6.25 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { useEffect, useMemo, useState } from "react";
import type { Address } from "viem";
import { parseUnits } from "viem";
import { useAccount, usePublicClient } from "wagmi";
import { useLiquidityActions } from "../../hooks/useLiquidityActions";
import { getTokenIcon } from "../../../../lib/getTokenIcon";
import styles from "../../modal.module.css";
import TokenField from "../../../swap/components/TokenField";
import { quoteOutFromReserves } from "../../../../utils/quotes";
import { abi, addresses } from "@trustswap/sdk";
import { useErc20Read } from "../../../../lib/erc20Read";
import { useTokenModule } from "../../../../hooks/useTokenModule";
type PairData = {
pair: Address;
token0: Address;
token1: Address;
reserve0: bigint;
reserve1: bigint;
};
export function AddLiquidityDrawer({
tokenA,
tokenB,
metaA,
metaB,
onPendingChange,
}: {
tokenA?: Address;
tokenB?: Address;
metaA?: { address: Address; symbol: string; decimals: number; isNative?: boolean };
metaB?: { address: Address; symbol: string; decimals: number; isNative?: boolean };
onPendingChange?: (p: boolean) => void;
}) {
const { address: to } = useAccount();
const pc = usePublicClient();
const { toWrapped } = useTokenModule();
const { isZeroAddress } = useErc20Read();
const { addLiquidity } = useLiquidityActions();
const [tokenIn, setTokenIn] = useState<Address | undefined>(tokenA);
const [tokenOut, setTokenOut] = useState<Address | undefined>(tokenB);
const [amountIn, setAmountIn] = useState("");
const [amountOut, setAmountOut] = useState("");
const [pairData, setPairData] = useState<PairData | null>(null);
const readA = tokenIn ? toWrapped(tokenIn) : undefined;
const readB = tokenOut ? toWrapped(tokenOut) : undefined;
// décimales (fallback 18 si meta absente)
const decA = metaA?.decimals ?? 18;
const decB = metaB?.decimals ?? 18;
function asBigInt(v: any): bigint | undefined {
if (typeof v === "bigint") return v;
if (typeof v === "number") return BigInt(v);
if (typeof v === "string" && v.trim() !== "") return BigInt(v);
return undefined;
}
// charge la paire (token0/1, reserves) dès que readA/readB changent
useEffect(() => {
let cancelled = false;
(async () => {
setPairData(null);
if (!pc || !readA || !readB) return;
try {
const pair = await pc.readContract({
address: addresses.UniswapV2Factory as Address,
abi: abi.UniswapV2Factory,
functionName: "getPair",
args: [readA, readB],
}) as Address;
if (!pair || isZeroAddress(pair)) return;
const [t0, t1] = await Promise.all([
pc.readContract({ address: pair, abi: abi.UniswapV2Pair, functionName: "token0" }) as Promise<Address>,
pc.readContract({ address: pair, abi: abi.UniswapV2Pair, functionName: "token1" }) as Promise<Address>,
]);
const raw = await pc.readContract({
address: pair,
abi: abi.UniswapV2Pair,
functionName: "getReserves",
}) as any;
const reserve0 =
asBigInt(raw?._reserve0) ?? asBigInt(raw?.reserve0) ?? (Array.isArray(raw) ? asBigInt(raw[0]) : undefined) ?? 0n;
const reserve1 =
asBigInt(raw?._reserve1) ?? asBigInt(raw?.reserve1) ?? (Array.isArray(raw) ? asBigInt(raw[1]) : undefined) ?? 0n;
if (!cancelled) setPairData({ pair, token0: t0, token1: t1, reserve0, reserve1 });
} catch (e) {
if (!cancelled) setPairData(null);
}
})();
return () => { cancelled = true; };
}, [pc, readA, readB]);
const reserves = useMemo(() => {
if (!pairData || !readA) return null;
if (pairData.token0.toLowerCase() === readA.toLowerCase()) {
return { reserveA: pairData.reserve0, reserveB: pairData.reserve1 };
} else if (pairData.token1.toLowerCase() === readA.toLowerCase()) {
return { reserveA: pairData.reserve1, reserveB: pairData.reserve0 };
}
return null;
}, [pairData, readA]);
function onChangeAmountIn(v: string) {
setAmountIn(v);
if (!reserves || reserves.reserveA === 0n || reserves.reserveB === 0n) return;
const nextB = quoteOutFromReserves(v, decA, decB, reserves.reserveA, reserves.reserveB);
setAmountOut(nextB ?? "");
}
function onChangeAmountOut(v: string) {
setAmountOut(v);
if (!reserves || reserves.reserveA === 0n || reserves.reserveB === 0n) return;
const nextA = quoteOutFromReserves(v, decB, decA, reserves.reserveB, reserves.reserveA);
setAmountIn(nextA ?? "");
}
async function onSubmit() {
if (!tokenIn || !tokenOut || !to) return;
onPendingChange?.(true);
try {
await addLiquidity(
tokenIn, // UI addr (peut être natif placeholder)
tokenOut,
parseUnits(amountIn || "0", decA),
parseUnits(amountOut || "0", decB),
0n, 0n, // slippage mins (à calculer si besoin)
to,
Math.floor(Date.now() / 1000) + 60 * 15
);
} finally {
onPendingChange?.(false);
}
}
return (
<div className={styles.bodyAddModal}>
<div className={styles.inputAddLiquidityContainer}>
<div className={styles.inputAddContainer}>
<TokenField
label=""
token={tokenIn}
onTokenChange={(addr) => { setTokenIn(addr); setAmountIn(""); setAmountOut(""); }}
amount={amountIn}
onAmountChange={onChangeAmountIn}
readOnly={false}
/>
</div>
<div className={styles.inputAddContainer}>
<TokenField
label=""
token={tokenOut}
onTokenChange={(addr) => { setTokenOut(addr); setAmountIn(""); setAmountOut(""); }}
amount={amountOut}
onAmountChange={onChangeAmountOut}
readOnly={false}
/>
</div>
</div>
{metaB && tokenOut && (
<img src={getTokenIcon(tokenOut)} alt={metaB.symbol} className={styles.tokenImgWorLeft} />
)}
{metaA && tokenIn && (
<img src={getTokenIcon(tokenIn)} alt={metaA.symbol} className={styles.tokenImgWorRight} />
)}
<div className={styles.wormholeContainer}>
<button onClick={onSubmit} className={styles.btnAddLiquidity}>
Add Liquidity
</button>
</div>
</div>
);
}