Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor #18

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/libraries/SwapFeeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
11 changes: 3 additions & 8 deletions src/pool-bin/interfaces/IBinDynamicFeeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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%
Expand Down
2 changes: 1 addition & 1 deletion src/pool-cl/CLPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
18 changes: 9 additions & 9 deletions test/libraries/FeeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Loading