UniswapV2Router02.sol has 2 interesting functions which i'd love to combine swapExactETHForTokens and swapExactTokensForETH. Basic idea is arbitrage from ETH to tokens and back to ETH. As far I understand swapExactETHForTokens deposits ETH and performs swaps and swapExactTokensForETH performs swaps and withdraws ETH. Is it possible to implement my idea by simply adding withdrawal code to the end of swapExactETHForTokens?
function swapExactETHForTokensForETH(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
virtual
override
payable
ensure(deadline)
returns (uint[] memory amounts)
{
require(path[0] == WETH, 'UniswapV2Router: INVALID_PATH');
amounts = UniswapV2Library.getAmountsOut(factory, msg.value, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
IWETH(WETH).deposit{value: amounts[0]}();
assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
_swap(amounts, path, to);
// Adding code from swapExactETHForTokens here
IWETH(WETH).withdraw(amounts[amounts.length - 1]);
TransferHelper.safeTransferETH(to, amounts[amounts.length - 1]);
}