Overview of Uniswap v3 JIT-liquidity MEV

12 min read
💬

In this article I analyze the Uniswap-v3 Just-in-time MEV strategy. This is just a brief overview focusing mostly on the game-theoretic effects without going in technical implementation details.

Prerequisites & Good to read

🔍

What is MEV?

intro to MEV by flashbots

docs.flashbots.net
🔍

What is a JIT-liquidity attack?

intro to JIT-liquidity attacks by chainsight labs

twitter.com
🔍

How can JIT benefit DeFi?

an overview of pro and cons of JIT on Ethereum

www.coindesk.com

Motivation

  • As DEXes become responsible for more volume every year, it is of foremost importance that they find the most efficient way to utilize the liquidity available.
  • The first generation of AMMs deployed capital in an uniform way across the price pool. This turned out to be quite inefficient, because most often all the trades happen in a specific range of prices.
  • Uniswap v3 brought us concentrated liquidity, which solved the aforementioned problem, but added a lot of complexity to management for LPs.
  • On-chain analyses show that the majority of v3 LPs are actually losing money due to impermanent loss and their positions falling out of range, while a few bigger players have the manpower to actively manage their positions all the time and profit.
  • This has already made the playing field hostile towards passive LPs. Smaller players now need to rely on aggregators, vault optimizers or other automation services on top of AMMs just to keep their positions online for long.
  • It's becoming increasingly clear that passive pool management will cease existing soon unless someone comes out with a new AMM strategy.
  • JIT builds upon this idea, pushing active LP management to the extreme, with bots that try to snipe the trading fees associated with specific big trades.

The Strategy

In essence, this strategy consists in a series of quasi-atomic steps:

  1. Scanning for pending transactions in the mempool that are trying to perform large swaps in particular pools (e.g. trying to swap 1M USDC for 300 ETH on Uniswap)

  2. Creating a transaction bundle that contains the following transactions:

    1. We add concentrated liquidity in the exact range of the target swap
    2. The target swap is performed
    3. We remove our liquidity position
  3. This bundle will be mined in the same block and the result is that our “tailored” liquidity will take the majority of the fees associated with our target swap.


For those familiar with the notion of “Sandwich attack” this is not new – with the main difference being that sandwich removes value from the user who performs the swap, while this strategy removes value for passive LPs. We will see this more in detail later.

Are there existing bots doing this live on Mainnet?

Researcher 0xfbifemboy scraped the ETH/USDC Mainnet pool for instances of this type of MEV.

The results are updated as of March 23rd, 2022. From this research, only a small number of bots was found, with one in particular taking the lion's share of fees: 0xa57Bd00134B2850B2a1c55860c9e9ea100fDd6CF (over 92% of the total for JIT on that pool, ever). 0xa57 is also one of the most active MEV bots on Mainnet.

An example of this strategy in the wild

Check out this transaction. It's the one in the middle of this "sandwich":

jit mev

In the above screenshot we can see the famous JIT sandwich consisting of "minting liquidity", "user trade" and "removing liquidity". The target trade is the one highlighted in the middle, which was a swap of about 365k USDC for 130 ETH. This operation can generate about $1000 in trading fees. Analyzing the mint and burn transactions, the difference between the value of added and removed tokens was in fact very close to $1000, which the bot collected as profit.

What are the effects of this strategy on other players?

  • For users: they get liquidity exactly where they want to trade. This means that the end user will have lower slippage and a more favorable trade in general. We can consider it a net positive.

  • For other LPs: this is where it gets tricky. Since we will take the majority of the fees, this can be considered as an “attack” on other LPs, especially if they are passively managed and don't constantly rebalance their positions.

Consideration #1: Efficiency Trifecta

JIT positions are very narrowly focused. In fact, they are as focused as possible in order to capture the entirety of the fees for a specific trade that we know by fact is about to happen.

Capital efficiency in DeFi is a hard concept to master, but in essence it's a measure of how much we are getting out of our deployed capital. DeFi is making giant leaps forward in this aspect, and Uniswap v3 has indeed been a huge milestone. A great thread by Hayden explains how a v3 pool can easily be 10 times more efficient than the v2 equivalent: basically a v3 pool with $20M can have the same benefits (both in terms of fees paid to LPs and low slippage for traders) in the same time period, as a $200M v2 pool. This is already an insane result, but recent developments in MEV have started to shed light on what's technically possible out there:

crocswap charts

(source: CrocSwap)

On the left chart, we can see the normalized distribution of all liquidity positions for ETH/USDC in terms of range. The ones highlighted in blue supposedly correspond to JIT attacks where liquidity was added and removed in the same block. As we can see, the blue gaussian distribution is a lot narrower, because our JIT positions are tailored for a specific range and cease to exist quickly.

On the right chart, we can see the distribution of fees collected by positions normalized by time duration of their existence. We can call this “Fee Efficiency”. Indeed, the distribution of JIT fee efficiency falls essentially completely outside the distribution of non-JIT fee efficiency.

Consideration #2: Non Atomicity

From my experience reading about MEV for some time, I have learned that atomicity is very important. Indeed, a bot is 100 times easier to build if it's going to deal with the same currency for fees, initial and final balances – because then it's a matter of just checking that the balance difference after the transaction bundle amounts to a positive margin and we know for sure that the operation is going to be profitable.

This is not so simple with JIT. Let's take a quick example:

  • MEV bot provides 10.9M USDC and 8.3k ETH
  • A trade of 1.9M USDC for 400 ETH lands
  • MEV bot removes 12.6M USDC and 7.9k ETH

Basically, we end up with different amounts of tokens from the starting balances. This makes checking for existing profit more difficult at best, and almost impossible in some cases.

This means that for a strategy of this kind we will probably have to choose one of two routes:

  1. Focus on a larger scope and play out a strategy of rebalancing and accumulation where we seek to get higher APYs for our existing LPs.
  2. Spend “more” (time/effort/gas/research/etc) to find a deterministic way to judge JIT trades and perhaps sell all assets after each attack, eliminating inventory risk.

Is this considered long-tail MEV?

Well, there isn't a proper definition for long-tail, but I believe most can agree that every opportunity where there is a good difficulty-reward ratio and low competition is long-tail.

I wouldn't say outright that JIT is long-tail anymore, because it's been around for a while and there are already bots that are doing it. However, it's still a very niche strategy and it's not as easy to execute as a simple flash loan due to the non-atomicity of the operation.

Research suggests that there are only a handful of bots doing this kind of strategy. Moreover, as of now only 14% of total tracked MEV is done on Uniswap V3, and this metric probably just tracks arbitrage. Going by the cost of this strategy on mainnet, it's likely that research on cheaper networks where Uni-V3 is available might be more conclusive.

EDIT: I finally had the time to perform some queries on Polygon to find potential occurrences of JIT attacks there. From what I've found, it turns out that nobody ever used a bot for V3 JIT liquidity on this network yet. In fact, the only occurrences of V3 positions minting + collecting in the same block are due to some "rebalance" calls made by Chainlink Keeper bots or similar, not by a searcher. This makes sense because polygon doesn't offer a reliable flashbots relay: the only available one was the MEV-bor client implementation made by Marlin Protocol which is not validating blocks currently. Moreover, flashbots-style MEV seems to be heavily disincentivized and made more difficult by other architectural choices employed by the Polygon team, such as block reorgs that can go 1000 blocks deep, and random propagation delays, but this is out of scope for this article.

polygon jit

How can this strategy be neutralized?

It's entirely possible to neutralize this strategy completely, but it would require significant architectural cost since Uniswap V3 is already very popular. The best approach I've seen to date is the one explained in Crocswap's Whitepaper, which is to keep track of the LP position creation timestamp and prevent positions to be destroyed unless they are older than a certain threshold. This means that the bot would have to be exposed to inventory risk for longer, which undermines the whole atomicity of the strategy, making it pointless.

Can Flashloans be used for this strategy?

The only answer right now is no. JIT is not much different from traditional sandwich attacks, in that it's not possible to be executed in a single function call, because we need to encapsulate a third-party user swap in the Flashbots bundle – this is why it's called a "sandwich" in the first place.

One interesting turn-around could be EIP-3074, which is a very interesting proposal. In short, it would make it possible for contracts to execute internal calls as if they were EOAs. This means that you would be able to effectively frontrun pending swaps with a single transction, making flashloans very viable. Luckily (for Uniswap LPs, at least) this EIP looks like it has been sitting idle for more than two years because it has significant security implications for existing smart contracts.

Footnotes

1.

Robert Miller, CEO @ Flashbots

(twitter.com)

2.

MEV notion master doc

(forms.gle)

3.

In-depth analysis by 0xfbifemboy

(crocswap.medium.com)

4.

About Uniswap v3 capital efficiency

(twitter.com)

5.

Automated market makers: versioning up

(ambergroup.medium.com)

6.

JIT MEV opportunities on Ethereum

(twitter.com)

7.

Flash Boys 2.0

(arxiv.org)

8.

MEV day talks @ Amsterdam 2022

(mev.day)

9.

JIT attack on Popsicle

(twitter.com)

10.

JIT MEV dune dashboard

(dune.xyz)

11.

Frontrunning with EIP-3074

(ghiliweld.github.io)

12.

JIT liquidity profit curve Desmos tool

(www.desmos.com)