async function estimateClosePosition(
pool,
position,
amountABigint,
amountBBigint
) {
const slippage = new Percentage(new BN(5), new BN(1000));
const { curSqrtPrice, lowerSqrtPrice, upperSqrtPrice } = getCoinsRate(
pool,
position
);
const tokenAmounts = adjustForCoinSlippage(
{
coinA: new BN(amountABigint.toFixed(0)),
coinB: new BN(amountBBigint.toFixed(0)),
},
slippage,
false
);
const liquidity = ClmmPoolUtil.estimateLiquidityFromcoinAmounts(
curSqrtPrice,
Number(position.tick_lower_index),
Number(position.tick_upper_index),
{
coinA: new BN(Math.ceil(amountABigint)),
coinB: new BN(Math.ceil(amountBBigint)),
}
);
return {
tokenMaxA: tokenAmounts.tokenMaxA,
tokenMaxB: tokenAmounts.tokenMaxB,
lowerTick: lowerSqrtPrice,
upperTick: upperSqrtPrice,
curSqrtPrice,
liquidity,
};
}
function getCoinsRate(pool, position) {
const curSqrtPrice = new BN(pool.current_sqrt_price);
const lowerSqrtPrice = TickMath.tickIndexToSqrtPriceX64(
position.tick_lower_index
);
const upperSqrtPrice = TickMath.tickIndexToSqrtPriceX64(
position.tick_upper_index
);
const amounts = ClmmPoolUtil.getCoinAmountFromLiquidity(
new BN(position.liquidity),
curSqrtPrice,
lowerSqrtPrice,
upperSqrtPrice,
false
);
const coinAValue = fromDecimalsAmount(
amounts.coinA.toNumber(),
pool.coinA?.decimals ?? 1
);
const coinBValue = fromDecimalsAmount(
amounts.coinB.toNumber(),
pool.coinB?.decimals ?? 1
);
const total = coinAValue + coinBValue;
const percent = {
ratioA: coinAValue / total,
ratioB: coinBValue / total,
};
return {
percent,
curSqrtPrice,
lowerSqrtPrice,
upperSqrtPrice,
};
}