3

With frontrunning in the headlines these days due to raids on platforms like Uniswap I was wondering from a technical point of view how these guys are able to do it ? I'm still learning Ethereum with all the pros and cons, especially with subjects such as the txpool.

I've watched the successful bots closely and they all seem to be using smart contracts. My initial guess was that everything was done on-chain, however, after learning that smart contracts cannot listen to events of other smart contracts, I'm not so sure that is the case anymore. How can a smart contract trigger a trade if it is not notified by some event of doing so ?

So all that I can think of is that an off-chain bot is going through every single pending transaction in the txpool and when it detects what it's looking for, it calls the on-chain smart contract to execute the trade.

Is my thinking correct on this ?
My second question would be is there a way to stop them?

I did not mark a correct answer below as both answers add a lot of value and completed the picture in my opinion

2 Answers 2

3

The bots are continuously listening to both onchain changes (events, transactions, etc) and offchain changes (for example, price changes).

Whenever they find that the time is right, they send a transaction to one of their already-deployed smart contracts, or possibly even deploy a new smart contract.

In both cases, the desired set of operations is performed atomically within the transaction.

5
  • Thanks for the explanation. So am I right in assuming the "listening" happens off-chain (using Node with web3 etc.)? My understanding is you cannot do that on-chain
    – ceds
    Sep 28, 2020 at 15:44
  • @ceds: Both the listening AND the triggering of the transaction, always happen offchain. Nothing happens "willingly on its own" on the blockchain. Sep 28, 2020 at 15:45
  • Very interesting. A lot of people believe these "magicians" run everything onchain. Glad you cleared that one up. Now I'm also wondering why they don't just send their trading transactions off-chain? Why use a smart contract in the first place? What possible advantage would that give you ?
    – ceds
    Sep 28, 2020 at 15:47
  • @ceds: Sounds like you're misunderstanding the whole thing. You can't change anything on the blockchain without executing transactions on it. You send a transaction to be executed on the blockchain, and that transaction can consist of either one of two things - ether transfer, or a smart contract function (which can also include ether-transfer). Sep 28, 2020 at 16:37
  • Yeah thanks I got it now thanks for the explanation. It's like @Rob Hitchens also explain in his answer
    – ceds
    Sep 28, 2020 at 17:03
3

You're thinking is heading in the right direction. A contract cannot monitor event logs so that activity needs to be off-chain. Using off-chain processes to identify opportunities has advantages - computing capacity, off-chain inputs, maintenance/optimization of the algorithms, cost, etc.

Many (most) attack strategies require transaction atomicity. That is, all the parts of the transaction must succeed entirely or the entire transaction must fail.

Consider a simplistic front-running arbitrage that wants to buy something from somewhere at a price that is lower than the price where it wants to sell it elsewhere. If this is done by signing two transactions and the buy succeeds but the sell fails then the arbitrageur will end up with an asset they don't want and a likely loss on the trade.

A contract on the chain can perform both trades from a single input. That is, the arbitrageur can sign a single transaction with instructions to the contract that will handle both trades. That transaction is atomic - it succeeds completely or fails entirely.

Hope it helps.

2

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.