Behind the Scenes of Just-In-Time MEV Bot On UniV3: A Technical Breakdown
From Adding to Removing in the Perspective of Liquidity
According to @jphackworth42’s dashboard on Dune, the revenue of JIT MEV on #UniV3 has reached $7.3M as of Jun 8. The newly released spec of UniV4 brings novel development to the field. It’s interesting to see how it proceeds when V4 is released.
#UniV3's concentrated liquidity created Just-In-Time MEV bots that extract fees from LPs, with risks for passive providers. JIT involves 3 more txs, it's hard to read the detail steps inside these txs. Let's dissect on of these JIT 🤖 and find out what's going on inside.
3 key transactions involved in a JIT trade: add liquidity, swap, and remove liquidity.
Let's take a look at the adding liquidity tx first.
Steps 0-1: The LP provider added $7.8M $USDC and 98 $WBTC as liquidity to a #UniV3 pool.
Internal Bookkeeping: What’s Under the Hood Wen Uniswap Manages Liquidity
A few things to know before moving on: to keep the balance of the bookkeeping in #UniV3, EigenTx marked the internal liquidity share changes using virtualLiquidity, a placeholder.
UniV3 uses position NFT as a voucher issued to liquidity providers and records the number of liquidity tokens and the liquidity share provided by liquidity providers. In this TX: UNI-V3-POS-350101.
In the chart, you can regard the UNI-V3-POS contract as being used internally by Uniswap to match the internal record of liquidity providers. Like when each of us opens an account at a bank, the bank's internal database has corresponding account data.
On the other hand, NFTs like UNI-V3-POS-350101 are mainly for external uses, i.e.; liquidity providers and other external users. It's like our own bank account after opening an account at the bank.
Hence, we use two virtual contracts: virtualLiquidity and virtualLiquidity-350101 to help balance the liquidity changes of #UniV3.
And this is UniswapV3Pool contract, representing the overall liquidity pool, stores liquidity tokens, and keeps records of the overall liquidity, identified as a virtual contract-virtualLiquidity-in the token flow chart.
Okay, time to move on to the liquidity adding tx.
Adding Liquidity: Prepping for MEV
Step 2: #UniV3 records the liquidity tokens added by LP internally, being marked as minting 228,594,721,898,820 virtualLiquidity sending to UNI-V3-POS contract. Internal share changes added.
Step 3: 228,594,721,898,820 virtualLiquidy-350101 was minted as a placeholder for the sake of bookkeeping on the virtual external account contract: virtualLiquidity-350101 and sending to UNI-V3-POS-350101. "External" share changed added.
To summarize, the JIT 🤖 added 97.925989 $WBTC and 7,793,784.212848 $USDC in the first two steps and got a position NFT identified as 350101, in step 4, representing the corresponding liquidity share, 228,594,721,898,820 it added.
Next is the user swap. The JIT 🤖 successfully bundled a user's swap transaction following its add-liquidity transaction within the same Uniswap V3 pool. The user was swapping a substantial USDC to WBTC using an aggregator.
Removing Liquidity: Wrapping Up and Burn
When removing liquidity, the JIT 🤖 effectively burned the position NFT to claim the entire liquidity share as well as the fee revenue. EigenTx's token flow chart ensures balanced token transfers with the support of complete internal accounting details.
In the liquidity removing process, we use virtualOwed0WBTC and virtualOwed1USDC to represent the account balance of UNI-V3-POS, virtualOwed0WBTC-350101 and virtualOwed1USDC-350101 for UNI-V3-POS-350101.
Steps 0-2: Created the internal account balances, virtualOwed0WBTC and virtualOwed1USDC, for UNI-V3-POS, and burned the JIT bot's corresponding liquidity share, virtualLiquidity. The lines indicating the transfers 0-1 were intersected with others.
You can read these internal share changes in the transfer list table beneath the token flow chart. Think of these 3 steps as balances moving from the liquidity of the pool to the internal account representation of the liquidity provider.
Steps 3-5: Similarly, EigenTx created the internal account balances for UNI-V3-POS-350101 and burned the corresponding liquidity share, virtualLiquidity-350101.
Think of these 3 steps as balances moving from the internal account representation of the LP to the external one.
Steps 6-7: UniswapV3Pool removed 89.0881 WBTC and 7,975,062 USDC and sent them to the JIT bot's contract.
Steps 8-11: Now that both the internal and external representation of the liquidity provider account should have no balance. UNI-V3-POS and UNI-V3-POS-350101 burned the corresponding balances of virtual liquidity tokens created in Steps 0-5.
Step 12: The JIT bot burned the position NFT, UNI-V3-POS-350101.
Step 13: The JIT bot paid a tip to the block builder.
Eventually, the JIT bot profited about $285.5 from this trade.
As you can see, EigenTx presents a flow chart that perfectly aligns with every stage of a resource's lifecycle! 📈 And the best part? Internal bookkeeping syncs with this flow so that you can keep track of all cash flows in one precise database.
If you want to read more use cases to inspect on-chain transactions, please feel free to check out our booklet: https://docsend.com/view/ce853cukgp9xyhiu.
Read some expert use cases study here:
EigenTx Use Cases for Advanced Learners: Tax Token, Liquidation and NFT Arbitrage
Have you read our EigenTx booklet, which explains complex definition transactions? We've been making video versions available for your convenience. Don't miss out! In the previous article, we used four real-world transaction examples for beginners: Simple Arbitrage, Back-Run, MEV Redistribution Using MEVBlocker, and Jaredfromsubway.eth, to help you get s…
How to Make a Fortune with Trading Synthetic Tokens: A Step-by-Step Guide to 100x-Return Russian-Doll Style MEV Arbitrage
On Jan 1st, 2023, When everyone was still enjoying the holiday, this MEV arbitrage bot flash-loaned 8668 $USDC, performed complex arbitrages across multiple protocols, and netted $3K profit with $30 gas fees. It's like trading a 4-layer matryoshka doll!
EigenTx Use Cases for Beginners: $3.2M Arbitrage, Its Back-Run, MEV Redistribution, and Jaredfromsubway.eth Sandwich
Blockchain technology makes financial transactions transparent and trustless. Understanding blockchain asset movement helps protocols improve product design and identify and mitigate risks. However, the composability of Smart Contact makes data analysts and regulators struggle to analyze blockchain transactions.
Follow us via these to dig more hidden wisdom of DeFi:
Website | Discord | Twitter | YouTube | Substack | Medium | Telegram