initnmjnjnj
This commit is contained in:
@@ -463,30 +463,32 @@ export async function executeAddLiquidity(p: {
|
||||
throw badRequest(`Недостаточно ${pool.sym1}: нужно ${formatSmallestUnits(a1.toString(), pool.dec1)}, есть ${formatSmallestUnits(bal.toString(), pool.dec1)}`);
|
||||
}
|
||||
}
|
||||
// WETH-нога при useNativeEth: заворачиваем ТОЛЬКО недостающее (учитываем уже имеющийся WETH от прошлых
|
||||
// попыток — не плодим WETH и не требуем лишний ETH). wethDeficit переиспользуется в шаге wrap ниже.
|
||||
let wethDeficit = 0n;
|
||||
if (p.useNativeEth) {
|
||||
const wrapAmt = t0IsWeth ? a0 : t1IsWeth ? a1 : 0n;
|
||||
if (wrapAmt > 0n) {
|
||||
const wethLegAddr = t0IsWeth ? pool.token0 : t1IsWeth ? pool.token1 : '';
|
||||
if (wrapAmt > 0n && wethLegAddr) {
|
||||
let wethBal = 0n;
|
||||
try { wethBal = await readErc20Balance('ETH', wethLegAddr, p.expectedFromAddress); } catch { /* assume 0 */ }
|
||||
wethDeficit = wethBal >= wrapAmt ? 0n : wrapAmt - wethBal;
|
||||
const ethBal = await readEvmNativeBalance('ETH', p.expectedFromAddress);
|
||||
const need = wrapAmt + ethers.utils.parseEther('0.001').toBigInt(); // wrap + буфер на газ
|
||||
const need = wethDeficit + ethers.utils.parseEther('0.001').toBigInt(); // недостающий WETH + буфер на газ
|
||||
if (ethBal < need) {
|
||||
throw badRequest(`Недостаточно ETH: нужно ~${formatSmallestUnits(need.toString(), 18)} (wrap + газ), есть ${formatSmallestUnits(ethBal.toString(), 18)}`);
|
||||
throw badRequest(`Недостаточно ETH: нужно ~${formatSmallestUnits(need.toString(), 18)} (wrap недостающего WETH + газ), есть ${formatSmallestUnits(ethBal.toString(), 18)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// useNativeEth: если у пула есть WETH-нога — заворачиваем нужный ETH в WETH (WETH.deposit, ждёт
|
||||
// 1 conf). Дальше WETH = обычный ERC20: fee/approve/mint без изменений. Юзеру нужен ETH + второй токен.
|
||||
// useNativeEth: заворачиваем ТОЛЬКО недостающий WETH (wethDeficit). Если WETH уже хватает — пропускаем
|
||||
// (используем имеющийся). Дальше WETH = обычный ERC20: fee/approve/mint без изменений.
|
||||
let wrapTxid: string | undefined;
|
||||
if (p.useNativeEth) {
|
||||
let wrapAmount = 0n;
|
||||
if (isWeth(pool.token0)) wrapAmount = a0;
|
||||
else if (isWeth(pool.token1)) wrapAmount = a1;
|
||||
if (wrapAmount > 0n) {
|
||||
const w = await signAndBroadcastWrapEth({
|
||||
mnemonic: p.mnemonic, expectedFromAddress: p.expectedFromAddress, amountWei: wrapAmount.toString(),
|
||||
});
|
||||
wrapTxid = w.txid;
|
||||
}
|
||||
if (p.useNativeEth && wethDeficit > 0n) {
|
||||
const w = await signAndBroadcastWrapEth({
|
||||
mnemonic: p.mnemonic, expectedFromAddress: p.expectedFromAddress, amountWei: wethDeficit.toString(),
|
||||
});
|
||||
wrapTxid = w.txid;
|
||||
}
|
||||
|
||||
// app fee 0.7% по каждому токену (net = то что реально пойдёт в пул)
|
||||
|
||||
Reference in New Issue
Block a user