-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathCounter.t.sol
124 lines (103 loc) · 4.4 KB
/
Counter.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "forge-std/Test.sol";
import {IHooks} from "v4-core/src/interfaces/IHooks.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {TickMath} from "v4-core/src/libraries/TickMath.sol";
import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {BalanceDelta} from "v4-core/src/types/BalanceDelta.sol";
import {PoolId, PoolIdLibrary} from "v4-core/src/types/PoolId.sol";
import {CurrencyLibrary, Currency} from "v4-core/src/types/Currency.sol";
import {PoolSwapTest} from "v4-core/src/test/PoolSwapTest.sol";
import {Counter} from "../src/Counter.sol";
import {StateLibrary} from "v4-core/src/libraries/StateLibrary.sol";
import {LiquidityAmounts} from "v4-core/test/utils/LiquidityAmounts.sol";
import {IPositionManager} from "v4-periphery/src/interfaces/IPositionManager.sol";
import {EasyPosm} from "./utils/EasyPosm.sol";
import {Fixtures} from "./utils/Fixtures.sol";
contract CounterTest is Test, Fixtures {
using EasyPosm for IPositionManager;
using PoolIdLibrary for PoolKey;
using CurrencyLibrary for Currency;
using StateLibrary for IPoolManager;
Counter hook;
PoolId poolId;
uint256 tokenId;
int24 tickLower;
int24 tickUpper;
function setUp() public {
// creates the pool manager, utility routers, and test tokens
deployFreshManagerAndRouters();
deployMintAndApprove2Currencies();
deployAndApprovePosm(manager);
// 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(manager); //Add all the necessary constructor arguments from the hook
deployCodeTo("Counter.sol:Counter", constructorArgs, flags);
hook = Counter(flags);
// Create the pool
key = PoolKey(currency0, currency1, 3000, 60, IHooks(hook));
poolId = key.toId();
manager.initialize(key, SQRT_PRICE_1_1);
// Provide full-range liquidity to the pool
tickLower = TickMath.minUsableTick(key.tickSpacing);
tickUpper = TickMath.maxUsableTick(key.tickSpacing);
uint128 liquidityAmount = 100e18;
(uint256 amount0Expected, uint256 amount1Expected) = LiquidityAmounts.getAmountsForLiquidity(
SQRT_PRICE_1_1,
TickMath.getSqrtPriceAtTick(tickLower),
TickMath.getSqrtPriceAtTick(tickUpper),
liquidityAmount
);
(tokenId,) = posm.mint(
key,
tickLower,
tickUpper,
liquidityAmount,
amount0Expected + 1,
amount1Expected + 1,
address(this),
block.timestamp,
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 //
bool zeroForOne = true;
int256 amountSpecified = -1e18; // negative number indicates exact input swap!
BalanceDelta swapDelta = swap(key, zeroForOne, amountSpecified, ZERO_BYTES);
// ------------------- //
assertEq(int256(swapDelta.amount0()), amountSpecified);
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;
posm.decreaseLiquidity(
tokenId,
liquidityToRemove,
MAX_SLIPPAGE_REMOVE_LIQUIDITY,
MAX_SLIPPAGE_REMOVE_LIQUIDITY,
address(this),
block.timestamp,
ZERO_BYTES
);
assertEq(hook.beforeAddLiquidityCount(poolId), 1);
assertEq(hook.beforeRemoveLiquidityCount(poolId), 1);
}
}