Blog

A Beginner’s Guide to Sandwich Attacks in DeFi

A sandwich attack is a sophisticated form of front-running in the cryptocurrency market, primarily targeting decentralized exchanges (DEXs). This type of attack exploits the time delay between a user's transaction being broadcasted and confirmed on the blockchain.

In a sandwich attack, the malicious actor places two transactions: one just before and one just after the victim's transaction. By manipulating the market price, the attacker profits at the expense of the unsuspecting user.

Unlike smart contract vulnerabilities, sandwich attacks leverage the mechanics of transaction ordering and market liquidity, making them a unique threat in the realm of crypto trading.


How Does a Sandwich Attack Work?

A sandwich attack begins when a trader initiates a transaction to swap Ether (ETH) for a popular ERC-20 token, such as Chainlink (LINK) or any other token pairs. Once the transaction is broadcasted to the blockchain, it is temporarily held in the mempool, waiting to be confirmed in the next block. This period creates a window of opportunity for malicious actors.

A trading bot monitors the mempool for large transactions. Upon detecting a substantial trade, the bot executes a front-run transaction by buying LINK using the same liquidity pool before the original transaction is confirmed. This initial purchase causes the price of LINK to increase due to the price impact and slippage mechanisms within the AMM. As a result, when the original transaction is processed, the trader buys LINK at the inflated price, unwittingly paying more than expected.

After the trader's transaction is confirmed, the bot completes the sandwich attack by executing a back-run transaction. The bot sells LINK at a new and higher price, profiting from the price difference created by its front-running activity. The trader, meanwhile, suffers a loss due to the increased purchase price and the manipulation of the transaction sequence.


How Flashbot Transactions Protect Against Sandwich Attacks

In response to the growing threat of sandwich attacks, some Automated Market Makers (AMMs) and decentralized exchanges have implemented protective measures. One such measure is the use of “flashbot transactions” order type.

Flashbot transactions are designed to enhance the privacy and security of trades by keeping them hidden from potential attackers until they are fully processed.

When a trader uses a flashbot transaction, the transaction is sent directly to a network of miners using a private relay, bypassing the public transaction mempool. In the public mempool, transactions are visible to all network participants, including malicious actors and bots. By avoiding the mempool, the transaction remains hidden from potential attackers.

The flashbot transaction remains concealed until it is included in a block and confirmed on the blockchain. Only after the transaction is mined and added to the blockchain does it become visible to the public. This prevents attackers from seeing and reacting to the transaction before it is finalized.

Decentralized exchange (DEX) aggregators are a powerful defense against sandwich attacks as they spread a single trade across multiple liquidity pools. This strategy significantly reduces the price impact and slippage for each individual transaction, making it much harder for attackers to manipulate prices. With trades fragmented and dispersed, attackers face the daunting task of coordinating an attack on multiple fronts, drastically lowering their chances of success. The reduced price manipulation opportunities also mean that the potential profits for attackers shrink, making sandwich attacks far less appealing and keeping your trades secure and efficient.

Finally, to avoid this kind of attack, avoid executing high-value transactions during peak hours, especially when market volatility is high. Always use slippage detection and protection tools to safeguard your trades, but be aware that even if a transaction fails due to slippage protection, you'll still need to pay the gas fees. By taking these precautions, you can significantly reduce the risk of falling victim to sandwich attacks and other market manipulations.