Skip to content

Commit

Permalink
just have 1 check function (#714)
Browse files Browse the repository at this point in the history
* just have 1 check function

* remove double pool lookup

---------

Co-authored-by: Alice Henshaw <[email protected]>
  • Loading branch information
snreynolds and hensha256 authored May 24, 2024
1 parent 05f986d commit 3118115
Show file tree
Hide file tree
Showing 34 changed files with 51 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149414
149347
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
326118
326051
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 @@
280232
280165
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 @@
139620
139553
Original file line number Diff line number Diff line change
@@ -1 +1 @@
297592
297525
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 @@
106629
106609
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 @@
147124
147104
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20003
19971
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181711
181644
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136000
135933
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115828
115761
2 changes: 1 addition & 1 deletion .forge-snapshots/set protocol fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32444
32478
Original file line number Diff line number Diff line change
@@ -1 +1 @@
102856
102789
2 changes: 1 addition & 1 deletion .forge-snapshots/simple addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
165348
165281
Original file line number Diff line number Diff line change
@@ -1 +1 @@
96571
96504
2 changes: 1 addition & 1 deletion .forge-snapshots/simple removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88611
88544
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 @@
114950
114887
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130115
130052
2 changes: 1 addition & 1 deletion .forge-snapshots/swap CA custom curve + swap noop.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132084
132021
2 changes: 1 addition & 1 deletion .forge-snapshots/swap CA fee on unspecified.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179590
179527
Original file line number Diff line number Diff line change
@@ -1 +1 @@
110342
110279
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121689
121626
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 @@
133518
133455
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 @@
122806
122743
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 @@
144594
144531
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 @@
161224
161161
Original file line number Diff line number Diff line change
@@ -1 +1 @@
217673
217547
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 @@
145425
145362
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138665
138602
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with lp fee and protocol fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
176623
176594
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with return dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
152437
152374
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 @@
155039
155010
29 changes: 14 additions & 15 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
bytes calldata hookData
) external override onlyWhenUnlocked returns (BalanceDelta callerDelta, BalanceDelta feesAccrued) {
PoolId id = key.toId();
_checkPoolInitialized(id);
Pool.State storage pool = _getPool(id);
pool.checkPoolInitialized();

key.hooks.beforeModifyLiquidity(key, params, hookData);

BalanceDelta principalDelta;
(principalDelta, feesAccrued) = _pools[id].modifyLiquidity(
(principalDelta, feesAccrued) = pool.modifyLiquidity(
Pool.ModifyLiquidityParams({
owner: msg.sender,
tickLower: params.tickLower,
Expand Down Expand Up @@ -193,9 +194,9 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
returns (BalanceDelta swapDelta)
{
if (params.amountSpecified == 0) SwapAmountCannotBeZero.selector.revertWith();

PoolId id = key.toId();
_checkPoolInitialized(id);
Pool.State storage pool = _getPool(id);
pool.checkPoolInitialized();

BeforeSwapDelta beforeSwapDelta;
{
Expand All @@ -205,6 +206,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim

// execute swap, account protocol fees, and emit swap event
swapDelta = _swap(
pool,
id,
Pool.SwapParams({
tickSpacing: key.tickSpacing,
Expand All @@ -227,9 +229,11 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
}

/// @notice Internal swap function to execute a swap, take protocol fees on input token, and emit the swap event
function _swap(PoolId id, Pool.SwapParams memory params, Currency inputCurrency) internal returns (BalanceDelta) {
(BalanceDelta delta, uint256 feeForProtocol, uint24 swapFee, Pool.SwapState memory state) =
_pools[id].swap(params);
function _swap(Pool.State storage pool, PoolId id, Pool.SwapParams memory params, Currency inputCurrency)
internal
returns (BalanceDelta)
{
(BalanceDelta delta, uint256 feeForProtocol, uint24 swapFee, Pool.SwapState memory state) = pool.swap(params);

// The fee is on the input currency.
if (feeForProtocol > 0) _updateProtocolFees(inputCurrency, feeForProtocol);
Expand All @@ -248,12 +252,12 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
onlyWhenUnlocked
returns (BalanceDelta delta)
{
PoolId id = key.toId();
_checkPoolInitialized(id);
Pool.State storage pool = _getPool(key.toId());
pool.checkPoolInitialized();

key.hooks.beforeDonate(key, amount0, amount1, hookData);

delta = _pools[id].donate(amount0, amount1);
delta = pool.donate(amount0, amount1);

_accountPoolBalanceDelta(key, delta, msg.sender);

Expand Down Expand Up @@ -330,11 +334,6 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
_accountDelta(key.currency1, delta.amount1(), target);
}

/// @notice Checks if a given pool has been initialized
function _checkPoolInitialized(PoolId id) internal view {
if (_pools[id].isNotInitialized()) PoolNotInitialized.selector.revertWith();
}

/// @notice implementation of the _getPool function defined in ProtocolFees
function _getPool(PoolId id) internal view override returns (Pool.State storage) {
return _pools[id];
Expand Down
10 changes: 5 additions & 5 deletions src/libraries/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,13 @@ library Pool {
}

function setProtocolFee(State storage self, uint24 protocolFee) internal {
if (self.isNotInitialized()) PoolNotInitialized.selector.revertWith();

self.checkPoolInitialized();
self.slot0 = self.slot0.setProtocolFee(protocolFee);
}

/// @notice Only dynamic fee pools may update the lp fee.
function setLPFee(State storage self, uint24 lpFee) internal {
if (self.isNotInitialized()) PoolNotInitialized.selector.revertWith();
self.checkPoolInitialized();
self.slot0 = self.slot0.setLpFee(lpFee);
}

Expand Down Expand Up @@ -572,8 +571,9 @@ library Pool {
}
}

function isNotInitialized(State storage self) internal view returns (bool) {
return self.slot0.sqrtPriceX96() == 0;
/// @notice Reverts if the given pool has not been initialized
function checkPoolInitialized(State storage self) internal view {
if (self.slot0.sqrtPriceX96() == 0) PoolNotInitialized.selector.revertWith();
}

/// @notice Clears tick data
Expand Down

0 comments on commit 3118115

Please sign in to comment.