What is a Liquidity Pool and how does it work

di0nix
7 min readJul 19, 2023

--

In this article I will explain (by giving many examples) how a Liquidity Pool works, its advantages and risks. Uniswap is definitely home to Liquidity Pools, but it’s certainly not the only place. Recently it is possible to find excellent liquidity pools also on DEXs (decentralized exchanges) on WAX, such as Alcor and Taco Swap!

What is a Liquidity Pool?

Imagine a Liquidity Pool as a large vessel that holds two types of tokens. For example we could create a Liquidity Pool which contains EOS and WaxP; or WaxE and Ether, or WaxP and TLM... Anyone will be able to add or swap tokens from our Liquidity Pool, and does not have to ask our permission: there is a smart contract that takes care of regulating all incoming and outgoing flows.

A Liquidity Pool contains 2 tokens

How does a classic liquidity pool work?

There are only three ways to interact with a classic Liquidity Pool: add liquidity, swap tokens, remove previously added liquidity.

ADD LIQUIDITY TO A POOL

“Add liquidity” means “add tokens to the pool”. There is only one rule: you can only add tokens in the same proportion as the current between the 2 tokens. Let’s take an example: if there were 200 WAX and 100 EOS in the pool, the proportion WAX:EOS would be 2 : 1 . So you could add 2 WAX and 1 EOS, or 4 WAX and 2 EOS, or even 1 WAX and 0.5 EOS, but not 3 WAX and 1 EOS

You can only add tokens in the same proportion of tokens already in the Pool!

NOTE: After adding liquidity, the proportion of tokens in the pool has not changed

SWAP TOKENS IN A POOL

“Swap tokens” means “exchange a token for another”. This functionality is the real reason why Liquidity Pools were invented: allowing users to exchange one token for another without having to trade with other users, but only interacting with a smart contract! Also this time there is only one rule: in a swap you will receive as many tokens so that the product between the quantities of the two tokens after the exchange is equal to the product before the exchange.

Let’s take an example: suppose the Liquidity Pool contains 24 WaxP and 12 EOS (the product of which equals 288). If we put 4 EOS in the pool, we will receive exactly 6 WaxP in return: in fact, 12+4=16 EOS and 24–6=18 WaxP will remain in the pool, and 18 times 16 still equals 288!

SWAP TOKENS: the product of the tokens must remain constant

NOTE: after doing a swap, the proportion of tokens in the pool changed (in the example before the swap it was 2:1 while after the swap it became 1.125:1)

- REMOVE LIQUIDITY FROM A POOL

This is the tricky part, which most people struggle to understand. “Remove liquidity” simply means “withdrawing your tokens from the pool”: the problem is that they won’t be exactly the same as you added! This is due to the fact that, as we have seen, each swap changes the proportion between the two tokens present in the pool. So how do you calculate the exact amount of tokens you will get by removing liquidity? Every time someone adds liquidity, the smart contract gives them new tokens in exchange (this does not happen in swaps). When someone wants to remove liquidity, he returns these tokens and receives a certain amount of the initial two tokens, again in the same proportion of the current pool.

Let’s take an example: the Liquidity Pool contains 200 waxP and 100 EOS, and cumulatively those who provided this liquidity received 10 Liquidity Pool tokens (let’s call them W|E tokens, “WaxP|EOS” tokens)

REMOVE LIQUIDITY 1: the initial situation of the Liquidity Pool

Now suppose that Lisa arrives and wants to add liquidity: she adds 20 waxP and 10 EOS (the ratio of the pool was 2:1, as well as that of the tokens she added). Lisa added 10% of the pool: since the liquidity providers had 10 W|E tokens, she will receive W|E tokens equal to 10% of the existing ones, i.e. 1 token. Now the total W|E tokens are 11, and the Pool contains 220 WaxP and 110 EOS.

REMOVE LIQUIDITY 2: Lisa adds liquidity and receives 1 W|E token

Suppose Marc arrives now and wants to swap 22 WaxP: the smart contract that manages the pool will take his 22 WaxP and give him 10 EOS in exchange; now in the pool there are 100 EOS and 242 WaxP (in fact before his arrival the product of the tokens was 220x110 = 24200, and after his swap… 100x242 = 24200)! The W|E tokens remain the same as before, 11, because Marc didn’t add liquidity.

REMOVE LIQUIDITY 3: Marc swaps his 22 WaxP tokens for 10 EOS tokens

Now suppose Lisa wants to remove all of her liquidity. Lisa gives back her W|E token; the smart contract knows that there are a total of 11 W|E tokens, so it gives Lisa back exactly one eleventh of the entire current pool! So 242 : 11 = 22 WaxP and 100 : 11 = 9.09 EOS. If you remember Lisa had given 20 WaxP to 10 EOS when she provided liquidity, and now she has returned 22 WaxP (2 more) and 9.09 EOS (almost 1 less)

REMOVE LIQUIDITY 4: after adding and removig liquidity Lisa has 2 more WaxP and 1 less EOS

Note that:

  • if Lisa had removed her liquidity before Marc made a swap she would have got her initial tokens back, 20 WaxP and 10 EOS
  • Lisa could also have removed only a portion of the liquidity provided, not all

What is the benefit of adding liquidity?

Providing liquidity to a pool has one big advantage: every time someone uses the pool to make a swap, they pay a small surcharge (which depends on the pool; usually it’s 0.3%). This fee is immediately added to the pool, then split among all those who previously had added liquidity in proportion to their share. In practice, with each swap the pool increases a little, and when someone removes liquidity they’re getting a portion of all that addition too!

Adding Liquidity vs Hodl: the Impermanent Loss

So providing liquidity only has advantages? Unfortunately not! As we saw in the previous example, the tokens obtained when liquidity was removed are not in the same proportion as the tokens given when liquidity was provided. And it almost always happens that their value in dollars is lower than the starting one. To understand why, let’s go back to our example, this time calculating the value of tokens in dollars. Suppose 1 WaxP is worth $5 and 1 EOS is worth $10: the Liquidity Pool contains the same amount of dollars in WaxP ($1000) and EOS ($1000). Lisa adds liquidity by pooling 20 WaxP and 10 EOS, or $200.

Lisa adds liquidity to the pool, and now she has 1 W|E token

Now suppose that the dollar price of EOS rises from $10 to $15, while that of WaxP remains $5. Marc knows that if he swaps his 22 WaxP the pool will give him 10 EOS (as in the previous example): he will gladly do it, because 22 WaxP is still worth $110 while 10 EOS is now worth $150, and he’ll be earning $40 in no time doing the swap!

Now suppose that the dollar price of WaxP and EOS no longer changes, and Lisa decides to remove her liquidity. As always, she will return his W|E token and in exchange she will receive an eleventh of the pool, i.e. 22 WaxP and 9.09 EOS. Well, it’s time to do the math! What if Lisa, instead of putting her 20 WaxP and 10 EOS in the pool, had initially HODL (kept) them?

As you can see, there is no good news: if Lisa had kept her tokens she would now have $250, while providing liquidity and then removing it has less money, $236.5! To be precise, she would have some more money, because Marc paid a small commission (not counted) by making the swap; in any case, it would have been better for Lisa to keep her own tokens, rather than providing liquidity. This is called impermanent loss: it is not a real loss (in fact Lisa previously had $200, now she has more, $236.5) however it is a loss compared to holding her initial tokens (which would now be worth $250)

TL, DR: so is it never worth participating in a liquidity pool??

Absolutely not! Participating in a liquidity pool is often advantageous compared to keeping your own tokens, because the fees paid by those who use the liquidity pool to make swaps continue to add up, and after a while they become greater than the impermanent loss! Without going into too much detail, we can say that adding liquidity to a pool is all the better the more:

  • the pool is used for swaps
  • the price of the two tokens in dollars changes in a similar way (both go up in value, or both go down in value)
  • liquidity is left for longer before removing it

Conversely, providing liquidity is impractical when:

  • few swaps are made using the pool
  • the dollar price of the two tokens changes very differently
  • there is little time before having to remove liquidity

My Cloud Wallet | Official WAX Discord | The defi channel | WAX Twitter | My Twitter | WAX Whitepaper

--

--

di0nix

I'm here to explain to anyone, even my grandmother, what the WAX ​​blockchain is, how it works and how you can best interact with it :)