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:

SwapPool XPool YTrade XReceive YTotal Y Received
1100100109.0909099.090909
211090.90909107.57575816.66667
312083.33333106.41025623.07692
413076.92308105.49450528.57143
514071.42857104.76190533.33333
615066.66667104.16666737.5
716062.5103.67647141.17647
817058.82353103.26797444.44445
918055.55556102.92397747.36842
1019052.63158102.63157950

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.

How Trading Tokens Affects Price

As we saw above, swapping x tokens for y 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 x y ratio curve, and hence each next swap will start at where the previous swap ended up, and therefore present different values for the swap.

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 x tokens, we do that through the division of x by y (x/y). The values from this division are plotted on the chart below.

As shown in the chart above, as we move along the blue x y ratio curve, the red curve shows the price of y tokens in relation to x tokens. As we can clearly see, the more x tokens we swap for y 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 x tokens and reducing the amount of y tokens, and therefore the price of y tokens in relation to x tokens should increase.

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.

«
»