Skip to content

Commit 3118115

Browse files
just have 1 check function (#714)
* just have 1 check function * remove double pool lookup --------- Co-authored-by: Alice Henshaw <[email protected]>
1 parent 05f986d commit 3118115

34 files changed

+51
-52
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
149414
1+
149347
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
326118
1+
326051
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
280232
1+
280165
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
139620
1+
139553
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
297592
1+
297525
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
106629
1+
106609
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
147124
1+
147104
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20003
1+
19971
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
181711
1+
181644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
136000
1+
135933
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
115828
1+
115761
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
32444
1+
32478
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
102856
1+
102789
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
165348
1+
165281
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
96571
1+
96504
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
88611
1+
88544
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
114950
1+
114887

.forge-snapshots/simple swap.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
130115
1+
130052
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
132084
1+
132021
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
179590
1+
179527
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
110342
1+
110279
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
121689
1+
121626
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
133518
1+
133455
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
122806
1+
122743
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
144594
1+
144531
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
161224
1+
161161
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
217673
1+
217547
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
145425
1+
145362

.forge-snapshots/swap with hooks.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
138665
1+
138602
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
176623
1+
176594
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
152437
1+
152374
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
155039
1+
155010

src/PoolManager.sol

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,13 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
156156
bytes calldata hookData
157157
) external override onlyWhenUnlocked returns (BalanceDelta callerDelta, BalanceDelta feesAccrued) {
158158
PoolId id = key.toId();
159-
_checkPoolInitialized(id);
159+
Pool.State storage pool = _getPool(id);
160+
pool.checkPoolInitialized();
160161

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

163164
BalanceDelta principalDelta;
164-
(principalDelta, feesAccrued) = _pools[id].modifyLiquidity(
165+
(principalDelta, feesAccrued) = pool.modifyLiquidity(
165166
Pool.ModifyLiquidityParams({
166167
owner: msg.sender,
167168
tickLower: params.tickLower,
@@ -193,9 +194,9 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
193194
returns (BalanceDelta swapDelta)
194195
{
195196
if (params.amountSpecified == 0) SwapAmountCannotBeZero.selector.revertWith();
196-
197197
PoolId id = key.toId();
198-
_checkPoolInitialized(id);
198+
Pool.State storage pool = _getPool(id);
199+
pool.checkPoolInitialized();
199200

200201
BeforeSwapDelta beforeSwapDelta;
201202
{
@@ -205,6 +206,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
205206

206207
// execute swap, account protocol fees, and emit swap event
207208
swapDelta = _swap(
209+
pool,
208210
id,
209211
Pool.SwapParams({
210212
tickSpacing: key.tickSpacing,
@@ -227,9 +229,11 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
227229
}
228230

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

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

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

256-
delta = _pools[id].donate(amount0, amount1);
260+
delta = pool.donate(amount0, amount1);
257261

258262
_accountPoolBalanceDelta(key, delta, msg.sender);
259263

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

333-
/// @notice Checks if a given pool has been initialized
334-
function _checkPoolInitialized(PoolId id) internal view {
335-
if (_pools[id].isNotInitialized()) PoolNotInitialized.selector.revertWith();
336-
}
337-
338337
/// @notice implementation of the _getPool function defined in ProtocolFees
339338
function _getPool(PoolId id) internal view override returns (Pool.State storage) {
340339
return _pools[id];

src/libraries/Pool.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,13 @@ library Pool {
110110
}
111111

112112
function setProtocolFee(State storage self, uint24 protocolFee) internal {
113-
if (self.isNotInitialized()) PoolNotInitialized.selector.revertWith();
114-
113+
self.checkPoolInitialized();
115114
self.slot0 = self.slot0.setProtocolFee(protocolFee);
116115
}
117116

118117
/// @notice Only dynamic fee pools may update the lp fee.
119118
function setLPFee(State storage self, uint24 lpFee) internal {
120-
if (self.isNotInitialized()) PoolNotInitialized.selector.revertWith();
119+
self.checkPoolInitialized();
121120
self.slot0 = self.slot0.setLpFee(lpFee);
122121
}
123122

@@ -572,8 +571,9 @@ library Pool {
572571
}
573572
}
574573

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

579579
/// @notice Clears tick data

0 commit comments

Comments
 (0)