Skip to content

Commit

Permalink
fix increaseLiquidityOptimal (#396)
Browse files Browse the repository at this point in the history
* fix increaseLiquidityOptimal

* version

* version
  • Loading branch information
tommyzhao451 authored Jan 29, 2025
1 parent c247980 commit c4f4b67
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aperture_finance/uniswap-v3-automation-sdk",
"version": "3.14.5",
"version": "3.14.6",
"description": "SDK for Aperture's CLMM automation platform",
"author": "Aperture Finance <[email protected]>",
"license": "MIT",
Expand Down
12 changes: 12 additions & 0 deletions src/viem/automan/automan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ export async function simulateIncreaseLiquidityOptimalV3(
position: Position,
increaseParams: IncreaseLiquidityParams,
swapData: Hex = '0x',
token0FeeAmount = BigInt(0),
token1FeeAmount = BigInt(0),
blockNumber?: bigint,
): Promise<IncreaseLiquidityReturnType> {
const returnData = await requestIncreaseLiquidityOptimalV3(
Expand All @@ -405,6 +407,8 @@ export async function simulateIncreaseLiquidityOptimalV3(
position,
increaseParams,
swapData,
token0FeeAmount,
token1FeeAmount,
blockNumber,
);
return decodeFunctionResult({
Expand Down Expand Up @@ -447,6 +451,8 @@ export async function estimateIncreaseLiquidityOptimalV3Gas(
position: Position,
increaseParams: IncreaseLiquidityParams,
swapData: Hex = '0x',
token0FeeAmount = BigInt(0),
token1FeeAmount = BigInt(0),
blockNumber?: bigint,
): Promise<bigint> {
return hexToBigInt(
Expand All @@ -459,6 +465,8 @@ export async function estimateIncreaseLiquidityOptimalV3Gas(
position,
increaseParams,
swapData,
token0FeeAmount,
token1FeeAmount,
blockNumber,
),
);
Expand Down Expand Up @@ -527,11 +535,15 @@ export async function requestIncreaseLiquidityOptimalV3<
position: Position,
increaseParams: IncreaseLiquidityParams,
swapData: Hex = '0x',
token0FeeAmount = BigInt(0),
token1FeeAmount = BigInt(0),
blockNumber?: bigint,
): Promise<RpcReturnType[M]> {
const data = getAutomanV3IncreaseLiquidityOptimalCallData(
increaseParams,
swapData,
token0FeeAmount,
token1FeeAmount,
);
const { apertureAutomanV3 } = getAMMInfo(chainId, amm)!;

Expand Down
36 changes: 20 additions & 16 deletions src/viem/solver/increaseLiquidityOptimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ export async function increaseLiquidityOptimalV3(
? position.pool.tickSpacing
: position.pool.fee;

// Subtract fees from poolAmountIn before passing to solver
// to prevent ERC20 Error: transfer amount exceeds balance.
const { poolAmountIn, zeroForOne } = await getOptimalSwapAmountV3(
chainId,
amm,
Expand All @@ -543,6 +545,12 @@ export async function increaseLiquidityOptimalV3(
increaseParams.amount1Desired,
blockNumber,
);
const swapFeeAmount = BigInt(
new Big(poolAmountIn.toString()).mul(FEE_ZAP_RATIO).toFixed(0),
);
const swapAmountIn = poolAmountIn - swapFeeAmount;
const token0FeeAmount = zeroForOne ? swapFeeAmount : 0n;
const token1FeeAmount = zeroForOne ? 0n : swapFeeAmount;

const estimateGas = async (swapData: Hex) => {
try {
Expand All @@ -556,6 +564,8 @@ export async function increaseLiquidityOptimalV3(
position,
increaseParams,
swapData,
token0FeeAmount,
token1FeeAmount,
blockNumber,
),
]);
Expand All @@ -581,7 +591,7 @@ export async function increaseLiquidityOptimalV3(
try {
const slippage =
Number(increaseOptions.slippageTolerance.toSignificant()) / 100;
if (poolAmountIn > 0n) {
if (swapAmountIn > 0n) {
({ swapData, swapRoute } = await getSolver(solver).mintOptimal({
chainId,
amm,
Expand All @@ -592,7 +602,7 @@ export async function increaseLiquidityOptimalV3(
tickLower,
tickUpper,
slippage,
poolAmountIn,
poolAmountIn: swapAmountIn,
zeroForOne,
}));
[liquidity, amount0, amount1] =
Expand All @@ -604,27 +614,20 @@ export async function increaseLiquidityOptimalV3(
position,
increaseParams,
swapData,
token0FeeAmount,
token1FeeAmount,
blockNumber,
);
gasFeeEstimation = await estimateGas(swapData);
}

const token0FeeAmount = zeroForOne
? BigInt(new Big(poolAmountIn.toString()).mul(FEE_ZAP_RATIO).toFixed(0))
: 0n;
const token1FeeAmount = zeroForOne
? 0n
: BigInt(
new Big(poolAmountIn.toString()).mul(FEE_ZAP_RATIO).toFixed(0),
);
const tokenInPrice = zeroForOne ? tokenPricesUsd[0] : tokenPricesUsd[1];
const decimals = zeroForOne
const tokenInDecimals = zeroForOne
? token0Amount.currency.decimals
: token1Amount.currency.decimals;
const feeUSD = new Big(poolAmountIn.toString())
.div(10 ** decimals)
.mul(tokenInPrice)
.mul(FEE_ZAP_RATIO);
const feeUSD = new Big(swapFeeAmount.toString())
.div(10 ** tokenInDecimals)
.mul(tokenInPrice);

getLogger().info('SDK.increaseLiquidityOptimalV3.fees ', {
amm: amm,
Expand All @@ -638,7 +641,8 @@ export async function increaseLiquidityOptimalV3(
amount0Desired: increaseParams.amount0Desired.toString(),
amount1Desired: increaseParams.amount1Desired.toString(),
zeroForOne,
poolAmountIn: poolAmountIn.toString(),
poolAmountIn: poolAmountIn.toString(), // before fees
swapAmountIn: swapAmountIn.toString(), // after fees
});

return {
Expand Down

0 comments on commit c4f4b67

Please sign in to comment.