From f22910069aec5a5365020e282d1bc07e10c1d00c Mon Sep 17 00:00:00 2001 From: JulesCrown Date: Tue, 12 Mar 2024 09:59:33 +0100 Subject: [PATCH] setup --- .gitignore | 1 + .gitmodules | 18 ------- onchain/src/LiquidityManager.sol | 85 ++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 18 deletions(-) delete mode 100644 .gitmodules diff --git a/.gitignore b/.gitignore index 541edf4..bfd9fe6 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ docs/ .env .secret .infura +.DS_Store \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index cb4c0bd..0000000 --- a/.gitmodules +++ /dev/null @@ -1,18 +0,0 @@ -[submodule "lib/forge-std"] - path = onchain/lib/forge-std - url = https://github.com/foundry-rs/forge-std -[submodule "lib/solmate"] - path = onchain/lib/solmate - url = https://github.com/Rari-Capital/solmate -[submodule "lib/openzeppelin-contracts"] - path = onchain/lib/openzeppelin-contracts - url = https://github.com/OpenZeppelin/openzeppelin-contracts -[submodule "lib/v3-core"] - path = onchain/lib/v3-core - url = https://github.com/Uniswap/v3-core -[submodule "lib/v3-periphery"] - path = onchain/lib/v3-periphery - url = https://github.com/Uniswap/v3-periphery -[submodule "lib/uni-v3-lib"] - path = onchain/lib/uni-v3-lib - url = https://github.com/Aperture-Finance/uni-v3-lib diff --git a/onchain/src/LiquidityManager.sol b/onchain/src/LiquidityManager.sol index 0952a86..e6a370b 100644 --- a/onchain/src/LiquidityManager.sol +++ b/onchain/src/LiquidityManager.sol @@ -84,6 +84,7 @@ contract LiquidityManager { ) { factory = _factory; WETH9 = _WETH9; + } function getToken(address token0, address token1) internal view returns (bool token0isWeth, address token) { @@ -254,5 +255,89 @@ contract LiquidityManager { position.liquidity = positionLiquidity - params.liquidity; emit DecreaseLiquidity(token, params.liquidity, amount0, amount1); + } + + + function compareTokenToEthBalance(uint256 ethAmountInPosition, uint256 tokenAmountInPosition) external view returns (bool hasMoreToken) { + // Fetch the current sqrtPriceX96 from the pool + (uint160 sqrtPriceX96,,,) = uniswapV3Pool.slot0(); + + // Convert sqrtPriceX96 to a conventional price format + // Note: The price is calculated as (sqrtPriceX96^2 / 2^192), simplified here as (price / 2^96) for the sake of example + uint256 price = uint256(sqrtPriceX96) * uint256(sqrtPriceX96) / (1 << 96); + + // Calculate the equivalent token amount for the ETH in the position at the current price + // Assuming price is expressed as the amount of token per ETH + uint256 equivalentTokenAmountForEth = ethAmountInPosition * price; + + // Compare to the actual token amount in the position + hasMoreToken = tokenAmountInPosition > equivalentTokenAmountForEth; + + return hasMoreToken; + } + +//////// +// - check if tick in range, otherwise revert +// - check if the position has more Token or more ETH, at current price +// - if more ETH, +// - calculate the amount of Token needed to be minted to bring the position to 50/50 +// - mint +// - deposit Token into pool +// - if more TOKEN +// - calculate the amount of token needed to be withdrawn from the position, to bring the position to 50/50 +// - withdraw +// - burn tokens + + function rebalance(address token, int24 tickLower, int24 tickUpper) external { + bool ETH_TOKEN_ZERO = _weth < token; + + PoolKey memory poolKey = PoolAddress.getPoolKey(params.token0, params.token1, FEE); + IUniswapV3Pool pool = IUniswapV3Pool(PoolAddress.computeAddress(factory, poolKey)); + + // Fetch the current tick from the Uniswap V3 pool + (, int24 currentTick, , , , , ) = pool.slot0(); + + // Check if current tick is within the specified range + require(currentTick >= tickLower && currentTick <= tickUpper, "Current tick out of range"); + + // load position + TokenPosition memory position = _positions[posKey(token, tickLower, tickUpper)]; + + // take the position out + uint256 (amount0, amount1) = pool.burn(tickLower, tickUpper, position.liquidity); + // TODO: this position might have earned fees, update them here + + // calculate liquidity + uint128 liquidity; + if (ETH_TOKEN_ZERO) { + // extend/contract the range up + uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(tickLower); + uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(currentTick + (currentTick - tickLower)); + liquidity = LiquidityAmounts.getLiquidityForAmount0( + sqrtRatioAX96, sqrtRatioBX96, amount0 + ) + // calculate amount for new liquidity + uint256 newAmount1 = LiquidityAmounts.getAmount1ForLiquidity( + sqrtRatioAX96, sqrtRatioBX96, liquidity + ) + if (newAmount1 > amount1) { + IERC20(token).mint(address(this), newAmount1 - amount1 + 1); + } else { + IERC20(token).burn(address(this), amount1 - newAmount1 + 1); + } + + } else { + // extend/contract the range down + uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(tickUpper); + uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(currentTick - (tickUpper - currentTick)); + liquidity = LiquidityAmounts.getLiquidityForAmount1( + sqrtRatioAX96, sqrtRatioBX96, ethAmountToProvide + ); + } + + + + + } } \ No newline at end of file