ISheep

ISheep

Badminton | Coding | Writing | INTJ
github

AMM Algorithm

AMM, the principle of Defi, automated market maker, compared to traditional exchange market makers, the quotes of buyers and sellers are completely determined by machines.

The role of market makers is to provide liquidity to a market. Market makers primarily make money through three methods: designated market making arrangements (traditionally paid by asset issuers), transaction fee rebates (traditionally paid by exchanges), and earning spread from market making (transaction fees in Dex).

AMM Principle#

In fact, AMM can be explained with just one function

xy=kx*y=k

which is an inverse proportion function.

Liquidity Impact#

The so-called constant product algorithm means that in the absence of adding or reducing liquidity to the liquidity pool, the value of k remains unchanged only when transaction behavior occurs.

Several situations that affect the value of k:

  1. Transaction fees affect k

    For example, Uniswap transactions require a fee of 0.3%, and after the transaction is completed, the fee will be added to the liquidity pool.

  2. Adding liquidity affects k

    If there is xETH/yUSDT in the liquidity pool, where K=x*y. Now someone adds liquidity, increasing dx ETH and dy USDT, and dx/dy=x/y.

    If you use the default setting of Uniswap, which is to input the quantity of ETH first when adding liquidity, then Uniswap will calculate the required amount of USDT, and this default calculation is to maintain dx/dy=x/y.

    However, after adding liquidity, the funds in the liquidity pool become: (x+dx)ETH/(y+dy)USDT. At this time, (x+dx)(y+dy)>x*y. So adding liquidity increases the value of K.

    Similarly, reducing liquidity will decrease the value of K.

  3. Donations affect k

    If you add liquidity by directly transferring dx>0 to the contract address instead of following the dx/dy=x/y ratio, it is also possible. This will also change the value of K.

    This kind of thing happens frequently, where some people mistakenly transfer ETH to a Uniswap liquidity pool contract address, resulting in a donation, giving away coins to all original liquidity providers for free.

    This behavior of adding liquidity not in proportion to K can be exploited by others. For example, if someone deposits dx>0 and dy=0, another person can deposit dy according to the K ratio and perform an add liquidity operation to take away the dx donation. This is quite complicated, and I don't understand the details.

    Any liquidity added not in proportion to K will result in a donation. For example, if dx/dy>x/y, first add (dx-ddx)/dy=x/y liquidity according to the K ratio, and return the corresponding amount of LP tokens to the user. Then ddx becomes a donation.

    Also, in the same block, there may be transactions that add liquidity and buy/sell, which will cause more complex changes in K.

IDO#

Taking PancakeSwap as an example, all calculations ignore transaction fees.

Suppose I have issued a token named $SHEEP on the BSC chain, and I put SHEEP/BNB as a trading pair into the liquidity pool, with 5000 SHEEP and 100 BNB in the pool. At this time, the IDO price of my SHEEP is determined by me.

Assuming BNB/USDT=500, then

  • SHEEP/BNB=0.02
  • SHEEP/USDT=10

That is, one SHEEP is equal to 10U.

According to the formula, we get k=500000, and draw the curve. Since the slope is not constant, each transaction will affect the price of the token.

In this scenario, there will be three roles:

  1. Liquidity Providers (A)
  2. People who actually need to buy storage services (B)
  3. Arbitrageurs/Speculators (C)

PancakeSwap will reward liquidity providers with transaction fees (so there is no need to worry about no one providing liquidity), and the calculation formula is

Transaction Volume * 0.3% * Proportion of Provided Liquidity in the Pool

At this point, A thinks that my project has good prospects and expects a large number of people to trade, so he decides to provide some liquidity. He puts 500 SHEEP and 10 BNB into the pool (assuming 500 SHEEP is obtained through airdrop), so A occupies about 9% of the pool liquidity (10/110), and now there are 5500 tokens in the pool. (If there are large players entering later, this proportion will become smaller and smaller) (To ensure that the value of k remains unchanged, the case of providing liquidity is removed, but the principle is the same)

B doesn't trust centralized storage services and wants to try my project, so he buys 500 SHEEP. Since k is constant, x=4500, then y=500000/4500=111.1, so he needs to pay 11.1 BNB to buy 500 SHEEP. At this time, there are 4500 SHEEP and 111.1 BNB in the pool.

  • SHEEP/BNB=0.024689
  • SHEEP/USDT=12.34

This means that the price of SHEEP has increased. At this time, C sees that the price has risen, so he sells the 500 SHEEP obtained through airdrop, and the pool returns to its original state.

It must be understood that this transaction is a dynamic process reflected in the curve. Arbitrageurs usually do not place large orders at once.

Calculation Method for Exchange Price#

Assuming the quantity of DAI and ETH before the transaction is X and Y, and after the transaction is X' and Y', the transaction follows the rule X*Y=k=X'*Y', where k remains constant.

So how much DAI can get how much ETH

AssumingthatΔxDAIisexchangedforΔyETH,thenPtransaction=ΔxΔy=Poriginal×(1+)Assuming that Δx DAI is exchanged for Δy ETH, then P transaction=\frac{\Delta x}{\Delta y} =P original\times (1+\partial )

According to the formula, the actual transaction price is directly proportional to ∂ (where ∂ means the proportion of DAI paid to the DAI in the pool, 0<∂). That is, the larger the transaction amount, the larger ∂, and the price is a linear function that increases proportionally with the proportion of the payment currency. ∂=△x/X

Calculation Method for Market Price after Exchange#

Market Price=Quantity of DAI in the pool/Quantity of ETH in the pool (P market=X/Y). Assuming that the market quantity tends to infinity, the exchange price tends to X/Y.

Exchange Price=Quantity of DAI paid/Quantity of ETH obtained (P exchange=△x /△y)

Since user transactions will change the quantity of DAI and ETH in the pool, the market prices before and after the transaction will change. In other words, traders will change the market price. The formula for the market price after the transaction is:
According to

XY=XY=(X+Δx)(YΔy)Δy=YΔxX+ΔxX*Y=X^{'} * Y^{'}=(X+\Delta x) *(Y-\Delta y)\Delta y=Y*\frac{\Delta x}{X+\Delta x}
Δy=YΔxX+Δx\Delta y=Y*\frac{\Delta x}{X+\Delta x}

It can be obtained that the market price after purchasing ETH is

XY=1XY(X+Δx)2=XY(1+)2\frac{X^{'} }{Y^{'}} =\frac{1}{X*Y}*(X+\Delta x)^{2} =\frac{X}{Y}*(1+\partial ) ^{2}

The final calculation formula is

Pexchangeafter=ΔxΔy=Poriginal×(1+)2P exchange after=\frac{\Delta x}{\Delta y} =P original\times (1+\partial )^{2}

Impermanent Loss#

It is basically the loss of income from the price increase of the token after providing liquidity, compared to holding the entire amount of the token.

Reference articles:

  1. Pintail
  2. Bancor
  3. Why Automated Market Makers May Lose Money

Slippage#

Slippage refers to the phenomenon where the trading price at the time of placing an order differs from the actual trading price.

Uniswap#

Version 3 seems to have many updates, you can refer to the whitepaper

Defi in 2022#

The Defi frenzy has been going on for a year, and I missed it. I can't say it's a missed opportunity, after all, 7000% annualized return is not available every day, and the risk is high. I can only say that I missed the opportunity to lose money. Defi will certainly not occupy a large share of the heat in 2022, after all, it is just a financial tool (although decentralized financial derivatives already exist). Defi platform tokens will always face a large amount of selling pressure from liquidity providers, especially after experiencing DYDX, I no longer plan to buy any market-making tokens. 2022 will definitely be the year when Web3 truly comes into play, starting with infrastructure construction.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.