What is a front-running bot in crypto?
A front-running bot (sandwich attack bot) is a fully automated program used in the cryptocurrency market to make a profit by trading based on pending transactions in the mempool. The front-running bot is one of the MEV (Miner Extractable Value) bot strategies called sandwich attacks.
How they work: When a user submits a buy transaction to a blockchain network like Ethereum, it enters a waiting area called the mempool, waiting to be picked by validators. Front-running bots monitor this mempool for large transactions with specific criteria (e.g., slippage). Once a transaction meets the bot’s logic/conditions, the bot quickly submits its own buy transaction, known as a front-run transaction, with a higher gas fee than the targeted transaction, ensuring that their transaction is processed first.
After the front-run transaction and the targeted transaction are confirmed on the blockchain, a second sell transaction, known as a back-run, is executed to extract the profit caused by the targeted transaction. The combination of front-run and back-run transactions is called a sandwich attack, commonly referred to in the crypto community as front-running.
Components of a Front Running Bot
Off-Chain Bot (Oracle):
- Function: Monitors the blockchain for pending transactions, calculates potential profit, and estimates price impact.
- Description: The off-chain bot is a piece of code that continuously scans the mempool (the pool of pending transactions) to identify large transactions that can move the market. It evaluates the profitability of executing a sandwich attack by considering factors such as gas fees and slippage. and other rules and logics to refine the bot’s decision-making process.
On-Chain Bot (Solidity MEV Contract):
- Function: Executes the sandwich attack transactions on the blockchain.
- Description: The on-chain bot, also known as the Solidity MEV contract, is a smart contract written in Solidity (the programming language for Ethereum). This contract is responsible for placing the necessary buy and sell transactions.
How a Sandwich Bot Works
Monitoring Transactions: The off-chain bot (oracle) continuously monitors the mempool for large pending transactions that are likely to impact the market price.
Analyzing Opportunities: The bot calculates the potential profit from a sandwich attack by estimating the price impact of the target transaction. It considers gas fees and other factors to ensure the attack will be profitable.
Crafting Transactions: If a profitable opportunity is identified, the off-chain bot constructs two transactions:
- A buy order to be placed before the target transaction.
- A sell order to be placed after the target transaction.
Submitting Transactions: The on-chain bot (Solidity MEV contract) submits these transactions to the blockchain with optimized gas fees to ensure they are mined in the correct order. The buy order is placed first to benefit from the price increase caused by the target transaction, and the sell order is placed immediately after to sell at the higher price.
Execution and Profit Collection: The on-chain bot monitors the execution of the transactions. Once confirmed, the bot collects the profit from the successful sandwich attack and transfers it to a designated wallet.
Example:
- The bot monitors the mempool for pending transactions.
- The bot detects a 2 ETH buy transaction to buy 1000 Memetoken, which will increase the value of the Memetoken by 10%.
- The bot calculates the right amount of tokens to buy so it will not significantly increase the token price, which could cause the targeted transaction to fail.
- The bot submits a front-run transaction to buy 500 Memetoken for 1 ETH with high gas fees before the targeted transaction ( Front-Run ).
- The bot waits for both transactions to be confirmed.
- The bot executes a sell order of 500 Memetoken for 1.1 ETH ( Back-Run ).
- The bot makes a 0.1 ETH profit.
Repeating this process 24/7 can yield substantial profits without the risk of losing money, unlike classic trading.