generated from uniswapfoundation/v4-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCounterScript.sol
80 lines (64 loc) · 3.25 KB
/
CounterScript.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import {console} from "forge-std/console.sol";
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 {PoolManager} from "@uniswap/v4-core/contracts/PoolManager.sol";
import {FeeLibrary} from "@uniswap/v4-core/contracts/libraries/FeeLibrary.sol";
import {TickMath} from "@uniswap/v4-core/contracts/libraries/TickMath.sol";
import {Currency} from "@uniswap/v4-core/contracts/types/Currency.sol";
import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolId.sol";
import {CounterFactory} from "../src/hooks/CounterHook.sol";
import {CallType} from "../src/router/UniswapV4Router.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 CounterScript is Script, TestPoolManager {
PoolKey poolKey;
uint256 privateKey;
address signerAddr;
function setUp() public {
privateKey = vm.envUint("PRIVATE_KEY");
signerAddr = vm.addr(privateKey);
console.log("signer %s", signerAddr);
console.log("script %s", address(this));
vm.startBroadcast(privateKey);
TestPoolManager.initialize();
// Deploy the hook
CounterFactory factory = new CounterFactory();
// Deploy has to mine a salt to match the Uniswap hook flags so can use a lot of gas
// If this counter script is executed against a new Anvil node,
// the PoolManager address will be 0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9.
// The first salt from 0 to get the required address perfix is 471
// so starting from that to not burn up too much gas.
IHooks hook = IHooks(factory.mineDeploy(manager, 471));
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)),
FeeLibrary.HOOK_SWAP_FEE_FLAG | FeeLibrary.HOOK_WITHDRAW_FEE_FLAG | 3000,
60,
hook
);
// Create the pool in the Uniswap Pool Manager
manager.initialize(poolKey, SQRT_RATIO_1_TO_1, "");
console.log("currency0 %s", Currency.unwrap(poolKey.currency0));
console.log("currency1 %s", Currency.unwrap(poolKey.currency1));
// Provide liquidity to the pool
caller.addLiquidity(poolKey, signerAddr, -60, 60, 10 ether);
caller.addLiquidity(poolKey, signerAddr, -120, 120, 10 ether);
caller.addLiquidity(poolKey, signerAddr, TickMath.minUsableTick(60), TickMath.maxUsableTick(60), 10 ether);
vm.stopBroadcast();
}
function run() public {
vm.startBroadcast(privateKey);
// Perform a test swap
caller.swap(poolKey, signerAddr, signerAddr, poolKey.currency0, 1e18);
// Perform a flash loan
bytes memory callbackData = abi.encodeWithSelector(tokenA.balanceOf.selector, router);
caller.flashLoan(address(tokenA), 1e6, address(tokenA), CallType.Call, callbackData);
vm.stopBroadcast();
}
}