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)
    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]);
  • You can't make any profit by swapping from eth to tokens and then back from tokens to eth. You will only lose the tx and uniswap fee. It's unclear what you are trying to achieve.
    – Xavier59
    Feb 17, 2021 at 0:02
  • @Xavier59 these is what most arbitrage bots do, they first convert to the currency of devalued asset, then at the end of the transaction they exchange it back to ETH or whatever token they started with
    – Nulik
    Feb 17, 2021 at 1:37
  • Basic idea is arbitrage from ETH to tokens and back to ETH. Ok that's assuming a path length > 4. And yes, you can transfer eth from a payable function, so your idea should work.
    – Xavier59
    Feb 17, 2021 at 1:59

1 Answer 1


Technically speaking what you propose is possible. Perhaps I would leave the two functions swapExactETHForTokens & swapExactTokensForETH as they are, and I would create a new function to call them in a single transaction in order to adjust the function parameters as needed.

However, if your goal is to do arbitrage in order to make some profit, you should be leveraging a price discrepancy across different exchanges. If you do the arbitrage within the same exchange, you won't get any profit unless the time period between the swaps is long enough to affect the prices (luckily increasing, otherwise you will lose value), but that would be more like a long term investment rather than arbitrage, and apparently, you want to do the two swaps in the same transaction.

It is nowadays a common practise to use flashloan pattern for arbitrage: basic idea is to listen for price discrepancies between different decentralized exchanges, and then do all these steps in a single transaction:

  1. Borrow an amount X of tokens at DeFi platform D
  2. Exchange X amount of tokens at DEX A (e.g.: Uniswap)
  3. Exchange X amount of tokens at DEX B (e.g.: Kyber)
  4. Return the loan at DeFi platform D

This way, you don't need to have ethers or tokens in advance to perform all these operations. The price discrepancy between A & B must be higher than the fees you pay for the loan and swaps if you want to have any profit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.