forked from Uniswap/v4-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrencyReserves.sol
More file actions
48 lines (39 loc) · 1.46 KB
/
CurrencyReserves.sol
File metadata and controls
48 lines (39 loc) · 1.46 KB
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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.20;
import {Currency} from "../types/Currency.sol";
import {CustomRevert} from "./CustomRevert.sol";
library CurrencyReserves {
using CustomRevert for bytes4;
/// @notice Thrown when a user has already synced a currency, but not yet settled
error AlreadySynced();
/// bytes32(uint256(keccak256("ReservesOf")) - 1)
bytes32 constant RESERVES_OF_SLOT = 0x1e0745a7db1623981f0b2a5d4232364c00787266eb75ad546f190e6cebe9bd95;
/// bytes32(uint256(keccak256("Currency")) - 1)
bytes32 constant CURRENCY_SLOT = 0x27e098c505d44ec3574004bca052aabf76bd35004c182099d8c575fb238593b9;
function requireNotSynced() internal view {
if (!getSyncedCurrency().isZero()) {
AlreadySynced.selector.revertWith();
}
}
function getSyncedCurrency() internal view returns (Currency currency) {
assembly {
currency := tload(CURRENCY_SLOT)
}
}
function resetCurrency() internal {
assembly {
tstore(CURRENCY_SLOT, 0)
}
}
function syncCurrencyAndReserves(Currency currency, uint256 value) internal {
assembly {
tstore(CURRENCY_SLOT, and(currency, 0xffffffffffffffffffffffffffffffffffffffff))
tstore(RESERVES_OF_SLOT, value)
}
}
function getSyncedReserves() internal view returns (uint256 value) {
assembly {
value := tload(RESERVES_OF_SLOT)
}
}
}