Learn about Uniswap liquidity pool design.
Original title: “Uniswap’s LP Token casting and destruction and rights and interests”
Written by: Huang Shiliang
This article introduces the casting, destruction and equity of LP Token in Uniswap.
Uniswap’s counterparty mode
In a centralized exchange, users place buy and sell orders, and the exchange’s matching engine will sort them by time and price. Once the price of the buy order and the sell order overlap, the transaction can be completed.
However, there is no pending order design in Uniswap. Instead, the liquidity pool acts as the counterparty for all transactions.
The liquidity pool contains two currencies that need to be traded, such as ETH and USDT. For example, there are x ETH and y USDT in a liquidity pool. Now k=x*y (k is a constant)
When user A wants to buy ETH with dy USDT, the price that user A buys is y/x without considering the slippage. When a real transaction is made, the amount of ETH the user can buy dx is (not considering the 0.3% handling fee): k=(x-dx)*(y+dy), dx=xk/(y+dy)
On the exchange, the funds in the liquidity pool become: (x-dx) ETH and (y+dy) USDT, and at this time the price of ETH becomes (y+dy)/(x-dx).
The above is the basic mode of Uniswap trading.
Let’s talk about the design of this liquidity pool.
LP Token casting
In Uniswap, the person who adds liquidity to a certain pair for the first time can set the price of the pair arbitrarily, that is, x, y and k are all set by this person.
Add dx and dy after creating liquidity. If you add them according to the system default values, they will all be subject to the constraints of dx/dy=x/y.
After adding liquidity, Uniswap will return an erc20 token to the user, which is the LP Token.
The total amount of LP Token changes. Adding liquidity will cast a new LP Token, that is, additional issuance, while redeeming liquidity will destroy LP Token.
Assuming that users have injected x ETH and y USDT into the liquidity of Genesis, the number of LP Tokens minted by Genesis = sqrt(x*y). (Note: sqrt is a square root; Note: The formula in the code is sqrt(x*y)-1000*10^-18, this 1000*10^-18 is nothing, I didn’t study it, it doesn’t matter if it is so small anyway)
After the creation of liquidity, some users added (dx,dy) currency liquidity, and dx/dy=x/y, then the system newly minted d (number of LP Token)=dx/x* LP Token before minting The total amount is returned to the user.
But if (dx-ddx)/dy=x/y (Note: ddx>0), the newly minted d(LP Token quantity)=dy/y*the total amount before minting, and these ddx coins are equivalent to donation All LP Token holders.
If dx/(dy-ddy)=x/y (Note: ddy>0), then the newly minted d(LP Token quantity)=dx/x*the total amount before casting. ddy becomes a donation.
It can be summarized as: the number of LP Tokens obtained by adding liquidity = min (dx/x*total before casting, dy/y*total before casting). (Note: min is the minimum value function)
In extreme cases, someone mistakenly made dx>0 coins, but dy=0 coins, then no new LP Token will be minted, and dx will all become components of the existing LP Token and donated to all existing Users of LP Token.
This kind of misprinting of coins to a certain LP Token contract address often happens.
LP Token destruction
When users want to redeem liquidity, they need to destroy the LP Token. The destruction can be completed according to the procedure of the Uniswap system.
Assuming that the liquidity of a trading pair (ETH/USDT) has x ETH and y USDT, and now there are users destroying z tokens, the user can get the amount of ETH and USDT respectively: dx=(z/total LP Token )*x,dy=(z/total LP Token)*y
Rights and interests of LP Token
From the above analysis, it is clear that LP Token represents the ownership of the fund pool of a transaction pair, and the share of the transaction pair token can be drawn from this fund pool in proportion.
The components of LP Token include two currencies. For example, the LP Token of the ETH/USDT trading pair includes ETH and USDT.
As the composition of the liquidity pool is different, the value of LP Token is also different.
Users need to pay a 0.3% commission for transactions in Uniswap. For example, the user uses dy USDT to buy dx ETH in the ETH/USDT trading pair.
Uniswap will first deduct 0.3%*dy of the user’s principal, and then calculate the dx to the user. After giving the user dx, the 0.3%*dy principal will be injected into the LP Token component, which is equivalent to LP Token Added value.
LP Token will receive all donation proceeds. For example, if the user mistakenly transfers dx ETH to the LP Token contract address of the ETH/USDT pair, dx ETH will become donated to LP Token, increasing the value of LP Token.
Thanks to Plancker Dao member Li Shisheng for his contribution to this article