diff --git a/onchain/src/BaseLineLP.sol b/onchain/src/BaseLineLP.sol index 3bfb0c5..c78318a 100644 --- a/onchain/src/BaseLineLP.sol +++ b/onchain/src/BaseLineLP.sol @@ -155,7 +155,6 @@ contract BaseLineLP { if (tokenAmount == 0) { sqrtPriceX96 = MIN_SQRT_RATIO; } else { - emit DEBUG(tokenAmount,ethAmount,0,0,token0isWeth); // Use a fixed-point library or more precise arithmetic for the division here. // For example, using ABDKMath64x64 for a more precise division and square root calculation. int128 priceRatio = ABDKMath64x64.div( @@ -166,7 +165,6 @@ contract BaseLineLP { sqrtPriceX96 = uint160( int160(ABDKMath64x64.sqrt(priceRatio) << 32) ); - emit DEBUG(uint256(int256(priceRatio >> 64)),uint256(sqrtPriceX96 >> 96),0,0,token0isWeth); } // Proceed as before. tick_ = TickMath.getTickAtSqrtRatio(sqrtPriceX96); @@ -252,10 +250,13 @@ contract BaseLineLP { uint256 _outstanding = outstanding(); if (_outstanding > 0) { floorTick = tickAtPrice(_outstanding, ethInFloor); - emit DEBUG(ethInFloor,_outstanding,floorTick,startTick,token0isWeth); - // put a position symetrically around the price, startTick being edge on one side floorTick = token0isWeth ? startTick + (floorTick - startTick) : floorTick - (startTick - floorTick); + + bool isOvercollateralized = token0isWeth ? floorTick < startTick : floorTick > startTick; + if (isOvercollateralized) { + floorTick = startTick + ((token0isWeth ? int24(1) : int24(-1)) * 400); + } } else { floorTick = startTick + ((token0isWeth ? int24(1) : int24(-1)) * 400); } @@ -269,7 +270,7 @@ contract BaseLineLP { token0isWeth ? ethInFloor : 0, token0isWeth ? 0 : ethInFloor ); - emit DEBUG(ethInFloor,uint256(liquidity),floorTick,startTick,token0isWeth); + // mint _mint(Stage.FLOOR, startTick, floorTick, liquidity); } @@ -387,8 +388,6 @@ contract BaseLineLP { // cap anchor size at 10 % of total ETH uint256 ethBalance = address(this).balance + weth.balanceOf(address(this)); - // event DEBUG(uint256 indexed eth, uint256 indexed outstanding, int24 indexed floorTick, int24 startTick, bool ethIs0); - emit DEBUG(ethInAnchor,weth.balanceOf(address(this)),0,0, token0isWeth); ethInAnchor = (ethInAnchor > ethBalance / 10) ? ethBalance / 10 : ethInAnchor; currentTick = currentTick / TICK_SPACING * TICK_SPACING; @@ -405,13 +404,15 @@ contract BaseLineLP { // Check if current tick is within the specified range int24 anchorTickLower = positions[Stage.ANCHOR].tickLower; int24 anchorTickUpper = positions[Stage.ANCHOR].tickUpper; + emit DEBUG(0, 0, anchorTickLower, anchorTickUpper, token0isWeth); // center tick can be calculated positive and negative numbers the same int24 centerTick = anchorTickLower + ((anchorTickUpper - anchorTickLower) / 2); - int24 amplitudeTick = anchorTickLower + (anchorTickUpper - anchorTickLower) * 3 / 20; + uint256 minAmplitude = uint256(uint24((anchorTickUpper - anchorTickLower) * 3 / 20)); // Determine the correct comparison direction based on token0isWeth - bool isDown = token0isWeth ? currentTick < centerTick : currentTick > centerTick; - bool isEnough = token0isWeth ? currentTick < amplitudeTick : currentTick > amplitudeTick; + bool isDown = token0isWeth ? currentTick > centerTick : currentTick < centerTick; + emit DEBUG(minAmplitude, 0, currentTick, centerTick, token0isWeth); + bool isEnough = SignedMath.abs(currentTick - centerTick) > minAmplitude; // Check Conditions require(isDown, "call shift(), not slide()"); @@ -451,7 +452,6 @@ contract BaseLineLP { // but cap anchor size at 10 % of total ETH ethInNewAnchor = (ethInNewAnchor > ethBalance / 10) ? ethBalance / 10 : ethInNewAnchor; - emit DEBUG(0, 0, currentTick, currentTick / TICK_SPACING * TICK_SPACING, token0isWeth); currentTick = currentTick / TICK_SPACING * TICK_SPACING; _set(sqrtPriceX96, currentTick, ethInNewAnchor); } diff --git a/onchain/test/BaseLineLP.t.sol b/onchain/test/BaseLineLP.t.sol index 0f86346..ce0e2f6 100644 --- a/onchain/test/BaseLineLP.t.sol +++ b/onchain/test/BaseLineLP.t.sol @@ -100,17 +100,15 @@ contract BaseLineLPTest is Test { vm.expectRevert(); liquidityManager.shift(); + // Setup of liquidity liquidityManager.slide(); - // Approve the pool to transfer WETH on behalf of the account + // Small buy into Anchor vm.prank(account); weth.deposit{value: 5 ether}(); vm.prank(account); weth.approve(address(this), 5 ether); - - - // Execute the swap pool.swap( account, // Recipient of the output tokens token0isWeth, @@ -120,6 +118,47 @@ contract BaseLineLPTest is Test { ); liquidityManager.shift(); + + // large buy into discovery + pool.swap( + account, // Recipient of the output tokens + token0isWeth, + int256(3 ether), + token0isWeth ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1, + abi.encode(account, int256(3 ether), true) + ); + + liquidityManager.shift(); + + + vm.prank(account); + harb.approve(address(this), 2000000 ether); + // sell into anchor + pool.swap( + account, // Recipient of the output tokens + !token0isWeth, + int256(300000 ether), + !token0isWeth ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1, + abi.encode(account, int256(300000 ether), false) + ); + + liquidityManager.slide(); + + harb.setLiquidityManager(address(account)); + vm.prank(account); + harb.mint(900000 ether); + harb.setLiquidityManager(address(liquidityManager)); + + // sell into floor + pool.swap( + account, // Recipient of the output tokens + !token0isWeth, + int256(900000 ether), + !token0isWeth ? TickMath.MIN_SQRT_RATIO + 1 : TickMath.MAX_SQRT_RATIO - 1, + abi.encode(account, int256(900000 ether), false) + ); + + liquidityManager.slide(); } /*//////////////////////////////////////////////////////////////