This video explains how to arbitrage between Uniswap V2 and V3 pools using a two-case algorithm: when liquidity ranges are disjoint, you drain one pool by pushing prices to their tick boundaries; when ranges intersect, you use the optimal price equation from V2 pools since only liquidity around the optimal price affects the outcome. The algorithm maximizes profit by minimizing price differences between pools through strategic token swaps.
Arbitrage Algorithm for Uniswap V2 and V3 Pools
Added:If you were to arbitrage two unis swap B2 pools, then what is the optimal amount of token that goes into one pool to maximize the profit? There's an exact answer to this question. The equation is given over here. You plug in all of these variables and it will give you the optimal amount of token to put in to maximize your profit. If you're curious, check out my other video where I explain how to derive this equation. The link will be somewhere in the descriptions.
Okay, so this equation only works with two unis swap B2 pools. But how would you arbitrage between a unisoft B2 or a unisoft B3 pool with a unisoft B2 or a unisoft B3 pool? There's an algorithm to arbitrage between unisoft B2 and B3 pools. But before I show you the algorithm, I first want to explain why this equation does not work with unisoft B3 pools. To see why this equation cannot be directly be used with unis swap B3 pools. Let's first see why it works with unis swap b2 pools. We're going to graph unis swap B2 pools. On the horizontal axis, we'll map the price that is given by the pools. And on the vertical axis, we'll map the liquidity of the pool. For example, let's say that we have pool A with this much amount of liquidity and the current price given by this pool is somewhere over here. And likewise, we'll have another pool where the price given by this pool is over here. Let's see what happens when we apply this equation to the two pools.
Using this equation, we can calculate the optimal price. This will give us how much token that we need to put in to pool A. And this will push the price of pool A to this optimal price. And likewise, swapping in the token that just came out of pool A into pool B. The price quoted on pool B will be pushed down to this optimal price. Now the reason why the equation works with unis swap b2 pools is because liquidity is constant and it stretches out from plus infinity to minus infinity. Now I can explain why this equation does not work with unis swap b3 pools. In unis swap b3 pool what you usually have is a liquidity over some price range. For example on pool a you might have liquidity from this price up to this price. and pool A we might have liquidity ranging from here to here. So notice the situation is different with unis swap B2 pools the liquidity stretched out from minus infinity to plus infinity whereas in unis swap B3 pools we have a range of liquidities. So this is what makes arbitrage with unis swap B3 pools a little bit more difficult than unis swap B2 pools.
However there is an algorithm that will allow us to do arbitrage between unis swap b2 and unis swap b3 pools. Let's start with the arbitrage condition.
Under what condition should we execute an arbitrage? If the current price on pool A, let's say, is P of A and the current price on pool B is P of B, then under what condition should we execute a arbitrage? Well, the simple answer is when the price of pool A is less than the price of pool B. So this is the arbitrage condition. Our goal is to maximize the profit under this condition. And to maximize the profit, what we need to do is minimize the price difference. So how do you minimize the price difference between two pools?
Well, there's two ways. The first way is to increase the price on pool A and then decrease the price on pool B. This is done by swapping token Y for token X on pool A and then swapping token X for token Y on pool B. This will increase the price on pool A and decrease the price on pool B. The other way is to first decrease the price on pool B and then increase the price on pool A. We can accomplish this by first swapping in token X for token Y on pool B and then swapping token Y for token X on pool A.
This will decrease the price on pool B and then increase the price on pool A.
But these are two ways to minimize the price difference of two pools. For the rest of the video, I'll explain the algorithm using the first approach. But the second approach should be straightforward to implement once you understand the first way to do the arbitrage. Finally, here is the algorithm. There's two cases to consider when the liquidity ranges between the two poles are disjoint and when the liquidity ranges between the two pools intersect. Let's first analyze what to do when the liquidity ranges are disjoint. What do I mean by liquidity ranges being disjoint? Let's say that pool A's liquidity ranges from somewhere here and up to here. And likewise for pool B the liquidity ranges from somewhere here to up to here. Notice that these two liquidity between pool A and pool B do not intersect here. There is an empty space where there is no liquidity. And that is what I mean by disjoint liquidity ranges. In this case, what is a strategy that we need to do?
In simple terms, what we need to do is drain one of the liquidity from one of the pools. So, it's either we do some kind of swap to push the price of pool A all the way up to the upper tick. I label this as PA up high since this is the higher tick. Or we push the price of pool B down to the lower tick range of pool B. I label this as PB low. So, which one should you swap first? Should you swap pool A to push the price up to PA high or should you swap on pool B first to push the current price on pool B down to PB low? Well, the answer is this all depends on the amount of X and amount of Y in both pools. For example, let's say that we were to swap all of this amount of X inside pool A. Then the question that we should be asking is if he were to take all of this X inside pool A and then dump it onto pool B, then does pool B have enough liquidity so that the new price will be greater than or equal to PB below. For example, if you were to dump all of this token inside pool B and let's say that this would push the price of P B all the way down to here, then what this mean is that there isn't enough liquidity to put all of token X from pool A into pool B.
So this is what I mean when I said that either swap to PA high or PB low depends on the amount of token X and amount of Y in both pools. The details are explained inside the code which I will show you later. So this is the case of disjoint liquidity range. The basic idea is you're going to drain one of the pool.
Okay. So let's move on to the other case. What happens when this liquidity ranges intersect? Let's first visualize what it looks like for the liquidity to intersect. Here's an example graph of two liquidity ranges intersecting. On pool A, let's say that the liquidity ranges from here all the way up to here.
And for pool B, liquidity ranges from here to here. What I mean by liquidity ranges intersecting is that the liquidity from pool A ends over here and the liquidity on pool B starts over here. There is a overlap, a intersection where the two liquidities meet. So under this condition then what strategy should we do to execute the arbitrage? The answer is we should swap to the optimal price. We'll use the equation from unis swap b2 pools to calculate the optimal price. And let's say that this optimal price is somewhere here. So now you might be asking the equation for the optimal prices only applies to two unis swap b2 pools. So how is it that we can still apply that equation over here?
Okay. And here is the answer. So let's say that we calculate the optimal price using the unisoft B2 pools equations. If you were to swap on pool A, then the price of pool A goes all the way up to here up to the optimal price but not above. This means it doesn't matter if the liquidity stretches out to infinity like unis swap B2 or if it's finite like unis swap B3. All we care about is that this liquidity is above the optimal price. As long as it is above the optimal price, then the pool A's price will come up to here, but no greater than here. So the only liquidity that we need is up to here. So that is why it doesn't matter if the liquidity is here or to infinity. And for the same reasoning, we don't need liquidity of pool B to stretch out to minus infinity like a unis swap B2 pool. If he were to do a swap, the pool B's price will decrease up to the optimal price, but it doesn't go any further. So the liquidity that is needed here is up to the optimal price. And it doesn't matter if you have liquidity below the optimal price. So that is the explanation why we can use the optimal price equation from unis swap B2 pools in this situation. when we do a swap here, pools A's price will be increased to the optimal price and the pool B's price will be decreased to the optimal price. So that's the basic idea.
But now we need to consider the edge case. What happens if you calculate the optimal price and it turns out to be above the liquidity for pool A or below the liquidity for pool B? Let's start with the first case. If the optimal price that we calculate is above the pool A's liquidity, then what should we do? Well, then what we should do is try to swap so that pools A's price will come up all the way to here. And why does this work? Well, consider that if we have liquidity up to here, then what this means is that pools A's price will go all the way to the optimal price and pools B's price will come up to here, but not below. Okay. How about if the liquidity on pool A is less than the optimal price? then we can increase the price up to here. And since the amount of token X that we took out from pool A is less than the amount that we can take out up to the optimal price, we can conclude that the pool's B's price will decrease, but it's not going to hit the optimal price. Since the only condition for it to decrease to the optimal price is for us to swap pool A up to the optimal price. And for a similar reason, let's say that the optimal price is below the liquidity of pool B. Then what we should do is swap until pools B's price reaches here. And let's now see what happens to pool A's price. If he had enough liquidity in pool B to swap until the optimal price, then pool A's price will increase up to the optimal price. However, since we have less in pool B, this means that pools A's price will be less than the optimal price as well. Okay, so these are the two strategies that Wui will execute to arbitrage between unis swap B2 and B3 pools. Now let's move on to the code.
Okay, here's the code in Python. The function that calculates the optimal amount of tokens to put inside pool A is called calc DYA. Basically, it runs a while loop executing swaps depending whether the liquidity ranges are disjoint or intersect until the two prices of the pool are equal or if swapping results in negative profit. If you wanted to try out this function, there's a Python notebook where we can execute that function with random data and also with real data. Here I'll show you both. So the first one is with real data. Real data of the two pools.
execute it and you can see a graph. The blue line represents the liquidity of pool A and the orange line represents the liquidity of pool B. This light blue line represents the tick of pool A before the swap and the pink line represents the tick of pool B before the swap. The green line represents the tick of pool A after the swap and the purple line represents the tick of pool B after the swap. You'll notice that the tick for pool A went from here to here and the tick for pool B went from here decreased to here. So this is a simulation with real data. And now here's a simulation with some random data. Feel free to play around with this Python code. And for the final part of the video, I want to show you an integration with Solidity contracts.
What we're going to do is inside our foundry test, we're going to execute this Python code that will calculate the amount of token to put inside pool 8.
Here's a foundry test script that will simulate our arbitrage between unisoft B3 pools. How this script works is it's going to first fetch live data from unisoft B3 pools and then it's going to store it into a file. Once that's done, it's going to execute a Python file that's going to read those files that was just saved, do some calculation, and then return the result. And then the result will be used to simulate a swap.
So that's how it works. To execute this script, I'm going to copy this command.
And then inside my terminal, I'll paste this and then execute it. Okay. And here is the results. Let's look at the logs.
Now before the test is executed, I swapped on pool A so that the price difference between pool A and pool B will be large enough to show you this demo. So with that said, let's look at the logs. Here are the tool pools that are involved. Here are their liquidity and their fees. And you can see here that the JSON file for pool A and pool B are saved to here. So once these files are saved, next the Python script is going to read the data from here and then calculate the optimal amount of token in to put into pool A. The optimal amount is this number over here. And for putting in this much amount, you'll get back this much amount. The difference is the profit. And here are the ticks after the swaps. And the final log shows the actual simulation of the swap. Put in this much amount and the profit is this much, which approximately equals the difference calculated from the Python script. So this was an example of simulating arbitrage using Python scripts and foundry tests. All of the code and the instructions for how to execute will be somewhere in the descriptions.
Up Next

Crypto Arbitrage Bot Development: A Step-by-Step Approach
@DappUniversity
67.2K views•2025-01-07

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Building a Uniswap V3 Arbitrage Smart Contract in Solidity
@smartcontractprogrammer
9.8K views•2024-03-24

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto




































![Spend Less Gas - Solidity Gas Optimization [Deep Dive]](https://i.ytimg.com/vi_webp/IkdoQI7API0/maxresdefault.webp)






