forked from Uniswap/v4-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.t.sol
More file actions
137 lines (112 loc) · 4.83 KB
/
Copy pathCounter.t.sol
File metadata and controls
137 lines (112 loc) · 4.83 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import {Test} from "forge-std/Test.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol";
import {CurrencyLibrary, Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {LiquidityAmounts} from "@uniswap/v4-core/test/utils/LiquidityAmounts.sol";
import {IPositionManager} from "@uniswap/v4-periphery/src/interfaces/IPositionManager.sol";
import {Constants} from "@uniswap/v4-core/test/utils/Constants.sol";
import {EasyPosm} from "./utils/libraries/EasyPosm.sol";
import {Counter} from "../src/Counter.sol";
import {BaseTest} from "./utils/BaseTest.sol";
contract CounterTest is BaseTest {
using EasyPosm for IPositionManager;
using PoolIdLibrary for PoolKey;
using CurrencyLibrary for Currency;
using StateLibrary for IPoolManager;
Currency currency0;
Currency currency1;
PoolKey poolKey;
Counter hook;
PoolId poolId;
uint256 tokenId;
int24 tickLower;
int24 tickUpper;
function setUp() public {
// Deploys all required artifacts.
deployArtifactsAndLabel();
(currency0, currency1) = deployCurrencyPair();
// Deploy the hook to an address with the correct flags
address flags = address(
uint160(
Hooks.BEFORE_SWAP_FLAG | Hooks.AFTER_SWAP_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG
| Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG
) ^ (0x4444 << 144) // Namespace the hook to avoid collisions
);
bytes memory constructorArgs = abi.encode(poolManager); // Add all the necessary constructor arguments from the hook
deployCodeTo("Counter.sol:Counter", constructorArgs, flags);
hook = Counter(flags);
// Create the pool
poolKey = PoolKey(currency0, currency1, 3000, 60, IHooks(hook));
poolId = poolKey.toId();
poolManager.initialize(poolKey, Constants.SQRT_PRICE_1_1);
// Provide full-range liquidity to the pool
tickLower = TickMath.minUsableTick(poolKey.tickSpacing);
tickUpper = TickMath.maxUsableTick(poolKey.tickSpacing);
uint128 liquidityAmount = 100e18;
(uint256 amount0Expected, uint256 amount1Expected) = LiquidityAmounts.getAmountsForLiquidity(
Constants.SQRT_PRICE_1_1,
TickMath.getSqrtPriceAtTick(tickLower),
TickMath.getSqrtPriceAtTick(tickUpper),
liquidityAmount
);
(tokenId,) = positionManager.mint(
poolKey,
tickLower,
tickUpper,
liquidityAmount,
amount0Expected + 1,
amount1Expected + 1,
address(this),
block.timestamp,
Constants.ZERO_BYTES
);
}
function testCounterHooks() public {
// positions were created in setup()
assertEq(hook.beforeAddLiquidityCount(poolId), 1);
assertEq(hook.beforeRemoveLiquidityCount(poolId), 0);
assertEq(hook.beforeSwapCount(poolId), 0);
assertEq(hook.afterSwapCount(poolId), 0);
// Perform a test swap //
uint256 amountIn = 1e18;
BalanceDelta swapDelta = swapRouter.swapExactTokensForTokens({
amountIn: amountIn,
amountOutMin: 0, // Very bad, but we want to allow for unlimited price impact
zeroForOne: true,
poolKey: poolKey,
hookData: Constants.ZERO_BYTES,
receiver: address(this),
deadline: block.timestamp + 1
});
// ------------------- //
assertEq(int256(swapDelta.amount0()), -int256(amountIn));
assertEq(hook.beforeSwapCount(poolId), 1);
assertEq(hook.afterSwapCount(poolId), 1);
}
function testLiquidityHooks() public {
// positions were created in setup()
assertEq(hook.beforeAddLiquidityCount(poolId), 1);
assertEq(hook.beforeRemoveLiquidityCount(poolId), 0);
// remove liquidity
uint256 liquidityToRemove = 1e18;
positionManager.decreaseLiquidity(
tokenId,
liquidityToRemove,
0, // Max slippage, token0
0, // Max slippage, token1
address(this),
block.timestamp,
Constants.ZERO_BYTES
);
assertEq(hook.beforeAddLiquidityCount(poolId), 1);
assertEq(hook.beforeRemoveLiquidityCount(poolId), 1);
}
}