What are Sandwich Attacks in DeFi? How to Avoid them?

The decentralized finance landscape in the cryptocurrency universe offers multiple opportunities to the crypto holders, but it is widely prone to various kinds of attacks or cyber strikes.

The exploitation of smart contracts is a constant concern for investors as criminals take advantage of vulnerabilities in DeFi code systems, which leads to flash loan attacks, rug pulls, and more recently, the bitter ones- Sandwich Attacks.

One must not have heard about the Sandwich Attacks much as they are the new fad in the crypto town and haven’t gained much popularity as a cyber threat.

However, Sandwich Attacks can cause pretty problematic situations in decentralized finance (DeFi) enough. Even the forefather of Ethereum Vitalik Buterin warned about them. The co-founder of Ethereum warned about them way back in 2018.

When it comes to the many attack vectors in technology, it is crucial to understand the basic concept of an attack and the potential ramifications. A sandwich attack, mainly targeted at DeFi protocols and platforms, can have significant consequences and result in market manipulation.

The Sandwich Attack Concept

At the core, a sandwich attack is a form of front-running, which primarily targets decentralized finance protocols and services. In these attacks, the nefarious traders look for a pending transaction on the network of their choice. For example, Ethereum.

The sandwiching occurs by placing one order right before the trade and one right after it. In essence, the attacker will front-run and back-run simultaneously, with the original pending transaction sandwiched in between.

The purpose of placing these two orders simultaneously and surrounding pending transactions is to manipulate asset prices. First, the culprit will buy the asset the user is swapping to, for instance, using Chainlink (LINK) to exchange to Ethereum (ETH), with their knowledge of ETH’s price increasing. Then the culprit will buy Ethereum for a lower price in order to let the victim buy at a higher value. The attacker will then sell ETH at a higher price afterwards.

The transaction will have sandwiching affects the amount of Ethereum the initial user will receive. As the culprit succeeded in filling the order at their desired price, the next trade will be at a higher cost. This sequence causes the Ethereum price to increase, allowing the culprit to pocket a profit by front and back running a trader and creating an artificial price rise.

Various Factors to Consider in Sandwich Attacks

The sandwich attack method makes this type of attack sound straightforward. In reality, it is perhaps too easy to perform this type of attack. Even if the profit is small, one can use this method repeatedly without any repercussions.

However, a malicious trader needs to be well-prepared to pull off a sandwich attack. There are many intricacies in decentralized finance to consider which may impact the chance of success.

The majority of sandwich attacks will be performed through automated market maker solutions (AMMs). Some key examples include Uniswap, PancakeSwap, Sushi and more.

Through their pricing algorithms, liquidity is always in high demand, and trades execute continuously. But you can not forget about the price slippage aspect, which occurs when the volume and liquidity of an asset change.

Traders also face an expected execution price, an actual execution price and an unexpected slippage rate. Blockchain transactions can take some time to execute and the inter-exchange rates of assets can fluctuate wildly, resulting in more unexpected price slippage.

A Sandwich Attack: A sandwich attack in two ways. Let us have a look at the two Possible Scenarios

Liquidity Taker vs Taker

It is not out of the ordinary to see different liquidity takers attack one another.

For example, if a regular market taker has a pending AMM transaction on the blockchain, the culprit can emit subsequent transactions which are front-running and back-running, for financial gain. As the liquidity pool and asset pair have three pending transactions, miners will decide which is approved first.

If the culprit pays a higher transaction cost than the other individual, there is a bigger chance for the malicious transaction to be picked up first. It is not a guaranteed outcome, but merely an illustration of how easy it can be to attempt a sandwich attack.

Liquidity Provider vs Taker

A liquidity provider can attack a liquidity taker in a very similar manner. The initial setup remains identical, although the bad actor will need to perform three actions this time.

Firstly, they remove liquidity – as a front-running method – to increase the slippage of the victim. Secondly, they re-add liquidity by back-running to restore the initial pool balance. And Finally, they swap asset Y for X to restore the asset balance of X to how it was before the attack.

Withdrawing someone’s liquidity before the victim’s transaction goes through negates the commission fee for the particular transaction. Usually, liquidity providers earn a small fee for the activity occurring in their pool of choice. This ensures that commission doesn’t get granted and can cause financial harm to any taker, at the cost of commission.

Are Sandwich Attacks worth the efforts?

Despite the clear cut financial incentive, doing so much in the sandwich attack, may not always be worth a shot. The cost of performing these transactions to front and back run other traders will often outweigh the financial gain for attackers.

This is more ambiguous and wasteful, especially when used on the Ethereum network as it notes transaction costs (per action) rather regularly.

However, a sandwich attack can still be a profitable attack for the cyber invaders if the commission earned from ‘normal behaviour’ and the transaction cost for a sandwich attack is lower than the victim’s trade amount.

In other words, the efforts in the sandwich attack are much higher than the incentive. However, the reward might be low, but the threat of attacks still looms around.

The advent of decentralized trading through automated market makers continues to pose a serious risk to the users availing these services. A sandwich attack can occur at any moment.

As DeFi attracts more and more people of late, there will be more opportunities for culprits to strike and reap higher profits through the sandwich attacks.

Though this does not mean there will be more successful sandwich attacks in the coming future, but it is something to keep in mind for the crypto buyers.

How to Protect Against Sandwich Attacks

For automated market makers (AMMs), it is crucial to develop countermeasures capable of protecting users from sandwich attacks.

For example, the 1inch platform introduced a new order type known as ‘flashbot transactions’ which are not visible in the mempool as they are never broadcasted to it. Instead, the 1inch platform has a direct connection to trustworthy miners to make transactions visible after they are mined.

Till today, it appears to be the only way for users to protect themselves against a sandwich attack. However, it remains unclear if other AMMs will forge partnerships with miners to include transactions without broadcasting them to the mempool. Other solutions may be found in the future, although that will likely take some time.

 

Disclaimer: Angel One Limited does not endorse investment and trade in cryptocurrencies. This article is only for education and information purposes. Discuss with your investment advisor before making such risky calls.