This is the second article of the deep dive series into how Uniswap V2 works, and it focuses on how price is discovered on Uniswap V2. It continues the reasoning process detailed in the previous article – An Introduction to The Uniswap V2 Math Function – where the core math function from Uniswap V2 is presented, and the calculation for simple token swaps is detailed.

If you haven’t read the previous article, and you have trouble following what is explained here, perhaps it will be best if you read it first before continuing further.

In the previous article, we started by presenting the main Uniswap V2 equation below

And from that formula deduced the following equation for calculating a swap

And used it to calculate the amount of **△ y** tokens we would receive for a different swap amounts. We want to explore this equation further in this article. One thing from the previous article that might have struck you is the following question:

Since both sides of the pool had the same amount of tokens to start with, shouldn’t I have received 10 ** y** tokens in exchange for the 10

**tokens I gave?**

*x*At first glance it might seem like that would be how it should behave, but actually it’s quite different, and for good reasons, which are mostly related to enabling price discovery. If the exchange was proportionate, we would just convert the same amount of ** x** tokens into

**tokens and no price discovery would have happened. If we exchanged 100**

*y***tokens we would receive 100**

*x***tokens and no more tokens would be available inside the pool (In the example presented in the previous article the pool was loaded with 100**

*y***tokens and 100**

*x***tokens). This would’ve been just a conversion, and no price discovery would have taken place.**

*y*But as we saw, this was not what happened, we received less ** y** tokens for the

**tokens we put in. This is due to the nature of the Uniswap V2 equation. Furthermore, if we now tried to swap again 10**

*x***tokens, since the pool amounts changed from the previous trade (from 100 to 90.909 on the**

*x***side, and from 100 to 110 on the**

*x***side), we would get:**

*y*As we can see, swapping again 10 ** x** tokens would result in receiving only 7.576

**tokens. We can take this further, and calculate 10 consecutive swaps of 10**

*y***tokens for**

*x***tokens, here is the table below:**

*y*Swap | Pool X | Pool Y | Trade X | Receive Y | Total Y Received |

1 | 100 | 100 | 10 | 9.090909 | 9.090909 |

2 | 110 | 90.90909 | 10 | 7.575758 | 16.66667 |

3 | 120 | 83.33333 | 10 | 6.410256 | 23.07692 |

4 | 130 | 76.92308 | 10 | 5.494505 | 28.57143 |

5 | 140 | 71.42857 | 10 | 4.761905 | 33.33333 |

6 | 150 | 66.66667 | 10 | 4.166667 | 37.5 |

7 | 160 | 62.5 | 10 | 3.676471 | 41.17647 |

8 | 170 | 58.82353 | 10 | 3.267974 | 44.44445 |

9 | 180 | 55.55556 | 10 | 2.923977 | 47.36842 |

10 | 190 | 52.63158 | 10 | 2.631579 | 50 |

We can also plot this in a chart and see how many tokens we receive back after each consecutive trade.

Furthermore, if instead of doing 10 consecutive trades of 10 ** x** tokens we did only one trade of 100

**tokens, we should end up with the same amount of**

*x***tokens. Let’s check, and see if it’s the case! We use the same formula again, plug in the numbers, and we get:**

*y*Cross checking with the table above, if we sum all ** y** tokens received from the 10 consecutive trades, we do also arrive at 50

**tokens!**

*y*This in turn means that as we buy and sell tokens from the liquidity pool, we just follow along a curved line that maps the supply of tokens on each side of the pool. And we can plot that line. If we do multiple buys and sells, and plot the resulting token amounts on each side of the pool, we end up with a chart like the one below.

Notice how the line is curved, passes through the point (x=100, y=100) and never actually touches the horizontal X axis and the vertical Y axis.

Let us try to understand why this is the case. If we do another thought experiment, where instead of swapping a small amount of ** x** tokens, we instead tried to swap 1,000,000

**tokens, what would happen? Let’s run the numbers:**

*x*As we can see, we would receive almost all ** y** tokens, but not all of them. We would have jumped to point (

**=1,000,100,**

*x***=0.009999) along the curve.**

*y*And if we take this further, we can actually prove that we would need an infinite amount of ** x** tokens to exhaust all

**tokens. This can be mathematically proven using limits as such:**

*y*From this we can conclude that if we push the amount of ** x** tokens given to infinity, the amount of tokens received

**△**will equal the amount of tokens in the pool

*y***. But since we don’t have infinite tokens to exchange, we will never exhaust the other side of the pool.**

*y**For the savvy reader, to be really precise here, since solidity doesn’t handle infinite amounts nor infinite precision, we would reach a limit where we wouldn’t be able to swap x for y any longer, either because the largest value for x or the smallest value for y had been reached. That is a technical detail we are not focusing on in this article. Instead, we are analyzing what the mathematical function tells us.*

How Trading Tokens Affects Price

As we saw above, swapping ** x** tokens for

**tokens and vice-versa changes the amounts in the pool, and changes the amounts that will be received for the next swap. Each swap will move us along the**

*y***curve, and hence each next swap will start at where the previous swap ended up, and therefore present different values for the swap.**

*x y ratio*Another way to see this is through token price. Token price is usually measured by having the amount on one side of the pool be divided by the amount on the other side. Specifically, if we want to calculate the price of ** y** tokens in relation to

**tokens, we do that through the division of**

*x***by**

*x***(**

*y***). The values from this division are plotted on the chart below.**

*x/y*As shown in the chart above, as we move along the blue ** x y ratio** curve, the red curve shows the price of

**tokens in relation to**

*y***tokens. As we can clearly see, the more**

*x***tokens we swap for**

*x***tokens, and therefore move along the blue curve to the right, the higher the price of**

*y***tokens gets. Intuitively this also makes sense, we are slowly increasing the amount of**

*y***tokens and reducing the amount of**

*x***tokens, and therefore the price of**

*y***tokens in relation to**

*y***tokens should increase.**

*x*Notice too that the red price line is a curve and not a straight line. This shows us that price doesn’t change in a linear way but instead changes more and more rapidly as we move along the curve to the right. One subtle aspect to understand here, is that though it changes more and more rapidly, the overall price move is slowing down. To make this perfectly clear, at point (x = 100, y = 100), price stands at 1. If we now move to the point (x = 200, y = 50), price is 4, it has 4x’d. But if we now move to the point (x = 300, y = 33.333) price is 9, meaning it only barely 2x’d from the previous price hike.

This is how price is discovered in Uniswap V2 liquidity pools. In the next article we will publish, we will dive deep into slippage and liquidity depth.