How Liquidity Pools work on Uniswap

How Liquidity Pools work on Uniswap

10 March 2024, 11:20
Lorentzos Roussos
0
76

Here is how the liquidity pools of uniswap work on chain :

Assume we are on the ethereum chain . 

We will assume euro and usdollar are 2 memecoins for which we want to create a liquidity pool.

Why ? Because it will be easier to draw parallels with forex pairs .

So , we have 1 million EUR and 1 million USD , which are tokens on the ethereum chain . Ok ? 

Skip all other questions in your head for now (what are tokens , how we moved them , how we created them etc)

and imagine we are trying to create a EURUSD pair on chain .(with our own exchange rate)

We create a new liquidity pool , uniswap will ask for the amount of the first currency (token/coin).

We deposit 1000000 EUR , 1 million euros . 

Then it will ask for the amount of the other currency (token/coin) , so we deposit 1000000 USD , 1 million Us Dollars

(and other things like fees etc , but that is not for a different time)

Now we have a pool of 1 million dollars and 1 million euros .

On chain these are called reserves , unsuprisingly . 

The price of a EUR in USD is = reserves_USD/reserves_EUR in the POOL ! (not on the chain)

The price of a USD in EUR is = reserves_EUR/reserves_USD in the POOL ! 

The initial price of a EUR for instance is 1.0$ (1000000/1000000)

Now you realize that if someone starts buying from the pool they will affect the price per cent that is taken out in a somewhat geometric function . Right ? 

Yes . That means if im an outside buyer and i have 1 million dollars i can't buy 1 million euros from the pool instantly because it is not a market maker based on limit orders .

This type of exchange is called AMM (Automated Market Maker)

So what is the calculation ? 

Assume K = reserves of asset A * reserves of asset B

K must always be stable prior the purchase and after the purchase!

So at the initial point K = 1000000 * 1000000 = 1 trillion 

Lets assume i want to buy 1 million USD worth of euros then (from THAT POOL): 

  • the pool calculates the K , 1 trillion 
  • then what am i giving the pool ? 1 million USD so it calculates the new USD reserves (2million now)
  • then based on the equation of K = reserves_EUR*reserves_USD it has to find what the NEW EUR reserves should be for K to remain constant . That is reserves_EUR=K/reserves_USD , => 1trillion/2million => 500000 , 500K EUR 
  • It then calculates how many EUR it will give me by subtracting the new EUR Reserves (500K) from the old EUR Reserves (1million)
  • And it gives me 500K EUR
  • It then calculates the new price of EURUSD , 2million(USD)/500K(EUR) = 4$

That way i can never buy up the entire supply , and if my liquidity is low someone else can waltz in (if they have the same assets) and provide liquidity at better prices ,

Note that in an AMM you create EURUSD and USDEUR at the same time essentially 

Can also briefly explain what rug pulls and snipe bots are too based on the above.

Snipe Bots 

Imagine you create your little cute old coin on an evm chain .

(EVM Chains : Ethereum , Bsc , avalanche , optimism , arbitrum , base , polygon and more)

Its a fun project so you just deposit $500 for liquidity , right ? 

And let's call this project FatTraders . FAT

You start a liquidity pool with the usd dollar (Tether USDT essentially)

and you initially deposit 500$ and 1 million FAT

1 FAT is initially worth (500$/1million) = 0.0005$

This is your starting price . 

The snipe bot is listening (reading blocks) for liquidity initialization events .

When it finds the poor guy (us) with an imbalanced liquidity pool it comes after us.

It buys 100$ total worth of tokens for example , succesively (to avoid the price impact limit of uniswap).

Now at first glance it is not that bad , you think someone came in and bought $100 right ? 

Your reserves post that look like this : 

$100 comes in the USD reserves => 500$+100$ = 600$

K_before=500*1million=500000000

FAT_reserves_after=500000000/600=833333

So that bot bought ~167 000 FAT !

New price FATUSD = 600/833333 = 0.00072$

Okay , and let's also throw in the Market cap calculation in there .

What is the market cap in $ ? price x total quantity

Market cap initially = 0.0005 * 1000000 = 500$
Market cap now = 0.00072 * 1000000 = 720$

So what is the problem ? (and why did the snipe bot did this ?)

Well let's say you market your token and it manages to attract a total of 200K$ in purchases 

Success right ? 

Here is how your token pool looks like after 200K$ of net purchases 

$200000 comes in the USD reserves => 600$+200000$ = 200600$

K_before=600*833333=499 999 800

FAT_reserves_after=499 999 800 / 200600 = 2492

New price FATUSD = 200600/2492 = 80.49$ ! 

New market cap = 1000000FAT * 80.49$ = 80 490 000 $

And there is a bot that holds 167 000 of that !

So let's see what happens if the bot dumps it all 

167 000 FAT is to be added to the FAT reserves => 2492 + 167000 => 169492 FAT

K_before = 2492 * 200600 = 499 895 200

USD_reserves_after= 499 895 200/169492=2949 

So the bot will receive 200600$ - 2949$ => 197651$ (maybe a bit less because it will have to split the orders)

New price FATUSD = 2949/169492 = 0.017$ !

New market cap = 1000000FAT * 0.017$ = 17 000 $

This is what snipe bots aim to do , and frankly ANYONE , who gets in that early in an AMM pool can do that . 

That is also one type of rug pull , although technically it is not a rug pull .

That means sometimes the token collapses but it may not be the creators fault . But you know who to blame because you can't see the bots or the early buyers.

The "pure" rug pull happens in these 2 ways : 

  • The creator pulls out the liquidity from the pool , so nobody can trade the pair.
  • The creator has reserved "tokens" not in the liquidity (100% of the token supply , FAT for instance , is not in the liquidity pool) and sells them on the pool after it grows.

PS : Also notice how adjusting the initial liquidity values makes it harder or easier for the market cap to rise (i.e. get noticed by buyers).In our example we hit an 80 million market cap with 200600$ of investment , that could be 200$ across 1000 people !



Share it with friends: