generated from uniswapfoundation/v4-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MyHookScript.sol
68 lines (52 loc) · 2.59 KB
/
MyHookScript.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import "forge-std/Script.sol";
import {Hooks} from "@uniswap/v4-core/contracts/libraries/Hooks.sol";
import {IHooks} from "@uniswap/v4-core/contracts/interfaces/IHooks.sol";
import {TickMath} from "@uniswap/v4-core/contracts/libraries/TickMath.sol";
import {CurrencyLibrary, Currency} from "@uniswap/v4-core/contracts/types/Currency.sol";
import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolId.sol";
import {TestERC20} from "@uniswap/v4-core/contracts/test/TestERC20.sol";
import {MyHookFactory} from "../src/hooks/MyHook.sol";
import {TestPoolManager} from "../test/utils/TestPoolManager.sol";
/// @notice Forge script for deploying v4 & hooks to **anvil**
/// @dev This script only works on an anvil RPC because v4 exceeds bytecode limits
contract MyHookScript is Script, TestPoolManager {
using CurrencyLibrary for Currency;
PoolKey poolKey;
uint256 privateKey;
address signerAddr;
function setUp() public {
privateKey = vm.envUint("PRIVATE_KEY");
signerAddr = vm.addr(privateKey);
vm.startBroadcast(privateKey);
TestPoolManager.initialize();
// Deploy the hook
MyHookFactory factory = new MyHookFactory();
// Any changes to the MyHook contract will mean a different salt will be needed
// so just starting from 0 in this script
IHooks hook = IHooks(factory.mineDeploy(manager, 0));
console.log("Deployed hook to address %s", address(hook));
// Derive the key for the new pool
poolKey = PoolKey(Currency.wrap(address(tokenA)), Currency.wrap(address(tokenB)), 3000, 60, hook);
// Create the pool in the Uniswap Pool Manager
manager.initialize(poolKey, SQRT_RATIO_1_TO_1, "");
// Provide liquidity to the pool
caller.addLiquidity(poolKey, signerAddr, -60, 60, 10e18);
caller.addLiquidity(poolKey, signerAddr, -120, 120, 20e18);
caller.addLiquidity(poolKey, signerAddr, TickMath.minUsableTick(60), TickMath.maxUsableTick(60), 30e18);
vm.stopBroadcast();
}
function run() public {
vm.startBroadcast(privateKey);
// Perform a test swap
caller.swap(poolKey, signerAddr, signerAddr, poolKey.currency0, 1e18);
console.log("swapped token 0 for token 1");
// Remove liquidity from the pool
caller.removeLiquidity(poolKey, signerAddr, -60, 60, 4e18);
console.log("removed liquidity");
// Deposit token 0 to the pool manager
// caller.deposit(address(tokenA), signerAddr, signerAddr, 6e18);
vm.stopBroadcast();
}
}