improve efficiency

This commit is contained in:
JulesCrown 2024-08-16 12:00:13 +02:00
parent 3ddf53b9e4
commit ff316a9fa1

View file

@ -61,7 +61,6 @@ contract LiquidityManager {
IUniswapV3Pool private immutable pool;
bool private immutable token0isWeth;
PoolKey private poolKey;
uint256 private harbPulled; // temporary variable to store amount of harb pulled by Uni
// the 3 positions this contract is managing
enum Stage { FLOOR, ANCHOR, DISCOVERY }
@ -116,7 +115,7 @@ contract LiquidityManager {
function uniswapV3MintCallback(uint256 amount0Owed, uint256 amount1Owed, bytes calldata) external {
CallbackValidation.verifyCallback(factory, poolKey);
// take care of harb
harbPulled = token0isWeth ? amount1Owed : amount0Owed;
uint256 harbPulled = token0isWeth ? amount1Owed : amount0Owed;
harb.mint(harbPulled);
// pack ETH
uint256 ethOwed = token0isWeth ? amount0Owed : amount1Owed;
@ -222,6 +221,27 @@ contract LiquidityManager {
});
}
/// @notice calculates token0/token1 amount based on range and liquidity
/// @dev Use TOKEN_ZERO as tokenZero value, when need to calculate TOKEN amount
/// Use !TOKEN_ZERO as tokenZero value, when need to calculate WETH amount
function getAmountForLiquidity(
int24 tickLower,
int24 tickUpper,
uint128 liquidity,
bool tokenZero
)
internal
pure
returns (uint256 amount)
{
uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(tickLower);
uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(tickUpper);
amount = tokenZero
? LiquidityAmounts.getAmount0ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity)
: LiquidityAmounts.getAmount1ForLiquidity(sqrtRatioAX96, sqrtRatioBX96, liquidity);
}
/// @notice Internal function to set or adjust the floor, anchor, and discovery positions based on current market conditions and the manager's strategy.
/// @param currentTick The current market tick.
/// @dev Recalculates and realigns all liquidity positions according to the latest market data and strategic requirements.
@ -246,6 +266,7 @@ contract LiquidityManager {
}
// set Anchor position
uint256 pulledHarb;
{
int24 tickLower = token0isWeth ? currentTick - ANCHOR_SPACING : vwapTick;
int24 tickUpper = token0isWeth ? vwapTick : currentTick + ANCHOR_SPACING;
@ -267,7 +288,7 @@ contract LiquidityManager {
sqrtRatioAX96, sqrtRatioX96, anchorEthBalance
);
}
pulledHarb = getAmountForLiquidity(tickLower, tickUpper, anchorLiquidity, !token0isWeth);
_mint(Stage.ANCHOR, tickLower, tickUpper, anchorLiquidity);
}
currentTick = currentTick / TICK_SPACING * TICK_SPACING;
@ -279,7 +300,7 @@ contract LiquidityManager {
uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(tickLower);
uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(tickUpper);
uint256 discoveryAmount = harbPulled * uint24(DISCOVERY_SPACING) * uint24(DISCOVERY_DEPTH) / uint24(ANCHOR_SPACING) / 100;
uint256 discoveryAmount = pulledHarb * uint24(DISCOVERY_SPACING) * uint24(DISCOVERY_DEPTH) / uint24(ANCHOR_SPACING) / 100;
uint128 liquidity;
if (token0isWeth) {
liquidity = LiquidityAmounts.getLiquidityForAmount1(
@ -333,8 +354,7 @@ contract LiquidityManager {
int24 floorTick = token0isWeth ? vwapTick + TICK_SPACING: vwapTick - TICK_SPACING;
uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(floorTick);
// adding a 2% balance margin, because liquidity calculations are inherently unprecise
floorEthBalance = floorEthBalance * 98 / 100;
floorEthBalance = (address(this).balance + weth.balanceOf(address(this)));
uint128 liquidity;
if (token0isWeth) {