Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wjmelements committed Jun 26, 2024
1 parent ac47532 commit b26df6b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
9 changes: 3 additions & 6 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,13 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
function _accountDelta(Currency currency, int128 delta, address target) internal {
if (delta == 0) return;

int256 current = currency.getDelta(target);
int256 next = current + delta;
(int256 previous, int256 updated) = currency.applyDelta(target, delta);

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

currency.setDelta(target, next);
}

/// @notice Accounts the deltas of 2 currencies to a target address
Expand Down
15 changes: 5 additions & 10 deletions src/libraries/CurrencyDelta.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,16 @@ library CurrencyDelta {
}
}

/// @notice sets a new currency delta for a given caller and currency
function setDelta(Currency currency, address caller, int256 delta) internal {
/// @notice applies a new currency delta for a given caller and currency
function applyDelta(Currency currency, address caller, int128 delta) internal returns (int256 previous, int256 next) {
bytes32 hashSlot = _computeSlot(caller, currency);

assembly {
tstore(hashSlot, delta)
previous := tload(hashSlot)
}
}

/// @notice gets a new currency delta for a given caller and currency
function getDelta(Currency currency, address caller) internal view returns (int256 delta) {
bytes32 hashSlot = _computeSlot(caller, currency);

next = previous + delta;
assembly {
delta := tload(hashSlot)
tstore(hashSlot, next)
}
}
}

0 comments on commit b26df6b

Please sign in to comment.