Skip to content

Commit 7d19a7e

Browse files
committed
fuzz test
1 parent 35e2746 commit 7d19a7e

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

test/PoolManager.t.sol

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {IProtocolFees} from "../src/interfaces/IProtocolFees.sol";
3434
import {StateLibrary} from "../src/libraries/StateLibrary.sol";
3535
import {Actions} from "../src/test/ActionsRouter.sol";
3636

37+
import "forge-std/console2.sol";
38+
3739
contract PoolManagerTest is Test, Deployers, GasSnapshot {
3840
using Hooks for IHooks;
3941
using LPFeeLibrary for uint24;
@@ -808,7 +810,7 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
808810
assertEq(manager.protocolFeesAccrued(currency1), expectedProtocolFee);
809811
}
810812

811-
function test_swap_toLiquidity_fromMinPrice() public {
813+
function test_swap_toLiquidity_fromMinPrice_withLiquidity() public {
812814
PoolKey memory _key = PoolKey(currency0, currency1, 500, 10, IHooks(address(0)));
813815
manager.initialize(_key, TickMath.MIN_SQRT_PRICE);
814816

@@ -831,7 +833,59 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
831833
assertEq(TickMath.getTickAtSqrtPrice(sqrtPriceX96), -10);
832834
}
833835

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 {
835889
PoolKey memory _key = PoolKey(currency0, currency1, 500, 10, IHooks(address(0)));
836890
manager.initialize(_key, TickMath.MAX_SQRT_PRICE - 1);
837891

0 commit comments

Comments
 (0)