What Are Sandwich Attacks in DeFi?

Sandwich Attacks in DeFi

Decentralized finance DeFi has opened up new opportunities for financial services without traditional gatekeepers like banks, but there are some risks, such as sandwich attacks in DeFi.

As the sector grows in popularity, so do the risks of manipulation and bad actors looking to profit from unsuspecting users. There are risks, such as reentrancy attacks in smart contracts and sandwick attacks in DeFi.

Sandwich attacks, also known as front-running attacks, are directed mainly at decentralized exchanges (DEXs) that facilitate direct token exchange and involve the placement of two transactions before and after a victim's.

First, they keep an eye on upcoming transactions and then take advantage of the biggest or most advantageous ones.

An attacker may prevent a victim's transaction by paying a higher gas fee to acquire the identical token the victim was planning to buy. Slippage, the gap between predicted and actual transaction pricing, and increased demand are the primary causes of this higher transaction fee.

Join us in this blog as we explore what sandwich attacks are, how they work, and, most importantly, what you can do to protect yourself in DeFi. We will also explain DEXs and slippage in crypto for you to have a more comprehensive understanding of these attacks.

What are DEXs?

Before diving into sandwich attacks, it's important to understand the context in which they occur. Decentralized exchanges, also known as DEXs, allow crypto traders to swap tokens directly from their crypto wallets without an intermediary.

As the name suggests, DEXs operate on public blockchains like Ethereum in a decentralized manner using smart contract and smart contract 2.0 technologies. The most popular types of DEX are those that utilize automated market makers (AMMs) to provide liquidity and determine prices.

AMMs maintain liquidity pools containing various token pairs and use a formula to automatically set cryptocurrency exchange rates based on the relative proportions of assets in the pool. This provides a convenient way for traders to swap tokens at any time without relying on an order book.

However, as we'll see, the transparency of DEXs and predictability of AMM pricing leave the door open for manipulative practices like sandwich attacks.

What is a slippage in crypto?

Another key concept when discussing DEX vulnerabilities is slippage in crypto. Slippage refers to the difference between the expected price of an asset at the time an order is placed and the price at which the order is ultimately executed.

Slippage occurs due to volatility in decentralized markets; as orders are pending confirmation on the blockchain, market prices can fluctuate.

Most DEX users set a maximum slippage tolerance to prevent their order from going through at a vastly different price than intended. However, slippage becomes an opportunity for attackers in a sandwich attack scenario.

sandwich attack

How do sandwich attacks work?

A sandwich attack involves a bad actor deliberately manipulating the price of assets around a trader's pending DEX transaction to profit at their expense. Here's a typical scenario:

  • The attacker monitors pending transactions in the mempool, looking for large or profitable trades.
  • They find a trade where a user intends to swap one token for another on a DEX.
  • Using a bot, the attacker submits two transactions: one to purchase the incoming token before the victim's trade goes through, and another to sell the token after.
  • Because the attacker preemptively increases demand for the token by "front-running" the purchase, the victim ends up paying a higher price than expected due to slippage when their transaction is confirmed.
  • Then, after the victim's order, the bot "back-runs" with a sell transaction, keeping the excess profits from the inflated price.

In essence, the attacker "sandwiches" their transactions before and after the victim's, manipulating the price up and down to profit off the intended trade. The end result is a loss for the unsuspecting trader and ill-gotten gains for the manipulator exploiting DEX vulnerabilities.

According to a report by Flashbots, a research group focused on miner extractable value (MEV), sandwich attacks accounted for 23% of all MEV extracted on Ethereum in March 2021.

The report also estimated that sandwich attackers extracted $19.7 million worth of MEV in March 2021 alone, up significantly from $3.8 million in February 2021. On average, each sandwich attack in crypto generated a profit of $1,729 for the attacker, while the average loss per victim was $1,383.

What is an example of a sandwich attack in crypto?

To better understand how this plays out, let's consider a hypothetical example on Uniswap:

  • A trader intends to swap 1 ETH for 100 DAI tokens, setting a 1% slippage tolerance.
  • A monitor spots the pending transaction and swoops in to purchase 50 DAI for 0.5 ETH before the trade goes through.
  • Due to a 10% price pump that the frontrunner caused when the victim's transaction was confirmed, 1 ETH now only buys them 90 DAI.
  • After the fact, the bad actor dumps their 50 DAI for 0.55 ETH, pocketing the extra 0.05 ETH profit at the expense of the trader who lost value on slippage.

This artificial inflation and deflation of the DAI price around the target trade allows the attacker to effectively "skim" value from the user's transaction. Rinse and repeat hundreds of times, and it becomes a lucrative manipulation scheme—in other words, a sandwich attack in crypto.

What are the different types of attacks in DeFi?

While sandwich attacks specifically exploit DEX infrastructure, DeFi is vulnerable to various manipulative practices due to weaknesses in design and incentives.

  • Rug Pulls: Contract owners drain liquidity pools and disappear, leaving users with worthless tokens.
  • Flash Loan Attacks: Low or no collateral loans are taken out solely to exploit a flash weakness before repayment.
  • Short & Distort: Bad actors spread FUD about a project they've shorted to crash the price.
  • Under Collateralized Borrowing: loan positions are leveraged to the hilt; collapse occurs when the market turns.
  • Stablecoin Dilution: Stablecoin backing is reportedly fractional instead of fully backed.

Sandwich attacks in DeFi signify the broader challenges of protecting users in a permissionless network. With significant fortunes at stake, bad actors will keep devising ingenious ways to prey on unsuspecting participants if stronger safeguards are not implemented.

defi sandwich attack

What happens in sandwich attacks?

To recap, here's a simplified overview of the sequence of events in a typical sandwich attack on a DEX:

  • The attacker monitors pending swaps in the mempool.
  • A promising trade is identified, such as an ETH-DAI swap.
  • Bot front-runs the trade by rapidly purchasing the incoming token with a higher gas fee.
  • The victim's trade goes through, triggering a price pump from increased buyer demand.
  • Due to slippage, the victim pays an inflated price and receives less of the incoming token.
  • Both back-run by dumping the purchased token, crashing the inflated price.
  • The attacker cashes out tokens sold during the pump for a profit, while the victim suffers losses.
  • The process repeats undetected, accumulating six-figure profits over thousands of manipulations.
  • Victims are any traders whose pending swaps are identified and exploited in the described pump-and-dump manner.

The layer of obfuscation from decentralized networks currently makes sandwich attacks a "low-risk, high-reward" proposition for manipulators to prey upon average DeFi users.

How to avoid sandwich attacks in DeFi?

Given the anonymous nature of public blockchains, it's impossible to completely eliminate sandwich attacks and other manipulative behavior for individual traders. However, Cryptobunq, a crypto-friendly digital bank with expert crypto services, offers some strategies to minimize exposure:

  • Use lower slippage tolerances to avoid overly inflated prices. However, this increases the chance of transaction failures.
  • Set higher gas fees to "jump the queue" ahead of potential front-runners. But gas costs cut into profits.
  • Some platforms, like 1 inch, integrate with Flashbots to bypass the mempool, preventing the front-running of limit orders placed this way.
  • Stay up-to-date on network conditions and avoid volatile times with high volumes that attract manipulation. Traders should carefully consider whether trades are worthwhile given predictable risks.
  • For larger players, our custody and wallet solutions provide advanced market-making tools and crypto trading strategies to safely navigate DeFi on behalf of institutions.
  • Further research is needed to develop robust defenses against these unethical exploits. Stronger price arbitrage defenses, off-chain order books, and disincentives against manipulation could help.

Individual DeFi users should exercise caution, but institutions can benefit from the promise of DeFi through responsible professional intermediation, lowering the risks of loss due to manipulation.

Over time, technical and economic solutions can work to align user protections with permissionless innovation. Therefore, it is best to get help from an expert.

You can trust CBQ in this regard. As a one-stop-shop crypto service provider, CBQ is here to protect you and your business from these types of risks and enable you to have prospective business growth in the blockchain technology ecosystem.

The bottom line: why traders need to rethink "acceptable" price fluctuations

Sandwich attacks represent an emerging threat exploiting the transparency of DEXs and AMM pricing mechanisms to manipulate markets for illicit gain. By frontrunning and back-running trades, attackers effectively "skim" value from normal user transactions through induced slippage.

While impossible to eliminate entirely, traders and institutional players alike can take precautions and leverage services from Cryptobunq to minimize exposure to exploitation in permissionless DeFi environments.

Ultimately, further cooperation between industry and policymakers will be essential to enforcing disincentives against unethical behavior while upholding the decentralized ethos of open financial networks.

Platforms that continue developing proactive defenses and fair fees that align economic incentives will help DeFi reach its full potential as a trustless yet still trustworthy marketplace. If you want to make safe crypto transactions with the power of blockchain, trust CBQ and contact us!

Link Copied!