diff --git a/src/libraries/SwapFeeLibrary.sol b/src/libraries/SwapFeeLibrary.sol index b19640f1..4f9203af 100644 --- a/src/libraries/SwapFeeLibrary.sol +++ b/src/libraries/SwapFeeLibrary.sol @@ -29,7 +29,9 @@ library SwapFeeLibrary { return self > maxFee; } - function getSwapFee(uint24 self) internal pure returns (uint24 swapFee) { + /// @return swapFee initial swap fee for the pool. For dynamic fee pool, query + /// poolManager.getSlot0(poolId) to get the current swapFee instead + function getInitialSwapFee(uint24 self) internal pure returns (uint24 swapFee) { // the initial fee for a dynamic fee pool is 0 if (self.isDynamicSwapFee()) return 0; swapFee = self & STATIC_FEE_MASK; diff --git a/src/pool-bin/BinPoolManager.sol b/src/pool-bin/BinPoolManager.sol index feef7b4f..bd5312fe 100644 --- a/src/pool-bin/BinPoolManager.sol +++ b/src/pool-bin/BinPoolManager.sol @@ -111,7 +111,7 @@ contract BinPoolManager is IBinPoolManager, Fees, Extsload { _validateHookNoOp(key); /// @notice init value for dynamic swap fee is 0, but hook can still set it in afterInitialize - uint24 swapFee = key.fee.getSwapFee(); + uint24 swapFee = key.fee.getInitialSwapFee(); if (swapFee.isSwapFeeTooLarge(SwapFeeLibrary.TEN_PERCENT_FEE)) revert FeeTooLarge(); if (key.parameters.shouldCall(HOOKS_BEFORE_INITIALIZE_OFFSET)) { @@ -203,7 +203,7 @@ contract BinPoolManager is IBinPoolManager, Fees, Extsload { if (totalSwapFee > SwapFeeLibrary.TEN_PERCENT_FEE) revert FeeTooLarge(); } else { // clear the top 4 bits since they may be flagged - totalSwapFee = key.fee.getSwapFee(); + totalSwapFee = key.fee.getInitialSwapFee(); } (amountIn, amountOutLeft, fee) = pools[id].getSwapIn( @@ -228,7 +228,7 @@ contract BinPoolManager is IBinPoolManager, Fees, Extsload { IBinDynamicFeeManager(address(key.hooks)).getFeeForSwapInSwapOut(msg.sender, key, swapForY, amountIn, 0); if (totalSwapFee > SwapFeeLibrary.TEN_PERCENT_FEE) revert FeeTooLarge(); } else { - totalSwapFee = key.fee.getSwapFee(); + totalSwapFee = key.fee.getInitialSwapFee(); } (amountInLeft, amountOut, fee) = pools[id].getSwapOut( diff --git a/src/pool-bin/interfaces/IBinDynamicFeeManager.sol b/src/pool-bin/interfaces/IBinDynamicFeeManager.sol index 74b9f939..622c4965 100644 --- a/src/pool-bin/interfaces/IBinDynamicFeeManager.sol +++ b/src/pool-bin/interfaces/IBinDynamicFeeManager.sol @@ -4,16 +4,11 @@ pragma solidity ^0.8.24; import {PoolKey} from "../../types/PoolKey.sol"; import {IBinPoolManager} from "./IBinPoolManager.sol"; -/// @notice The dynamic fee manager determines fees for pools -/// @dev note that this pool is only called if the PoolKey fee value is equal to the DYNAMIC_FEE magic value +/// @notice The dynamic fee manager determines swap fees for pools +/// @dev note that this pool is only called if the PoolKey.fee has dynamic fee flag set. interface IBinDynamicFeeManager { - /// @notice Called to look up swap fee for pool when PoolManager#updateDynamicSwapFee is called - /// @return swapFee 10_000 represent 1%, 3_000 represent 0.3% - function getFee(address sender, PoolKey calldata key) external view returns (uint24); - /// @notice Called whenever BinPoolManager getSwapIn or getSwapOut is called - /// @dev getSwapIn or getSwapOut was added to allow on-chain quotes for integrators. For hook dev, this is similar to getFee, with extra swap parameter (swapForY and amount). - /// Hooks should ensure that this returns the same value as if the user was to perform an actual swap. + /// @dev getSwapIn or getSwapOut was added to allow on-chain quotes for integrators /// @param amountIn either amountIn or amountOut will be non-zero, if amountIn non-zero, imply a swap with amountIn /// @param amountOut either amountIn or amountOut will be non-zero, if amountOut non-zero, imply a swap with amountOut /// @return swapFee 10_000 represent 1%, 3_000 represent 0.3% diff --git a/src/pool-cl/CLPoolManager.sol b/src/pool-cl/CLPoolManager.sol index dbed1c4e..10044b49 100644 --- a/src/pool-cl/CLPoolManager.sol +++ b/src/pool-cl/CLPoolManager.sol @@ -106,7 +106,7 @@ contract CLPoolManager is ICLPoolManager, Fees, Extsload { _validateHookNoOp(key); /// @notice init value for dynamic swap fee is 0, but hook can still set it in afterInitialize - uint24 swapFee = key.fee.getSwapFee(); + uint24 swapFee = key.fee.getInitialSwapFee(); if (swapFee.isSwapFeeTooLarge(SwapFeeLibrary.ONE_HUNDRED_PERCENT_FEE)) revert FeeTooLarge(); if (key.parameters.shouldCall(HOOKS_BEFORE_INITIALIZE_OFFSET)) { diff --git a/test/libraries/FeeLibrary.sol b/test/libraries/FeeLibrary.sol index 1c273a65..8ebee853 100644 --- a/test/libraries/FeeLibrary.sol +++ b/test/libraries/FeeLibrary.sol @@ -30,19 +30,19 @@ contract SwapFeeLibraryTest is Test { function testGetSwapFee() public { // static - assertEq(SwapFeeLibrary.getSwapFee(0x000001), 0x000001); - assertEq(SwapFeeLibrary.getSwapFee(0x000002), 0x000002); - assertEq(SwapFeeLibrary.getSwapFee(0x0F0003), 0x0F0003); - assertEq(SwapFeeLibrary.getSwapFee(0x001004), 0x001004); - assertEq(SwapFeeLibrary.getSwapFee(0x111020), 0x011020); - assertEq(SwapFeeLibrary.getSwapFee(0x101020), 0x001020); + assertEq(SwapFeeLibrary.getInitialSwapFee(0x000001), 0x000001); + assertEq(SwapFeeLibrary.getInitialSwapFee(0x000002), 0x000002); + assertEq(SwapFeeLibrary.getInitialSwapFee(0x0F0003), 0x0F0003); + assertEq(SwapFeeLibrary.getInitialSwapFee(0x001004), 0x001004); + assertEq(SwapFeeLibrary.getInitialSwapFee(0x111020), 0x011020); + assertEq(SwapFeeLibrary.getInitialSwapFee(0x101020), 0x001020); // dynamic - assertEq(SwapFeeLibrary.getSwapFee(0xF00F05), 0); - assertEq(SwapFeeLibrary.getSwapFee(0x800310), 0); + assertEq(SwapFeeLibrary.getInitialSwapFee(0xF00F05), 0); + assertEq(SwapFeeLibrary.getInitialSwapFee(0x800310), 0); } function testFuzzIsStaicFeeTooLarge(uint24 self, uint24 maxFee) public { - assertEq(self.getSwapFee() > maxFee, self.getSwapFee().isSwapFeeTooLarge(maxFee)); + assertEq(self.getInitialSwapFee() > maxFee, self.getInitialSwapFee().isSwapFeeTooLarge(maxFee)); } }