All swapping pools between two currncies mostly base on the x·y=k equation. Usually, x: number of TokenA, y: number of TokenB. (Initial number of TokenA and TokenB are based on the exact price when the pool was created).
Here are two scenarios for buy and sell,
- User sells ETH for getting USDC: (x+dx)·(y-dy)=k
- User sells USDC for getting ETH: (x-dx)·(y+dy)=k
Let’s assume there is a pool, and there are 10ETH and 20,000USDC in it, then the k=10*20,000=200,000
-
User sells 5 ETH for getting USDC: (10+5)·(20,000-dy)=200,000 dy=6666.67USDC
-
User sells 10,000USDC for getting ETH: (10-dx)·(20000+10,000)=200,000 dx=3.33ETH
-
What if I am trying to sell 10,000ETH for USDC? Calculate it, for fun ^^
(10+10,000)·(20000-dy)=200,000 dy=19998USDC
Sell 5ETH get 6666.67USDC , then 6666.67/5 = 1333.33 USDC/ETH
Sell 10,000ETH get 19998USDC , then 19998/10,000 = 2 USDC/ETH
What is this?!! So, The bigger you try to swap , the worse the value you get. But don’t worry, usually for a pool that size, an everage ordinary trade barely moves the needle.
Concepts
-
Slippage: this is a concept about how much gap are there between expected price and buy-in price.
-
buy-in price: 1333.33USDC/ETH (just the value we calculated base on x*y=k law)
-
expected price: current market price that we ususally query from chain network, assume it 1340
-
Slippage for 1st bought (1333.33USDC/ETH) :
-
Slippage for 2nd bought (2USDC/ETH) :
Slippage is a key matric that tells you how cost-effective the transaction is.
-
-
Liquidity
x·y=k, Liquidity=√k
-
Liquidity provider (LP)
Sometimes you may hear someone says “I earned a lot doing LP” , it means being a liquidity provider putting your tokens in pool for lending them to borrowers , you’ll get interest.
-
Add liquidity
-
First add: tokens’ price are got from chain-net
-
After first add: tokens’ price are not got from chain-net, price keep with equation
-
-
Excess liquidity
- tokens’ price are not got from chain-net, price keep with equation
- tokens’ price are not got from chain-net, price keep with equation
-
No matter add or excess, the key principle is to don’t affect the price in pool
-
Advice for investors
-
Check the real‑time slippage first, then decide whether to trade
-
If you’re a whale, choose a larger pool