-
Notifications
You must be signed in to change notification settings - Fork 23
/
UniswapV3Swap.test.sol
124 lines (111 loc) · 4.12 KB
/
UniswapV3Swap.test.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 {Test, console2} from "forge-std/Test.sol";
import {IERC20} from "../../../src/interfaces/IERC20.sol";
import {IWETH} from "../../../src/interfaces/IWETH.sol";
import {ISwapRouter} from "../../../src/interfaces/uniswap-v3/ISwapRouter.sol";
import {
DAI,
WETH,
WBTC,
UNISWAP_V3_SWAP_ROUTER_02
} from "../../../src/Constants.sol";
contract UniswapV3SwapTest is Test {
IWETH private weth = IWETH(WETH);
IERC20 private dai = IERC20(DAI);
IERC20 private wbtc = IERC20(WBTC);
ISwapRouter private router = ISwapRouter(UNISWAP_V3_SWAP_ROUTER_02);
uint24 private constant POOL_FEE = 3000;
function setUp() public {
deal(DAI, address(this), 1000 * 1e18);
dai.approve(address(router), type(uint256).max);
}
// Exercise 1
// - Swap 1000 DAI for WETH on DAI/WETH pool with 0.3% fee
// - Send WETH from Uniswap V3 to this contract
function test_exactInputSingle() public {
// Write your code here
// Call router.exactInputSingle
uint256 amountOut = router.exactInputSingle(
ISwapRouter.ExactInputSingleParams({
tokenIn: DAI,
tokenOut: WETH,
fee: POOL_FEE,
recipient: address(this),
amountIn: 1000 * 1e18,
amountOutMinimum: 1,
sqrtPriceLimitX96: 0
})
);
console2.log("WETH amount out %e", amountOut);
assertGt(amountOut, 0);
assertEq(weth.balanceOf(address(this)), amountOut);
}
// Exercise 2
// Swap 1000 DAI for WETH and then WETH to WBTC
// - Swap DAI to WETH on pool with 0.3% fee
// - Swap WETH to WBTC on pool with 0.3% fee
// - Send WBTC from Uniswap V3 to this contract
// NOTE: WBTC has 8 decimals
function test_exactInput() public {
// Write your code here
// Call router.exactInput
bytes memory path =
abi.encodePacked(DAI, uint24(3000), WETH, uint24(3000), WBTC);
uint256 amountOut = router.exactInput(
ISwapRouter.ExactInputParams({
path: path,
recipient: address(this),
amountIn: 1000 * 1e18,
amountOutMinimum: 1
})
);
console2.log("WBTC amount out %e", amountOut);
assertGt(amountOut, 0);
assertEq(wbtc.balanceOf(address(this)), amountOut);
}
// Exercise 3
// - Swap maximum of 1000 DAI to obtain exactly 0.1 WETH from DAI/WETH pool with 0.3% fee
// - Send WETH from Uniswap V3 to this contract
function test_exactOutputSingle() public {
// Write your code here
// Call router.exactOutputSingle
uint256 amountIn = router.exactOutputSingle(
ISwapRouter.ExactOutputSingleParams({
tokenIn: DAI,
tokenOut: WETH,
fee: POOL_FEE,
recipient: address(this),
amountOut: 0.1 * 1e18,
amountInMaximum: 1000 * 1e18,
sqrtPriceLimitX96: 0
})
);
console2.log("DAI amount in %e", amountIn);
assertLe(amountIn, 1000 * 1e18);
assertEq(weth.balanceOf(address(this)), 0.1 * 1e18);
}
// Exercise 4
// Swap maximum of 1000 DAI to obtain exactly 0.01 WBTC
// - Swap DAI to WETH on pool with 0.3% fee
// - Swap WETH to WBTC on pool with 0.3% fee
// - Send WBTC from Uniswap V3 to this contract
// NOTE: WBTC has 8 decimals
function test_exactOutput() public {
// Write your code here
// Call router.exactOutput
bytes memory path =
abi.encodePacked(WBTC, uint24(3000), WETH, uint24(3000), DAI);
uint256 amountIn = router.exactOutput(
ISwapRouter.ExactOutputParams({
path: path,
recipient: address(this),
amountOut: 0.01 * 1e8,
amountInMaximum: 1000 * 1e18
})
);
console2.log("DAI amount in %e", amountIn);
assertLe(amountIn, 1000 * 1e18);
assertEq(wbtc.balanceOf(address(this)), 0.01 * 1e8);
}
}