DEXToken
Search…
DEXG Pool Trade Out
The use case is a "Trade Out" swap in a single transaction:
  • We have 1 ETH, and
  • Want to wrap this 1 ETH to 1 WETH, and
  • Swap 1 WETH for an amount of tokens from the pool poolAddress, and then
  • Sell the the amount of tokens in Uniswap V2
1
interface IUniswapV2Router {
2
function swapExactTokensForETH(
3
uint amountIn,
4
uint amountOutMin,
5
address[] calldata path,
6
address to,
7
uint deadline
8
)
9
external
10
returns (uint[] memory amounts);
11
}
12
13
interface IDextokenPool {
14
function swapExactETHForTokens(
15
uint amountIn,
16
uint minAmountOut,
17
uint maxPrice,
18
uint deadline
19
) external returns (uint);
20
}
21
22
function tradeOut(
23
address[] memory path,
24
address poolAddredss,
25
uint deadline
26
)
27
public
28
payable
29
{
30
uint amount = msg.value;
31
32
IWETH(WETH).deposit.value(amount)();
33
34
if (IERC20(WETH).allowance(address(this), pool) < amount) {
35
IERC20(WETH).safeApprove(pool, MAX);
36
}
37
38
/// DEXG Swap: WETH to Token
39
IDextokenPool(pool).swapExactETHForTokens(
40
amount,
41
0,
42
MAX,
43
deadline
44
);
45
46
/// Uniswap: [Token, ..., WETH]
47
address token = path[0];
48
uint tokenAmount = IERC20(token).balanceOf(address(this));
49
50
if (IERC20(token).allowance(address(this), uniV2Router) < tokenAmount) {
51
IERC20(token).safeApprove(uniV2Router, MAX);
52
}
53
54
IUniswapV2Router(uniV2Router).swapExactTokensForETH(
55
tokenAmount,
56
0,
57
path,
58
address(this),
59
deadline
60
);
61
62
msg.sender.transfer(address(this).balance);
63
}
Copied!
Last modified 5mo ago
Copy link