initkjbnkhj
This commit is contained in:
@@ -56,6 +56,12 @@ function badRequest(msg: string): StakingError {
|
||||
|
||||
const MINT_GAS_LIMIT = '600000';
|
||||
const REMOVE_GAS_LIMIT = '500000';
|
||||
// Оценочные gas-лимиты шагов LP-депозита (для networkFee в quote). Соответствуют реальным:
|
||||
// mint=MINT_GAS_LIMIT, approve≈APPROVE_GAS_LIMIT, fee≈ERC20 transfer, wrap≈WETH.deposit.
|
||||
const EST_GAS_MINT = 600_000n;
|
||||
const EST_GAS_APPROVE = 80_000n;
|
||||
const EST_GAS_FEE = 65_000n;
|
||||
const EST_GAS_WRAP = 60_000n;
|
||||
const DEFAULT_SLIPPAGE_BPS = 100n; // 1%
|
||||
const MAX_SLIPPAGE_BPS = 500n; // 5% — верхняя граница (дефолт 1%)
|
||||
const SLIPPAGE_DENOM = 10000n;
|
||||
@@ -220,6 +226,13 @@ export interface AddQuote {
|
||||
appFee1Usd: number | null;
|
||||
appFeeTotalUsd: number | null;
|
||||
appFeeTotalEth: string | null; // комиссия в монете сети (ETH-эквивалент)
|
||||
// ── Оценка сетевой (gas) комиссии всего LP-депозита (wrap?+fee×ноги+approve×ноги+mint) ──
|
||||
networkFee: {
|
||||
estGasUnits: string;
|
||||
gasPriceGwei: string | null;
|
||||
amountEthHuman: string | null; // оценка газа в ETH
|
||||
amountUsd: number | null; // та же оценка в USD
|
||||
};
|
||||
// ── Проверка баланса (только если передан ownerAddress) ──
|
||||
balance?: {
|
||||
token0: LegBalance;
|
||||
@@ -328,6 +341,25 @@ export async function quoteAdd(p: AddParams): Promise<AddQuote> {
|
||||
price1 = isWeth(pool.token1) ? ethPrice : (priceMap.get(`ETH:${pool.sym1}`) ?? null);
|
||||
} catch { /* prices optional */ }
|
||||
|
||||
// ── Оценка сетевой (gas) комиссии: суммируем gas-лимиты шагов, которые реально пойдут ──
|
||||
const t0IsWethN = isWeth(pool.token0);
|
||||
const t1IsWethN = isWeth(pool.token1);
|
||||
let estGas = EST_GAS_MINT; // mint всегда
|
||||
if (in0 > 0n) estGas += EST_GAS_APPROVE + EST_GAS_FEE; // нога token0: approve + fee
|
||||
if (in1 > 0n) estGas += EST_GAS_APPROVE + EST_GAS_FEE; // нога token1
|
||||
if (p.useNativeEth && (t0IsWethN || t1IsWethN)) estGas += EST_GAS_WRAP; // авто-wrap ETH→WETH
|
||||
let gasPriceWei = 0n;
|
||||
try {
|
||||
const fd = await provider.getFeeData();
|
||||
const gp = fd.maxFeePerGas ?? fd.gasPrice ?? null;
|
||||
if (gp) gasPriceWei = BigInt(gp.toString());
|
||||
} catch { /* gas price optional */ }
|
||||
const networkFeeWei = estGas * gasPriceWei;
|
||||
const networkFeeEthHuman = gasPriceWei > 0n ? formatSmallestUnits(networkFeeWei.toString(), 18) : null;
|
||||
const networkFeeUsd = (networkFeeEthHuman != null && ethPrice != null)
|
||||
? roundUsd(Number(networkFeeEthHuman) * ethPrice) : null;
|
||||
const gasPriceGwei = gasPriceWei > 0n ? formatSmallestUnits(gasPriceWei.toString(), 9) : null;
|
||||
|
||||
const fee0Usd = price0 != null ? roundUsd(Number(fee0Human) * price0) : null;
|
||||
const fee1Usd = price1 != null ? roundUsd(Number(fee1Human) * price1) : null;
|
||||
const usdParts = [fee0Usd, fee1Usd].filter((v): v is number => v != null);
|
||||
@@ -405,6 +437,12 @@ export async function quoteAdd(p: AddParams): Promise<AddQuote> {
|
||||
appFee1Usd: fee1Usd,
|
||||
appFeeTotalUsd,
|
||||
appFeeTotalEth,
|
||||
networkFee: {
|
||||
estGasUnits: estGas.toString(),
|
||||
gasPriceGwei,
|
||||
amountEthHuman: networkFeeEthHuman,
|
||||
amountUsd: networkFeeUsd,
|
||||
},
|
||||
...(balance ? { balance } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user