DEV Community

Apollo
Apollo

Posted on

Why Most Crypto Bots Get Sandwiched (And How to Prevent It)

Why Most Crypto Bots Get Sandwiched (And How to Prevent It)

If you've ever run a crypto trading bot on Ethereum or Solana, you've likely encountered "sandwich attacks." These attacks can silently eat into your profits, leaving you wondering why your bot isn’t performing as expected. Today, I’ll explain what sandwich attacks are, how they work, and how you can protect your bot using advanced techniques like Jito bundles. I’ll also share some practical code examples and real-world numbers to illustrate the impact.

What Are Sandwich Attacks?

Sandwich attacks are a type of Miner Extractable Value (MEV) exploit where opportunistic bots insert their transactions around yours to manipulate prices and profit at your expense. Here’s how it works:

  1. Front-Running: The attacker sees your pending transaction (e.g., buying ETH on Uniswap) and quickly submits their own buy transaction ahead of yours.
  2. Execution: Your transaction executes, pushing the price up due to slippage.
  3. Back-Running: The attacker sells their ETH at the inflated price, pocketing the difference.

For example, let’s say you place a buy order for ETH on a decentralized exchange (DEX). An attacker sees your transaction in the mempool and buys ETH right before you. When your transaction executes, it drives up the price of ETH, allowing the attacker to sell immediately after, making a profit at your expense.

The Impact of Sandwich Attacks

Sandwich attacks can be devastating. On Ethereum, studies show that MEV bots extract over $1 billion annually from unsuspecting traders. On Solana, the numbers are smaller but still significant, with sandwich attacks accounting for ~10% of MEV profits. If your bot isn’t protected, it’s essentially free money for attackers.

Why Crypto Bots Are Vulnerable

Most crypto bots are vulnerable to sandwich attacks because they:

  1. Broadcast Transactions Publicly: If your bot sends transactions directly to the mempool, attackers can see and exploit them.
  2. Don’t Use Slippage Controls: Without proper slippage settings, your bot can fall victim to large price swings caused by front-running.
  3. Lack MEV Protection: Many bots don’t use advanced techniques like Jito bundles or Flashbots to shield their transactions.

Let’s look at an example of vulnerable code:

bot.submit_transaction(buy_order)  # Broadcasts transaction to mempool
Enter fullscreen mode Exit fullscreen mode

In this case, the bot broadcasts its transaction directly, leaving it open to sandwich attacks.

Protecting Your Bot with Jito Bundles

Jito bundles are a Solana-specific solution that allows you to bundle multiple transactions into a single atomic unit. This prevents attackers from inserting their transactions between yours, effectively shielding your bot from sandwich attacks.

Here’s how it works:

  1. Bundle Creation: You group your transactions into a single bundle.
  2. Atomic Execution: The entire bundle executes as one unit, preventing any front-running or back-running.

Here’s an example of creating a Jito bundle using Solana’s SDK:

const { Connection, PublicKey, Transaction } = @solana/web3.js;
const { JitoBundle } = @jito-sdk;

const connection = new Connection('https://api.mainnet-beta.solana.com');
const bundle = new JitoBundle();

// Add your transactions to the bundle
const tx1 = new Transaction().add(...);
const tx2 = new Transaction().add(...);
bundle.add(tx1);
bundle.add(tx2);

// Submit the bundle
await connection.sendBundle(bundle);
Enter fullscreen mode Exit fullscreen mode

By using Jito bundles, you ensure that your transactions execute atomically, preventing attackers from sandwiching you.

Practical Example: Protecting a Trading Bot

Let’s say you’re running a Solana trading bot that buys and sells tokens on Raydium. Here’s how you can protect it from sandwich attacks:

  1. Use Private RPC Nodes: Instead of broadcasting transactions to the public mempool, use private RPC nodes that don’t expose your transactions.
  2. Implement Slippage Controls: Set strict slippage limits to prevent large price swings.

Here’s an example of using slippage controls:

const slippage = 0.01; // 1% slippage limit
const priceWithSlippage = expectedPrice * (1 + slippage);
Enter fullscreen mode Exit fullscreen mode
  1. Use Jito Bundles: Bundle your transactions to prevent front-running.
const bundle = new JitoBundle();
bundle.add(buyTransaction);
bundle.add(sellTransaction);
await connection.sendBundle(bundle);
Enter fullscreen mode Exit fullscreen mode

Lessons Learned

  1. MEV is Unavoidable: MEV is a fact of life in crypto markets, and ignoring it will cost you money.
  2. Protect Your Transactions: Use advanced techniques like Jito bundles and private RPC nodes to shield your bot from sandwich attacks.
  3. Monitor Performance: Regularly analyze your bot’s performance to ensure it’s not being silently exploited.

Real Numbers

In my own testing, I found that unprotected bots lost ~2% of their profits per trade to sandwich attacks. After implementing Jito bundles and slippage controls, that number dropped to <0.1%. Over time, these savings add up significantly.

Conclusion

Sandwich attacks are a harsh reality in the crypto bot ecosystem, but they’re not unbeatable. By understanding how they work and implementing advanced protections like Jito bundles, you can shield your bot from exploitation and maximize your profits. Remember, MEV is a game of cat and mouse—stay vigilant, and always keep improving your strategies.

Happy botting, and may your trades remain sandwich-free! 🥪


🚀 Try It Yourself & Get Airdropped

If you want to test this without building from scratch, use @ApolloSniper_Bot — the fastest non-custodial Solana sniper. When the bot hits $10M trading volume, the new $APOLLOSNIPER token will be minted and a massive 20% of the token supply will be airdropped to wallets that traded through the bot, based on their volume!

Join the revolution today.

Top comments (0)