This is the fourth article from the deep dive series on how Uniswap V2 liquidity pools work and it focuses on what Uniswap V2 liquidity is, and how it relates to liquidity pool tokens. It also explains how liquidity pool transaction fees work, as well as impermanent loss.
Up until now in our series, we have discussed the mathematical properties of the main Uniswap V2 math function
and explained how trading affects the amounts on each side of the pool, as well as slippage, and also price.
What we haven’t talked about yet, though, is how liquidity can be added to and removed from a liquidity pool. That will be the focus of this article. Furthermore, we will also explain the pros and cons from providing liquidity to a liquidity pool, specifically how earning from trading fees can be financially interesting, but risking impermanent loss can be dangerous too.
Adding Liquidity to a Liquidity Pool
Adding liquidity to a liquidity pool works differently when liquidity is added the first time, and when liquidity is added after that. The first time liquidity is added to the pool, the ratio between x and y tokens will determine their base exchange rate between them, which will have impact on all future trades. This was explained in the previous post on how slippage works in Uniswap V2, where we showed that there is a slingshot effect as prices rise or fall from their initial exchange rate, due to, in essence, slippage being pushed around as tokens are bought and sold. Furthermore, Liquidity Pool Tokens are minted and sent to the liquidity provider, and the amount of LP tokens also differs between adding liquidity the first time, and adding liquidity afterwards.
Liquidity Pool Tokens
Every time liquidity is added to the pool, the pool gives back liquidity pool tokens in exchange for the liquidity provided. These LP tokens can be used to redeem the provided liquidity back. A good way of thinking about it is as follows: if you have 100% of the LP tokens, you can redeem back 100% of the liquidity in both sides of the pool. If you less than that, you can redeem proportionally to the percentage you hold.
When you add liquidity for the first time, the amount of LP tokens issued by the pool to you are calculated as follows:
What this means is that the amount of LP Tokens is calculated by through the square root of the multiplication of the amount of x and y tokens added to the pool.
Does this math formula have something special? Not really, it is just a way to establish the amount of LP tokens that should be issued for the first liquidity provision. As you will see, all subsequent provisions of liquidity have LP tokens issued in proportion to the liquidity provided relative to the existing liquidity in the pool. Another way of putting it is as follows: if someone comes right after the first liquidity has been provided, and again provides liquidity to the pool, and provides exactly the same amount of x and y tokens as was provided previously, the liquidity pool will issue him the same amount of LP tokens as for the first provider of liquidity. However, if he instead provided only half of the x and y tokens, i.e. 50% of the liquidity initially provided, the pool would issue him only half of the LP tokens issued to the one who provided the first liquidity.
To put the explanation above into a neat formula, if we consider a pool with added liquidity of x and y tokens, and more liquidity is added to the pool, we can calculate the amount of LP tokens to receive as follows:
The division of △x with x allows us to easily determine the proportion of newly added tokens, to the ones already existing in the pool, and from there determine how many LP tokens should be issued to the one who provided the new liquidity. It’s that easy.
Notice though, that we excluded the △y and y amounts from the formula. We did this to simplify the concept at hand, the actual formula is a little more complex, but at its core has exactly the same reasoning as the one described above.
You might have noticed that we haven’t discussed the situation where more liquidity is added after some trades have occurred in the pool. In such situation, the original ratio between x and y tokens, which was established when the first liquidity injection too place, has changed. The only additional thing we have to consider is that we have to respect the relative ratios in the pool at liquidity provisioning time, and if we don’t respect them Uniswap V2 will return to us the excess either of x or y tokens we provided. Another way of thinking about it, is that if you want to provide 10% of liquidity to the x side of the pool, you will have to also balance it out with 10% of liquidity to the y side of the pool.
Therefore, the ratio of △x to x tokens and △y to y tokens is ensured to be the same, and hence the formula we described above, to calculate the amount of LP tokens to receive for the provision of new liquidity, applies even though it only considers the ratio of △x to x tokens.
Redeeming Liquidity Back
Liquidity Pool tokens can be used to redeem the liquidity previously provided. The way it works is very simple. If you have 10% of all LP tokens, you can exchange those for 10% of the x tokens in the pool, as well as 10% of the y tokens in the pool. You return the LP tokens to the pool, and in exchange for that you receive liquidity back.
One thing to take into account here is that the ratio of x to y tokens might have changed since you initially provided the liquidity, and therefore, when you redeem your liquidity back, you might receive different amounts of x and y tokens than you initially provided. This change might constitute for you a gain or loss, depending on how the amounts of x and y tokens changed, and which one of these two you consider more valuable. This changes in the pool are also called impermanent gains or losses, because since you haven’t exchanged your LP tokens back, future trades might reposition the pool balances again, deeming your previous gain/loss not permanent.
Earning Fees by Providing Liquidity
Liquidity providers can earn fees when traders exchange tokens. The exact way fees are collected from executed trades is quite complex, and explaining it in this article would be out of scope. In short, about 0.3% of fees are collected from each trade. When a liquidity provider redeems its liquidity back, it will collect a share of these taxes, according to the amount of LP tokens it exchanges for liquidity.