From 60fa38e62e734c4ea475f08dc3a034c5b13fa96f Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 22:32:41 -0700 Subject: [PATCH 1/5] add controllerhelper address --- packages/frontend/src/state/positions/atoms.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/state/positions/atoms.ts b/packages/frontend/src/state/positions/atoms.ts index 9ce484913..c429d5fa0 100644 --- a/packages/frontend/src/state/positions/atoms.ts +++ b/packages/frontend/src/state/positions/atoms.ts @@ -95,4 +95,4 @@ export const vaultHistoryUpdatingAtom = atom(false) export const isToHidePnLAtom = atom(false) export const swapsAtom = atom({ swaps: [] }) export const longPositionValueAtom = atom(BIG_ZERO) -export const shortPositionValueAtom = atom(BIG_ZERO) +export const shortPositionValueAtom = atom(BIG_ZERO) \ No newline at end of file From 519687771d029c420b008fbd115944e76413698a Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 12 Jul 2022 23:34:44 -0700 Subject: [PATCH 2/5] rebalance hook --- packages/frontend/src/state/lp/hooks.ts | 147 ++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index c367ab05a..15e3765f7 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -217,6 +217,153 @@ export const useCollectFees = () => { return collectFees } +// Rebalance via general swap +export const useRebalanceGeneralSwap = () => { + const address = useAtomValue(addressAtom) + const controllerHelperContract = useAtomValue(controllerHelperHelperContractAtom) + const { controllerHelper, weth, oSqueeth, squeethPool } = useAtomValue(addressesAtom) + const controllerContract = useAtomValue(controllerContractAtom) + const handleTransaction = useHandleTransaction() + const isWethToken0 = useAtomValue(isWethToken0Atom) + const getDebtAmount = useGetDebtAmount() + const getVault = useGetVault() + const getDecreaseLiquidity = useGetDecreaseLiquidity() + const getPosition = useGetPosition() + const getTwapSqueethPrice = useGetTwapSqueethPrice() + const squeethPoolContract = useAtomValue(squeethPoolContractAtom) + const getQuote = useGetQuote() + const rebalanceGeneralSwap = useAppCallback( + async (vaultId: number, lowerTickInput: number, upperTickInput: number, onTxConfirmed?: () => void) => { + const vaultBefore = await getVault(vaultId) + const uniTokenId = vaultBefore?.NFTCollateralId + const position = await getPosition(uniTokenId) + if (!controllerContract || !controllerHelperContract || !address || !position || !vaultBefore || !squeethPoolContract) return + const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) + const debtInEth = await getDebtAmount(shortAmount) + const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO) + + const amount0Min = new BigNumber(0) + const amount1Min = new BigNumber(0) + + const lowerTick = nearestUsableTick(lowerTickInput, TICK_SPACE) + const upperTick = nearestUsableTick(upperTickInput, TICK_SPACE) + + // Get current LP positions + const { amount0, amount1 } = await getDecreaseLiquidity(uniTokenId, position.liquidity, 0, 0, Math.floor(Date.now() / 1000 + 86400)) + const wPowerPerpAmountInLPBefore = isWethToken0 ? amount1 : amount0 + const wethAmountInLPBefore = isWethToken0 ? amount0 : amount1 + + // Calculate prices from ticks + const sqrtLowerPrice = new BigNumber(TickMath.getSqrtRatioAtTick(lowerTick).toString()).div(x96) + const sqrtUpperPrice = new BigNumber(TickMath.getSqrtRatioAtTick(upperTick).toString()).div(x96) + const { sqrtPriceX96 } = await getPoolState(squeethPoolContract) + const sqrtSqueethPrice = new BigNumber(sqrtPriceX96.toString()).div(x96) + const squeethPrice = await getTwapSqueethPrice() + + let newAmount0, newAmount1, amountIn, wethAmountInLPAfter, wPowerPerpAmountInLPAfter, tokenIn, tokenOut + if (sqrtUpperPrice.lt(sqrtSqueethPrice)) { + // All weth position + wPowerPerpAmountInLPAfter = 0 + const ethAmountOut = await getQuote(new BigNumber(wPowerPerpAmountInLPBefore), true) + wethAmountInLPAfter = new BigNumber(ethAmountOut).plus(wethAmountInLPBefore) + amountIn = wPowerPerpAmountInLPBefore + tokenIn = oSqueeth + tokenOut = weth + } else if (sqrtSqueethPrice.lt(sqrtLowerPrice)) { + // All squeeth position + wethAmountInLPAfter = 0 + const squeethAmountOut = await getQuote(new BigNumber(wethAmountInLPBefore), false) + wPowerPerpAmountInLPAfter = new BigNumber(squeethAmountOut).plus(wPowerPerpAmountInLPBefore) + amountIn = wethAmountInLPBefore + tokenIn = weth + tokenOut = oSqueeth + } else { + // Get previous liquidity ammount in ETH + const wPowerPerpAmountInLPBeforeInEth = await getQuote(new BigNumber(wPowerPerpAmountInLPBefore), true) + const positionEthValue = new BigNumber(wethAmountInLPBefore).plus(new BigNumber(wPowerPerpAmountInLPBeforeInEth)) + // newLiquidity = positionEthValue/(squeethPrice/sqrt(squeethPrice) - squeethPrice/sqrt(upperPrice) + sqrt(squeethPrice) - sqrt(lowerPrice)) + const liquidity = positionEthValue.div((new BigNumber(squeethPrice).div(sqrtSqueethPrice)) + .minus((new BigNumber(squeethPrice).div(sqrtUpperPrice))) + .plus(sqrtSqueethPrice) + .minus(sqrtLowerPrice)) + // Calculate amounts of each asset to LP + // x = L(sqrt(upperPrice) - sqrt(squeethPrice))) / sqrt(squeethPrice) * sqrt(upperPrice) + // y = L(sqrt(squeethPrice) - sqrt(lowerPrice)) + newAmount0 = liquidity.times(sqrtUpperPrice.minus(sqrtSqueethPrice)).div((sqrtSqueethPrice.times(sqrtUpperPrice))) + newAmount1 = liquidity.times(sqrtSqueethPrice.minus(sqrtLowerPrice)) + wethAmountInLPAfter = isWethToken0 ? newAmount0 : newAmount1 + wPowerPerpAmountInLPAfter = isWethToken0 ? newAmount1 : newAmount0 + const needMoreWeth = new BigNumber(wethAmountInLPBefore).lt(new BigNumber(wethAmountInLPAfter)) + const needMoreSqueeth = new BigNumber(wPowerPerpAmountInLPBefore).lt(new BigNumber(wPowerPerpAmountInLPAfter)) + tokenIn = needMoreWeth ? oSqueeth : weth + tokenOut = needMoreWeth ? weth : oSqueeth + amountIn = needMoreWeth && !needMoreSqueeth ? new BigNumber(wPowerPerpAmountInLPBefore).minus(new BigNumber(wPowerPerpAmountInLPAfter)).toFixed(0) : + needMoreSqueeth && !needMoreWeth ? new BigNumber(wethAmountInLPBefore).minus(new BigNumber(wethAmountInLPAfter)).toFixed(0) + : 0 + } + + const abiCoder = new ethers.utils.AbiCoder() + const liquidatePreviousLP = { + // Liquidate LP + rebalanceLpInVaultType: new BigNumber(1).toFixed(0), // DecreaseLpLiquidity: + // DecreaseLpLiquidityParams: [tokenId, liquidity, liquidityPercentage, amount0Min, amount1Min] + data: abiCoder.encode( + ['uint256', 'uint256', 'uint256', 'uint128', 'uint128'], + [ + uniTokenId, + position.liquidity, + fromTokenAmount(1, 18).toFixed(0), + 0, + 0, + ], + ), + } + const generalSwap = { + // Exchange necessary amount of oSQTH and ETH + rebalanceLpInVaultType: new BigNumber(5).toFixed(0), // GeneralSwap: + // GeneralSwap: [tokenIn, tokenOut, amountIn, limitPrice] + data: abiCoder.encode( + ['address', 'address', 'uint256', 'uint256', 'uint24'], + [tokenIn, tokenOut, amountIn, 0, POOL_FEE], + ), + } + const mintNewLP = { + // Mint new LP + rebalanceLpInVaultType: new BigNumber(4).toFixed(0), // MintNewLP + // lpWPowerPerpPool: [recipient, wPowerPerpPool, vaultId, wPowerPerpAmount, collateralToDeposit, collateralToLP, amount0Min, amount1Min, lowerTick, upperTick ] + data: abiCoder.encode( + ['address', 'address', 'uint256', 'uint256', 'uint256', 'uint256', 'uint256', 'uint256', 'int24', 'int24'], + [ + controllerHelper, + squeethPool, + vaultId, + wPowerPerpAmountInLPAfter.toFixed(0), + 0, + wethAmountInLPAfter.toFixed(0), + amount0Min.toFixed(0), + amount1Min.toFixed(0), + lowerTick, + upperTick, + ], + ), + } + + const rebalanceLpInVaultParams = amountIn > 0 ? [liquidatePreviousLP, generalSwap, mintNewLP] : [liquidatePreviousLP, mintNewLP] + + return handleTransaction( + await controllerHelperContract.methods + .rebalanceLpInVault(vaultId, collateralToFlashloan.toFixed(0), rebalanceLpInVaultParams) + .send({ + from: address, + }), + onTxConfirmed, + ) + }, + [address, controllerHelperContract, controllerHelper, weth, oSqueeth, squeethPool, controllerContract, handleTransaction, isWethToken0, getDebtAmount, getVault, getDecreaseLiquidity, getPosition, getTwapSqueethPrice, squeethPoolContract], + ) + return rebalanceGeneralSwap +} + /*** GETTERS ***/ export const useGetPosition = () => { From ba1c8f8a8021554ae76c739f4f43b96452126dfb Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Fri, 15 Jul 2022 18:13:18 -0700 Subject: [PATCH 3/5] update rebalance to math way --- packages/frontend/src/state/lp/hooks.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 15e3765f7..306fee297 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -258,29 +258,35 @@ export const useRebalanceGeneralSwap = () => { const sqrtUpperPrice = new BigNumber(TickMath.getSqrtRatioAtTick(upperTick).toString()).div(x96) const { sqrtPriceX96 } = await getPoolState(squeethPoolContract) const sqrtSqueethPrice = new BigNumber(sqrtPriceX96.toString()).div(x96) - const squeethPrice = await getTwapSqueethPrice() + const squeethPrice = sqrtSqueethPrice.pow(2) + + // Get previous liquidity amount in ETH + const wPowerPerpAmountInLPBeforeInEth = await getQuote(new BigNumber(wPowerPerpAmountInLPBefore), true) + const positionEthValue = new BigNumber(wethAmountInLPBefore).plus(new BigNumber(wPowerPerpAmountInLPBeforeInEth)) + console.log("positionEthValue", positionEthValue.toString()) let newAmount0, newAmount1, amountIn, wethAmountInLPAfter, wPowerPerpAmountInLPAfter, tokenIn, tokenOut if (sqrtUpperPrice.lt(sqrtSqueethPrice)) { // All weth position + // newLiquidity = positionEthValue/(sqrt(upperPrice) - sqrt(lowerPrice)) + const liquidity = positionEthValue.div(sqrtUpperPrice.minus(sqrtLowerPrice)) + // wethAmount = L(sqrt(upperPrice) - sqrt(lowerPrice)) + wethAmountInLPAfter = liquidity.times(sqrtUpperPrice.minus(sqrtLowerPrice)) wPowerPerpAmountInLPAfter = 0 - const ethAmountOut = await getQuote(new BigNumber(wPowerPerpAmountInLPBefore), true) - wethAmountInLPAfter = new BigNumber(ethAmountOut).plus(wethAmountInLPBefore) amountIn = wPowerPerpAmountInLPBefore tokenIn = oSqueeth tokenOut = weth } else if (sqrtSqueethPrice.lt(sqrtLowerPrice)) { // All squeeth position + // newLiquidity = positionEthValue/(squeethPrice/sqrt(lowerPrice) - squeethPrice/sqrt(upperPrice)) + const liquidity = positionEthValue.div((squeethPrice.div(sqrtLowerPrice)).minus((squeethPrice.div(sqrtUpperPrice)))) wethAmountInLPAfter = 0 - const squeethAmountOut = await getQuote(new BigNumber(wethAmountInLPBefore), false) - wPowerPerpAmountInLPAfter = new BigNumber(squeethAmountOut).plus(wPowerPerpAmountInLPBefore) + // wPowerPerpAmount = L/sqrt(lowerPrice) - L/sqrt(upperPrice) + wPowerPerpAmountInLPAfter = (liquidity.div(sqrtLowerPrice).minus(liquidity.div(sqrtUpperPrice))) amountIn = wethAmountInLPBefore tokenIn = weth tokenOut = oSqueeth } else { - // Get previous liquidity ammount in ETH - const wPowerPerpAmountInLPBeforeInEth = await getQuote(new BigNumber(wPowerPerpAmountInLPBefore), true) - const positionEthValue = new BigNumber(wethAmountInLPBefore).plus(new BigNumber(wPowerPerpAmountInLPBeforeInEth)) // newLiquidity = positionEthValue/(squeethPrice/sqrt(squeethPrice) - squeethPrice/sqrt(upperPrice) + sqrt(squeethPrice) - sqrt(lowerPrice)) const liquidity = positionEthValue.div((new BigNumber(squeethPrice).div(sqrtSqueethPrice)) .minus((new BigNumber(squeethPrice).div(sqrtUpperPrice))) From d5f31f8eb0fff63fceacb9932a645c5718524813 Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Fri, 15 Jul 2022 22:46:20 -0700 Subject: [PATCH 4/5] position null case --- packages/frontend/src/state/lp/hooks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 306fee297..2781a20be 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -236,7 +236,7 @@ export const useRebalanceGeneralSwap = () => { async (vaultId: number, lowerTickInput: number, upperTickInput: number, onTxConfirmed?: () => void) => { const vaultBefore = await getVault(vaultId) const uniTokenId = vaultBefore?.NFTCollateralId - const position = await getPosition(uniTokenId) + const position = uniTokenId ? await getPosition(uniTokenId) : null if (!controllerContract || !controllerHelperContract || !address || !position || !vaultBefore || !squeethPoolContract) return const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) const debtInEth = await getDebtAmount(shortAmount) From ad0a32fa5365f8ad23dea12b4de75ccd5130805a Mon Sep 17 00:00:00 2001 From: Darya Kaviani Date: Tue, 9 Aug 2022 00:26:35 -0700 Subject: [PATCH 5/5] review changes --- packages/frontend/src/state/lp/hooks.ts | 65 +++++++++++++------------ 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/packages/frontend/src/state/lp/hooks.ts b/packages/frontend/src/state/lp/hooks.ts index 2781a20be..037382b6a 100644 --- a/packages/frontend/src/state/lp/hooks.ts +++ b/packages/frontend/src/state/lp/hooks.ts @@ -229,50 +229,47 @@ export const useRebalanceGeneralSwap = () => { const getVault = useGetVault() const getDecreaseLiquidity = useGetDecreaseLiquidity() const getPosition = useGetPosition() - const getTwapSqueethPrice = useGetTwapSqueethPrice() const squeethPoolContract = useAtomValue(squeethPoolContractAtom) - const getQuote = useGetQuote() + const getExactIn = useGetExactIn() const rebalanceGeneralSwap = useAppCallback( - async (vaultId: number, lowerTickInput: number, upperTickInput: number, onTxConfirmed?: () => void) => { + async (vaultId: number, lowerTickInput: number, upperTickInput: number, slippage: number, onTxConfirmed?: () => void) => { const vaultBefore = await getVault(vaultId) const uniTokenId = vaultBefore?.NFTCollateralId const position = uniTokenId ? await getPosition(uniTokenId) : null if (!controllerContract || !controllerHelperContract || !address || !position || !vaultBefore || !squeethPoolContract) return const shortAmount = fromTokenAmount(vaultBefore.shortAmount, OSQUEETH_DECIMALS) const debtInEth = await getDebtAmount(shortAmount) - const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO) - - const amount0Min = new BigNumber(0) - const amount1Min = new BigNumber(0) - - const lowerTick = nearestUsableTick(lowerTickInput, TICK_SPACE) - const upperTick = nearestUsableTick(upperTickInput, TICK_SPACE) + const collateralToFlashloan = debtInEth.multipliedBy(COLLAT_RATIO_FLASHLOAN) // Get current LP positions const { amount0, amount1 } = await getDecreaseLiquidity(uniTokenId, position.liquidity, 0, 0, Math.floor(Date.now() / 1000 + 86400)) + const amount0MinOld = new BigNumber(amount0).times(new BigNumber(1).minus(slippage)).toFixed(0) + const amount1MinOld = new BigNumber(amount1).times(new BigNumber(1).minus(slippage)).toFixed(0) const wPowerPerpAmountInLPBefore = isWethToken0 ? amount1 : amount0 const wethAmountInLPBefore = isWethToken0 ? amount0 : amount1 // Calculate prices from ticks + const { tick, tickSpacing } = await getPoolState(squeethPoolContract) + const lowerTick = nearestUsableTick(lowerTickInput, Number(tickSpacing)) + const upperTick = nearestUsableTick(upperTickInput, Number(tickSpacing)) const sqrtLowerPrice = new BigNumber(TickMath.getSqrtRatioAtTick(lowerTick).toString()).div(x96) const sqrtUpperPrice = new BigNumber(TickMath.getSqrtRatioAtTick(upperTick).toString()).div(x96) - const { sqrtPriceX96 } = await getPoolState(squeethPoolContract) - const sqrtSqueethPrice = new BigNumber(sqrtPriceX96.toString()).div(x96) - const squeethPrice = sqrtSqueethPrice.pow(2) + const squeethPrice = isWethToken0 ? new BigNumber(1).div(new BigNumber(TickMath.getSqrtRatioAtTick(Number(tick)).toString()).div(x96).pow(2)) + : new BigNumber(TickMath.getSqrtRatioAtTick(Number(tick)).toString()).div(x96).pow(2) + const sqrtSqueethPrice = squeethPrice.sqrt() // Get previous liquidity amount in ETH - const wPowerPerpAmountInLPBeforeInEth = await getQuote(new BigNumber(wPowerPerpAmountInLPBefore), true) - const positionEthValue = new BigNumber(wethAmountInLPBefore).plus(new BigNumber(wPowerPerpAmountInLPBeforeInEth)) - console.log("positionEthValue", positionEthValue.toString()) + const wPowerPerpAmountInLPBeforeInEth = new BigNumber(wPowerPerpAmountInLPBefore).times(squeethPrice) + const positionEthValue = new BigNumber(wethAmountInLPBefore).plus(wPowerPerpAmountInLPBeforeInEth) - let newAmount0, newAmount1, amountIn, wethAmountInLPAfter, wPowerPerpAmountInLPAfter, tokenIn, tokenOut + let amountIn, wethAmountInLPAfter, wPowerPerpAmountInLPAfter, tokenIn, tokenOut if (sqrtUpperPrice.lt(sqrtSqueethPrice)) { // All weth position // newLiquidity = positionEthValue/(sqrt(upperPrice) - sqrt(lowerPrice)) const liquidity = positionEthValue.div(sqrtUpperPrice.minus(sqrtLowerPrice)) // wethAmount = L(sqrt(upperPrice) - sqrt(lowerPrice)) wethAmountInLPAfter = liquidity.times(sqrtUpperPrice.minus(sqrtLowerPrice)) - wPowerPerpAmountInLPAfter = 0 + wPowerPerpAmountInLPAfter = new BigNumber(0) amountIn = wPowerPerpAmountInLPBefore tokenIn = oSqueeth tokenOut = weth @@ -280,7 +277,7 @@ export const useRebalanceGeneralSwap = () => { // All squeeth position // newLiquidity = positionEthValue/(squeethPrice/sqrt(lowerPrice) - squeethPrice/sqrt(upperPrice)) const liquidity = positionEthValue.div((squeethPrice.div(sqrtLowerPrice)).minus((squeethPrice.div(sqrtUpperPrice)))) - wethAmountInLPAfter = 0 + wethAmountInLPAfter = new BigNumber(0) // wPowerPerpAmount = L/sqrt(lowerPrice) - L/sqrt(upperPrice) wPowerPerpAmountInLPAfter = (liquidity.div(sqrtLowerPrice).minus(liquidity.div(sqrtUpperPrice))) amountIn = wethAmountInLPBefore @@ -295,10 +292,8 @@ export const useRebalanceGeneralSwap = () => { // Calculate amounts of each asset to LP // x = L(sqrt(upperPrice) - sqrt(squeethPrice))) / sqrt(squeethPrice) * sqrt(upperPrice) // y = L(sqrt(squeethPrice) - sqrt(lowerPrice)) - newAmount0 = liquidity.times(sqrtUpperPrice.minus(sqrtSqueethPrice)).div((sqrtSqueethPrice.times(sqrtUpperPrice))) - newAmount1 = liquidity.times(sqrtSqueethPrice.minus(sqrtLowerPrice)) - wethAmountInLPAfter = isWethToken0 ? newAmount0 : newAmount1 - wPowerPerpAmountInLPAfter = isWethToken0 ? newAmount1 : newAmount0 + wPowerPerpAmountInLPAfter = liquidity.times(sqrtUpperPrice.minus(sqrtSqueethPrice)).div((sqrtSqueethPrice.times(sqrtUpperPrice))) + wethAmountInLPAfter = liquidity.times(sqrtSqueethPrice.minus(sqrtLowerPrice)) const needMoreWeth = new BigNumber(wethAmountInLPBefore).lt(new BigNumber(wethAmountInLPAfter)) const needMoreSqueeth = new BigNumber(wPowerPerpAmountInLPBefore).lt(new BigNumber(wPowerPerpAmountInLPAfter)) tokenIn = needMoreWeth ? oSqueeth : weth @@ -308,6 +303,16 @@ export const useRebalanceGeneralSwap = () => { : 0 } + // Get amount mins + const amount0New = isWethToken0 ? wethAmountInLPAfter : wPowerPerpAmountInLPAfter + const amount1New = isWethToken0 ? wPowerPerpAmountInLPAfter : wethAmountInLPAfter + const amount0MinNew = amount0New.times(new BigNumber(1).minus(slippage)).toFixed(0) + const amount1MinNew = amount1New.times(new BigNumber(1).minus(slippage)).toFixed(0) + + // Get limit price + const amountOut = await getExactIn(new BigNumber(amountIn), tokenIn == oSqueeth) + const limitPrice = new BigNumber(amountOut).div(new BigNumber(amountIn)).times(new BigNumber(1).minus(slippage)) + const abiCoder = new ethers.utils.AbiCoder() const liquidatePreviousLP = { // Liquidate LP @@ -319,8 +324,8 @@ export const useRebalanceGeneralSwap = () => { uniTokenId, position.liquidity, fromTokenAmount(1, 18).toFixed(0), - 0, - 0, + amount0MinOld, + amount1MinOld, ], ), } @@ -330,7 +335,7 @@ export const useRebalanceGeneralSwap = () => { // GeneralSwap: [tokenIn, tokenOut, amountIn, limitPrice] data: abiCoder.encode( ['address', 'address', 'uint256', 'uint256', 'uint24'], - [tokenIn, tokenOut, amountIn, 0, POOL_FEE], + [tokenIn, tokenOut, amountIn, fromTokenAmount(limitPrice, 18).toFixed(0), POOL_FEE], ), } const mintNewLP = { @@ -346,8 +351,8 @@ export const useRebalanceGeneralSwap = () => { wPowerPerpAmountInLPAfter.toFixed(0), 0, wethAmountInLPAfter.toFixed(0), - amount0Min.toFixed(0), - amount1Min.toFixed(0), + amount0MinNew, + amount1MinNew, lowerTick, upperTick, ], @@ -365,10 +370,10 @@ export const useRebalanceGeneralSwap = () => { onTxConfirmed, ) }, - [address, controllerHelperContract, controllerHelper, weth, oSqueeth, squeethPool, controllerContract, handleTransaction, isWethToken0, getDebtAmount, getVault, getDecreaseLiquidity, getPosition, getTwapSqueethPrice, squeethPoolContract], + [address, controllerHelperContract, controllerHelper, weth, oSqueeth, squeethPool, controllerContract, handleTransaction, isWethToken0, getDebtAmount, getVault, getDecreaseLiquidity, getPosition, squeethPoolContract, getExactIn], ) return rebalanceGeneralSwap -} + } /*** GETTERS ***/