Arbitrage Trading, Flash Loans, and Front-Running Miners

Flash Boys are taking over the blockchain

In 2014, American journalist Michael Lewis provoked public outrage and FBI investigations towards Wall Street with his book Flash Boys, narrating the story of how firms were making profit from beating their own clients at the very thing they were employed to do — stock trading.

Today, we can observe a similar phenomenon happening across cryptocurrency exchanges. Miners are cutting into users’ profits by beating users at the game miners are supposed to facilitate. That game is called arbitrage.

Arbitrage

Arbitrage is a trading strategy that aims to profit from price differences of the same asset in different markets. In the case of real-world currencies, these markets are represented by foreign exchange service providers such as banks.

Different banks may charge you different rates for the same pair of currencies as they need to make some profit from servicing your exchange, which amount varies depending on each bank and/or your relationship with them.

How this might look like in real-life:

John has 100 USD. If 1 USD is equal to 6.48 RMB in Bank A and 6.5 RMB in Bank B, John can conduct arbitrage by first exchanging his USD at Bank B (thus earning 100 x 6.5 = 650 RMB), then exchanging his RMB with USD at Bank B (thus earning 650 x 1 / 6.48 = 100.31 USD). The difference of 0.31 USD is John’s profit from arbitrage.

Bank A could actually get 6.49 RMB with 1 USD, but it has set a rule to only give John and other customers 6.48 RMB for each USD, and take the 0.01 RMB difference to cover its operating costs and/or fulfil profit margin requirements.

In short, arbitrage is just plain old common sense “buy low sell high” implemented in different markets. Time management is obviously crucial here: you have to capture the arbitrage opportunity before market prices shift. Otherwise, you might be stuck with a bag of assets that cost you more than they are worth.

On the blockchain, these markets are represented by cryptocurrency exchanges. Savvy parties are already profiting from arbitrage opportunities between crypto exchanges by either conducting arbitrage trading or providing arbitrage-enabling services. An example of the latter is AAVE with their flash loans.

Flash Loans & Arbitrage Bots

The flash loan offering allows you to borrow (and are expected to return) cryptocurrencies without any collateral. As their name implies, flash loans only take place within a very short duration — in one block of transactions. If it turns out that you cannot pay back the loaned amount in that block, then the loan will be treated as if it never happened in the first place.

This sort of arrangement is implausible in real-life. We can’t just borrow a large sum of money without collateral, use it to trade currencies, make a profit, and pay the whole loan back — or cancel the transaction if the trading is unprofitable — all in an instant. However, on the blockchain, we can build self-executing programs (smart contracts’) to do these transactions for us.

Take Mary, who is a blockchain developer and codes her own arbitrage bot. The bot automatically borrows a large amount of cryptocurrency on AAVE, uses the funds to capture identified arbitrage opportunities across exchanges, and returns the original amount plus some fees back to AAVE. Mary is thus able to make large sums of profits out of only a few lines of code.

If this sounds too good to be true, it does. Aside from platforms like AAVE or traders like Mary, there is another party that benefits from arbitrage in crypto: miners. And they are taking profits away from people like Mary and you.

Front-Running Miners

We know that miners verify transactions on the blockchain in exchange for gas, and will prioritise transactions offering the highest gas in order to maximize their own gains (‘miner-extractable value’, or ‘MEV’). However, somewhere somehow, some miners realised that they could gain higher value by front-running arbitrage bots instead.

Let’s briefly come back to the story of Flash Boys. By negotiating their server arrangements with stock exchanges, firms were able to access prices and send orders faster than the rest of the market. Firms used this speed advantage to purchase the stocks that the market wanted to purchase at the original price, and then selling those stocks to the market at a higher price — effectively earning profits at the expense of the market.

Bill’s trading firm pays Exchanges X, Y and Z a million dollars per month to place his server at locations closest to the exchanges’ servers. It only takes Bill 0.001 seconds to communicate with these exchanges. Peter’s smaller trading firm does not have a million dollars, so his communication with exchanges X, Y and Z takes the longer 0.005 seconds.

Peter’s client asked Peter to buy 100,000 shares of 123 Company. Peter’s firm sends a request to purchase those shares at the price to Exchange X. As it was only able to purchase 50,000 shares at Exchange X, Peter sends another request to purchase the rest of the shares at Exchange Y. Unfortunately, Exchange Y only had 30,000 shares, so Peter needed to send another request for the remaining 20,000 shares to Exchange Z.

By the time Peter obtained the 30,000 shares from Exchange Y, Bill’s firm had already noticed his requests and deduced that Peter would be making his next order at Exchange Z. Bill submits a request for 20,000 at the same time at Peter, and, due to his speed advantage, ends up purchasing the shares before Peter’s order arrived. When it finally does, Bill sells the prices at a slightly higher price to Peter, effectively making a profit.

Similarly, when miners find unverified transactions that seem to be part of an arbitrage attempt, they make their own transactions and insert them into the block in front of the arbitrage-seeking transactions. This way, miners can end up with some if not all of the transactions’ arbitrage profits.

In the mempool, Taylor the miner came across transactions by Mary’s arbitrage bot. Two of the transactions aim to purchase 1000 Dogecoins at Uniswap for 0.15 USDT, and sell 1000 Dogecoins at Pancake Swap for 0.16 USDT.

Taylor, being really smart, noticed that these transactions are trying to capture a (1000 x [ 0.16–0.15 ] = ) 10 USDT arbitrage opportunity for Dogecoin at cryptocurrency exchanges Uniswap and Pancake Swap. So Taylor verifies the transactions, but also makes similar transactions and arranges the block in the following way:

Row 1: Taylor’s — Purchase 1000 Dogecoin at Uniswap

Row 2: Mary’s — Purchase 1000 Dogecoin at Uniswap

Row 3: Taylor’s — Sell 1000 Dogecoin at Pancake Swap

Row 4: Mary’s — Sell 1000 Dogecoin at Pancake Swap

Under this arrangement, Taylor could buy 1000 Dogecoin at Uniswap for 0.15 USDT and sell them at Pancake Swap 0.16 USDT as Mary had planned, capturing the 10 USDT profits Mary had intended to. By front-running transactions, Taylor misses out on earning more gas from verifying more transactions, but it is able to capture greater MEV.

Following the law of supply and demand, Taylor’s transactions drive up prices for Mary’s latter transaction at Uniswap, and push down prices at Pancake Swap. Mary ends up purchasing 1000 Dogecoin for 0.155 USDT and selling them for 0.157 USDT, ultimately only pocketing (1000 x [ 0.157–0.155 ] = ) 2 USDT instead of the planned 10 USDT. Of course, she still pays good miner Taylor for its services.

The illustration above only talks about one miner. In practice, miners are many. When more than one miner find the same arbitrage-seeking transactions, they can end up trying to front-run each other through a bidding war on gas fees. Such a scenario can drive up gas fees throughout the network. Users ultimately bear the brunt twice: lower arbitrage profits, higher transaction costs.

Last updated