# How Price is Discovered in Uniswap V2

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

$$x*y=k$$

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

$$\triangle y= y\ – \dfrac {k}{x+\triangle x}$$

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 x tokens I gave?

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 y tokens and no price discovery would have happened. If we exchanged 100 x tokens we would receive 100 y 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 x tokens and 100 y tokens). This would’ve been just a conversion, and no price discovery would have taken place.

But as we saw, this was not what happened, we received less y tokens for the x 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 y side), we would get:

$$\triangle y = 90.909\ – \dfrac{10000}{(110+ 10)} = 7.576$$

As we can see, swapping again 10 x tokens would result in receiving only 7.576 y tokens. We can take this further, and calculate 10 consecutive swaps of 10 x tokens for y tokens, here is the table below:

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 x tokens, we should end up with the same amount of y tokens. Let’s check, and see if it’s the case! We use the same formula again, plug in the numbers, and we get:

$$\triangle y = 100\ – \dfrac{10000}{(100+ 100)} = 50$$

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

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 x tokens, what would happen? Let’s run the numbers:

$$\triangle y = 100\ – \dfrac{10000}{(100+ 1000000)} = 99.990001$$

As we can see, we would receive almost all y tokens, but not all of them. We would have jumped to point (x=1,000,100, y=0.009999) along the curve.

And if we take this further, we can actually prove that we would need an infinite amount of x tokens to exhaust all y tokens. This can be mathematically proven using limits as such:

$$\triangle y = y\ – \lim\limits_{\triangle x \to \infty} \dfrac{k}{(100+ \triangle x)}$$
$$\triangle y = y\ – \dfrac{k}{(100+ \infty)}$$
$$\triangle y = y\ – 0$$
$$\triangle y = y$$

From this we can conclude that if we push the amount of x tokens given to infinity, the amount of tokens received y 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.

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.