ISheep

ISheep

Badminton | Coding | Writing | INTJ
github

AMM Algorithm

AMM, the principle of Defi, is an automated market maker where machine determines the quotes for buyers and sellers, as opposed to traditional exchange market makers.

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), trading 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 in the liquidity pool, the value of k remains unchanged only when there is a transaction.

Several scenarios that affect the value of k:

  1. Transaction fees affect k value

    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 value

    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, where you input the quantity of ETH when adding liquidity, and Uniswap will calculate the corresponding quantity of USDT needed, 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 value

    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 the 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 the donation.

    Also, in the same block, there may be transactions that add liquidity as well as buy/sell, which will result in more complex changes in K value.

IDO#

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

Suppose I have released 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.

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, three roles will appear.

  1. Liquidity Provider (A)
  2. Real buyers who need to purchase storage services (B)
  3. Arbitrageurs/Speculators (C)

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

Trade 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 he obtained 500 SHEEP through an airdrop), so A occupies approximately 9% of the pool's liquidity, 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 the value of k remains unchanged, the case of providing liquidity is removed, but the principle remains 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 point, C sees the price increase and sells the 500 SHEEP obtained through the airdrop, so the pool returns to its original state.

It must be understood that this transaction is a dynamic process reflected on 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 be exchanged for how much ETH?

AssumingxDAIisexchangedforyETH,thenPtrade=ΔxΔy=Poriginal×(1+)Assuming △x DAI is exchanged for △y ETH, then P_trade=\frac{\Delta x}{\Delta y} =P_original\times (1+\partial )

According to the formula, the actual transaction price is directly proportional to ∂ (where ∂ represents the proportion of DAI paid in the pool, 0<∂). That is, the larger the transaction volume, 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 the quantity in the market 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 price 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 derived 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 returns are not available every day, and the risks are high. I can only say that I missed the opportunity to lose money. DeFi will definitely not dominate the hype 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.