@@ -34,6 +34,8 @@ import {IProtocolFees} from "../src/interfaces/IProtocolFees.sol";
34
34
import {StateLibrary} from "../src/libraries/StateLibrary.sol " ;
35
35
import {Actions} from "../src/test/ActionsRouter.sol " ;
36
36
37
+ import "forge-std/console2.sol " ;
38
+
37
39
contract PoolManagerTest is Test , Deployers , GasSnapshot {
38
40
using Hooks for IHooks;
39
41
using LPFeeLibrary for uint24 ;
@@ -808,7 +810,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
808
810
assertEq (manager.protocolFeesAccrued (currency1), expectedProtocolFee);
809
811
}
810
812
811
- function test_swap_toLiquidity_fromMinPrice () public {
813
+ function test_swap_toLiquidity_fromMinPrice_withLiquidity () public {
812
814
PoolKey memory _key = PoolKey (currency0, currency1, 500 , 10 , IHooks (address (0 )));
813
815
manager.initialize (_key, TickMath.MIN_SQRT_PRICE);
814
816
@@ -831,7 +833,59 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
831
833
assertEq (TickMath.getTickAtSqrtPrice (sqrtPriceX96), - 10 );
832
834
}
833
835
834
- function test_swap_toPrice_fromMaxPrice () public {
836
+ function test_fuzz_swap_toLiquidity (uint160 sqrtPriceX96 ) public {
837
+ PoolKey memory _key = PoolKey (currency0, currency1, 500 , 10 , IHooks (address (0 )));
838
+ sqrtPriceX96 = uint160 (bound (sqrtPriceX96, TickMath.MIN_SQRT_PRICE, TickMath.MAX_SQRT_PRICE - 1 ));
839
+ vm.assume (sqrtPriceX96 != 0 );
840
+ manager.initialize (_key, sqrtPriceX96);
841
+
842
+ int24 tick = TickMath.getTickAtSqrtPrice (sqrtPriceX96);
843
+
844
+ bool zeroForOne;
845
+ uint160 sqrtPriceX96Limit;
846
+ int24 tickBoundary;
847
+ if (tick <= - 10 ) {
848
+ // zeroForOne=false to swap higher
849
+ zeroForOne = false ;
850
+ sqrtPriceX96Limit = TickMath.MAX_SQRT_PRICE - 1 ;
851
+ tickBoundary = - 10 ;
852
+ } else if (tick > 10 ) {
853
+ // zeroForOne=true to swap lower
854
+ zeroForOne = true ;
855
+ sqrtPriceX96Limit = TickMath.MIN_SQRT_PRICE + 1 ;
856
+ tickBoundary = 9 ;
857
+ } else {
858
+ // the price is between the position, so let's just swap down
859
+ zeroForOne = true ;
860
+ sqrtPriceX96Limit = TickMath.MIN_SQRT_PRICE + 1 ;
861
+ // the tick will just stay the same, we're only swapping 1 wei
862
+ tickBoundary = TickMath.getTickAtSqrtPrice (sqrtPriceX96);
863
+ }
864
+
865
+ // deeeeeep liquidity so that swapping 1 wei doesn't change the price too much if the price is within the tick range
866
+ IPoolManager.ModifyLiquidityParams memory params = IPoolManager.ModifyLiquidityParams ({
867
+ tickLower: - 10 ,
868
+ tickUpper: 10 ,
869
+ liquidityDelta: 100000000000e18 ,
870
+ salt: 0
871
+ });
872
+
873
+ modifyLiquidityRouter.modifyLiquidity (_key, params, ZERO_BYTES);
874
+
875
+ IPoolManager.SwapParams memory swapParams = IPoolManager.SwapParams (zeroForOne, 1 , sqrtPriceX96Limit);
876
+ PoolSwapTest.TestSettings memory testSettings =
877
+ PoolSwapTest.TestSettings ({takeClaims: false , settleUsingBurn: false });
878
+
879
+ swapRouter.swap (_key, swapParams, testSettings, ZERO_BYTES);
880
+
881
+ int24 _tick;
882
+ (sqrtPriceX96, _tick,,) = manager.getSlot0 (_key.toId ());
883
+
884
+ // The swap pushes the price to one of the tick boundaries.
885
+ assertEq (TickMath.getTickAtSqrtPrice (sqrtPriceX96), tickBoundary);
886
+ }
887
+
888
+ function test_swap_toPrice_fromMaxPrice_withoutLiquidity () public {
835
889
PoolKey memory _key = PoolKey (currency0, currency1, 500 , 10 , IHooks (address (0 )));
836
890
manager.initialize (_key, TickMath.MAX_SQRT_PRICE - 1 );
837
891
0 commit comments