forked from Uniswap/v4-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoolManager.sol
418 lines (357 loc) Β· 15.1 KB
/
PoolManager.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.20;
import {Hooks} from "./libraries/Hooks.sol";
import {Pool} from "./libraries/Pool.sol";
import {SafeCast} from "./libraries/SafeCast.sol";
import {Position} from "./libraries/Position.sol";
import {FeeLibrary} from "./libraries/FeeLibrary.sol";
import {Currency, CurrencyLibrary} from "./types/Currency.sol";
import {PoolKey} from "./types/PoolKey.sol";
import {LockDataLibrary} from "./libraries/LockDataLibrary.sol";
import {NoDelegateCall} from "./NoDelegateCall.sol";
import {Owned} from "./Owned.sol";
import {IHooks} from "./interfaces/IHooks.sol";
import {IDynamicFeeManager} from "./interfaces/IDynamicFeeManager.sol";
import {IHookFeeManager} from "./interfaces/IHookFeeManager.sol";
import {IPoolManager} from "./interfaces/IPoolManager.sol";
import {ILockCallback} from "./interfaces/callback/ILockCallback.sol";
import {Fees} from "./Fees.sol";
import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import {IERC1155Receiver} from "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
import {PoolId, PoolIdLibrary} from "./types/PoolId.sol";
import {BalanceDelta} from "./types/BalanceDelta.sol";
import {PoolKey} from "./types/PoolKey.sol";
/// @notice Holds the state for all pools
contract PoolManager is IPoolManager, Fees, NoDelegateCall, ERC1155, IERC1155Receiver {
using PoolIdLibrary for PoolKey;
using SafeCast for *;
using Pool for *;
using Hooks for IHooks;
using Position for mapping(bytes32 => Position.Info);
using CurrencyLibrary for Currency;
using LockDataLibrary for IPoolManager.LockData;
using FeeLibrary for uint24;
/// @inheritdoc IPoolManager
int24 public constant override MAX_TICK_SPACING = type(int16).max;
/// @inheritdoc IPoolManager
int24 public constant override MIN_TICK_SPACING = 1;
/// @inheritdoc IPoolManager
IPoolManager.LockData public override lockData;
/// @dev Represents the currencies due/owed to each locker.
/// Must all net to zero when the last lock is released.
mapping(address locker => mapping(Currency currency => int256 currencyDelta)) public currencyDelta;
/// @inheritdoc IPoolManager
mapping(Currency currency => uint256) public override reservesOf;
mapping(PoolId id => Pool.State) public pools;
constructor(uint256 controllerGasLimit) Fees(controllerGasLimit) ERC1155("") {}
function _getPool(PoolKey memory key) private view returns (Pool.State storage) {
return pools[key.toId()];
}
/// @inheritdoc IPoolManager
function getSlot0(PoolId id)
external
view
override
returns (
uint160 sqrtPriceX96,
int24 tick,
uint8 protocolSwapFee,
uint8 protocolWithdrawFee,
uint8 hookSwapFee,
uint8 hookWithdrawFee
)
{
Pool.Slot0 memory slot0 = pools[id].slot0;
return (
slot0.sqrtPriceX96,
slot0.tick,
slot0.protocolSwapFee,
slot0.protocolWithdrawFee,
slot0.hookSwapFee,
slot0.hookWithdrawFee
);
}
/// @inheritdoc IPoolManager
function getLiquidity(PoolId id) external view override returns (uint128 liquidity) {
return pools[id].liquidity;
}
/// @inheritdoc IPoolManager
function getLiquidity(PoolId id, address _owner, int24 tickLower, int24 tickUpper)
external
view
override
returns (uint128 liquidity)
{
return pools[id].positions.get(_owner, tickLower, tickUpper).liquidity;
}
function getPosition(PoolId id, address owner, int24 tickLower, int24 tickUpper)
external
view
override
returns (Position.Info memory position)
{
return pools[id].positions.get(owner, tickLower, tickUpper);
}
/// @inheritdoc IPoolManager
function getLock(uint256 i) external view override returns (address locker) {
return LockDataLibrary.getLock(i);
}
/// @inheritdoc IPoolManager
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external override returns (int24 tick) {
if (key.fee.isStaticFeeTooLarge()) revert FeeTooLarge();
// see TickBitmap.sol for overflow conditions that can arise from tick spacing being too large
if (key.tickSpacing > MAX_TICK_SPACING) revert TickSpacingTooLarge();
if (key.tickSpacing < MIN_TICK_SPACING) revert TickSpacingTooSmall();
if (!key.hooks.isValidHookAddress(key.fee)) revert Hooks.HookAddressNotValid(address(key.hooks));
if (key.hooks.shouldCallBeforeInitialize()) {
if (key.hooks.beforeInitialize(msg.sender, key, sqrtPriceX96) != IHooks.beforeInitialize.selector) {
revert Hooks.InvalidHookResponse();
}
}
PoolId id = key.toId();
(uint8 protocolSwapFee, uint8 protocolWithdrawFee) = _fetchProtocolFees(key);
(uint8 hookSwapFee, uint8 hookWithdrawFee) = _fetchHookFees(key);
tick = pools[id].initialize(sqrtPriceX96, protocolSwapFee, hookSwapFee, protocolWithdrawFee, hookWithdrawFee);
if (key.hooks.shouldCallAfterInitialize()) {
if (key.hooks.afterInitialize(msg.sender, key, sqrtPriceX96, tick) != IHooks.afterInitialize.selector) {
revert Hooks.InvalidHookResponse();
}
}
emit Initialize(id, key.currency0, key.currency1, key.fee, key.tickSpacing, key.hooks);
}
/// @inheritdoc IPoolManager
function lock(bytes calldata data) external override returns (bytes memory result) {
lockData.push(msg.sender);
// the caller does everything in this callback, including paying what they owe via calls to settle
result = ILockCallback(msg.sender).lockAcquired(data);
if (lockData.length == 1) {
if (lockData.nonzeroDeltaCount != 0) revert CurrencyNotSettled();
delete lockData;
} else {
lockData.pop();
}
}
function _accountDelta(Currency currency, int128 delta) internal {
if (delta == 0) return;
address locker = lockData.getActiveLock();
int256 current = currencyDelta[locker][currency];
int256 next = current + delta;
unchecked {
if (next == 0) {
lockData.nonzeroDeltaCount--;
} else if (current == 0) {
lockData.nonzeroDeltaCount++;
}
}
currencyDelta[locker][currency] = next;
}
/// @dev Accumulates a balance change to a map of currency to balance changes
function _accountPoolBalanceDelta(PoolKey memory key, BalanceDelta delta) internal {
_accountDelta(key.currency0, delta.amount0());
_accountDelta(key.currency1, delta.amount1());
}
modifier onlyByLocker() {
address locker = lockData.getActiveLock();
if (msg.sender != locker) revert LockedBy(locker);
_;
}
/// @inheritdoc IPoolManager
function modifyPosition(PoolKey memory key, IPoolManager.ModifyPositionParams memory params)
external
override
noDelegateCall
onlyByLocker
returns (BalanceDelta delta)
{
if (key.hooks.shouldCallBeforeModifyPosition()) {
if (key.hooks.beforeModifyPosition(msg.sender, key, params) != IHooks.beforeModifyPosition.selector) {
revert Hooks.InvalidHookResponse();
}
}
PoolId id = key.toId();
Pool.FeeAmounts memory feeAmounts;
(delta, feeAmounts) = pools[id].modifyPosition(
Pool.ModifyPositionParams({
owner: msg.sender,
tickLower: params.tickLower,
tickUpper: params.tickUpper,
liquidityDelta: params.liquidityDelta.toInt128(),
tickSpacing: key.tickSpacing
})
);
_accountPoolBalanceDelta(key, delta);
unchecked {
if (feeAmounts.feeForProtocol0 > 0) {
protocolFeesAccrued[key.currency0] += feeAmounts.feeForProtocol0;
}
if (feeAmounts.feeForProtocol1 > 0) {
protocolFeesAccrued[key.currency1] += feeAmounts.feeForProtocol1;
}
if (feeAmounts.feeForHook0 > 0) {
hookFeesAccrued[address(key.hooks)][key.currency0] += feeAmounts.feeForHook0;
}
if (feeAmounts.feeForHook1 > 0) {
hookFeesAccrued[address(key.hooks)][key.currency1] += feeAmounts.feeForHook1;
}
}
if (key.hooks.shouldCallAfterModifyPosition()) {
if (key.hooks.afterModifyPosition(msg.sender, key, params, delta) != IHooks.afterModifyPosition.selector) {
revert Hooks.InvalidHookResponse();
}
}
emit ModifyPosition(id, msg.sender, params.tickLower, params.tickUpper, params.liquidityDelta);
}
/// @inheritdoc IPoolManager
function swap(PoolKey memory key, IPoolManager.SwapParams memory params)
external
override
noDelegateCall
onlyByLocker
returns (BalanceDelta delta)
{
if (key.hooks.shouldCallBeforeSwap()) {
if (key.hooks.beforeSwap(msg.sender, key, params) != IHooks.beforeSwap.selector) {
revert Hooks.InvalidHookResponse();
}
}
// Set the total swap fee, either through the hook or as the static fee set an initialization.
uint24 totalSwapFee;
if (key.fee.isDynamicFee()) {
totalSwapFee = IDynamicFeeManager(address(key.hooks)).getFee(key);
if (totalSwapFee >= 1000000) revert FeeTooLarge();
} else {
// clear the top 4 bits since they may be flagged for hook fees
totalSwapFee = key.fee.getStaticFee();
}
uint256 feeForProtocol;
uint256 feeForHook;
Pool.SwapState memory state;
PoolId id = key.toId();
(delta, feeForProtocol, feeForHook, state) = pools[id].swap(
Pool.SwapParams({
fee: totalSwapFee,
tickSpacing: key.tickSpacing,
zeroForOne: params.zeroForOne,
amountSpecified: params.amountSpecified,
sqrtPriceLimitX96: params.sqrtPriceLimitX96
})
);
_accountPoolBalanceDelta(key, delta);
// the fee is on the input currency
unchecked {
if (feeForProtocol > 0) {
protocolFeesAccrued[params.zeroForOne ? key.currency0 : key.currency1] += feeForProtocol;
}
if (feeForHook > 0) {
hookFeesAccrued[address(key.hooks)][params.zeroForOne ? key.currency0 : key.currency1] += feeForHook;
}
}
if (key.hooks.shouldCallAfterSwap()) {
if (key.hooks.afterSwap(msg.sender, key, params, delta) != IHooks.afterSwap.selector) {
revert Hooks.InvalidHookResponse();
}
}
emit Swap(
id,
msg.sender,
delta.amount0(),
delta.amount1(),
state.sqrtPriceX96,
state.liquidity,
state.tick,
totalSwapFee
);
}
/// @inheritdoc IPoolManager
function donate(PoolKey memory key, uint256 amount0, uint256 amount1)
external
override
noDelegateCall
onlyByLocker
returns (BalanceDelta delta)
{
if (key.hooks.shouldCallBeforeDonate()) {
if (key.hooks.beforeDonate(msg.sender, key, amount0, amount1) != IHooks.beforeDonate.selector) {
revert Hooks.InvalidHookResponse();
}
}
delta = _getPool(key).donate(amount0, amount1);
_accountPoolBalanceDelta(key, delta);
if (key.hooks.shouldCallAfterDonate()) {
if (key.hooks.afterDonate(msg.sender, key, amount0, amount1) != IHooks.afterDonate.selector) {
revert Hooks.InvalidHookResponse();
}
}
}
/// @inheritdoc IPoolManager
function take(Currency currency, address to, uint256 amount) external override noDelegateCall onlyByLocker {
_accountDelta(currency, amount.toInt128());
reservesOf[currency] -= amount;
currency.transfer(to, amount);
}
/// @inheritdoc IPoolManager
function mint(Currency currency, address to, uint256 amount) external override noDelegateCall onlyByLocker {
_accountDelta(currency, amount.toInt128());
_mint(to, currency.toId(), amount, "");
}
/// @inheritdoc IPoolManager
function settle(Currency currency) external payable override noDelegateCall onlyByLocker returns (uint256 paid) {
uint256 reservesBefore = reservesOf[currency];
reservesOf[currency] = currency.balanceOfSelf();
paid = reservesOf[currency] - reservesBefore;
// subtraction must be safe
_accountDelta(currency, -(paid.toInt128()));
}
function _burnAndAccount(Currency currency, uint256 amount) internal {
_burn(address(this), currency.toId(), amount);
_accountDelta(currency, -(amount.toInt128()));
}
function onERC1155Received(address, address, uint256 id, uint256 value, bytes calldata) external returns (bytes4) {
if (msg.sender != address(this)) revert NotPoolManagerToken();
_burnAndAccount(CurrencyLibrary.fromId(id), value);
return IERC1155Receiver.onERC1155Received.selector;
}
function onERC1155BatchReceived(address, address, uint256[] calldata ids, uint256[] calldata values, bytes calldata)
external
returns (bytes4)
{
if (msg.sender != address(this)) revert NotPoolManagerToken();
// unchecked to save gas on incrementations of i
unchecked {
for (uint256 i; i < ids.length; i++) {
_burnAndAccount(CurrencyLibrary.fromId(ids[i]), values[i]);
}
}
return IERC1155Receiver.onERC1155BatchReceived.selector;
}
function setProtocolFees(PoolKey memory key) external {
(uint8 newProtocolSwapFee, uint8 newProtocolWithdrawFee) = _fetchProtocolFees(key);
PoolId id = key.toId();
pools[id].setProtocolFees(newProtocolSwapFee, newProtocolWithdrawFee);
emit ProtocolFeeUpdated(id, newProtocolSwapFee, newProtocolWithdrawFee);
}
function setHookFees(PoolKey memory key) external {
(uint8 newHookSwapFee, uint8 newHookWithdrawFee) = _fetchHookFees(key);
PoolId id = key.toId();
pools[id].setHookFees(newHookSwapFee, newHookWithdrawFee);
emit HookFeeUpdated(id, newHookSwapFee, newHookWithdrawFee);
}
function extsload(bytes32 slot) external view returns (bytes32 value) {
/// @solidity memory-safe-assembly
assembly {
value := sload(slot)
}
}
function extsload(bytes32 startSlot, uint256 nSlots) external view returns (bytes memory) {
bytes memory value = new bytes(32 * nSlots);
/// @solidity memory-safe-assembly
assembly {
for { let i := 0 } lt(i, nSlots) { i := add(i, 1) } {
mstore(add(value, mul(add(i, 1), 32)), sload(add(startSlot, i)))
}
}
return value;
}
/// @notice receive native tokens for native pools
receive() external payable {}
}