-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathUniswapV2Swap.test.sol
76 lines (62 loc) · 2.26 KB
/
UniswapV2Swap.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
// 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 {IUniswapV2Router02} from
"../../../src/interfaces/uniswap-v2/IUniswapV2Router02.sol";
import {IUniswapV2Pair} from
"../../../src/interfaces/uniswap-v2/IUniswapV2Pair.sol";
import {
DAI,
WETH,
MKR,
UNISWAP_V2_PAIR_DAI_MKR,
UNISWAP_V2_ROUTER_02
} from "../../../src/Constants.sol";
contract UniswapV2SwapTest is Test {
IWETH private constant weth = IWETH(WETH);
IERC20 private constant dai = IERC20(DAI);
IERC20 private constant mkr = IERC20(MKR);
IUniswapV2Router02 private constant router =
IUniswapV2Router02(UNISWAP_V2_ROUTER_02);
IUniswapV2Pair private constant pair =
IUniswapV2Pair(UNISWAP_V2_PAIR_DAI_MKR);
address private constant user = address(100);
function setUp() public {
deal(user, 100 * 1e18);
vm.startPrank(user);
weth.deposit{value: 100 * 1e18}();
weth.approve(address(router), type(uint256).max);
vm.stopPrank();
// Add MKR liquidity to DAI/MKR pool
deal(DAI, address(pair), 1e6 * 1e18);
deal(MKR, address(pair), 1e5 * 1e18);
pair.sync();
}
// Swap all input tokens for as many output tokens as possible
function test_swapExactTokensForTokens() public {
address[] memory path = new address[](3);
path[0] = WETH;
path[1] = DAI;
path[2] = MKR;
uint256 amountIn = 1e18;
uint256 amountOutMin = 1;
// Write your code here
// Don’t change any other code
assertGe(mkr.balanceOf(user), amountOutMin, "MKR balance of user");
}
// Receive an exact amount of output tokens for as few input tokens
// as possible
function test_swapTokensForExactTokens() public {
address[] memory path = new address[](3);
path[0] = WETH;
path[1] = DAI;
path[2] = MKR;
uint256 amountOut = 0.1 * 1e18;
uint256 amountInMax = 1e18;
// Write your code here
// Don’t change any other code
assertEq(mkr.balanceOf(user), amountOut, "MKR balance of user");
}
}