Sandwich attacks: How to avoid getting eaten

It feels like only yesterday that DOGE, SHIB, ELON, FLOKI and other tokens like PEPE were nothing more than a joke. Today, that very same DOGE is being hyped by Elon Musk, the coin’s market capitalization is over USD 10 billion, and its creator has gone on record to say that 95% of crypto projects are “scams and garbage”. 

Low-liquidity crypto assets have suddenly become very popular. Too popular, some might say. Many people have eagerly bought up these “fun” coins but, if you decide to invest a large amount in this kind of asset after the next tweet from Musk, be careful not to spend more than you planned. There’s a chance that the coin’s price will jump at the exact moment you buy it. Why? Sandwich attacks.

The basic idea

When people try to beat the crowds and purchase crypto before its price goes up (after the latest celebrity endorsement on Twitter, for example), they often neglect “slippage”. This is the difference between the price a trader expects to pay and the actual value of an asset, which may change due to market fluctuations at the time the trade is executed.

Let’s take an example. You want to buy something for USD 100 and place a buy order on an exchange. In the period between placing and executing the order, however, the market moves and the value of the asset rises (or falls) by 1%. Depending on which way the market fluctuates, you will end up paying USD 1 more or less.

If you really need to buy this “thing”, and you are willing to lose a little in the hopes that the future growth in the asset’s value will compensate for these losses, this approach is justified. If the price was set rigidly, and the value of the coin increased slightly before the order was executed, you wouldn’t have received the asset you were trying to buy.

So what about sandwiches?

What causes the market to spike? Let's look at another example. Peter, a trader, decides to buy (or exchange) a large number of coins on a DEX (decentralized exchange), hoping that the price will soon creep up (perhaps because Elon Musk wrote about it again). He is certain of the need to buy at all costs, and thoughts of future profits drown out the voice of reason. Peter sets the slippage tolerance at 50% of the price, hoping that the market will not grow so much in such a short space of time. It usually doesn’t, but this time is different.

Another trader, Paul, is playing against Peter. He has a bot that checks unconfirmed transactions in blocks in the mempool and looks for high-volume purchases of volatile crypto assets (those very same meme coins) with a high slippage tolerance. When the bot detects a large transaction that will lead to an increase in the price of an asset, it places an order, offering a commission high enough to ensure that the transaction will be processed BEFORE Peter’s.

The following chain of events occurs as a result. Peter wanted to buy an asset at a rate of USD 1, but Paul’s bot placed an order that beat him to the transaction. The market rises sharply and, because of Peter’s high slippage range, the order is executed at a rate of USD 1.3. Immediately after that, the bot sells the asset it just purchased for USD 1 at the new price of USD 1.3. The horse has bolted.

Is this even legal?

If we were talking about standard securities, the answer would be “no”. This approach would be understood as involving the use of insider information, which is almost universally prohibited by law. With DeFi, however, things are a little different. The main principle of cryptocurrencies is transparency, so it’s hard to blame trader Paul for profiting from data that’s available to everybody. Legal? Yes. Ethical? Not particularly.

How common is it?

More common than it should be. Here’s a recent example. A trader on Uniswap using the jaredfromsubway.eth Ethereum wallet earned over USD 4 million on “sandwiches” in a single day, spending over one million dollars on network commissions over the same period.

Since this activity falls within a legislative grey area, exchanges aren’t in any hurry to introduce mechanisms to protect people from sandwich attacks (let’s not forget their profits here). Despite this, some progress is being made. 1inch, which supports the exchange of cryptocurrencies in Tangem Wallet, has launched the RabbitHole secure swaps feature. RabbitHole allows users to send high-risk transactions directly to validators, bypassing the mempool and eliminating the possibility of being attacked by sandwich bots.

Are there ways of protecting yourself?

Yes. If you’re going to invest in low-liquidity crypto assets, watch out for slippage, and if you want to buy a lot, make the effort to break up a large transaction into several smaller ones. True, it’s not as profitable, but it’s much safer.