Front-running bots, sandwich bots and the “mempool”

The title of this positing includes terms associated with blockchain and crypto, and many of you will have heard of them, but do you really know what they are, how they work, and the impact they can have on crypto trading?

Front-running is a bit like having “insider information” on a large trade that’s about to occur, which will drive the price of an asset up or down. Imagine knowing that next week, a huge company is going to invest in a smaller company on the stock market. The price of shares in the smaller company is currently tiny (let’s say $0.01 per share), but you know that as soon as the bigger company comes along and invests $500m in this company, it’s going to make the smaller share company soar to $2.00 per share. If you had your “unethical hat” on, then you could see that potentially buying some shares in that smaller company before next week, could provide an opportunity to make some profit, as you can purchase them now at $0.01 and sell them next week at $2.00

If you apply this example to the world of crypto trading, this is known as front-running, however the actual “insider information” is available for all the world to see…. you just need to be super, super quick to take advantage of it.

You see, when someone goes to a DEX to make a buy or sell order, no matter how big or small, the trade itself isn’t executed instantaneously. The speed of execution depends on the blockchain in question, but if we focus on the Ethereum Blockchain for the rest of this posting as an example, you’ll know (if you’ve ever traded with Ethereum) that there’s a delay between actually placing a trade, and the trade itself ultimately being “executed” and appearing on the blockchain.

Each Ethereum “block”, which is where all transactions have been added, takes around 12 seconds to be added to the chain. It doesn’t seem like a long time, however 12 seconds for a programmable script or “Bot” is a very long time to get things done!

So let’s explore what happens during that potentially 12-second period…

You place a trade, and the trade goes into the “mempool” — At a high-level, a mempool is basically a waiting room for pending transactions. They get sent to a node on the blockchain, and wait around before they are eventually processed and added to the blockchain.

In fact, there’s no guarantee that your transaction will even get processed in the current block. When the network is busy, you may even have to wait until the next block is processed, or even the block after that! In some cases, you might even be waiting over a minute for your transaction to get processed!

The interesting thing about mempools is that they are visible for all the world to see. Everything on a blockchain is public. Every transaction can be viewed. You can see the senders wallet address, the receiving wallet (or smart contract) address, the amount sent, etc.

So if you were a super-rich crypto “whale” trader, and wanted to buy $500m worth of Ethereum, then that pending trade will first go to the PUBLIC mempool, and wait around for a little while before it’s processed.

The other interesting thing about mempool is that they DO NOT operate on a “First-In-First-Out” (FIFO) basis.

If Bob and Mary both send a transaction Chris in the same block, and Bob sends his transaction 1 second before Mary, it doesn’t necessarily mean that Bob’s transaction will arrive at Chris’s wallet before Mary’s — How can this be? One of the critical factors is how much gas both Bob and Mary paid for their transaction.

You see, if Mary paid a significantly higher gas fee than Bob, then it’s highly likely that Mary’s transaction will arrive in Chris’s wallet before Bobs transaction.

When you use Metamask to make a transaction, you have the option to accept the default “Market Rate” gas fee, or instead edit the amount of gas you’d like to pay. You can see from the image below the estimated times for processing your transaction based on network conditions and current gas rates:

You don’t even have to accept one of the three default gas values…. you could reduce the amount of gas you want to pay to an amount even lower than the “low” value, although the risk of doing this is that the transaction takes a VERY long time to process, or potentially never get processed!

At the same time, you can pay a higher gas fee than “Aggressive” if you really want your transaction to get processed as soon as possible.

For all the transactions currently sat in the mempool, the transactions willing to pay the highest gas fee are the ones most likely to get processed first.

So going back to the example of Bob, Mary and Chris, you can see that if Mary paid a significantly higher amount of gas for her transaction than Bob did, her transaction is likely to reach Chris’s wallet before Bob’s transaction, even though Bob sent his transaction 1 second before Mary.

Front-Running Bots

With that information in mind, what if you knew the following information:

  1. A HUGE buy/sell transaction is sitting in the PUBLIC mempool, which will inevitably move the price of the asset once the transaction is processed
  2. This particular transaction was made using an average gas price, and it’s expected to take around 10 seconds to process

So what if you could create a “Bot” script that sits around all day, scanning the mempool, looking for huge (pending) transactions that will shift asset market prices, and when the “Bot” sees one of these transactions, it makes an instant transaction to buy the same asset, but pays a far higher gas fee?

You can probably see what will happen here! The “Bot” will receive the asset at the current price (probably still a low price) FIRST and then the huge transaction will get processed SECOND. By the time the Bot has received it’s asset at the low price, the new asset price (formed by the HUGE transaction) has now inflated the price, in which case the owner of the Bot can make a profit by selling the asset back to the market at the new higher price!

The smaller the pool, the bigger the impact

Up to this point I’ve described how you could use a bot for “front-running” a huge transaction that will move asset price, but it’s worth noting that this is based upon moving the price of an asset that is part of a huge liquidity pool.

For example, as of the time of writing this post, the USDC/ETH liquidity pool on Uniswap v3 sits at $221.94m

If you wanted to impact the asset price by just 0.1%, you have to spend $1.5m USDC!

But this is a huge liquidity pool, so it would require huge amounts to move the price. But what about smaller liquidity pools?

Imagine you were following a smaller crypto project that has been making steady progress, and starting to gain some attention. The current liquidity pool is small when compared to the USDC/ETH pool, and average daily transactions reflect this…. but then one morning a front-running bot spots a large transaction on this smaller project pair that will move price.

Here is an example of a fun new meme project on Ethereum called “Mouseworm”

Mouseworm logo

For this liquidity pair, you could impact the price of Mouseworm by over 20% by investing just $25,000 in the project.

So before this example trade is placed, 1 Mouseworm would cost $2.24 USDT, but after this $25,000 trade is placed, the price of 1 Mouseworm would be somewhere around $2.68. That’s a significant increase. So if you programmed a “Front-running” bot to place a trade (using more gas) than this $25,000 trade, then you could get the token at a lower rate than $2.68, and know that immediately after the bot makes the purchase, the rate will jump up considerably, potentially providing a sell opportunity at a profit.

This is why it’s not always necessarily a good thing to try and save on gas fees when trading on assets. When you next go to place a crypto trade on your phone, you might think that you’re not in any mad rush for your trade to go through, so you may as well just save some gas fees…..but there might be a bot watching what you’re doing, and preparing to front-run you!

No rush…. save some gas and just chill!

Sandwich bots

Sandwich bots use similar techniques, only they normally consist of a pair of trades (both a buy AND sell) which are carefully constructed and timed to form what is known as a “Sandwich Attack”.

not this kind of sandwich attack!

The sandwich attack involves placing two trades on a DEX, with the intention of profiting from the price movement that occurs BETWEEN the two trades. This process is a combination of “front-running” and “back-running”.

The first trade allows the bot to get the asset at a low price, as the real trade (executed after the bot’s trade due to not paying as much gas) will cause the price to go up.

The second trade will then execute immediately after the real trade to instantly sell the asset, before anyone even noticed. The attacker got in, made a profit, then sold the asset back to the market, all in the blink of an eye!

Whilst this would be practically impossibly for a human to do, sandwich bots on the other hand automate this process by continuously monitoring the order book of a DEX and placing the necessary trades at the appropriate times. They can be used to repeatedly profit from small price movements, and can be particularly effective when used on illiquid markets with low trading volumes.

What can be done?

There are some obvious ways to avoid front-running and sandwich bot attacks.

  1. The first is to trade within a large (or aggregated) liquidity pool. The likelihood of your trade moving market prices is miniscule, so the opportunity for a bot to profit from that trade (particularly when DEX fees are factored in) is negligible or doesn’t exist.
  2. Keep slippage low! If you set your slippage too high, then you are begging to be front-run! The slippage amount accounts for the likelihood of your trade succeeding or failing, and many crypto traders tend to set slippage too high as they are desperate to get hold of some tokens as the price is climbing fast and they don’t want to miss out. This could prove costly, and bots WILL take advantage of this!
  3. Pay Higher gas fees. If you want your transaction to get processed quicker than someone else, you’ll generally have to pay more gas than the competition. It’s usually better to pay more gas and keep slippage low, rather than the other way round.
  4. Make multiple low value orders rather than one big order. Bot developers are smart and they’ve already factored in gas fees, slippage, asset price etc., so there comes a point when a bot simply won’t try to execute a front-running trade, because it won’t be profitable when fees have been accounted for. In contrast, if you’re making a big trade on a smaller liquidity pool, the changes of the bot making a decent profit increase substantially.
  5. Use a reputable DEX — Most of the leading DEX exchanges include features such as quick matching, randomized transaction processing, trade match engines and periodic auction matching to minimize the odds of front-running.

Instadapp Turns on DeFi Automation, Powered by Gelato Network

Auto-refinance your debt position between Maker, Aave & Compound to avoid getting liquidated

Instadapp and Gelato are joining forces to release Instadapp Actions, a new service that is being rolled out today to Instadapp’s nearly 25,000 users and counting. As the first use case, users will be able to enjoy automated debt refinancing between various DeFi lending protocols to prevent costly collateral liquidations.

As one of the world’s most advanced decentralized crypto assets management platforms, Instadapp aims to make complex cross-protocol transactions simple and seamless. Up until today, Instadapp has managed more than $500 million crypto assets across leading protocols like MakerDAO, Aave, Compound, and Uniswap.

We all know that the crypto world is 24/7, but with the help of Instadapp and Gelato, this brand new service allows you to automate an important aspect of your crypto investing strategy. Using Instadapp Actions, you will finally be able to sleep peacefully at night again.

Why Instadapp Actions Is the Service That We All Have Been Waiting For?

So you finally figured out how the flash loan works, took out a DAI loan on MakerDAO using your ETH as collateral, and spent the DAI you just got on buying even more ETH. You thought ETH would go straight to the moon, and you were just about to order your Lambo from the car dealership. But in the middle of the night, the ETH price started crashing, and your debt position got undercollateralized.

As much as we want to become savvy investors, we physically can’t spend day and night optimizing our interest payments while keeping our collateral secure from liquidations by moving it from one protocol to another to avoid a hefty liquidation penalty. That’s where Instadapp Actions come in, which automatically refinances our debt and helps our portfolio reduce its liquidation price, so we don’t have to worry about the last-minute market volatility ever again.

What Does It Really Mean to the DeFi User — You?

As a DeFi user, you will benefit from paying the lowest possible interest rates on your DAI debt on MakerDAO while at the same time being able to capitalize on the significantly lower collateral requirements on protocols such as Aave and Compound.

“I believe refinancing automation will be one of the most used features to secure ETH-A vaults as it saves users from liquidation without selling any Ethereum. Automating Instadapp’s DeFi smart accounts with Gelato will enable many more equally powerful use cases in the future.” — Samyak Jain, Co-founder and CTO of Instadapp

Gelato Network — a decentralized network of bots, will refinance your position before it can get liquidated on one of the protocols to help you avoid having to pay the liquidation penalty.

Gelato and Instadapp will do the heavy lifting for you, monitoring prices and automatically reacting to changes in the ETH price. We know you have a busy life, and we want to make sure that your portfolio stays on track, so you can focus on whatever is more important to you or just relax.

The assets stored in your Instadapp DeFi Smart Account will be moving fully autonomous between the most liquid lending protocols, such as MakerDAO, Aave, and Compound, based on where the most favorable liquidation prices are for your debt position.

“Decentralized automation is one of the missing pieces of DeFi and blockchain today, using Gelato, projects like Instadapp can finally do it in an easy, reliable, and trustless way.” — Matthieu Marie Joseph, Legendary Member at Gelato Network

Ready to refinance your auto loan? Go to http://defi.instadapp.io/makerdao and try Instadapp Actions now.

About Instadapp

Instadapp is a DeFi portal that aggregates the major DeFi protocols using a smart wallet layer and bridge contracts to make complex cross-protocol transactions simple and seamless. Up until today, Instadapp has served and managed over $500 million in crypto assets across leading DeFi protocols like Maker, Aave, Compound, and Uniswap.

About Gelato Network

Gelato Network is Web3’s premier automation network, enabling developers to automate a wide variety of arbitrary smart contract executions on and across all EVM-based compatible blockchains such as Ethereum. Examples of use cases developers have built on top of Gelato include Limit Orders on AMMs like Uniswap, automatic compounding of yield farming vaults, Aave liquidation protection, MakerDAO debt ceiling updates, automated liquidity management, and even the petting of Aavegotchis.

Our ultimate goal is to automate everything and by giving developers the reliable tools they need to do this, we can empower their users to get the most out of their Web3 experience.