forked from Uniswap/v4-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LockDataLibrary.sol
50 lines (44 loc) · 2.03 KB
/
LockDataLibrary.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.20;
import {IPoolManager} from "../interfaces/IPoolManager.sol";
/// @dev This library manages a custom storage implementation for a queue
/// that tracks current lockers. The "sentinel" storage slot for this data structure,
/// always passed in as IPoolManager.LockData storage self, stores not just the current
/// length of the queue but also the global count of non-zero deltas across all lockers.
/// The values of the data structure start at OFFSET, and each value is a locker address.
library LockDataLibrary {
uint256 private constant OFFSET = uint256(keccak256("LockData"));
/// @dev Pushes a locker onto the end of the queue, and updates the sentinel storage slot.
function push(IPoolManager.LockData storage self, address locker) internal {
// read current value from the sentinel storage slot
uint128 length = self.length;
unchecked {
uint256 indexToWrite = OFFSET + length; // not in assembly because OFFSET is in the library scope
/// @solidity memory-safe-assembly
assembly {
// in the next storage slot, write the locker
sstore(indexToWrite, locker)
}
// update the sentinel storage slot
self.length = length + 1;
}
}
/// @dev Pops a locker off the end of the queue. Note that no storage gets cleared.
function pop(IPoolManager.LockData storage self) internal {
unchecked {
self.length--;
}
}
function getLock(uint256 i) internal view returns (address locker) {
unchecked {
uint256 position = OFFSET + i; // not in assembly because OFFSET is in the library scope
/// @solidity memory-safe-assembly
assembly {
locker := sload(position)
}
}
}
function getActiveLock(IPoolManager.LockData storage self) internal view returns (address locker) {
return getLock(self.length - 1);
}
}