forked from Uniswap/v4-core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSkipCallsTestHook.t.sol
212 lines (175 loc) Β· 9.35 KB
/
SkipCallsTestHook.t.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
import {Test} from "forge-std/Test.sol";
import {Vm} from "forge-std/Vm.sol";
import {PoolId} from "../src/types/PoolId.sol";
import {Hooks} from "../src/libraries/Hooks.sol";
import {IPoolManager} from "../src/interfaces/IPoolManager.sol";
import {IProtocolFees} from "../src/interfaces/IProtocolFees.sol";
import {IHooks} from "../src/interfaces/IHooks.sol";
import {PoolKey} from "../src/types/PoolKey.sol";
import {PoolManager} from "../src/PoolManager.sol";
import {PoolSwapTest} from "../src/test/PoolSwapTest.sol";
import {Deployers} from "./utils/Deployers.sol";
import {Currency} from "../src/types/Currency.sol";
import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol";
import {Constants} from "../test/utils/Constants.sol";
import {SkipCallsTestHook} from "../src/test/SkipCallsTestHook.sol";
contract SkipCallsTest is Test, Deployers {
PoolSwapTest.TestSettings testSettings = PoolSwapTest.TestSettings({takeClaims: false, settleUsingBurn: false});
function deploy(SkipCallsTestHook skipCallsTestHook) private {
SkipCallsTestHook impl = new SkipCallsTestHook();
vm.etch(address(skipCallsTestHook), address(impl).code);
deployFreshManagerAndRouters();
skipCallsTestHook.setManager(IPoolManager(manager));
deployMintAndApprove2Currencies();
assertEq(skipCallsTestHook.counter(), 0);
(key,) = initPool(currency0, currency1, IHooks(address(skipCallsTestHook)), 3000, SQRT_PRICE_1_1);
}
function approveAndAddLiquidity(SkipCallsTestHook skipCallsTestHook) private {
MockERC20(Currency.unwrap(key.currency0)).approve(address(skipCallsTestHook), Constants.MAX_UINT256);
MockERC20(Currency.unwrap(key.currency1)).approve(address(skipCallsTestHook), Constants.MAX_UINT256);
modifyLiquidityRouter.modifyLiquidity(key, LIQUIDITY_PARAMS, abi.encode(address(this)));
}
function test_beforeInitialize_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_INITIALIZE_FLAG))
);
// initializes pool and increments counter
deploy(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 1);
}
function test_afterInitialize_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_INITIALIZE_FLAG))
);
// initializes pool and increments counter
deploy(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 1);
}
function test_beforeAddLiquidity_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_ADD_LIQUIDITY_FLAG))
);
deploy(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 0);
// adds liquidity and increments counter
approveAndAddLiquidity(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 1);
// adds liquidity again and increments counter
modifyLiquidityRouter.modifyLiquidity(key, LIQUIDITY_PARAMS, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 2);
}
function test_afterAddLiquidity_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_ADD_LIQUIDITY_FLAG))
);
deploy(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 0);
// adds liquidity and increments counter
approveAndAddLiquidity(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 1);
// adds liquidity and increments counter again
modifyLiquidityRouter.modifyLiquidity(key, LIQUIDITY_PARAMS, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 2);
}
function test_beforeRemoveLiquidity_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG))
);
deploy(skipCallsTestHook);
approveAndAddLiquidity(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 0);
// removes liquidity and increments counter
modifyLiquidityRouter.modifyLiquidity(key, REMOVE_LIQUIDITY_PARAMS, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 1);
// adds liquidity again
modifyLiquidityRouter.modifyLiquidity(key, LIQUIDITY_PARAMS, abi.encode(address(this)));
// removes liquidity again and increments counter
modifyLiquidityRouter.modifyLiquidity(key, REMOVE_LIQUIDITY_PARAMS, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 2);
}
function test_afterRemoveLiquidity_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_REMOVE_LIQUIDITY_FLAG))
);
deploy(skipCallsTestHook);
approveAndAddLiquidity(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 0);
// removes liquidity and increments counter
modifyLiquidityRouter.modifyLiquidity(key, REMOVE_LIQUIDITY_PARAMS, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 1);
// adds liquidity again
modifyLiquidityRouter.modifyLiquidity(key, LIQUIDITY_PARAMS, abi.encode(address(this)));
// removes liquidity again and increments counter
modifyLiquidityRouter.modifyLiquidity(key, REMOVE_LIQUIDITY_PARAMS, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 2);
}
function test_beforeSwap_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_SWAP_FLAG))
);
deploy(skipCallsTestHook);
approveAndAddLiquidity(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 0);
// swaps and increments counter
swapRouter.swap(key, SWAP_PARAMS, testSettings, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 1);
// swaps again and increments counter
swapRouter.swap(key, SWAP_PARAMS, testSettings, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 2);
}
function test_gas_beforeSwap_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_SWAP_FLAG))
);
deploy(skipCallsTestHook);
approveAndAddLiquidity(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 0);
// swaps and increments counter
swapRouter.swap(key, SWAP_PARAMS, testSettings, abi.encode(address(this)));
vm.snapshotGasLastCall("swap skips hook call if hook is caller");
assertEq(skipCallsTestHook.counter(), 1);
}
function test_afterSwap_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook =
SkipCallsTestHook(address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_SWAP_FLAG)));
deploy(skipCallsTestHook);
approveAndAddLiquidity(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 0);
// swaps and increments counter
swapRouter.swap(key, SWAP_PARAMS, testSettings, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 1);
// swaps again and increments counter
swapRouter.swap(key, SWAP_PARAMS, testSettings, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 2);
}
function test_beforeDonate_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.BEFORE_DONATE_FLAG))
);
deploy(skipCallsTestHook);
approveAndAddLiquidity(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 0);
// donates and increments counter
donateRouter.donate(key, 100, 200, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 1);
// donates again and increments counter
donateRouter.donate(key, 100, 200, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 2);
}
function test_afterDonate_skipIfCalledByHook() public {
SkipCallsTestHook skipCallsTestHook = SkipCallsTestHook(
address(uint160(type(uint160).max & clearAllHookPermissionsMask | Hooks.AFTER_DONATE_FLAG))
);
deploy(skipCallsTestHook);
approveAndAddLiquidity(skipCallsTestHook);
assertEq(skipCallsTestHook.counter(), 0);
// donates and increments counter
donateRouter.donate(key, 100, 200, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 1);
// donates again and increments counter
donateRouter.donate(key, 100, 200, abi.encode(address(this)));
assertEq(skipCallsTestHook.counter(), 2);
}
}