0% found this document useful (0 votes)
31 views

Translated Uniswap v3 Liquidity Math

Uploaded by

nalioo10088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Translated Uniswap v3 Liquidity Math

Uploaded by

nalioo10088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNISWAP V3

Atis Elsts atis.e


[email protected]

2021 9 30

Uniswap (DEX) (DeFi) Uniswap


(AMM) Uniswap v3
concentrated liquidity

Uniswap v3

Uniswap v3 [1] [2 4]
Uniswap

P X Y
P X x Y
X x y Y P

1
Uniswap v3

2.1

[1] 6.5 6.6 L x y


√ yvirtual
L = xvirtual · P = √
P
1
For the accompanying example code, see https://github.com/atiselsts/uniswap-v3-liquidity-math/blob/
master/uniswap-v3-liquidity-math.py. Note that the code is meant to illustrate the math: it’s not directly suitable for
use in real projects!
L IQUIDITY M ATH IN U NISWAP V 3

1
Symbol name Whitepaper Uniswap code Notes

Price P sqrtRatioX96 Code tracks P
for efficiency reasons

Lower bound of a price range pa sqrtRatioAX96 Code tracks √pa
Upper bound of a price range pb sqrtRatioBX96 Code tracks pb
The first asset X token0
The second asset Y token1
Amount of the first asset x amount0
Amount of the second asset y amount1
Virtual liquidity L liquidity,
amount

x y virtual x y
6.29 6.30 LiquidityAmounts.sol2

2.2
L √
(xreal + √ )(yreal + L pa ) = L2
pb
2.2 L
X Y

1. P ≤ pa X y=0
L √
(x + √ )L pa = L2 (1)
pb

√ pa
x pa + L √ = L (2)
pb
L L
x= √ −√ (3)
pa pb
√ √
pb − pa
x=L √ √ (4)
pa · pb

√ √
pa · pb
L = x√ √ (5)
pb − pa

2. P ≥ pb Y x=0
L √
√ (y + L pa ) = L2 (6)
pb

y pa
√ + L √ =L (7)
pb pb
√ √
y = L( pb − pa ) (8)

y
L= √ √ (9)
pb − pa
2https://github.com/Uniswap/uniswap-v3-periphery/blob/main/contracts/libraries/LiquidityAmounts.sol#L120

2
L IQUIDITY M ATH IN U NISWAP V 3

3. pa < P < pb
x P, pb Lx y pa , P
Ly
5 9 P (pa , pb (
P, pb) X (pa , P ) Y 5 9
Lx (P, pb ) = Ly (pa , P )
√ √
P· pb y
x√ √ =√ √ (10)
pb − P P − pa
10 x, y, P, pa , pb x y
4 8
√ √
pb − P
x=L √ √ (11)
P · pb
√ √
y = L( P − pa ) (12)

P ≤ pa y=0 x 4 P ≥ pb x=
0 y 8 pa < P < pb x y 1
1 12

6.29 6.30 ∆

4-12 6.29 6.30


∆L = L

2.2

2.2.1
P both pa pb 10

P x y pa pb P x y pb pa P
x y z% pb pa %
pb pa
P P

pa < P < p b
L 12 11 pa pb

√ √
y
pa = P− (13)
√L
√ P
pb = L √ (14)
L− P ·x
3
The Uniswap source code calculates both Lx and Ly and returns the minimum of them: L = min(Lx , Ly ). Essentially, this
forces the position to be balanced, if it was unbalanced for whatever reasons – either because the user provided an incorrect amount
of x or y, or because of rounding errors that are likely if the current price is close to one of the boundaries of the range.

3
L IQUIDITY M ATH IN U NISWAP V 3

10 pa pa
√ y √ y
pa = √ + P−√ (15)
pb · x P ·x

√ Py
pb = √ √ (16)
pa P x − P x + y
2.2.2
c d c2 P
d2 P r
def pb
c= (17)
P
r
def pa
d= (18)
P

√ √
pb
P = (19)
√ √c
pa
P = (20)
d√

pb = c · P (21)
√ √
pa = d · P (22)
10
y
c= (23)
(d − 1) · P · x + y
(1 − c) · y
d=1+ (24)
c·P ·x
pa 70% c2 70% = 0.7 24 d2
pb

3
3.1

[1]
Ticks. Uniswap (tick) i p(i) = 1.00
i
01 Tick spacing.
1% 200 0.
3% 60 0.05% 10 Fixed point math. Uniswap v
4
3 Solidity
Decimals ERC20

ERC20 10decimals ERC20


DAI 18 USDC 6 1 USDC
1 DAI 1 USDC DAI 1018−6 = 1012

4 Uniswap JavaScript JSBI


Python 3.8

4
L IQUIDITY M ATH IN U NISWAP V 3

3.2

Uniswap U
I

3.2.1 1
Problem: x = 2 ETH ETH/USDC ETH P =2000 USD
C pa = 1500 pb = 2500 USDC USDC (y)

Solution: 5 pa P
√ √
P · pb
Lx = x √ √
pb − P

L 8 y
√ √
y = Lx ( P − pa )

y =5076.10 USDC

3.2.2 2
Problem: x = 2 ETH y = 4000 USDC pb = 3000 USDC/ETH
(pa)
Solution: 1 pa
15
y √ y
pa = ( √ + P−√ )2
pb · x P ·x

pa = 1333.33 USDC
USDC ETH

3.2.3 3
Problem: 2 ETH P = 2500 USDC

Solution: pa = 1333.33
√ √

pb
Lx = x √ √
pb − P
y
Ly = √ √
P − pa

64 Lx = 487.41718030204123 Ly = 487.4144693682443 L
L = min(Lx , Ly ) Lx
P 0 = 2500 4 8 x0 y0
√ √
0 pb − P 0
x =L √ √
P 0 · pb
√ √
y 0 = L( P 0 − pa )

x = 0.85 ETH y = 6572.89 USDC

5
L IQUIDITY M ATH IN U NISWAP V 3

6.14 6.16
√ √ √
∆ P = P0 − P
1 1 1
∆√ = √ − √
P P 0 P
1
∆x = ∆ √ · L
P

∆y = ∆ P · L
x0 = x + ∆x
y 0 = y + ∆y

∆x = −1.15 ETH ∆y = +2572.89 USDC

3.3 s

3.3.1 Uniswap v3
5
Tick Uniswap v3 API Uniswap v3
[1] Tick Uniswap v3 ticks
Tick tick base Uniswap 1.0001 i tick

p(i) = 1.0001i

i = log1.0001 p(i)

p
p(i) = 1.0001i/2
p
i = 2 · log1.0001 p(i)

3.3.2
y x UI X Y

Uniswap v3 NFT ID 600006 NFT 1 NFT


200240 200700

pa = 496452748.01
pb = 519821773.17

USDC X decimalsx = 6 ETH Y


y
decimalsy = 18 x
y x y 10decimalsx y
padjusted = / = · = · 10x−y = p · 10x−y
10decimalsy 10decimalsx x 10decimalsy x

496452748.01
padjusteda = 496452748.01 · 106−18 = = 0.00049645274801
1012
519821773.17
padjustedb = 519821773.17 · 106−18 = = 0.00051982177317
1012
5
https://thegraph.com/legacy-explorer/subgraph/uniswap/uniswap-v3
6
https://app.uniswap.org/#/pool/60000

6
L IQUIDITY M ATH IN U NISWAP V 3

1 Uniswap NFT

f ETH USDC USDC ETH

1
= 2014.29
padjusteda
1
= 1923.74
padjustedb

3.3.3

Uniswap v2 Uniswap v3

7
The Graph The Graph
0.3% USDC/ETH
{ {id 0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8 } {
}}

L = 22402462192838616433 tick = 195574 3211.84 USDC/ETH

The full code of this example can be found at https://github.com/atiselsts/uniswap-v3-liquidity-math/blob/


7

master/subgraph-liquidity-query-example.py

7
L IQUIDITY M ATH IN U NISWAP V 3

0.3% 60 60

tickbottom = 195540
ticktop = 195600

pa = 1.0001tickbottom = 1.0001195540
pb = 1.0001ticktop = 1.0001195600

11 12 USDC ETH

√ √
pb − P
x=L √ √ = 1649346952148
P · pb
√ √
y = L( P − pa ) = 671393300975203516416

x
xadjusted = ≈ 1 649 347
106
y
yadjusted = 18 ≈ 671.39
10
165 671 ETH 165
671 ETH

Uniswap Discord

[1] H. Adams N. Zinsmeister M. Salem R. Keefer D. Robinson Uniswap v3 Core Uniswap Labs
2021 [2] M. Neuder R. Rao D. J. Moroz D. C. Parkes Uniswap v3
arXiv preprint arXiv:2106.12033 2021 [3] J. Clark
Available at SSRN 2021 [4] J. Yin M. Ren Uniswap v3
arXiv preprint arXiv:2108.05800 2021

You might also like