Frontrunning Attack

Olympix
5 min readDec 2, 2022

--

The idea of frontrunning has been around for a while in the conventional stock market. Front-running is the term used in TradFi to describe the practice of obtaining customer transaction information through brokers before the trades are submitted to the exchange. In actuality, this is how the well-known and costless investment platform Robinhood generates revenue-by offering transaction details to brokers before the execution of its users’ trades. Blockchain is public, so it allows for much greater complexity for frontrunning in the Defi sector.

To understand frontrunning, let’s first look at how payments are managed on blockchains like Ethereum. Sending Eth or other ERC tokens, installing a smart contract, or engaging with one are all seen as transactions since they change the blockchain’s status. The auditors must put these operations on the subsequent block for them to be accepted.

As you initiate payments, they are forwarded to the mem pool, an intermediate resting pool from which the auditors can choose a subset to include on the subsequent block.

The “Maximum Extractable Value,” also referred to as “Miner Extractable Value,” is the maximum amount of money that the validator can earn by selecting which payments from the pool to add to the subsequent block and in what order, in addition to the regular block reward and gas charge.

Mempool is publically accessible. All the outstanding operations in the mempool are visible to anyone who wants to. And over 200,000 open transactions are present in the pool at any given moment. To view the current list of awaiting transactions within mempool,.

Frontrunning Bots

Tons of frontrunning bots examine each transaction that a user makes to the public mempool to determine whether they could gain profit from it.

Frontrunners are bots that keep an eye on the mempool, simulate every transaction, replicate the code of the possibly profitable exchange, swap out addresses for their own, and perform the transaction before the initial one by offering the validator extra. This straightforward instance of frontrunning. Let’s look at the several front-running strategies that can be used when all outstanding transactions are known.

Displacement Attack

An illustration of a Displacement Frontrunning Attack is the straightforward frontrunning attack from the preceding section. In implementing this attack, the completion of the initial transaction made by the real sender is not necessary for the attack to be successful. To clarify, let’s examine a few cases.

Your transaction is sent to register a sought-after ENS domain, but a bot spots it lying in the mempool as well as records the domain in front of you.

The bot notices your answer and transmits it ahead of you when you attempt to report a fault in an on-chain reward contest.

The advantage of the transactions relies on the sequence in which they execute, making these attacks possible. A “Race Condition” is another term for this circumstance.

Sandwich Attack

For such attacks to be successful, the original transaction submitted by the true sender must be completed following the transactions generated by the bot.

Let’s say you initiate a transaction to buy an NFT regardless of what the going price is because you want to buy it on the open market. A bot may keep an eye on your transaction, buy the NFT before you do, and then transfer it to you for a greater price so it can retain the difference.

Here’s another Sandwich Attack illustration. An address can authorize the other account to use tokens on its behalf using the “approve” method in the ERC20 token standard.

Assume Dev has given Sara permission to use 100 of his tokens. After that, Dev chooses to increase Sara’s consent to 50 tokens. Dev requests permission to call a function with the amount of 50 for Sara. As the owner of an Ethereum node, Sara is aware that Dev will increase her permission to 50. After then, Sara makes a transferFrom request and sends herself 100 of Dev’s tokens while charging a lot more gas for the transaction than Dev did. First, to execute, the transferFrom provides Sara 100 tokens as well as decreases her approval to 0. When Dev’s transaction is finished, Eve’s approval is changed to 50. The 50 tokens are then sent to Sara as well. Sara thus obtains 150 tokens when she should have only received 100, or the maximum (50,100).

How to avoid frontrunning?

Commit and Reveal Scheme

The Commit and Reveal hash method was created as a solution to the problem of discrimination in information submission for rewards. Instead of transmitting the actual response, the party with the answer sends a hash, which is stored in the contract along with the sender’s address. To receive the reward, the sender must subsequently send a transaction including salt and a response. If the hashes accurately reflect, the prize is given by the contract. The contract hashes and matches the produced hash to the pre-stored hash.

Take away the advantages of frontrunning

The smart contract’s programmers should create the smart contract so that the order does not favor one transaction over another.

Let’s go back to the preceding section’s “ERC20 Approve” example. The attacker can be stopped by adding a parameter to the accept function’s arguments that specify the expected current value, and if Sara’s present limit is less than what Dev said he anticipates, the execution can be stopped. Remember that implementing this change will affect an ERC20 token’s fundamental functionality. Although there is no legal prohibition against such changes, doing so will render your contract incompatible with the ERC standard.

Ensure that the mempool cannot see your transaction.

The solutions mentioned above are written from the viewpoint of the smart contract’s developer. What if you, as a user, don’t desire your transactions to end up in the mempool’s shadowy depths? Here is where products like FlashBot Auction are useful. We can communicate with the miners privately thanks to the FlashBots Auction. Our transaction will therefore avoid the public mempool as a result. The bots won’t be able to watch the transaction as a consequence. Here is a hyperlink to the documentation if you want to read further about using Flashbot Auction in your contracts.

Alternately, Flashbot Protect endpoint has the advantages of Flashbot Auction and also has a lot more straightforward deployment. Simply enter the information below in the add customized network configuration in metamask to set your RPC endpoint, let’s suppose it is metamask, to transmit data to Flashbot’s endpoint rather than the mainnet’s endpoint.

Network Name: Flashbots Protect RPC

New RPC URL: https://rpc.flashbots.net

ChainID: 1

Currency Symbol: ETH

Transactions sent using your wallet on this system will be transmitted to the miners through flashbot and won’t appear on block searchers or the pool until they have been confirmed.

Conclusion

The base layers of the Ethereum blockchain now have a very hostile environment. The development of secure smart contracts and public projects like FlashBots are the only defenses against cyberattacks like frontrunning.

Join our beta program now https://www.olympix.ai/

Originally published at https://www.linkedin.com.

--

--