Decentralized exchanges (DEXs) nip in the bud several issues concerning their centralized counterparts such as concentration of liquidity in the hands of a few players, compromise of funds in case of a security breach, closed control structure and more. One issue, however, that has refused to subside is front-running. Unscrupulous players are still finding ways to defraud unsuspecting traders.

If you have received less than expected when placing a trade on a DEX, there is a pretty good chance of you getting hit by front runners. These bad actors exploit the automated market maker (AMM) model to make profits at the expense of unsuspecting traders.

This article will explain the attack vector and help you understand the basic concept of front-running in crypto trading, the potential consequences and how to prevent crypto front-running.

What is front-running in crypto?

The term “front-running” refers to the process when someone uses technology or market advantage to get prior knowledge of upcoming transactions. This allows the bad actors to take advantage of the forthcoming price movement and make economic gains at the cost of those who had introduced these transactions. Front-running happens via manipulations of gas prices or timestamps, also known as slow matching.

On centralized as well as decentralized exchanges, front-running is a frequent activity. The objective of a front runner is to buy a chunk of tokens at a low price and later sell them at a higher price while simultaneously exiting the position. When executed precisely, it brings in risk-free profits for the traders committing it.

Related: DeFi vs. CeFi: Comparing decentralized to centralized finance

Trading of stocks and assets based on insider knowledge to take advantage of the price movement has been a well-known tactic. Though illegal and unethical, brokers have been engaging in it. The tactic closely resembles insider trading with just a minor difference that the executioner works for the client’s brokerage and not the client’s business.

What is a front-running bot?

Frontrunning is done using crypto front-running bots functioning on a millisecond-scale timeframe. Before a person blinks, they can read a string of transactions, calculate the optimum transaction size and gas price, configure the transactions and run them.

The core of a front-running bot functions by listening for the pending transaction on the blockchain. Interacting with the blockchain using an interactive script, the bot buys before the buyer and sells right after it. The bot analyzes the trends of the crypto and executes transactions to make a profit.

Front-running tactics on decentralized exchanges

When a trade occurs, the system broadcasts it to the blockchain, requiring miners to verify the transaction. However, in any blockchain of significance, the stream of incoming transactions is more than the capacity of the subsequent block. Unmined transactions are left in a pending transaction pool called a mempool.

Blockchain mempools are transparent, a feature that the front runners exploit. Getting insight into the mood of the traders, they are able to predict the upcoming price movements and place their own orders accordingly. They set a higher gas price on their transactions to encourage picking their transactions before the pending ones, thus front-running the unsuspecting traders.

Ways to prevent front-running on the trader side

There is no single solution to solve issues regarding front running on all platforms. Rather, various anti-front-running approaches need to be followed on different projects, depending on the scenarios.

Use large liquidity pools

Front runners are fond of low liquidity pools, as there is less chance of competition as well as disruption of their transaction by a large order that unexpectedly alters the pool weighting. Executing your trades in large liquidity pools makes it less likely to get hit by frontrunning.

Keep maximum slippage low

Does the question “How do I avoid slippage” perennially pricks you? What you can do is set a maximum slippage tolerance in most decentralized exchanges. In other words, you can fix the maximum deviation from the expected return. An example will help you understand the scenario better.

Suppose you place an order on a DEX and expect a return of 500 Tether (USDT) for it. If you set your slippage to 1% of your order, then you won’t be receiving less than 495 USDT. However, if higher slippage tolerance is greater, there will be more deviation.

So, the formula is simple: Keep maximum slippage low, around 0.5%-2%, to fend off front runners. If you are going to place a large order, keep your slippage at the lower keel. Front runners want you to keep slippage high, so better to do just the opposite of it.

Overpay on gas

Front runners are delighted to see slow transactions because it gives them more time to devise an order for riding your trade and making profits. Underpaying on gas makes your transactions queue up for longer, providing front runners more time to formulate their strategy and damage your interests.

Overpaying on gas motivates miners to validate your transaction faster, minimizing your odds of becoming the target of a bad actor. For this purpose, you might set the gas price to higher than average or simply use the fast gas option on your wallet. If you are placing a large value order, it becomes even more important to try and complete the transactions with alacrity.

Place a low-value order

To make a profit, front runners have to meet some minimum thresholds. They have to pay the gas fees twice, on entering and exiting the market, and also recover the amount paid as the trading fee. Their profits begin only after they get back their expenses.

Currently, prominent Ethereum-based automated market makers such as Uniswap, Balancer and SushiSwap pay a gas fee of $25 per transaction. As they need two transactions to execute frontrunning, they are likely to spend $50 to complete the trade. It also means if your trade is raking in less than $50 profit, there are negligible odds of you becoming the target of the frontrunners.

When you enter a low-value trade, you practically make it an unprofitable venture for frontrunners. Most of the time, trading with amounts under $1,000 is safe.

Find a taker

A publicly-hosted order book is the first thing to draw front runners. If you manage to find a taker, you will be able to fill a given order and will be able to keep away from public markets and, subsequently, front-running. When you find a taker and negotiate a price successfully, a trustless on-chain exchange gets executed.

Ways to prevent front-running on DEX side

DEXs can take care of several design points to make front-running difficult to execute:

Quick matching

Front runners look for slow matching speed to place their orders before the execution. A DEX can ensure fast matching to leave little room for front runners to put their process to motion. Super quick block time will effectively make the front runners unable to react. For most frontrunners, who are not so technically inclined, this should be enough.

Decentralized match engine

In the case of a centralized matching engine, it is hard to establish that the exchange itself is not involved in front running. Decentralized matching engines, on the other hand, allow anyone running a full node to view the matching in real time. They can match the transaction in the latest block manually with the current order book.

Periodic auction matching

Periodic auction matching adds an extra layer of security from the front running. It adds a non-deterministic layer making it improbable for anyone to know the next execution price unless they are aware of the matching logic, the incoming orders for the next block and the trade price and the current order book from the last match.

How to avoid front-running in Ethereum 2.0

Some traders wonder about the impact of the full release of Ethereum 2.0 later in 2023, merging the mainnet with Beacon Chain and introducing a proof-of-stake (PoS) consensus mechanism. Validators who are supposed to process transactions in an epoch will be intimated of their positions beforehand.

An epoch is a defined span of time in a blockchain network. This time frame is used to describe when certain events in a blockchain network will take place, such as when incentives will be distributed or when a new group of validators will be allocated to validate transactions.

Related: Ethereum upgrades: A Beginner’s Guide to Eth2

In this scenario, it will be harder for front runners to find profitable trades in a blockchain teeming with transactions, but not impossible. Many frontrunners out there are technically proficient, so you can’t just rule out the possibility. DEXs can cover design points like quick matching, decentralized match engine and periodic auction matching to minimize the odds of front-running.