Skip to content

Commit

Permalink
store boolean instead of address (#493)
Browse files Browse the repository at this point in the history
* store boolean instead of address

* adjustments

* remove unnecessary constants

* rename locker to caller everywhere
  • Loading branch information
dianakocsis authored Mar 11, 2024
1 parent 0e8e6c9 commit 702f547
Show file tree
Hide file tree
Showing 35 changed files with 146 additions and 194 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 @@
312661
312292
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 @@
192843
192474
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192821
192452
Original file line number Diff line number Diff line change
@@ -1 +1 @@
184735
184412
2 changes: 1 addition & 1 deletion .forge-snapshots/cached dynamic fee, no hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138762
138439
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 @@
131854
131592
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 @@
176958
176613
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 @@
24982
24960
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51725
51767
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 @@
36655
36633
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23345
23279
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 @@
99632
99307
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 @@
201005
200680
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
197282
196957
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 @@
187335
187012
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
195872
195549
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117998
117675
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
105462
105139
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 @@
125651
125350
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 @@
121604
121303
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 @@
189861
189538
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 @@
206666
206343
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 @@
183836
183513
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
105440
105117
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 @@
190540
190217
23 changes: 11 additions & 12 deletions src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {Fees} from "./Fees.sol";
import {ERC6909Claims} from "./ERC6909Claims.sol";
import {PoolId, PoolIdLibrary} from "./types/PoolId.sol";
import {BalanceDelta, BalanceDeltaLibrary} from "./types/BalanceDelta.sol";
import {Locker} from "./libraries/Locker.sol";
import {Lock} from "./libraries/Lock.sol";
import {NonZeroDeltaCount} from "./libraries/NonZeroDeltaCount.sol";
import {PoolGetters} from "./libraries/PoolGetters.sol";

Expand All @@ -40,10 +40,10 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, ERC6909Claims {
/// @inheritdoc IPoolManager
int24 public constant MIN_TICK_SPACING = TickMath.MIN_TICK_SPACING;

/// @dev Represents the currencies due/owed to each locker.
/// Must all net to zero when the last lock is released.
/// @dev Represents the currencies due/owed to each caller.
/// Must all net to zero when the lock is released.
/// TODO this needs to be transient
mapping(address locker => mapping(Currency currency => int256 currencyDelta)) public currencyDelta;
mapping(address caller => mapping(Currency currency => int256 currencyDelta)) public currencyDelta;

/// @inheritdoc IPoolManager
mapping(Currency currency => uint256) public override reservesOf;
Expand Down Expand Up @@ -88,14 +88,13 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, ERC6909Claims {
return pools[id].positions.get(_owner, tickLower, tickUpper);
}

/// @inheritdoc IPoolManager
function getLocker() external view override returns (address locker) {
return Locker.getLocker();
function isLockSet() external view returns (bool) {
return Lock.isLocked();
}

/// @notice This will revert if a function is called by any address other than the current locker OR the most recently called, pre-permissioned hook.
/// @notice This will revert if the contract is not locked
modifier isLocked() {
if (!Locker.isLocked()) revert ManagerNotLocked();
if (!Lock.isLocked()) revert ManagerNotLocked();
_;
}

Expand Down Expand Up @@ -129,15 +128,15 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, ERC6909Claims {

/// @inheritdoc IPoolManager
function lock(bytes calldata data) external payable override returns (bytes memory result) {
if (Locker.isLocked()) revert AlreadyLocked();
if (Lock.isLocked()) revert AlreadyLocked();

Locker.setLocker(msg.sender);
Lock.lock();

// the caller does everything in this callback, including paying what they owe via calls to settle
result = ILockCallback(msg.sender).lockAcquired(data);

if (NonZeroDeltaCount.read() != 0) revert CurrencyNotSettled();
Locker.clearLocker();
Lock.unlock();
}

function _accountDelta(Currency currency, int128 delta) internal {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IHooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ interface IHooks {
/// @param sender The initial msg.sender for the swap call
/// @param key The key for the pool
/// @param params The parameters for the swap
/// @param delta The amount owed to the locker (positive) or owed to the pool (negative)
/// @param delta The amount owed to the caller (positive) or owed to the pool (negative)
/// @param hookData Arbitrary data handed into the PoolManager by the swapper to be be passed on to the hook
/// @return bytes4 The function selector for the hook
function afterSwap(
Expand Down
10 changes: 5 additions & 5 deletions src/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ interface IPoolManager is IFees, IERC6909Claims {
/// @notice Returns the reserves for a given ERC20 currency
function reservesOf(Currency currency) external view returns (uint256);

/// @notice Returns the locker of the pool
function getLocker() external view returns (address locker);
/// @notice Returns whether the contract is locked
function isLockSet() external view returns (bool);

/// @notice Returns the number of nonzero deltas open on the PoolManager that must be zerod by the close of the initial lock.
function getLockNonzeroDeltaCount() external view returns (uint256 _nonzeroDeltaCount);
Expand All @@ -131,10 +131,10 @@ interface IPoolManager is IFees, IERC6909Claims {
external
returns (int24 tick);

/// @notice Get the current delta for a locker in the given currency
/// @param locker The address of the locker
/// @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 locker, Currency currency) external view returns (int256);
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 `ILockCallback(msg.sender).lockAcquired(data)`
Expand Down
1 change: 0 additions & 1 deletion src/libraries/Hooks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {IHooks} from "../interfaces/IHooks.sol";
import {FeeLibrary} from "./FeeLibrary.sol";
import {BalanceDelta} from "../types/BalanceDelta.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {Locker} from "./Locker.sol";

/// @notice V4 decides whether to invoke specific hooks by inspecting the leading bits of the address that
/// the hooks contract is deployed to.
Expand Down
33 changes: 33 additions & 0 deletions src/libraries/Lock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.20;

import {IHooks} from "../interfaces/IHooks.sol";

/// @notice This is a temporary library that allows us to use transient storage (tstore/tload)
/// TODO: This library can be deleted when we have the transient keyword support in solidity.
library Lock {
// The slot holding the lock, transiently
uint256 constant LOCK_SLOT = uint256(keccak256("Lock")) - 1;

function lock() internal {
uint256 slot = LOCK_SLOT;
assembly {
// set the lock
tstore(slot, true)
}
}

function unlock() internal {
uint256 slot = LOCK_SLOT;
assembly {
tstore(slot, false)
}
}

function isLocked() internal view returns (bool locked) {
uint256 slot = LOCK_SLOT;
assembly {
locked := tload(slot)
}
}
}
44 changes: 0 additions & 44 deletions src/libraries/Locker.sol

This file was deleted.

Loading

0 comments on commit 702f547

Please sign in to comment.