-
Notifications
You must be signed in to change notification settings - Fork 642
Expand file tree
/
Copy pathPositionManager.modifyLiquiditiesWithoutUnlock.t.sol
More file actions
301 lines (246 loc) · 13.6 KB
/
PositionManager.modifyLiquiditiesWithoutUnlock.t.sol
File metadata and controls
301 lines (246 loc) · 13.6 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
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "forge-std/Test.sol";
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {IERC721} from "forge-std/interfaces/IERC721.sol";
import {CustomRevert} from "@uniswap/v4-core/src/libraries/CustomRevert.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {IPositionManager} from "../../src/interfaces/IPositionManager.sol";
import {INotifier} from "../../src/interfaces/INotifier.sol";
import {ISubscriber} from "../../src/interfaces/ISubscriber.sol";
import {ReentrancyLock} from "../../src/base/ReentrancyLock.sol";
import {Actions} from "../../src/libraries/Actions.sol";
import {PositionConfig} from "../shared/PositionConfig.sol";
import {LiquidityFuzzers} from "../shared/fuzz/LiquidityFuzzers.sol";
import {PosmTestSetup} from "../shared/PosmTestSetup.sol";
import {MockReentrantSubscriber} from "../mocks/MockReentrantSubscriber.sol";
contract PositionManagerModifyLiquiditiesTest is Test, PosmTestSetup, LiquidityFuzzers {
using StateLibrary for IPoolManager;
address alice;
uint256 alicePK;
address bob;
MockReentrantSubscriber sub;
PositionConfig config;
function setUp() public {
(alice, alicePK) = makeAddrAndKey("ALICE");
(bob,) = makeAddrAndKey("BOB");
deployFreshManagerAndRouters();
deployMintAndApprove2Currencies();
// Requires currency0 and currency1 to be set in base Deployers contract.
deployAndApprovePosm(manager);
seedBalance(alice);
approvePosmFor(alice);
// must deploy after posm
// Deploys a hook which can accesses IPositionManager.modifyLiquiditiesWithoutUnlock
deployPosmHookModifyLiquidities();
seedBalance(address(hookModifyLiquidities));
(key,) = initPool(currency0, currency1, IHooks(hookModifyLiquidities), 3000, SQRT_PRICE_1_1);
wethKey = initPoolUnsorted(Currency.wrap(address(_WETH9)), currency1, IHooks(address(0)), 3000, SQRT_PRICE_1_1);
sub = new MockReentrantSubscriber(lpm);
config = PositionConfig({poolKey: key, tickLower: -60, tickUpper: 60});
}
/// @dev calling modifyLiquiditiesWithoutUnlock without a lock will revert
function test_modifyLiquiditiesWithoutUnlock_revert() public {
bytes memory calls = getMintEncoded(config, 10e18, address(this), ZERO_BYTES);
(bytes memory actions, bytes[] memory params) = abi.decode(calls, (bytes, bytes[]));
vm.expectRevert(IPoolManager.ManagerLocked.selector);
lpm.modifyLiquiditiesWithoutUnlock(actions, params);
}
/// @dev subscribers cannot re-enter posm on-subscribe since PM is not unlocked
function test_fuzz_subscriber_subscribe_reenter_revert(uint256 seed) public {
uint256 tokenId = lpm.nextTokenId();
mint(config, 100e18, address(this), ZERO_BYTES);
// approve the subscriber to modify liquidity
IERC721(address(lpm)).approve(address(sub), tokenId);
// randomly sample a single action
bytes memory calls = getFuzzySingleEncoded(seed, tokenId, config, 10e18, ZERO_BYTES);
vm.expectRevert(
abi.encodeWithSelector(
CustomRevert.WrappedError.selector,
address(sub),
ISubscriber.notifySubscribe.selector,
abi.encodeWithSelector(IPoolManager.ManagerLocked.selector),
abi.encodeWithSelector(INotifier.SubscriptionReverted.selector)
)
);
lpm.subscribe(tokenId, address(sub), calls);
}
/// @dev subscribers cannot re-enter posm on-unsubscribe since PM is not unlocked
function test_fuzz_subscriber_unsubscribe_reenter(uint256 seed) public {
uint256 tokenId = lpm.nextTokenId();
mint(config, 100e18, address(this), ZERO_BYTES);
// approve the subscriber to modify liquidity
IERC721(address(lpm)).approve(address(sub), tokenId);
lpm.subscribe(tokenId, address(sub), ZERO_BYTES);
// randomly sample a single action
bytes memory calls = getFuzzySingleEncoded(seed, tokenId, config, 10e18, ZERO_BYTES);
(bytes memory actions, bytes[] memory params) = abi.decode(calls, (bytes, bytes[]));
sub.setActionsAndParams(actions, params);
lpm.unsubscribe(tokenId);
// subscriber did not modify liquidity
assertEq(IERC721(address(lpm)).ownerOf(tokenId), address(this)); // owner still owns the position
assertEq(lpm.nextTokenId(), tokenId + 1); // no new token minted
assertEq(lpm.getPositionLiquidity(tokenId), 100e18); // liquidity unchanged
// token was unsubscribed
assertEq(address(lpm.subscriber(tokenId)), address(0));
assertEq(lpm.positionInfo(tokenId).hasSubscriber(), false);
}
/// @dev subscribers cannot re-enter posm on-notifyModifyLiquidity because of no reentrancy guards
function test_fuzz_subscriber_notifyModifyLiquidity_reenter_revert(uint256 seed0, uint256 seed1) public {
uint256 tokenId = lpm.nextTokenId();
mint(config, 100e18, address(this), ZERO_BYTES);
// approve the subscriber to modify liquidity
IERC721(address(lpm)).approve(address(sub), tokenId);
lpm.subscribe(tokenId, address(sub), ZERO_BYTES);
// randomly sample a single action
bytes memory action = getFuzzySingleEncoded(seed0, tokenId, config, 10e18, ZERO_BYTES);
(bytes memory actions, bytes[] memory params) = abi.decode(action, (bytes, bytes[]));
sub.setActionsAndParams(actions, params);
// modify the token (dont mint)
bytes memory calls;
if (seed1 % 3 == 0) {
calls = getIncreaseEncoded(tokenId, config, 10e18, ZERO_BYTES);
vm.expectRevert(
abi.encodeWithSelector(
CustomRevert.WrappedError.selector,
address(sub),
ISubscriber.notifyModifyLiquidity.selector,
abi.encodeWithSelector(ReentrancyLock.ContractLocked.selector),
abi.encodeWithSelector(INotifier.ModifyLiquidityNotificationReverted.selector)
)
);
} else if (seed1 % 3 == 1) {
calls = getDecreaseEncoded(tokenId, config, 10e18, ZERO_BYTES);
vm.expectRevert(
abi.encodeWithSelector(
CustomRevert.WrappedError.selector,
address(sub),
ISubscriber.notifyModifyLiquidity.selector,
abi.encodeWithSelector(ReentrancyLock.ContractLocked.selector),
abi.encodeWithSelector(INotifier.ModifyLiquidityNotificationReverted.selector)
)
);
} else {
calls = getBurnEncoded(tokenId, config, ZERO_BYTES);
vm.expectRevert(
abi.encodeWithSelector(
CustomRevert.WrappedError.selector,
address(sub),
ISubscriber.notifyBurn.selector,
abi.encodeWithSelector(ReentrancyLock.ContractLocked.selector),
abi.encodeWithSelector(INotifier.BurnNotificationReverted.selector)
)
);
}
// should revert because subscriber is re-entering modifyLiquiditiesWithoutUnlock
lpm.modifyLiquidities(calls, _deadline);
}
/// @dev subscribers cannot re-enter posm on-notifyUnsubscribe because position manager is not unlocked
function test_fuzz_subscriber_transfer_reenter_unmodified(uint256 seed) public {
uint256 tokenId = lpm.nextTokenId();
mint(config, 100e18, address(this), ZERO_BYTES);
uint256 liquidityBefore = lpm.getPositionLiquidity(tokenId);
// approve the subscriber to modify liquidity
IERC721(address(lpm)).approve(address(sub), tokenId);
lpm.subscribe(tokenId, address(sub), ZERO_BYTES);
// randomly sample a single action
bytes memory action = getFuzzySingleEncoded(seed, tokenId, config, 10e18, ZERO_BYTES);
(bytes memory actions, bytes[] memory params) = abi.decode(action, (bytes, bytes[]));
sub.setActionsAndParams(actions, params);
// on transfer, the subscriber is called `notifyUnsubscribe` which will attempt to modify liquidity
// this call is reverted, but the unsubscribe is still successful
IERC721(address(lpm)).transferFrom(address(this), address(sub), tokenId);
// verify the position's liquidity is not modified
assertEq(lpm.getPositionLiquidity(tokenId), liquidityBefore);
}
/// @dev hook cannot re-enter modifyLiquiditiesWithoutUnlock in beforeAddLiquidity
function test_fuzz_hook_beforeAddLiquidity_reenter_revert(uint256 seed) public {
uint256 initialLiquidity = 100e18;
uint256 tokenId = lpm.nextTokenId();
mint(config, initialLiquidity, address(this), ZERO_BYTES);
uint256 liquidityToChange = 10e18;
// a random action be provided as hookData, so beforeAddLiquidity will attempt to modifyLiquidity
bytes memory hookCall = getFuzzySingleEncoded(seed, tokenId, config, liquidityToChange, ZERO_BYTES);
bytes memory calls = getIncreaseEncoded(tokenId, config, liquidityToChange, hookCall);
// should revert because hook is re-entering modifyLiquiditiesWithoutUnlock
vm.expectRevert(
abi.encodeWithSelector(
CustomRevert.WrappedError.selector,
address(hookModifyLiquidities),
IHooks.beforeAddLiquidity.selector,
abi.encodeWithSelector(ReentrancyLock.ContractLocked.selector),
abi.encodeWithSelector(Hooks.HookCallFailed.selector)
)
);
lpm.modifyLiquidities(calls, _deadline);
}
/// @dev hook cannot re-enter modifyLiquiditiesWithoutUnlock in beforeRemoveLiquidity
function test_fuzz_hook_beforeRemoveLiquidity_reenter_revert(uint256 seed) public {
uint256 initialLiquidity = 100e18;
uint256 tokenId = lpm.nextTokenId();
mint(config, initialLiquidity, address(this), ZERO_BYTES);
uint256 liquidityToChange = 10e18;
// a random action be provided as hookData, so beforeAddLiquidity will attempt to modifyLiquidity
bytes memory hookCall = getFuzzySingleEncoded(seed, tokenId, config, liquidityToChange, ZERO_BYTES);
bytes memory calls = getDecreaseEncoded(tokenId, config, liquidityToChange, hookCall);
// should revert because hook is re-entering modifyLiquiditiesWithoutUnlock
vm.expectRevert(
abi.encodeWithSelector(
CustomRevert.WrappedError.selector,
address(hookModifyLiquidities),
IHooks.beforeRemoveLiquidity.selector,
abi.encodeWithSelector(ReentrancyLock.ContractLocked.selector),
abi.encodeWithSelector(Hooks.HookCallFailed.selector)
)
);
lpm.modifyLiquidities(calls, _deadline);
}
/// @dev hook cannot re-enter modifyLiquiditiesWithoutUnlock in afterAddLiquidity
function test_fuzz_hook_afterAddLiquidity_reenter_revert(uint256 seed) public {
uint256 initialLiquidity = 100e18;
uint256 tokenId = lpm.nextTokenId();
mint(config, initialLiquidity, address(this), ZERO_BYTES);
uint256 liquidityToChange = 10e18;
// a random action be provided as hookData, so afterAddLiquidity will attempt to modifyLiquidity
bytes memory hookCall = getFuzzySingleEncoded(seed, tokenId, config, liquidityToChange, ZERO_BYTES);
bytes memory calls = getIncreaseEncoded(tokenId, config, liquidityToChange, hookCall);
// should revert because hook is re-entering modifyLiquiditiesWithoutUnlock
vm.expectRevert(
abi.encodeWithSelector(
CustomRevert.WrappedError.selector,
address(hookModifyLiquidities),
IHooks.beforeAddLiquidity.selector,
abi.encodeWithSelector(ReentrancyLock.ContractLocked.selector),
abi.encodeWithSelector(Hooks.HookCallFailed.selector)
)
);
lpm.modifyLiquidities(calls, _deadline);
}
/// @dev hook cannot re-enter modifyLiquiditiesWithoutUnlock in afterRemoveLiquidity
function test_fuzz_hook_afterRemoveLiquidity_reenter_revert(uint256 seed) public {
uint256 initialLiquidity = 100e18;
uint256 tokenId = lpm.nextTokenId();
mint(config, initialLiquidity, address(this), ZERO_BYTES);
uint256 liquidityToChange = 10e18;
// a random action be provided as hookData, so afterAddLiquidity will attempt to modifyLiquidity
bytes memory hookCall = getFuzzySingleEncoded(seed, tokenId, config, liquidityToChange, ZERO_BYTES);
bytes memory calls = getDecreaseEncoded(tokenId, config, liquidityToChange, hookCall);
// should revert because hook is re-entering modifyLiquiditiesWithoutUnlock
vm.expectRevert(
abi.encodeWithSelector(
CustomRevert.WrappedError.selector,
address(hookModifyLiquidities),
IHooks.beforeRemoveLiquidity.selector,
abi.encodeWithSelector(ReentrancyLock.ContractLocked.selector),
abi.encodeWithSelector(Hooks.HookCallFailed.selector)
)
);
lpm.modifyLiquidities(calls, _deadline);
}
}