solidity – Is It Possible to Prevent Front-Running Using Own Contract in Uniswap – A Detailed Solution

frontrunningsolidityuniswap

If instead of using my wallet I use a contract in Solidity to make the swaps in Uniswap (That is, calling the uniswap API from the contract instead of using the Uniswap interface)

Is it possible to avoid front-running in this way?

Is there a way to obfuscate the transaction using a contract so that it is not seen by the front runers?

Or could the approach be to do two transactions together (in the same transaction) to avoid the front-run?

Best Answer

There are two parts in Uniswap: the centralized frontend (website) and decentralized trading contracts. Most(?) users utilize the frontend to trade, but it's perfectly ok to also use the contracts directly.

What frontrunners do, is that they monitor and analyze the mempool for transactions. So they monitor transactions which are pending, but not yet processed. If they notice a transaction which profits from some trade, they try to frontrun it with their own similar transaction - by sending a direct transaction to the Uniswap contracts.

Therefore it's not possible to prevent frontrunning by simply utilizing the contracts directly.

Furthemore, you can't really do two transactions in one transaction. If you utilize a contract which performs operations on multiple contracts, you can achieve multiple internal transactions within one transaction. Unfortunately that probably won't help you in obfuscating the trade - frontrunners will simply emulate what the transaction does and analyze whether it would give them profits.