From a3af722bb86c8625e588d0c4810b61f467178ec4 Mon Sep 17 00:00:00 2001 From: Eric Zhong Date: Fri, 15 Dec 2023 10:00:06 -0500 Subject: [PATCH] fix: faling fuzz tests (#441) * Add failing fuzz test * Add bounds * added out of range checks * changed assume to bound * forge fmt * changed Position -> Liquidity on merge main * moved amount helper to utils --------- Co-authored-by: Austin Adams --- .forge-snapshots/simpleSwapEOAInitiated.snap | 2 +- test/PoolManager.t.sol | 16 +++++++++-- test/utils/AmountHelpers.sol | 29 ++++++++++++++++++++ test/utils/Deployers.sol | 4 +-- 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 test/utils/AmountHelpers.sol diff --git a/.forge-snapshots/simpleSwapEOAInitiated.snap b/.forge-snapshots/simpleSwapEOAInitiated.snap index c71947255..8076015f9 100644 --- a/.forge-snapshots/simpleSwapEOAInitiated.snap +++ b/.forge-snapshots/simpleSwapEOAInitiated.snap @@ -1 +1 @@ -175475 \ No newline at end of file +173543 \ No newline at end of file diff --git a/test/PoolManager.t.sol b/test/PoolManager.t.sol index f458c8825..3dcfdcd15 100644 --- a/test/PoolManager.t.sol +++ b/test/PoolManager.t.sol @@ -26,6 +26,9 @@ import {PoolLockTest} from "../src/test/PoolLockTest.sol"; import {PoolId, PoolIdLibrary} from "../src/types/PoolId.sol"; import {FeeLibrary} from "../src/libraries/FeeLibrary.sol"; import {Position} from "../src/libraries/Position.sol"; +import {SafeCast} from "../src/libraries/SafeCast.sol"; +import {LiquidityAmounts} from "./utils/LiquidityAmounts.sol"; +import {AmountHelpers} from "./utils/AmountHelpers.sol"; contract PoolManagerTest is Test, Deployers, GasSnapshot { using Hooks for IHooks; @@ -373,13 +376,20 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot { snapEnd(); } - function test_swap_EOAInitiated() public { + function test_swap_EOAInitiated(uint256 swapAmount) public { IPoolManager.ModifyLiquidityParams memory liqParams = IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1e18}); modifyLiquidityRouter.modifyLiquidity(key, liqParams, ZERO_BYTES); - IPoolManager.SwapParams memory params = - IPoolManager.SwapParams({zeroForOne: true, amountSpecified: 100, sqrtPriceLimitX96: SQRT_RATIO_1_2}); + (uint256 amount0,) = AmountHelpers.getMaxAmountInForPool(manager, Deployers.LIQ_PARAMS, key); + // lower bound for precision purposes + swapAmount = uint256(bound(swapAmount, 100, amount0)); + + IPoolManager.SwapParams memory params = IPoolManager.SwapParams({ + zeroForOne: true, + amountSpecified: SafeCast.toInt256(swapAmount), + sqrtPriceLimitX96: SQRT_RATIO_1_2 + }); PoolSwapTest.TestSettings memory testSettings = PoolSwapTest.TestSettings({withdrawTokens: false, settleUsingTransfer: true, currencyAlreadySent: false}); diff --git a/test/utils/AmountHelpers.sol b/test/utils/AmountHelpers.sol new file mode 100644 index 000000000..0d48b9db8 --- /dev/null +++ b/test/utils/AmountHelpers.sol @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +import {LiquidityAmounts} from "./LiquidityAmounts.sol"; +import {IPoolManager} from "../../src/interfaces/IPoolManager.sol"; +import {PoolManager} from "../../src/PoolManager.sol"; +import {PoolId, PoolIdLibrary} from "../../src/types/PoolId.sol"; +import {TickMath} from "../../src/libraries/TickMath.sol"; +import {PoolKey} from "../../src/types/PoolKey.sol"; + +/// @title Calculate token<>liquidity +/// @notice Helps calculate amounts for bounding fuzz tests +library AmountHelpers { + function getMaxAmountInForPool( + PoolManager manager, + IPoolManager.ModifyLiquidityParams memory params, + PoolKey memory key + ) public view returns (uint256 amount0, uint256 amount1) { + PoolId id = PoolIdLibrary.toId(key); + uint128 liquidity = manager.getLiquidity(id); + (uint160 sqrtPriceX96,,) = manager.getSlot0(id); + + uint160 sqrtPriceX96Lower = TickMath.getSqrtRatioAtTick(params.tickLower); + uint160 sqrtPriceX96Upper = TickMath.getSqrtRatioAtTick(params.tickUpper); + + amount0 = LiquidityAmounts.getAmount0ForLiquidity(sqrtPriceX96Lower, sqrtPriceX96, liquidity); + amount1 = LiquidityAmounts.getAmount0ForLiquidity(sqrtPriceX96Upper, sqrtPriceX96, liquidity); + } +} diff --git a/test/utils/Deployers.sol b/test/utils/Deployers.sol index 23fc12c68..02ac8f494 100644 --- a/test/utils/Deployers.sol +++ b/test/utils/Deployers.sol @@ -36,9 +36,9 @@ contract Deployers { uint160 constant SQRT_RATIO_1_4 = Constants.SQRT_RATIO_1_4; uint160 constant SQRT_RATIO_4_1 = Constants.SQRT_RATIO_4_1; - IPoolManager.ModifyLiquidityParams internal LIQ_PARAMS = + IPoolManager.ModifyLiquidityParams public LIQ_PARAMS = IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: 1e18}); - IPoolManager.ModifyLiquidityParams internal REMOVE_LIQ_PARAMS = + IPoolManager.ModifyLiquidityParams public REMOVE_LIQ_PARAMS = IPoolManager.ModifyLiquidityParams({tickLower: -120, tickUpper: 120, liquidityDelta: -1e18}); // Global variables