From e4d9d90781befcfc1a9bb78eb9d1b1c352d57d63 Mon Sep 17 00:00:00 2001 From: Nishim Goyal <115455855+nishim3@users.noreply.github.com> Date: Thu, 28 Mar 2024 00:57:06 +0530 Subject: [PATCH] use CurrencyDelta library (#539) --- .forge-snapshots/addLiquidity with empty hook.snap | 2 +- .forge-snapshots/addLiquidity with native token.snap | 2 +- .forge-snapshots/addLiquidity.snap | 2 +- .forge-snapshots/donate gas with 1 token.snap | 2 +- .forge-snapshots/donate gas with 2 tokens.snap | 2 +- .forge-snapshots/poolManager bytecode size.snap | 2 +- .forge-snapshots/removeLiquidity with empty hook.snap | 2 +- .forge-snapshots/removeLiquidity with native token.snap | 2 +- .forge-snapshots/removeLiquidity.snap | 2 +- .forge-snapshots/simple swap with native.snap | 2 +- .forge-snapshots/simple swap.snap | 2 +- .../swap against liquidity with native token.snap | 2 +- .forge-snapshots/swap against liquidity.snap | 2 +- .forge-snapshots/swap burn 6909 for input.snap | 2 +- .forge-snapshots/swap burn native 6909 for input.snap | 2 +- .forge-snapshots/swap mint native output as 6909.snap | 2 +- .forge-snapshots/swap mint output as 6909.snap | 2 +- .forge-snapshots/swap with dynamic fee.snap | 2 +- .forge-snapshots/swap with hooks.snap | 2 +- .forge-snapshots/update dynamic fee in before swap.snap | 2 +- src/PoolManager.sol | 7 ++++--- src/libraries/CurrencyDelta.sol | 4 ++-- 22 files changed, 26 insertions(+), 25 deletions(-) diff --git a/.forge-snapshots/addLiquidity with empty hook.snap b/.forge-snapshots/addLiquidity with empty hook.snap index 03d5c6ab2..e2bd4e7ac 100644 --- a/.forge-snapshots/addLiquidity with empty hook.snap +++ b/.forge-snapshots/addLiquidity with empty hook.snap @@ -1 +1 @@ -265619 \ No newline at end of file +265799 \ No newline at end of file diff --git a/.forge-snapshots/addLiquidity with native token.snap b/.forge-snapshots/addLiquidity with native token.snap index 2d1833724..2e5687233 100644 --- a/.forge-snapshots/addLiquidity with native token.snap +++ b/.forge-snapshots/addLiquidity with native token.snap @@ -1 +1 @@ -140462 \ No newline at end of file +140642 \ No newline at end of file diff --git a/.forge-snapshots/addLiquidity.snap b/.forge-snapshots/addLiquidity.snap index 7bb44201a..b36da47c1 100644 --- a/.forge-snapshots/addLiquidity.snap +++ b/.forge-snapshots/addLiquidity.snap @@ -1 +1 @@ -145779 \ No newline at end of file +145959 \ No newline at end of file diff --git a/.forge-snapshots/donate gas with 1 token.snap b/.forge-snapshots/donate gas with 1 token.snap index 017f6b150..3829a9cc6 100644 --- a/.forge-snapshots/donate gas with 1 token.snap +++ b/.forge-snapshots/donate gas with 1 token.snap @@ -1 +1 @@ -101510 \ No newline at end of file +101654 \ No newline at end of file diff --git a/.forge-snapshots/donate gas with 2 tokens.snap b/.forge-snapshots/donate gas with 2 tokens.snap index 5c5549ba0..f59cb30b4 100644 --- a/.forge-snapshots/donate gas with 2 tokens.snap +++ b/.forge-snapshots/donate gas with 2 tokens.snap @@ -1 +1 @@ -128451 \ No newline at end of file +128667 \ No newline at end of file diff --git a/.forge-snapshots/poolManager bytecode size.snap b/.forge-snapshots/poolManager bytecode size.snap index 8a8c7ef26..3cf278ca1 100644 --- a/.forge-snapshots/poolManager bytecode size.snap +++ b/.forge-snapshots/poolManager bytecode size.snap @@ -1 +1 @@ -22999 \ No newline at end of file +23026 \ No newline at end of file diff --git a/.forge-snapshots/removeLiquidity with empty hook.snap b/.forge-snapshots/removeLiquidity with empty hook.snap index fe8c734e8..8c729b8ed 100644 --- a/.forge-snapshots/removeLiquidity with empty hook.snap +++ b/.forge-snapshots/removeLiquidity with empty hook.snap @@ -1 +1 @@ -56394 \ No newline at end of file +56574 \ No newline at end of file diff --git a/.forge-snapshots/removeLiquidity with native token.snap b/.forge-snapshots/removeLiquidity with native token.snap index dc17a2850..ab62f35c6 100644 --- a/.forge-snapshots/removeLiquidity with native token.snap +++ b/.forge-snapshots/removeLiquidity with native token.snap @@ -1 +1 @@ -148580 \ No newline at end of file +148760 \ No newline at end of file diff --git a/.forge-snapshots/removeLiquidity.snap b/.forge-snapshots/removeLiquidity.snap index 3b2dd3a5c..24c0b40b7 100644 --- a/.forge-snapshots/removeLiquidity.snap +++ b/.forge-snapshots/removeLiquidity.snap @@ -1 +1 @@ -150044 \ No newline at end of file +150224 \ No newline at end of file diff --git a/.forge-snapshots/simple swap with native.snap b/.forge-snapshots/simple swap with native.snap index 43adf6fd1..df22d79b2 100644 --- a/.forge-snapshots/simple swap with native.snap +++ b/.forge-snapshots/simple swap with native.snap @@ -1 +1 @@ -133193 \ No newline at end of file +133409 \ No newline at end of file diff --git a/.forge-snapshots/simple swap.snap b/.forge-snapshots/simple swap.snap index ff7f3622e..ba806de9b 100644 --- a/.forge-snapshots/simple swap.snap +++ b/.forge-snapshots/simple swap.snap @@ -1 +1 @@ -147069 \ No newline at end of file +147285 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity with native token.snap b/.forge-snapshots/swap against liquidity with native token.snap index 14ccdf1f5..addfbca9d 100644 --- a/.forge-snapshots/swap against liquidity with native token.snap +++ b/.forge-snapshots/swap against liquidity with native token.snap @@ -1 +1 @@ -72652 \ No newline at end of file +72868 \ No newline at end of file diff --git a/.forge-snapshots/swap against liquidity.snap b/.forge-snapshots/swap against liquidity.snap index 0ec8c8c2e..386cc244e 100644 --- a/.forge-snapshots/swap against liquidity.snap +++ b/.forge-snapshots/swap against liquidity.snap @@ -1 +1 @@ -60655 \ No newline at end of file +60871 \ No newline at end of file diff --git a/.forge-snapshots/swap burn 6909 for input.snap b/.forge-snapshots/swap burn 6909 for input.snap index 7be6ba8dd..0061a05c6 100644 --- a/.forge-snapshots/swap burn 6909 for input.snap +++ b/.forge-snapshots/swap burn 6909 for input.snap @@ -1 +1 @@ -80769 \ No newline at end of file +80985 \ No newline at end of file diff --git a/.forge-snapshots/swap burn native 6909 for input.snap b/.forge-snapshots/swap burn native 6909 for input.snap index cc3b4460a..ab62c1cbb 100644 --- a/.forge-snapshots/swap burn native 6909 for input.snap +++ b/.forge-snapshots/swap burn native 6909 for input.snap @@ -1 +1 @@ -76724 \ No newline at end of file +76940 \ No newline at end of file diff --git a/.forge-snapshots/swap mint native output as 6909.snap b/.forge-snapshots/swap mint native output as 6909.snap index 5877ae2fc..566df1219 100644 --- a/.forge-snapshots/swap mint native output as 6909.snap +++ b/.forge-snapshots/swap mint native output as 6909.snap @@ -1 +1 @@ -139052 \ No newline at end of file +139268 \ No newline at end of file diff --git a/.forge-snapshots/swap mint output as 6909.snap b/.forge-snapshots/swap mint output as 6909.snap index e99fc2278..982481321 100644 --- a/.forge-snapshots/swap mint output as 6909.snap +++ b/.forge-snapshots/swap mint output as 6909.snap @@ -1 +1 @@ -155861 \ No newline at end of file +156077 \ No newline at end of file diff --git a/.forge-snapshots/swap with dynamic fee.snap b/.forge-snapshots/swap with dynamic fee.snap index 58fd2b094..b978e8a3e 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -89960 \ No newline at end of file +90176 \ No newline at end of file diff --git a/.forge-snapshots/swap with hooks.snap b/.forge-snapshots/swap with hooks.snap index e39566067..160b64063 100644 --- a/.forge-snapshots/swap with hooks.snap +++ b/.forge-snapshots/swap with hooks.snap @@ -1 +1 @@ -60633 \ No newline at end of file +60849 \ No newline at end of file diff --git a/.forge-snapshots/update dynamic fee in before swap.snap b/.forge-snapshots/update dynamic fee in before swap.snap index 98cf04b64..98aa37ccb 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -140965 \ No newline at end of file +141181 \ No newline at end of file diff --git a/src/PoolManager.sol b/src/PoolManager.sol index b1ce4c771..cfea4768c 100644 --- a/src/PoolManager.sol +++ b/src/PoolManager.sol @@ -31,6 +31,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim using Hooks for IHooks; using Position for mapping(bytes32 => Position.Info); using CurrencyLibrary for Currency; + using CurrencyDelta for Currency; using SwapFeeLibrary for uint24; using PoolGetters for Pool.State; @@ -85,7 +86,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim /// @inheritdoc IPoolManager function currencyDelta(address caller, Currency currency) external view returns (int256) { - return CurrencyDelta.get(caller, currency); + return currency.getDelta(caller); } /// @inheritdoc IPoolManager @@ -142,7 +143,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim function _accountDelta(Currency currency, int128 delta) internal { if (delta == 0) return; - int256 current = CurrencyDelta.get(msg.sender, currency); + int256 current = currency.getDelta(msg.sender); int256 next = current + delta; unchecked { @@ -153,7 +154,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim } } - CurrencyDelta.set(msg.sender, currency, next); + currency.setDelta(msg.sender, next); } /// @dev Accumulates a balance change to a map of currency to balance changes diff --git a/src/libraries/CurrencyDelta.sol b/src/libraries/CurrencyDelta.sol index a886e7805..9a0d91d5d 100644 --- a/src/libraries/CurrencyDelta.sol +++ b/src/libraries/CurrencyDelta.sol @@ -21,7 +21,7 @@ library CurrencyDelta { } } - function set(address caller, Currency currency, int256 delta) internal { + function setDelta(Currency currency, address caller, int256 delta) internal { bytes32 hashSlot = _computeSlot(caller, currency); assembly { @@ -29,7 +29,7 @@ library CurrencyDelta { } } - function get(address caller, Currency currency) internal view returns (int256 delta) { + function getDelta(Currency currency, address caller) internal view returns (int256 delta) { bytes32 hashSlot = _computeSlot(caller, currency); assembly {