-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniswap.sol
65 lines (52 loc) · 1.48 KB
/
Uniswap.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
pragma solidity ^0.7.0;
interface IUniswap {
function swapExactTokensForEth(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline)
external
returns (uint[] memory amounts);
function WETH() external pure returns (address); //to return the address of wrapped ether
}
interface IERC20 {
function transferFrom(
address sender,
address recipient,
uint256 amount
)
external
returns (bool);
function approve(address spender,
uint256 amount
)
external
returns (bool);
}
contract Uniswap {
IUniswap uniswap;
constructor(address _uniswap) {
uniswap = IUniswap(_uniswap);
}
function swapExactTokensForEth(
address token,
uint amountIn, // how much token are we willing to spend
uint amountOutMin, //how much token are we willing to buy
uint deadline)
external {
IERC20(token).transferFrom(msg.sender, address(this), amountIn);
address[] memory path = new address[](2);
path[0] = token;
path[1] = uniswap.WETH(); //returns the address of WETH
//approve uniswap to approve our token
IERC20(token).approve(address(uniswap), amountIn);
uniswap.swapExactTokensForEth(
amountIn,
amountOutMin,
path,
msg.sender,
deadline
);
}
}