Skip to content

Commit

Permalink
fix: faling fuzz tests (Uniswap#441)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and hyunchel committed Feb 21, 2024
1 parent f07fcd3 commit a3af722
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/simpleSwapEOAInitiated.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175475
173543
16 changes: 13 additions & 3 deletions test/PoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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});
Expand Down
29 changes: 29 additions & 0 deletions test/utils/AmountHelpers.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 2 additions & 2 deletions test/utils/Deployers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a3af722

Please sign in to comment.