-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_Swap.s.sol
67 lines (56 loc) · 3.03 KB
/
03_Swap.s.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "forge-std/Script.sol";
import "forge-std/console.sol";
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";
import {PoolSwapTest} from "v4-core/src/test/PoolSwapTest.sol";
import {TickMath} from "v4-core/src/libraries/TickMath.sol";
import {CurrencyLibrary, Currency} from "v4-core/src/types/Currency.sol";
import {IHooks} from "v4-core/src/interfaces/IHooks.sol";
contract SwapScript is Script {
address constant POOL_MANAGER = address(0xE5dF461803a59292c6c03978c17857479c40bc46);
address constant TOKEN1_ADDRESS = address(0xA47757c742f4177dE4eEA192380127F8B62455F5);
address constant TOKEN2_ADDRESS = address(0xFDA93151f6146f763D3A80Ddb4C5C7B268469465);
address constant HOOK_ADDRESS = address(0x030C65FFc979C367e58DE454bBB5841bF7aF8573);
PoolSwapTest swapRouter = PoolSwapTest(0x5bA874E13D2Cf3161F89D1B1d1732D14226dBF16);
// slippage tolerance to allow for unlimited price impact
uint160 public constant MIN_PRICE_LIMIT = TickMath.MIN_SQRT_RATIO + 1;
uint160 public constant MAX_PRICE_LIMIT = TickMath.MAX_SQRT_RATIO - 1;
function run() external {
address token0 = uint160(TOKEN2_ADDRESS) < uint160(TOKEN1_ADDRESS) ? TOKEN2_ADDRESS : TOKEN1_ADDRESS;
address token1 = uint160(TOKEN2_ADDRESS) < uint160(TOKEN1_ADDRESS) ? TOKEN1_ADDRESS : TOKEN2_ADDRESS;
uint24 swapFee = 4000;
int24 tickSpacing = 10;
// Using a hooked pool
PoolKey memory pool = PoolKey({
currency0: Currency.wrap(token0),
currency1: Currency.wrap(token1),
fee: swapFee,
tickSpacing: tickSpacing,
hooks: IHooks(HOOK_ADDRESS)
});
// approve tokens to the swap router
vm.broadcast();
IERC20(token0).approve(address(swapRouter), type(uint256).max);
vm.broadcast();
IERC20(token1).approve(address(swapRouter), type(uint256).max);
// ---------------------------- //
// Swap 100e18 token0 into token1 //
// ---------------------------- //
bool zeroForOne = true;
IPoolManager.SwapParams memory params = IPoolManager.SwapParams({
zeroForOne: zeroForOne,
amountSpecified: 3e18,
sqrtPriceLimitX96: zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT // unlimited impact
});
// in v4, users have the option to receive native ERC20s or wrapped ERC1155 tokens
// here, we'll take the ERC20s
PoolSwapTest.TestSettings memory testSettings =
PoolSwapTest.TestSettings({withdrawTokens: true, settleUsingTransfer: true, currencyAlreadySent: false});
bytes memory hookData = hex"b109d41a4f2a6bc531deb49ae7a44bb823bb1c80144a51232369975b8e8bec813207c1d41c4720eb9357989c91b288de0945b60dfe380b80dc665b998687917201"; // TODO: Use allowed sig
vm.broadcast();
swapRouter.swap(pool, params, testSettings, hookData);
}
}