From 3ad999af50d02622179ca5fd4527eac6ebb99838 Mon Sep 17 00:00:00 2001 From: Sara Reynolds Date: Tue, 26 Sep 2023 19:18:56 -0400 Subject: [PATCH] clean tests --- contracts/test/BaseTestHooks.sol | 87 +++++++++++++++++ contracts/test/DynamicFeesTest.sol | 47 ++++++++++ test/foundry-tests/DynamicFees.t.sol | 123 +++---------------------- test/foundry-tests/utils/Deployers.sol | 6 ++ 4 files changed, 151 insertions(+), 112 deletions(-) create mode 100644 contracts/test/BaseTestHooks.sol create mode 100644 contracts/test/DynamicFeesTest.sol diff --git a/contracts/test/BaseTestHooks.sol b/contracts/test/BaseTestHooks.sol new file mode 100644 index 000000000..7404f5851 --- /dev/null +++ b/contracts/test/BaseTestHooks.sol @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +import {IHooks} from "../interfaces/IHooks.sol"; +import {PoolKey} from "../types/PoolKey.sol"; +import {BalanceDelta} from "../types/BalanceDelta.sol"; +import {IPoolManager} from "../interfaces/IPoolManager.sol"; + +contract BaseTestHooks is IHooks { + error HookNotImplemented(); + + function beforeInitialize(address sender, PoolKey calldata key, uint160 sqrtPriceX96, bytes calldata hookData) + external + virtual + returns (bytes4) + { + revert HookNotImplemented(); + } + + function afterInitialize( + address sender, + PoolKey calldata key, + uint160 sqrtPriceX96, + int24 tick, + bytes calldata hookData + ) external virtual returns (bytes4) { + revert HookNotImplemented(); + } + + function beforeModifyPosition( + address sender, + PoolKey calldata key, + IPoolManager.ModifyPositionParams calldata params, + bytes calldata hookData + ) external virtual returns (bytes4) { + revert HookNotImplemented(); + } + + function afterModifyPosition( + address sender, + PoolKey calldata key, + IPoolManager.ModifyPositionParams calldata params, + BalanceDelta delta, + bytes calldata hookData + ) external virtual returns (bytes4) { + revert HookNotImplemented(); + } + + function beforeSwap( + address sender, + PoolKey calldata key, + IPoolManager.SwapParams calldata params, + bytes calldata hookData + ) external virtual returns (bytes4) { + revert HookNotImplemented(); + } + + function afterSwap( + address sender, + PoolKey calldata key, + IPoolManager.SwapParams calldata params, + BalanceDelta delta, + bytes calldata hookData + ) external virtual returns (bytes4) { + revert HookNotImplemented(); + } + + function beforeDonate( + address sender, + PoolKey calldata key, + uint256 amount0, + uint256 amount1, + bytes calldata hookData + ) external virtual returns (bytes4) { + revert HookNotImplemented(); + } + + function afterDonate( + address sender, + PoolKey calldata key, + uint256 amount0, + uint256 amount1, + bytes calldata hookData + ) external virtual returns (bytes4) { + revert HookNotImplemented(); + } +} diff --git a/contracts/test/DynamicFeesTest.sol b/contracts/test/DynamicFeesTest.sol new file mode 100644 index 000000000..9ccb10978 --- /dev/null +++ b/contracts/test/DynamicFeesTest.sol @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +import {BaseTestHooks} from "./BaseTestHooks.sol"; +import {IDynamicFeeManager} from "../interfaces/IDynamicFeeManager.sol"; +import {PoolKey} from "../types/PoolKey.sol"; +import {IPoolManager} from "../interfaces/IPoolManager.sol"; +import {IHooks} from "../interfaces/IHooks.sol"; + +contract DynamicFeesTest is BaseTestHooks, IDynamicFeeManager { + uint24 internal fee; + IPoolManager manager; + + constructor() {} + + function setManager(IPoolManager _manager) external { + manager = _manager; + } + + function setFee(uint24 _fee) external { + fee = _fee; + } + + function getFee(address, PoolKey calldata) public view returns (uint24) { + return fee; + } + + function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata, bytes calldata hookData) + external + override + returns (bytes4) + { + // updates the dynamic fee in the pool if update is true + bool _update; + uint24 _fee; + + if (hookData.length > 0) { + (_update, _fee) = abi.decode(hookData, (bool, uint24)); + } + if (_update == true) { + fee = _fee; + + manager.setDynamicFee(key); + } + return IHooks.beforeSwap.selector; + } +} diff --git a/test/foundry-tests/DynamicFees.t.sol b/test/foundry-tests/DynamicFees.t.sol index d01bfc546..91a108921 100644 --- a/test/foundry-tests/DynamicFees.t.sol +++ b/test/foundry-tests/DynamicFees.t.sol @@ -9,126 +9,18 @@ import {FeeLibrary} from "../../contracts/libraries/FeeLibrary.sol"; import {IPoolManager} from "../../contracts/interfaces/IPoolManager.sol"; import {IFees} from "../../contracts/interfaces/IFees.sol"; import {IHooks} from "../../contracts/interfaces/IHooks.sol"; -import {Currency} from "../../contracts/types/Currency.sol"; import {PoolKey} from "../../contracts/types/PoolKey.sol"; import {PoolManager} from "../../contracts/PoolManager.sol"; import {PoolSwapTest} from "../../contracts/test/PoolSwapTest.sol"; import {Deployers} from "./utils/Deployers.sol"; import {IDynamicFeeManager} from "././../../contracts/interfaces/IDynamicFeeManager.sol"; -import {Fees} from "./../../contracts/Fees.sol"; import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol"; -import {IHooks} from "../../contracts/interfaces/IHooks.sol"; -import {IPoolManager} from "../../contracts/interfaces/IPoolManager.sol"; -import {BalanceDelta} from "../../contracts/types/BalanceDelta.sol"; - -contract DynamicFees is IHooks, IDynamicFeeManager { - uint24 internal fee; - IPoolManager manager; - - constructor() {} - - function setManager(IPoolManager _manager) external { - manager = _manager; - } - - function setFee(uint24 _fee) external { - fee = _fee; - } - - function getFee(address, PoolKey calldata) public view returns (uint24) { - return fee; - } - - function beforeSwap( - address sender, - PoolKey calldata key, - IPoolManager.SwapParams calldata params, - bytes calldata hookData - ) external returns (bytes4) { - // updates the dynamic fee in the pool if update is true - bool _update; - uint24 _fee; - - if (hookData.length > 0) { - (_update, _fee) = abi.decode(hookData, (bool, uint24)); - } - if (_update == true) { - fee = _fee; - - manager.setDynamicFee(key); - } - return IHooks.beforeSwap.selector; - } - - function beforeInitialize(address, PoolKey calldata, uint160, bytes calldata) - external - view - override - returns (bytes4) - { - revert("not implemented"); - } - - function afterInitialize(address, PoolKey calldata, uint160, int24, bytes calldata) - external - view - override - returns (bytes4) - { - revert("not implemented"); - } - - function beforeModifyPosition(address, PoolKey calldata, IPoolManager.ModifyPositionParams calldata, bytes calldata) - external - view - override - returns (bytes4) - { - revert("not implemented"); - } - - function afterModifyPosition( - address, - PoolKey calldata, - IPoolManager.ModifyPositionParams calldata, - BalanceDelta, - bytes calldata - ) external view override returns (bytes4) { - revert("not implemented"); - } - - function afterSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, BalanceDelta, bytes calldata) - external - view - override - returns (bytes4) - { - revert("not implemented"); - } - - function beforeDonate(address, PoolKey calldata, uint256, uint256, bytes calldata) - external - view - override - returns (bytes4) - { - revert("not implemented"); - } - - function afterDonate(address, PoolKey calldata, uint256, uint256, bytes calldata) - external - view - override - returns (bytes4) - { - revert("not implemented"); - } -} +import {DynamicFeesTest} from "../../contracts/test/DynamicFeesTest.sol"; contract TestDynamicFees is Test, Deployers, GasSnapshot { using PoolIdLibrary for PoolKey; - DynamicFees dynamicFees = DynamicFees( + DynamicFeesTest dynamicFees = DynamicFeesTest( address( uint160(0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF) & uint160( @@ -139,7 +31,7 @@ contract TestDynamicFees is Test, Deployers, GasSnapshot { ) ); - DynamicFees dynamicFeesNoHook = DynamicFees( + DynamicFeesTest dynamicFeesNoHook = DynamicFeesTest( address( uint160(0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF) & uint160( @@ -156,7 +48,7 @@ contract TestDynamicFees is Test, Deployers, GasSnapshot { PoolSwapTest swapRouter; function setUp() public { - DynamicFees impl = new DynamicFees(); + DynamicFeesTest impl = new DynamicFeesTest(); vm.etch(address(dynamicFees), address(impl).code); vm.etch(address(dynamicFeesNoHook), address(impl).code); @@ -173,6 +65,13 @@ contract TestDynamicFees is Test, Deployers, GasSnapshot { swapRouter = new PoolSwapTest(manager); } + function testPoolInitializeFailsWithTooLargeFee() public { + dynamicFees.setFee(1000000); + PoolKey memory key0 = Deployers.createKey(IHooks(address(dynamicFees)), FeeLibrary.DYNAMIC_FEE_FLAG); + vm.expectRevert(IFees.FeeTooLarge.selector); + manager.initialize(key0, SQRT_RATIO_1_1, ZERO_BYTES); + } + function testSwapFailsWithTooLargeFee() public { dynamicFees.setFee(1000000); vm.expectRevert(IFees.FeeTooLarge.selector); diff --git a/test/foundry-tests/utils/Deployers.sol b/test/foundry-tests/utils/Deployers.sol index ce7aef2e0..55eadb7d9 100644 --- a/test/foundry-tests/utils/Deployers.sol +++ b/test/foundry-tests/utils/Deployers.sol @@ -54,6 +54,12 @@ contract Deployers { manager.initialize(key, sqrtPriceX96, initData); } + function createKey(IHooks hooks, uint24 fee) internal returns (PoolKey memory key) { + MockERC20[] memory tokens = deployTokens(2, 2 ** 255); + (Currency currency0, Currency currency1) = SortTokens.sort(tokens[0], tokens[1]); + key = PoolKey(currency0, currency1, fee, fee.isDynamicFee() ? int24(60) : int24(fee / 100 * 2), hooks); + } + function createFreshPool(IHooks hooks, uint24 fee, uint160 sqrtPriceX96) internal returns (PoolManager manager, PoolKey memory key, PoolId id)