Skip to content

Commit

Permalink
Move isUnlocked, getNonzeroDeltaCount and currencyDelta view function…
Browse files Browse the repository at this point in the history
…s to pool state library
  • Loading branch information
gretzke committed May 9, 2024
1 parent 19ecb4c commit 845c7e9
Show file tree
Hide file tree
Showing 31 changed files with 63 additions and 50 deletions.
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 @@
278828
282676
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 @@
136196
140044
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
145987
149835
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 @@
108837
114084
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 @@
149364
154568
2 changes: 1 addition & 1 deletion .forge-snapshots/erc20 collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
57332
57310
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61911
61880
2 changes: 1 addition & 1 deletion .forge-snapshots/native collect protocol fees.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59565
59543
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21828
21609
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 @@
115824
119712
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 @@
113120
116487
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115812
119700
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 @@
130495
135697
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148846
154048
Original file line number Diff line number Diff line change
@@ -1 +1 @@
113390
118592
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124760
129962
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 @@
136744
141989
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 @@
125859
131104
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 @@
147958
153160
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 @@
164861
170063
Original file line number Diff line number Diff line change
@@ -1 +1 @@
224082
230616
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 @@
149062
154264
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124772
129974
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 @@
181373
186553
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 @@
159620
164800
14 changes: 0 additions & 14 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,6 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
return pools[id];
}

/// @inheritdoc IPoolManager
function currencyDelta(address caller, Currency currency) external view returns (int256) {
return currency.getDelta(caller);
}

/// @inheritdoc IPoolManager
function isUnlocked() external view override returns (bool) {
return Lock.isUnlocked();
}

/// @notice This will revert if the contract is locked
modifier onlyWhenUnlocked() {
if (!Lock.isUnlocked()) revert ManagerLocked();
Expand Down Expand Up @@ -269,10 +259,6 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim
pools[id].setLPFee(newDynamicLPFee);
}

function getNonzeroDeltaCount() external view returns (uint256 _nonzeroDeltaCount) {
return NonZeroDeltaCount.read();
}

function getPoolTickInfo(PoolId id, int24 tick) external view returns (Pool.TickInfo memory) {
return pools[id].getPoolTickInfo(tick);
}
Expand Down
11 changes: 0 additions & 11 deletions src/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,11 @@ interface IPoolManager is IProtocolFees, IERC6909Claims, IExtsload, IExttload {
/// @dev This MUST be called before any ERC20 tokens are sent into the contract.
function sync(Currency currency) external returns (uint256 balance);

/// @notice Returns whether the contract is unlocked or not
function isUnlocked() external view returns (bool);

/// @notice Returns the number of nonzero deltas open on the PoolManager that must be zerod out before the contract is locked
function getNonzeroDeltaCount() external view returns (uint256 _nonzeroDeltaCount);

/// @notice Initialize the state for a given pool ID
function initialize(PoolKey memory key, uint160 sqrtPriceX96, bytes calldata hookData)
external
returns (int24 tick);

/// @notice Get the current delta for a caller in the given currency
/// @param caller The address of the caller
/// @param currency The currency for which to lookup the delta
function currencyDelta(address caller, Currency currency) external view returns (int256);

/// @notice All operations go through this function
/// @param data Any data to pass to the callback, via `IUnlockCallback(msg.sender).unlockCallback(data)`
/// @return The data returned by the call to `IUnlockCallback(msg.sender).unlockCallback(data)`
Expand Down
31 changes: 31 additions & 0 deletions src/libraries/PoolStateLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ library PoolStateLibrary {
uint256 public constant RESERVES_OF_SLOT =
uint256(0x1e0745a7db1623981f0b2a5d4232364c00787266eb75ad546f190e6cebe9bd95);

// The slot holding the number of nonzero deltas. uint256(keccak256("NonzeroDeltaCount")) - 1
uint256 public constant NONZERO_DELTA_COUNT_SLOT =
uint256(0x7d4b3164c6e45b97e7d87b7125a44c5828d005af88f9d751cfd78729c5d99a0b);

// The slot holding the unlocked state, transiently. uint256(keccak256("Unlocked")) - 1;
uint256 public constant IS_UNLOCKED_SLOT =
uint256(0xc090fc4683624cfc3884e9d8de5eca132f2d0ec062aff75d43c0465d5ceeab23);

uint256 public constant ZERO_BALANCE = type(uint256).max;

/// @notice Thrown when someone has not called sync before calling settle for the first time.
Expand Down Expand Up @@ -351,6 +359,29 @@ library PoolStateLibrary {
if (value == ZERO_BALANCE) value = 0;
}

/// @notice Returns the number of nonzero deltas open on the PoolManager that must be zerod out before the contract is locked
function getNonzeroDeltaCount(IPoolManager manager) internal view returns (uint256) {
return uint256(manager.exttload(bytes32(NONZERO_DELTA_COUNT_SLOT)));
}

/// @notice Get the current delta for a caller in the given currency
/// @param caller_ The address of the caller
/// @param currency The currency for which to lookup the delta
function currencyDelta(IPoolManager manager, address caller_, Currency currency) external view returns (int256) {
bytes32 key;
assembly {
mstore(0, caller_)
mstore(32, currency)
key := keccak256(0, 64)
}
return int256(uint256(manager.exttload(key)));
}

/// @notice Returns whether the contract is unlocked or not
function isUnlocked(IPoolManager manager) external view returns (bool) {
return manager.exttload(bytes32(IS_UNLOCKED_SLOT)) != 0x0;
}

function _getPoolStateSlot(PoolId poolId) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(PoolId.unwrap(poolId), bytes32(POOLS_SLOT)));
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/PoolNestedActionsTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ enum Action {
}

contract PoolNestedActionsTest is Test, IUnlockCallback {
using PoolStateLibrary for IPoolManager;

IPoolManager manager;
NestedActionExecutor public executor;
address user;
Expand Down
3 changes: 3 additions & 0 deletions src/test/PoolTestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {IERC20Minimal} from "../interfaces/external/IERC20Minimal.sol";
import {IUnlockCallback} from "../interfaces/callback/IUnlockCallback.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";

import {PoolStateLibrary} from "../libraries/PoolStateLibrary.sol";

abstract contract PoolTestBase is IUnlockCallback {
using CurrencyLibrary for Currency;
using PoolStateLibrary for IPoolManager;

IPoolManager public immutable manager;

Expand Down
2 changes: 2 additions & 0 deletions src/test/SkipCallsTestHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {PoolTestBase} from "./PoolTestBase.sol";
import {Constants} from "../../test/utils/Constants.sol";
import {Test} from "forge-std/Test.sol";
import {CurrencySettleTake} from "../libraries/CurrencySettleTake.sol";
import {PoolStateLibrary} from "../libraries/PoolStateLibrary.sol";

contract SkipCallsTestHook is BaseTestHooks, Test {
using CurrencySettleTake for Currency;
using PoolIdLibrary for PoolKey;
using Hooks for IHooks;
using PoolStateLibrary for IPoolManager;

uint256 public counter;
IPoolManager manager;
Expand Down

0 comments on commit 845c7e9

Please sign in to comment.