I am using Python and web3.py.

I have to swap eth to tokens using swapExactEthforTokens in Uniswap. And then I have to tokens to eth using swapExactTokensforEth in Uniswap.

How do get calculate profit?

I have to predict the profit.

  • This is a very broad question. Try to be more specific. Do you want to profit from arbitrage? Do you want to day trade? Do you want to frontrun transactions in the mempool? Jul 14, 2021 at 16:42
  • I want to frontrun transactions in the mempool. I think this mode is very dangerous. But I want to this method. In this method, how do get calculate profit?
    – Alex Weber
    Jul 14, 2021 at 17:09
  • in order to know how much ETH you will receive for your tokens, you must call the function on the last State database. But there is no method to simulate transactions when they modify state, only read-only simulation is possible with Call function.
    – Nulik
    Jul 14, 2021 at 17:39
  • so you need to modify geth to add this custom modification of running the transaction on latest StateDB , and then this transaction will produce Event logs, you check the event log for the amount of tokens you would receive if the transaction is successful
    – Nulik
    Jul 14, 2021 at 17:40
  • but since Uniswap accounts for about 30% of transaction traffic it would be very difficult that predicted profit would match real profit, since there will be many transactions before yours in the block that would also swap the token you are swapping
    – Nulik
    Jul 14, 2021 at 17:42

1 Answer 1


This is a case of an Arbitrage. In summary, an Arbitrage is the transferring of goods from market to market while buying at a lower price in one and selling for higher in the other market. The practice is enabled by the fact that markets are not that fast to know exactly the price of a certain good in all other markets in order to fix its pricing.

The arbitrage is a product of that issue and is the best way to fixing it. The prediction is an exercise of "The shortest route" algorithms.

here is a more elaborate post on that : Finance And money StackExchange

  • Thank you for your reply. I saw the URL that you support. Do you have a sample code like this? If you had, Could you show me this code?
    – Alex Weber
    Jul 14, 2021 at 19:07
  • @AlexWeber i am afraid that i don't. but the shortest path problems are not that hard if i remember correctly. The arbitrage in general is not that easy to pull out (huge competition), and your best bet would be very long paths of swaps with cross Dex/Chain operations. Jul 14, 2021 at 19:12

Your Answer

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

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