If you are looking to understand how Uniswap V2 works, you have come to the right place. This article is the first of the deep dive series into Uniswap V2 inner workings, and focuses on how the Uniswap V2 math function works, and how you can calculate how many tokens you should receive for a basic token swap.
So, let’s begin. At the heart of UniswapV2 lies the following equation:
This is a very simple equation, and what it states is that for a given liquidity pool, with a certain amount of tokens x on one side of the pool, and y amount of tokens on the other side of the pool, the result from the multiplication of these two amounts k, has to be maintained when trades are executed. This means that k should stay fixed when we exchange x tokens for y tokens, and vice-versa. Surprisingly, this is the gist behind an UniswapV2 liquidity pool. And from here, we can draw many interesting conclusions we believe most traders are unaware of.
Let’s start by exploring the concept of having an invariant k when a trade occurs. What this means is that when we want to exchange x tokens for y tokens, we will add a certain amount of x tokens to the pool, and take a certain amount of y tokens in exchange, amount which must satisfy the rule of maintaining k invariant, meaning that k can’t change from this operation. We can convert the above trading requirement into a mathematical formula.
What we are saying, mathematically, is the following:
Which means, we are adding (giving) a certain amount of tokens △x to the left side of the pool, and subtracting (taking) a certain amount of tokens △y from the right side of the pool, in exchange for the tokens we gave. Most importantly, this operation has to maintain an invariant k value, therefore the equals sign.
The Math Behind an Exchange of Tokens
When we do a trade, we know how many tokens we want to give, but the amount we should receive needs to be calculated. Using the formula above, we can determine how many tokens we will receive. To do that, we need to manipulate the equation above to isolate △y (which is the value we want to figure out).
Through the above steps, we were able to isolate △y on one side of the equation. Since all variables on the right-hand side are know at the time a trade occurs, we can plug the values into the formula and figure out how many tokens we will receive in return.
As an example, if we consider a liquidity pool that has 100 tokens on each side of the pool, meaning x = 100, y = 100, k = x * y = 10,000, we can plug in these values in the equation, obtaining:
And now, we can experiment with different amounts of △x tokens we want to trade. For example, if we want to exchange △x = 10, we plug that into the equation and we can figure out the amount of tokens △y we will receive:
From the above result we can conclude that we would receive 9.091 y tokens in exchange for 10 x tokens. Also, that after the trade had been executed, the tokens inside the pool would stand at 110 x tokens and 90.909 y tokens.
If instead of buying 10 x tokens, we wanted to buy 30 x tokens, we could also figure out how many y tokens we would receive. Let’s do the calculation:
After this trade, the pool tokens would stand at 130 x tokens and 76.923 y tokens.
If instead of exchanging x tokens for y tokens, you wanted to exchange y tokens for x tokens, you would need to use a slight variation from the formula we’ve been using, but still derived a similar fashion from the main function x*y=k. And that formula is:
If you notice carefully, the end result is just the inversion of all x, △x, to y and △y, respectively.
With these two formulas, you can now calculate how many tokens you will receive for a simple token swap through Uniswap V2, and you have understood the underlying mathematical principal.
In the next article, we will explore how trading affects price in Uniswap V2 pools, which is a continuation on what we have understood here.