Skip to content

Commit

Permalink
Uncheck negation after toInt128 (#589)
Browse files Browse the repository at this point in the history
The majority of changes applied surround the `toInt128` method where subtraction operations inside `take` and `mint` methods in PoolManager.sol are now unchecked. This is coupled with removing an unnecessary unchecked block in the same file. These modifications contribute to the slight alteration in snapshot data.
  • Loading branch information
shuhuiluo authored and nishim3 committed Apr 18, 2024
1 parent d7411cc commit 10bce5b
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131807
131731
2 changes: 1 addition & 1 deletion .forge-snapshots/DynamicFees.swap_withDynamicFee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
90781
90705
2 changes: 1 addition & 1 deletion .forge-snapshots/PoolManager.bytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22630
22628
2 changes: 1 addition & 1 deletion .forge-snapshots/PoolManager.removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137995
137843
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53945
53793
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141717
141565
Original file line number Diff line number Diff line change
@@ -1 +1 @@
71233
71157
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135671
135595
2 changes: 1 addition & 1 deletion .forge-snapshots/PoolManager.swap_mintOutputAs6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
152294
152218
2 changes: 1 addition & 1 deletion .forge-snapshots/PoolManager.swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138291
138215
2 changes: 1 addition & 1 deletion .forge-snapshots/PoolManager.swap_simpleWithNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126898
126822
2 changes: 1 addition & 1 deletion .forge-snapshots/PoolManager.swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61531
61455
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61552
61476
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 @@
79445
79369
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 @@
75288
75212
Original file line number Diff line number Diff line change
@@ -1 +1 @@
158953
158801
26 changes: 14 additions & 12 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,10 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
int256 current = currency.getDelta(msg.sender);
int256 next = current + delta;

unchecked {
if (next == 0) {
NonZeroDeltaCount.decrement();
} else if (current == 0) {
NonZeroDeltaCount.increment();
}
if (next == 0) {
NonZeroDeltaCount.decrement();
} else if (current == 0) {
NonZeroDeltaCount.increment();
}

currency.setDelta(msg.sender, next);
Expand Down Expand Up @@ -265,9 +263,11 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim

/// @inheritdoc IPoolManager
function take(Currency currency, address to, uint256 amount) external override onlyWhenUnlocked {
// subtraction must be safe
_accountDelta(currency, -(amount.toInt128()));
currency.transfer(to, amount);
unchecked {
// subtraction must be safe
_accountDelta(currency, -(amount.toInt128()));
currency.transfer(to, amount);
}
}

/// @inheritdoc IPoolManager
Expand All @@ -285,9 +285,11 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim

/// @inheritdoc IPoolManager
function mint(address to, uint256 id, uint256 amount) external override onlyWhenUnlocked {
// subtraction must be safe
_accountDelta(CurrencyLibrary.fromId(id), -(amount.toInt128()));
_mint(to, id, amount);
unchecked {
// subtraction must be safe
_accountDelta(CurrencyLibrary.fromId(id), -(amount.toInt128()));
_mint(to, id, amount);
}
}

/// @inheritdoc IPoolManager
Expand Down

0 comments on commit 10bce5b

Please sign in to comment.