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

interface IUniswapV2Router {
function swapExactTokensForETH(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
)
external
returns (uint[] memory amounts);
}
interface IDextokenPool {
function swapExactETHForTokens(
uint amountIn,
uint minAmountOut,
uint maxPrice,
uint deadline
) external returns (uint);
}
function tradeOut(
address[] memory path,
address poolAddredss,
uint deadline
)
public
payable
{
uint amount = msg.value;
IWETH(WETH).deposit.value(amount)();
if (IERC20(WETH).allowance(address(this), pool) < amount) {
IERC20(WETH).safeApprove(pool, MAX);
}
/// DEXG Swap: WETH to Token
IDextokenPool(pool).swapExactETHForTokens(
amount,
0,
MAX,
deadline
);
/// Uniswap: [Token, ..., WETH]
address token = path[0];
uint tokenAmount = IERC20(token).balanceOf(address(this));
if (IERC20(token).allowance(address(this), uniV2Router) < tokenAmount) {
IERC20(token).safeApprove(uniV2Router, MAX);
}
IUniswapV2Router(uniV2Router).swapExactTokensForETH(
tokenAmount,
0,
path,
address(this),
deadline
);
msg.sender.transfer(address(this).balance);
}