All You Need to Know About Sandwich Attacks & JIT on Uniswap

Johnny Chuang
Diamond Protocol
Published in
11 min readNov 8, 2022
src: https://forkast.news/uniswap-smartbch-defi-gas/

Overview

All transactions on Ethereum are sent to the Memory Pool (Mempool) first, so arbitrageurs have kept their eyes on all transactions flowing into the Mempool. The Ethereum community and developers have long discussed the issues of MEV, but more research still needs to be done.

In this article, we hope to shed light on two prominent MEVs: Sandwich and JIT, by analyzing the on-chain data of Uniswap V3 combined with our experiences as arbitrageurs and liquidity providers.

You can see the implementations of this research here:

Methodology

Overview

As of 2022/10/31, WETH-USDC 0.05% Pool accounts for 56.32% ($531.48M) of the total volume ($916.44M) traded on Uniswap V3 Ethereum. As the most iconic trading pair with more than half of the trading volume, we believe digging into the data of this pair will give us insights into the general idea of MEV in Uniswap V3. Therefore, in this research, we decided to focus on the trades & liquidity provision in WETH-USDC 0.05% Pool.

We parsed the swap, added/removed liquidity event data in the past six months from Subgraph, put them into human-readable tables, and used the Python Pandas package to analyze them. To be more specific, for Sandwich Attacks, we used swap data only. In contrast, for JIT (Just In Time) Liquidity, we used swap data and liquidity provision data combined to see if the prevalence of such a move exists.

Data Source

We used the Subgraph created by @ianlapham, the dev from Uniswap, to get the data we need for this research. We used swap/add liquidity/remove liquidity data from 2022/05/01 to 2022/10/28 (exact 180 days) to get all the results displayed in this article.

Sandwich Attacks

The key to understanding Sandwich attacks is to understand their profitability. We simplified any arbitrary Sandwich attacker’s profitability can de defined as Revenue - Cost, where:

  • Sandwich Revenue: (Post-Swap Amount in USD) — (Pre-Swap Amount in USD)
  • Sandwich Cost: Gas Cost for the two swaps in a Sandwich attack

Sandwich Profit = Sandwich Revenue — Sandwich Cost

A Sandwich attack will be profitable if revenue exceeds the cost in this simplified model. We did not consider the cost of bribing miners, so the actual situation might be slightly different.

JIT

To get an approximation of the profitability of each JIT, We simplified the model into three parts:

  • JIT Fee: The swap fee earned from JIT
  • V3 PnL: The change in liquidity USD value due to impermanent loss
  • JIT Gas Cost: The gas costs of adding/removing liquidity

JIT Profit = JIT Fee + V3 PnL — JIT Gas Cost

For JIT Fee, we used the change in tick / provided liquidity tick range to approximate the JIT fee earned by any given JIT liquidity provider.

The idea is that when the tick range one provides is exhausted, the LP can actually earn 0.05% based on his/her liquidity. Therefore, we can approximate the fee earned by observing the change in ticks without visiting the total liquidity distribution.

Analysis — Sandwich Attacks

Sandwich Exploits

In the past 180 days, there were 5,346 Sandwich Attacks. The graph below shows the cumulative number of Sandwich Attacks in this period. As the graph shows, more than half of the attacks happened in 2022/5–2022/7 (the first 60 days), the reason for this is that the market was quite volatile due to the depeg event of LUNA-UST, and there was a lot of panics selling going on back then. After the market cooled down, the frequency of Sandwich Attacks also decreased.

Sandwich Cumulative PnL

The graph shows the breakdown of the cumulative PnL of Sandwich Attacks. It moved similarly to the number count of Sandwich Attacks, which also accumulated fast in the first two months of the backtest period. This is because each Sandwich Attack is a + EV move for the attackers, so they are positively correlated.

Sandwich PnL Distribution

Of the 5,346 Sandwich Attacks, only 42.36% were profitable, and the profit distribution of these Sandwich Attacks is shown below. Even though the “win rate” of Sandwich Attacks was not that lucrative, we can see that the PnL is significantly greater when winning, thus making the risk-return ratio of Sandwich Attacks attractive.

Pre/Post Merge Sandwich Comparison

The merge happened at block 15537393, we made a comparison between Sandwich Attacks that happened pre-merge and post-merge. As we can see from the table below, after the merge, the frequency of Sandwich Attacks has reduced by 55.69% compared to pre-merge. However, each exploit has an averagely earned Sandwich Attackers 74.80% more.

We believe the frequency of Sandwich Attacks is correlated to market volatility. As the market cooled down and started a period of extremely low volatility right after the merge, the frequency of such attacks dropped significantly.

Sandwich Attackers

In the past 6 months:

  • there were 42 attackers who tried Sandwich Attacks
  • 32 out of the 42 attackers were profitable
  • The top 5 profitable traders accounted for 78% of the total sandwich profit
Sandwich Attackers, sorted by cum profits

Analysis — Just In Time Liquidity

JIT Exploit

In the past six months, there were 4,666 JIT. During the market turmoil in May, JIT traders earned a lot more than other periods.

Cumulative JIT PnL

Compared to Sandwich Attacks that happened in the same period, the cumulative PnL curve of JIT moved more smoothly.

Daily JIT Liquidity / Daily Trading Volume

We used (Daily JIT Liquidity / Daily Trading Volume) as a metric to evaluate the prevalence of JIT at any given time. In the graph below, when the ratio hits 100%, it means the value of JIT liquidity on that day is the same as the daily trading volume.

The graph shows that the ratio has climbed up a lot since late September. We believe this is because market volatility was extremely low (therefore, low trading volume), but JIT bots were still working.

JIT Gas Cost Analysis

We also dug into the impact of gas on the profitability of JIT. Firstly, we define a metric called Gas Swap Ratio.

Gas Swap Ratio = Gas Cost / Swap Amount

The rationale is that when a JIT trader spots a large swap in Mempool, he will know the approximate gas cost and the swap amount in advance.

When gas cost is too high compared to the swap amount (which implies the potential profit for this JIT), the JIT attempt should lead to less profit or even cause JIT traders to lose money. We believe these two parameters should have been embedded in most JIT traders’ algorithms.

We used the Gas Swap Ratio as the X axis and the end profit of the JIT as the Y to plot the graph that shows their relationship. As we can see from the graph below, when the Gas Swap Ratio increases, the general trend of their profits moves downwards. This implies that when the gas cost rises compared to the underlying swap amount, JIT traders are worse off.

We further filtered those successful (positive profit JITs) and unsuccessful (negative profit JITs) to see if this assumption holds. We can see that profits will decrease when the Gas Swap Ratio increases in both graphs, thereby verifying our assumptions.

JIT Tick Range Analysis

The tick range of JIT trades was all 10, this is because the minimal tick spacing for Uniswap 0.05% pool is 10, and for JIT traders, they want to maximize their fee income by pouring their liquidity into the minimal possible range.

JIT Traders

59 traders conducted at least one JIT in the past six months, and the top 20 profitable traders contributed 88.6% of JIT trades in this period. We can see from the table that the average ROI per JIT trade is low (~0.003%), even for the most profitable traders. The reason for this is that JIT is a capital-intensive work, many JIT traders provide liquidity that was worth $20–30M just to JIT a $1M trade.

Liquidity Providers

In the past six months, liquidity providers of the WETH-USDC 0.05% pool have earned a $100M Fee in total. Out of the $100M fee, $2.26M was earned by JIT trades. For an average liquidity provider, JIT has caused them to make about 2.26% less.

We also looked into the daily stats for liquidity providers. We divided the daily JIT fee by the daily total LP fee and came up with the following graph:

Based on the graph, most of the time, the JIT fee accounted for 2 to 6% of the total LP fee in the past six months, and we also see that the daily JIT fee / LP fee has risen since October.

Closing Thoughts

In this research, we got some interesting findings that we believe are helpful for Uniswap community to understand the general situation of sandwich & JIT in the past 6 months.

Findings For Sandwich Attacks

  • There were 5,346 Sandwich Attacks in the past 6 months, with more than half of them happened in the first 2 months (2022/5–2022/7)
  • Out of the 5,346 Sandwich Attacks, only 42.36% of them were profitable. However, the profit distribution of successful & unsuccessful attacks was skewed, which made the risk-return ratio of Sandwich Attacks still lucrative
  • After the merge, the frequency of Sandwich Attacks has reduced 55.69% compared to pre-merge. However, each exploit has earned Sandwich Attackers 74.80% more on average
  • 32 out of the total 42 sandwich traders were profitable in the past 6 months, and the top 5 profitable traders contributed 78% of the total sandwich profit

Findings For JIT

  • There were 4,666 JIT in the past 6 months, the profit curve is smoother compared to that of Sandwich Attacks
  • Daily JIT Liquidity / Daily Trading Volume is a metric for the prevalence of JIT in a given date. We found that this index rises in October
  • Gas Swap Ratio, which is defined as the percentage of gas cost divided by swap amount, serves as a solid indicator for JIT profits. When Gas Swap Ratio is too high, JIT traders tend to make less or even lose money
  • JIT traders all provided liquidity in the minimal possible tick range, in the case of 5 bps pool, the minimal tick spacing is 10
  • There were 59 traders conducted at least once JIT in the past 6 months, and the top 20 profitable traders contributed 88.6% of JIT trades in this period
  • The average ROI for each JIT trade is low even for the most profitable traders (~0.003%), this implies that JIT is a capital-intensive strategy

Now that we understand how two of the most prominent MEV work in Uniswap, we would like to also shed some light on how to deal with JIT and Sandwich Attacks.

For Traders and Liquidity Providers

Sandwich Attacks

Sandwich Attacks increase the swapping costs for Uniswap traders, and most of the benefits would go directly into attackers’ pockets. Therefore, we believe it is of utmost importance for Uniswap traders to understand different ways to cope with Sandwich Attacks.

We believe two ways can help prevent, or at least alleviate, the impacts of Sandwich Attacks on Uniswap traders:

  • Setting Stricter Slippage For Large Swaps

By setting strict slippage, traders can protect their swaps from crazy Sandwich Attacks since when the slippage is larger than the limitation, the transaction itself will be reverted.

  • Using Flashbot RPC/API To Send Transactions

By using Flashbot, traders can send the transaction directly to miners instead of broadcasting the transaction in the Mempool, and therefore reduce the possibility of being attacked.

JIT

We believe that JIT should be deemed as a LP strategy rather than a form of attack. The rationale is that JIT requires real liquidity provisions in order to make profits, so technically these JIT traders are also liquidity providers and deserve to earn a fraction of the swap fee.

JIT can help reduce the slippage for large swaps, so it’s good for traders. On the other hand, the impact of JIT for passive liquidity providers was not that significant. Therefore, we prefer to see JIT as a feature instead of a problem.

For Uniswap Community

Sandwich Attacks

Sandwich Attacks have negative impacts on Uniswap traders; however, liquidity providers are better off because of the rise in trading volume. We think the two effects do not cancel out each other since the need for “swap” is the core of Uniswap, and only a small fraction of sandwich profits go into liquidity providers. Therefore, we believe Sandwich Attacks have a net negative impact on the Uniswap community.

JIT

JIT benefits Uniswap traders with lower slippage, but it erodes liquidity providers’ profits. For both parties, the impact is insignificant; for instance, JIT only took up 2.26% of total fees earned for liquidity providers in the past six months. Therefore, for JIT, we tend to believe that the two effects (for traders & LPs) will cancel out, therefore making JIT a neutral feature to the Uniswap community.

References

--

--