# Uniswap 学习笔记

By [June](https://paragraph.com/@june-3) · 2022-01-27

---

Uniswap v1
----------

*   Constant Product Market Maker
    
*   ETH/ERC20， ETH bridge currency
    

任意两个数量的资产价格只取决于数量（quantities）本身，而不取决于它们之间的路径。这提供了两个重要的好处: (path dependence vs. path independence)

1.  由于交易者在单次交易中获得的价格与在一组小量交易中获得的价格相同，交易者不需要为其制定交易策略
    
2.  它尽可能地给出了一项资产的状态：我们只需要知道资产的数量就可以为其定价
    

Uniswap v2
----------

*   Arbitrary ERC20/ERC20
    
*   price oracle ==> keep track of the cumulative sum of prices at the beginning of each block in which someone interacts with the contract. ==> time-weighted average price
    
*   track price a/b and b/a at the same time
    
*   [Impermanent loss calculator](https://dailydefi.org/tools/impermanent-loss-calculator/)
    

### Understanding math

x, y represent the quantity of token x and y.

    x * y = k = L^2
    

how to get the Price of token x denominated in token y

    y = L^2 / x
    dy = - L^2 / x^2 dx
    ==> 
    P = - dy / dx = - (- L^2 / x^2 dx) / dx = L^2 / x^2 = y / x
    ==>  
    x = L / sqrt(P)
    y = L * sqrt(P)
    

![](https://storage.googleapis.com/papyrus_images/9cd872eacf1312a509cd3b210eb44259540d8c19118dc2d1f224b1a6662820fe.png)

### Impermanent Loss

    w10: wealth at time t1 if participate as UNISWAP LP at time t0 
    w11: wealth at time t1 if hold half asset x and half asset y at time t0
    p(t1): price for x at time t1 
    p(t0): price for x at 
    time t0
     
    !!! all the wealth are denominated in y. 
    
    w10 = p(t1) * x1 + y1 
        = p(t1) * L / sqrt(p(t1)) + y1
        = L * sqrt(p(t1)) + y1
        = 2 * L * sqrt(p(t1))
    
    Assume r = p(t1) / p(t0)
    
    w11 = p(t1) * x0 + y0 【the quantities remain the same】
        = p(t1) * L / sqrt(p(t0)) + y0
        = r * P(t0) * L / sqrt(p(t0)) + y0 
        = r * sqrt(p(t0)) * L + sqrt(p(t0)) * L
        = (1 + r) * sqrt(p(t0)) * L
     
    IL = w10 / w11 - 1   
       = 2 * L * sqrt(p(t1)) / ((1 + r) * sqrt(p(t0)) * L) - 1
       = 2 * sqrt(p(t1)) / ((1 + r) * sqrt(p(t0))) - 1 
       = 2 * sqrt(r) / (1 + r) - 1 
    

If we only care about the initial investment in the value of y,

    w00: wealth at time t0 (in token y)
    w12: wealth at time t1 if hold all asset y at time t0
    
    w12 = w00 
        = p(t0) * x0 + y0
        = p(t0) * L / sqrt(P(t0)) + L * sqrt(P(t0))
        = 2 * L * sqrt(p(t0))
    
    Assume r = p(t1) / p(t0)
    
    IL = w10 / w12 - 1   
       = 2 * L * sqrt(p(t1)) / (2 * L * sqrt(p(t0))) - 1
       = sqrt(p(t1)/p(t0))- 1 
       = sqrt(r) - 1
    

If we only care about the initial investment in the value of x,

    w13: wealth at time t1 if hold all asset x at time t0
    
    w13 = p(t1) * (2 * x0) [hold 2*x0 all time]
        = 2 * p(t1) * L / sqrt(P(t0))
        = 2 * r * P(t0) * L / sqrt(P(t0))
        = 2 * L * r * sqrt(P(t0))
    
    IL = w10 / w13 - 1   
       = 2 * L * sqrt(p(t1)) / (2 * L * r * sqrt(p(t0))) - 1
       = sqrt(r) / r - 1 
       = 1 / sqrt(r) - 1
    

![Uniswap v2 -- Impermanent loss](https://storage.googleapis.com/papyrus_images/79f68e8399c3855acb85ec702b5d3c8a63c7de6252de70ee9ee161ec8ad94987.png)

Uniswap v2 -- Impermanent loss

[https://docs.google.com/spreadsheets/d/1ZBS1IYeZLP-qioZhEi3FaLHX2j1BmHaI2lB2uI3JjsQ/edit?usp=sharing](https://docs.google.com/spreadsheets/d/1ZBS1IYeZLP-qioZhEi3FaLHX2j1BmHaI2lB2uI3JjsQ/edit?usp=sharing)

If we only care about the initial investment in the value of y, and we participate the pool and hedge x0 at time t0【we don’t consider the hedging fees here】

    w00: wealth at time t0 (in token y)
    w12: wealth at time t1 if hold all asset y at time t0
    
    k: hedge ratio in terms of x0 
    w14: wealth at time t1 if participate pool and hedge k * x0 at time t0 
    

![wealth at time t1 with hedge](https://storage.googleapis.com/papyrus_images/f7ead47fc0d51be4702b9cb3e18748a4e0731b8a8f258c32f37737d035dd317c.png)

wealth at time t1 with hedge

![Impermanent loss ](https://storage.googleapis.com/papyrus_images/8dedc264fae30cc5ea693e5161fe7d947a135e347ca40f20b5a56e409a2b0725.png)

Impermanent loss

### Conclusion

The loss varies depending on how investors think of their initial deposit. For example, x=BTC, y=USDT, investors who originally hold all their position in BTC and think of possible downtrend may provide liquidity in this case and actually benefit from the decline of BTC price compared to simply holding all BTC. But still, the liquidity provider has some exposure to the upside compared to converting all BTC to USDT. Meantime, the LP generates profit from transactions.

For investors who seek only capital gains in USDT (y here), becoming liquidity providers could be beneficial, as long as they employ some hedge.

Uniswap v3
----------

*   concentrated liquidity ==> increased capital efficiency
    
*   smaller range ==> higher fees
    
*   range order ==> limit order
    
*   in range ==> fees; not in range ==> no fees
    

If the current price = Pc, we have x quantity of asset X and y quantity of asset Y in the pool.

In v2, if the price moves between Pa and Pb, only x\_real and y\_real are utilized. The quantity (x- x\_real) and (y - y\_real) are never touch. Given that in mind, we only need to keep x\_real and y\_real in that pool to provide the same amount of liquidity instead of having x and y (larger than x\_real and y\_real).

![](https://storage.googleapis.com/papyrus_images/f1cd3eddcf6966050ff5e6adc16df507767e5cc3debbfa41529634341ca5146d.png)

In the graph below, the green line and the orange line have the same liquidity, but the green line need x, y, the orange line only needs x\_real and y\_real. Why we put x and y if we only need x\_real and y\_real?

![](https://storage.googleapis.com/papyrus_images/0126aecef3bf9d3c779f5071142f3cbad218c7cd8f70fc4315751c7f11b0e202.png)

    what's new:
    Pa: low range for x
    Pb: up range for x
    Pc: current price for x 
    Pa < Pc < Pb 
    
    x_v: virtual reserve for x 
    y_v: virtual reserve for y 
    L: virtual liquidity 
    
    (x + L / sqrt(Pb)) * （y + L * sqrt(Pa)） = L^2 
    
    x_v * y_v =  L^2 
    x_v = x + L / sqrt(Pb)
    y_v = y + L * sqrt(Pa)
    
    x = x_v -  L / sqrt(Pb)
      = L / sqrt(Pc) - L / sqrt(Pb)
    
    y = y_v - L * sqrt(Pa)
      = L * sqrt(Pc) - L * sqrt(Pa)
    

### Impermanent Loss

    w10: wealth at time t1 if participate as UNISWAP LP at time t0 
    w11: wealth at time t1 if hold half asset x and half asset y at time t0
    
    Assume r = p(x1) / p(x0)
    

![](https://storage.googleapis.com/papyrus_images/f86d9173b80b017d02a02ad85e9fea322f9914fbf943c01e01aa319fddc93370.png)

![](https://storage.googleapis.com/papyrus_images/accc9ec17caa63261ad2514925974f43ab7ae38f774f7dd44dd3f70b266e3d54.png)

![](https://storage.googleapis.com/papyrus_images/4742d0ebca7adfc96f1c32a9b5ba5c42dcb18b068ff8267a81ba65688822dcfe.png)

[https://medium.com/bollinger-investment-group/constant-function-market-makers-defis-zero-to-one-innovation-968f77022159](https://medium.com/bollinger-investment-group/constant-function-market-makers-defis-zero-to-one-innovation-968f77022159)

[https://docs.uniswap.org/protocol/V2/concepts/advanced-topics/understanding-returns](https://docs.uniswap.org/protocol/V2/concepts/advanced-topics/understanding-returns)

[https://myself659.github.io/post/blockchain/uniswap-impermanent-loss-formula/](https://myself659.github.io/post/blockchain/uniswap-impermanent-loss-formula/)

[https://www.fxajax.com/20210625223438.html](https://www.fxajax.com/20210625223438.html)

[https://medium.com/auditless/impermanent-loss-in-uniswap-v3-6c7161d3b445](https://medium.com/auditless/impermanent-loss-in-uniswap-v3-6c7161d3b445)

---

*Originally published on [June](https://paragraph.com/@june-3/uniswap)*
