Skip to content

Commit

Permalink
Merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
codyborn committed Apr 24, 2024
2 parents 8634f30 + e3c4dad commit 0e4fecb
Show file tree
Hide file tree
Showing 56 changed files with 546 additions and 219 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1947
2248
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactInPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3238
3038
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2208
1987
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactOutPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3238
3278
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1937
2238
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2826
3191
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2198
1977
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactOutPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2826
2866
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
259318
259212
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136661
136577
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139495
139411
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 1 token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
98620
98578
2 changes: 1 addition & 1 deletion .forge-snapshots/donate gas with 2 tokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127073
126989
2 changes: 1 addition & 1 deletion .forge-snapshots/erc20 collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24938
24960
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51159
51181
2 changes: 1 addition & 1 deletion .forge-snapshots/native collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36611
36633
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22646
22732
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126742
126824
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138135
138217
Original file line number Diff line number Diff line change
@@ -1 +1 @@
71077
71119
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61396
61438
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79289
79427
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn native 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
75132
75270
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint native output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135515
135557
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
152138
152220
Original file line number Diff line number Diff line change
@@ -1 +1 @@
158641
158759
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
90622
90704
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61375
61417
1 change: 1 addition & 0 deletions .forge-snapshots/swap with lp fee and protocol fee.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
151034
2 changes: 1 addition & 1 deletion .forge-snapshots/update dynamic fee in before swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131648
131731
20 changes: 10 additions & 10 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Hooks} from "./libraries/Hooks.sol";
import {Pool} from "./libraries/Pool.sol";
import {SafeCast} from "./libraries/SafeCast.sol";
import {Position} from "./libraries/Position.sol";
import {SwapFeeLibrary} from "./libraries/SwapFeeLibrary.sol";
import {LPFeeLibrary} from "./libraries/LPFeeLibrary.sol";
import {Currency, CurrencyLibrary} from "./types/Currency.sol";
import {PoolKey} from "./types/PoolKey.sol";
import {TickMath} from "./libraries/TickMath.sol";
Expand Down Expand Up @@ -34,7 +34,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
using Position for mapping(bytes32 => Position.Info);
using CurrencyLibrary for Currency;
using CurrencyDelta for Currency;
using SwapFeeLibrary for uint24;
using LPFeeLibrary for uint24;
using PoolGetters for Pool.State;
using Reserves for Currency;

Expand All @@ -57,11 +57,11 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
external
view
override
returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 swapFee)
returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 lpFee)
{
Pool.Slot0 memory slot0 = pools[id].slot0;

return (slot0.sqrtPriceX96, slot0.tick, slot0.protocolFee, slot0.swapFee);
return (slot0.sqrtPriceX96, slot0.tick, slot0.protocolFee, slot0.lpFee);
}

/// @inheritdoc IPoolManager
Expand Down Expand Up @@ -117,14 +117,14 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
if (key.currency0 >= key.currency1) revert CurrenciesOutOfOrderOrEqual();
if (!key.hooks.isValidHookAddress(key.fee)) revert Hooks.HookAddressNotValid(address(key.hooks));

uint24 swapFee = key.fee.getInitialSwapFee();
uint24 lpFee = key.fee.getInitialLPFee();

key.hooks.beforeInitialize(key, sqrtPriceX96, hookData);

PoolId id = key.toId();
(, uint24 protocolFee) = _fetchProtocolFee(key);

tick = pools[id].initialize(sqrtPriceX96, protocolFee, swapFee);
tick = pools[id].initialize(sqrtPriceX96, protocolFee, lpFee);

key.hooks.afterInitialize(key, sqrtPriceX96, tick, hookData);

Expand Down Expand Up @@ -298,11 +298,11 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
_burnFrom(from, id, amount);
}

function updateDynamicSwapFee(PoolKey memory key, uint24 newDynamicSwapFee) external {
if (!key.fee.isDynamicFee() || msg.sender != address(key.hooks)) revert UnauthorizedDynamicSwapFeeUpdate();
newDynamicSwapFee.validate();
function updateDynamicLPFee(PoolKey memory key, uint24 newDynamicLPFee) external {
if (!key.fee.isDynamicFee() || msg.sender != address(key.hooks)) revert UnauthorizedDynamicLPFeeUpdate();
newDynamicLPFee.validate();
PoolId id = key.toId();
pools[id].setSwapFee(newDynamicSwapFee);
pools[id].setLPFee(newDynamicLPFee);
}

function getNonzeroDeltaCount() external view returns (uint256 _nonzeroDeltaCount) {
Expand Down
11 changes: 6 additions & 5 deletions src/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload {
/// @notice PoolKey must have currencies where address(currency0) < address(currency1)
error CurrenciesOutOfOrderOrEqual();

/// @notice Thrown when a call to updateDynamicSwapFee is made by an address that is not the hook,
/// @notice Thrown when a call to updateDynamicLPFee is made by an address that is not the hook,
/// or on a pool that does not have a dynamic swap fee.
error UnauthorizedDynamicSwapFeeUpdate();
error UnauthorizedDynamicLPFeeUpdate();

///@notice Thrown when native currency is passed to a non native settlement
error NonZeroNativeValue();
Expand Down Expand Up @@ -69,6 +69,7 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload {
/// @param sqrtPriceX96 The sqrt(price) of the pool after the swap, as a Q64.96
/// @param liquidity The liquidity of the pool after the swap
/// @param tick The log base 1.0001 of the price of the pool after the swap
/// @param fee The swap fee in hundredths of a bip
event Swap(
PoolId indexed id,
address sender,
Expand All @@ -90,7 +91,7 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload {
function getSlot0(PoolId id)
external
view
returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 swapFee);
returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 lpFee);

/// @notice Get the current value of liquidity of the given pool
function getLiquidity(PoolId id) external view returns (uint128 liquidity);
Expand Down Expand Up @@ -193,8 +194,8 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload {
/// @notice Called by the user to pay what is owed
function settle(Currency token) external payable returns (uint256 paid);

/// @notice Updates the pools swap fees for the a pool that has enabled dynamic swap fees.
function updateDynamicSwapFee(PoolKey memory key, uint24 newDynamicSwapFee) external;
/// @notice Updates the pools lp fees for the a pool that has enabled dynamic lp fees.
function updateDynamicLPFee(PoolKey memory key, uint24 newDynamicLPFee) external;

function getReserves(Currency currency) external view returns (uint256 balance);
}
4 changes: 2 additions & 2 deletions src/libraries/Hooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.24;

import {PoolKey} from "../types/PoolKey.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {SwapFeeLibrary} from "./SwapFeeLibrary.sol";
import {LPFeeLibrary} from "./LPFeeLibrary.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";

Expand All @@ -12,7 +12,7 @@ import {IPoolManager} from "../interfaces/IPoolManager.sol";
/// For example, a hooks contract deployed to address: 0x9000000000000000000000000000000000000000
/// has leading bits '1001' which would cause the 'before initialize' and 'after add liquidity' hooks to be used.
library Hooks {
using SwapFeeLibrary for uint24;
using LPFeeLibrary for uint24;
using Hooks for IHooks;

uint256 internal constant BEFORE_INITIALIZE_FLAG = 1 << 159;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ pragma solidity ^0.8.20;

import {PoolKey} from "../types/PoolKey.sol";

library SwapFeeLibrary {
using SwapFeeLibrary for uint24;
library LPFeeLibrary {
using LPFeeLibrary for uint24;

/// @notice Thrown when the static or dynamic fee on a pool exceeds 100%.
error FeeTooLarge();

uint24 public constant STATIC_FEE_MASK = 0x7FFFFF;
uint24 public constant DYNAMIC_FEE_FLAG = 0x800000;

// the swap fee is represented in hundredths of a bip, so the max is 100%
uint24 public constant MAX_SWAP_FEE = 1000000;
// the lp fee is represented in hundredths of a bip, so the max is 100%
uint24 public constant MAX_LP_FEE = 1000000;

function isDynamicFee(uint24 self) internal pure returns (bool) {
return self & DYNAMIC_FEE_FLAG != 0;
}

function validate(uint24 self) internal pure {
if (self > MAX_SWAP_FEE) revert FeeTooLarge();
if (self > MAX_LP_FEE) revert FeeTooLarge();
}

function getInitialSwapFee(uint24 self) internal pure returns (uint24 swapFee) {
function getInitialLPFee(uint24 self) internal pure returns (uint24 lpFee) {
// the initial fee for a dynamic fee pool is 0
if (self.isDynamicFee()) return 0;
swapFee = self & STATIC_FEE_MASK;
swapFee.validate();
lpFee = self & STATIC_FEE_MASK;
lpFee.validate();
}
}
9 changes: 5 additions & 4 deletions src/libraries/NonZeroDeltaCount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import {IHooks} from "../interfaces/IHooks.sol";
/// TODO: This library can be deleted when we have the transient keyword support in solidity.
library NonZeroDeltaCount {
// The slot holding the number of nonzero deltas. uint256(keccak256("NonzeroDeltaCount")) - 1
uint256 constant NONZERO_DELTA_COUNT = uint256(0x7d4b3164c6e45b97e7d87b7125a44c5828d005af88f9d751cfd78729c5d99a0b);
uint256 constant NONZERO_DELTA_COUNT_SLOT =
uint256(0x7d4b3164c6e45b97e7d87b7125a44c5828d005af88f9d751cfd78729c5d99a0b);

function read() internal view returns (uint256 count) {
uint256 slot = NONZERO_DELTA_COUNT;
uint256 slot = NONZERO_DELTA_COUNT_SLOT;
assembly {
count := tload(slot)
}
}

function increment() internal {
uint256 slot = NONZERO_DELTA_COUNT;
uint256 slot = NONZERO_DELTA_COUNT_SLOT;
assembly {
let count := tload(slot)
count := add(count, 1)
Expand All @@ -29,7 +30,7 @@ library NonZeroDeltaCount {
/// @notice Potential to underflow.
/// Current usage ensures this will not happen because we call decrement with known boundaries (only up to the number of times we call increment).
function decrement() internal {
uint256 slot = NONZERO_DELTA_COUNT;
uint256 slot = NONZERO_DELTA_COUNT_SLOT;
assembly {
let count := tload(slot)
count := sub(count, 1)
Expand Down
Loading

0 comments on commit 0e4fecb

Please sign in to comment.