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

[PART-2] support same owner multiple positions #46

Merged
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
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181453
182049
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185322
185296
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137514
137576
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
330672
331250
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193574
193836
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137944
138533
Original file line number Diff line number Diff line change
@@ -1 +1 @@
29761
32563
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30350
30417
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147904
148635
Original file line number Diff line number Diff line change
@@ -1 +1 @@
295588
296486
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131153
131743
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120046
120042
Original file line number Diff line number Diff line change
@@ -1 +1 @@
976344
977200
Original file line number Diff line number Diff line change
@@ -1 +1 @@
338538
339390
Original file line number Diff line number Diff line change
@@ -1 +1 @@
342112
342867
Original file line number Diff line number Diff line change
@@ -1 +1 @@
145053
145810
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180358
180562
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186343
186545
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138725
138925
Original file line number Diff line number Diff line change
@@ -1 +1 @@
308628
309383
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34422
34487
Original file line number Diff line number Diff line change
@@ -1 +1 @@
362191
362972
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177359
178140
Original file line number Diff line number Diff line change
@@ -1 +1 @@
247478
248255
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170385
170425
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114308
114348
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59812
59778
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123719
124482
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141376
141710
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175053
175389
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25079271
25094185
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79245
79576
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153591
153925
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
95978
96383
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79248
79579
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32293
32291
2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsload.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7468
7446
28 changes: 26 additions & 2 deletions src/libraries/LPFeeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ library LPFeeLibrary {
error FeeTooLarge();

/// @dev the flag and mask
uint24 public constant STATIC_FEE_MASK = 0x0FFFFF;
uint24 public constant FEE_MASK = 0x7FFFFF;
uint24 public constant OVERRIDE_MASK = 0xBFFFFF;

// the top bit of the fee in a PoolKey is used to signal if a Pool's LP fee is dynamic
uint24 public constant DYNAMIC_FEE_FLAG = 0x800000;

// the second bit of the fee returned by beforeSwap is used to signal if the stored LP fee should be overridden in this swap
// only dynamic-fee pools can return a fee via the beforeSwap hook
uint24 public constant OVERRIDE_FEE_FLAG = 0x400000;

/// @dev the fee is represented in hundredths of a bip
/// @dev the max fee for cl pool is 100% and for bin, it is 10%
uint24 public constant ONE_HUNDRED_PERCENT_FEE = 1_000_000;
Expand All @@ -33,6 +40,23 @@ library LPFeeLibrary {
function getInitialLPFee(uint24 self) internal pure returns (uint24 lpFee) {
// the initial fee for a dynamic fee pool is 0
if (self.isDynamicLPFee()) return 0;
lpFee = self & STATIC_FEE_MASK;
lpFee = self & FEE_MASK;
}

/// @notice returns true if the fee has the override flag set (top bit of the uint24)
function isOverride(uint24 self) internal pure returns (bool) {
return self & OVERRIDE_FEE_FLAG != 0;
}

/// @notice returns a fee with the override flag removed
function removeOverrideFlag(uint24 self) internal pure returns (uint24) {
return self & OVERRIDE_MASK;
}

/// @notice Removes the override flag and validates the fee (reverts if the fee is too large)
function removeOverrideAndValidate(uint24 self, uint24 maxFee) internal pure returns (uint24) {
uint24 fee = self.removeOverrideFlag();
fee.validate(maxFee);
return fee;
}
}
34 changes: 24 additions & 10 deletions src/pool-bin/BinPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {LPFeeLibrary} from "../libraries/LPFeeLibrary.sol";
import {PackedUint128Math} from "./libraries/math/PackedUint128Math.sol";
import {Extsload} from "../Extsload.sol";
import {BinHooks} from "./libraries/BinHooks.sol";
import {BeforeSwapDelta} from "../types/BeforeSwapDelta.sol";
import "./interfaces/IBinHooks.sol";

/// @notice Holds the state for all bin pools
Expand Down Expand Up @@ -69,13 +70,13 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
}

/// @inheritdoc IBinPoolManager
function getPosition(PoolId id, address owner, uint24 binId)
function getPosition(PoolId id, address owner, uint24 binId, bytes32 salt)
external
view
override
returns (BinPosition.Info memory position)
{
return pools[id].positions.get(owner, binId);
return pools[id].positions.get(owner, binId, salt);
}

/// @inheritdoc IBinPoolManager
Expand Down Expand Up @@ -141,13 +142,19 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
PoolId id = key.toId();
_checkPoolInitialized(id);

(uint128 amountToSwap, int128 hookDeltaSpecified) = BinHooks.beforeSwap(key, swapForY, amountIn, hookData);
(uint128 amountToSwap, BeforeSwapDelta beforeSwapDelta, uint24 lpFeeOverride) =
BinHooks.beforeSwap(key, swapForY, amountIn, hookData);

/// @dev fix stack too deep
{
BinPool.SwapState memory state;
(delta, state) = pools[id].swap(
BinPool.SwapParams({swapForY: swapForY, binStep: key.parameters.getBinStep()}), amountToSwap
BinPool.SwapParams({
swapForY: swapForY,
binStep: key.parameters.getBinStep(),
lpFeeOverride: lpFeeOverride
}),
amountToSwap
);

unchecked {
Expand All @@ -164,7 +171,7 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
}

BalanceDelta hookDelta;
(delta, hookDelta) = BinHooks.afterSwap(key, swapForY, amountIn, delta, hookData, hookDeltaSpecified);
(delta, hookDelta) = BinHooks.afterSwap(key, swapForY, amountIn, delta, hookData, beforeSwapDelta);

if (hookDelta != BalanceDeltaLibrary.ZERO_DELTA) {
vault.accountPoolBalanceDelta(key, hookDelta, address(key.hooks));
Expand Down Expand Up @@ -248,7 +255,8 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
to: msg.sender,
liquidityConfigs: params.liquidityConfigs,
amountIn: params.amountIn,
binStep: key.parameters.getBinStep()
binStep: key.parameters.getBinStep(),
salt: params.salt
})
);

Expand All @@ -260,7 +268,7 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {
}

/// @notice Make sure the first event is noted, so that later events from afterHook won't get mixed up with this one
emit Mint(id, msg.sender, mintArray.ids, mintArray.amounts, compositionFee, feeForProtocol);
emit Mint(id, msg.sender, mintArray.ids, params.salt, mintArray.amounts, compositionFee, feeForProtocol);

BalanceDelta hookDelta;
(delta, hookDelta) = BinHooks.afterMint(key, params, delta, hookData);
Expand Down Expand Up @@ -290,11 +298,17 @@ contract BinPoolManager is IBinPoolManager, ProtocolFees, Extsload {

uint256[] memory binIds;
bytes32[] memory amountRemoved;
(delta, binIds, amountRemoved) =
pools[id].burn(BinPool.BurnParams({from: msg.sender, ids: params.ids, amountsToBurn: params.amountsToBurn}));
(delta, binIds, amountRemoved) = pools[id].burn(
BinPool.BurnParams({
from: msg.sender,
ids: params.ids,
amountsToBurn: params.amountsToBurn,
salt: params.salt
})
);

/// @notice Make sure the first event is noted, so that later events from afterHook won't get mixed up with this one
emit Burn(id, msg.sender, binIds, amountRemoved);
emit Burn(id, msg.sender, binIds, params.salt, amountRemoved);

BalanceDelta hookDelta;
(delta, hookDelta) = BinHooks.afterBurn(key, params, delta, hookData);
Expand Down
9 changes: 7 additions & 2 deletions src/pool-bin/interfaces/IBinHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {PoolKey} from "../../types/PoolKey.sol";
import {BalanceDelta} from "../../types/BalanceDelta.sol";
import {IBinPoolManager} from "./IBinPoolManager.sol";
import {IHooks} from "../../interfaces/IHooks.sol";
import {BeforeSwapDelta} from "../../types/BeforeSwapDelta.sol";

uint8 constant HOOKS_BEFORE_INITIALIZE_OFFSET = 0;
uint8 constant HOOKS_AFTER_INITIALIZE_OFFSET = 1;
Expand Down Expand Up @@ -111,10 +112,14 @@ interface IBinHooks is IHooks {
/// @param amountIn Amount of tokenX or tokenY in
/// @param hookData Arbitrary data handed into the PoolManager by the swapper to be be passed on to the hook
/// @return bytes4 The function selector for the hook
/// @return int128 The hook's delta in specified currency
/// @return BeforeSwapDelta The hook's delta in specified and unspecified currencies.
/// @return uint24 Optionally override the lp fee, only used if three conditions are met:
/// 1) the Pool has a dynamic fee,
/// 2) the value's override flag is set to 1 i.e. vaule & OVERRIDE_FEE_FLAG = 0x400000 != 0
/// 3) the value is less than or equal to the maximum fee (100_000) - 10%
function beforeSwap(address sender, PoolKey calldata key, bool swapForY, uint128 amountIn, bytes calldata hookData)
external
returns (bytes4, int128);
returns (bytes4, BeforeSwapDelta, uint24);

/// @notice The hook called after a swap
/// @param sender The initial msg.sender for the swap call
Expand Down
Loading
Loading