setup
This commit is contained in:
parent
97e36bc025
commit
f22910069a
3 changed files with 86 additions and 18 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -14,3 +14,4 @@ docs/
|
|||
.env
|
||||
.secret
|
||||
.infura
|
||||
.DS_Store
|
||||
18
.gitmodules
vendored
18
.gitmodules
vendored
|
|
@ -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
|
||||
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue