forked from uniswapfoundation/v4-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.t.sol
87 lines (72 loc) · 3.68 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
// 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 {Deployers} from "v4-core/test/utils/Deployers.sol";
import {Counter} from "../src/Counter.sol";
import {HookMiner} from "./utils/HookMiner.sol";
contract CounterTest is Test, Deployers {
using PoolIdLibrary for PoolKey;
using CurrencyLibrary for Currency;
Counter counter;
PoolId poolId;
function setUp() public {
// creates the pool manager, utility routers, and test tokens
Deployers.deployFreshManagerAndRouters();
Deployers.deployMintAndApprove2Currencies();
// Deploy the hook to an address with the correct flags
uint160 flags = uint160(
Hooks.BEFORE_SWAP_FLAG | Hooks.AFTER_SWAP_FLAG | Hooks.BEFORE_ADD_LIQUIDITY_FLAG
| Hooks.BEFORE_REMOVE_LIQUIDITY_FLAG
);
(address hookAddress, bytes32 salt) =
HookMiner.find(address(this), flags, type(Counter).creationCode, abi.encode(address(manager)));
counter = new Counter{salt: salt}(IPoolManager(address(manager)));
require(address(counter) == hookAddress, "CounterTest: hook address mismatch");
// Create the pool
key = PoolKey(currency0, currency1, 3000, 60, IHooks(address(counter)));
poolId = key.toId();
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
// Provide liquidity to the pool
modifyLiquidityRouter.modifyLiquidity(key, IPoolManager.ModifyLiquidityParams(-60, 60, 10 ether), ZERO_BYTES);
modifyLiquidityRouter.modifyLiquidity(key, IPoolManager.ModifyLiquidityParams(-120, 120, 10 ether), ZERO_BYTES);
modifyLiquidityRouter.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams(TickMath.minUsableTick(60), TickMath.maxUsableTick(60), 10 ether),
ZERO_BYTES
);
}
function testCounterHooks() public {
// positions were created in setup()
assertEq(counter.beforeAddLiquidityCount(poolId), 3);
assertEq(counter.beforeRemoveLiquidityCount(poolId), 0);
assertEq(counter.beforeSwapCount(poolId), 0);
assertEq(counter.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(counter.beforeSwapCount(poolId), 1);
assertEq(counter.afterSwapCount(poolId), 1);
}
function testLiquidityHooks() public {
// positions were created in setup()
assertEq(counter.beforeAddLiquidityCount(poolId), 3);
assertEq(counter.beforeRemoveLiquidityCount(poolId), 0);
// remove liquidity
int256 liquidityDelta = -1e18;
modifyLiquidityRouter.modifyLiquidity(key, IPoolManager.ModifyLiquidityParams(-60, 60, liquidityDelta), ZERO_BYTES);
assertEq(counter.beforeAddLiquidityCount(poolId), 3);
assertEq(counter.beforeRemoveLiquidityCount(poolId), 1);
}
}