-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathliquidity.py
89 lines (56 loc) · 2.52 KB
/
liquidity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import numpy as np
def get_amount0(sqrtA,sqrtB,liquidity,decimals):
if (sqrtA > sqrtB):
(sqrtA,sqrtB)=(sqrtB,sqrtA)
amount0=((liquidity*2**96*(sqrtB-sqrtA)/sqrtB/sqrtA)/10**decimals)
return amount0
def get_amount1(sqrtA,sqrtB,liquidity,decimals):
if (sqrtA > sqrtB):
(sqrtA,sqrtB)=(sqrtB,sqrtA)
amount1=liquidity*(sqrtB-sqrtA)/2**96/10**decimals
return amount1
def get_amounts(asqrt,asqrtA,asqrtB,liquidity,decimal0,decimal1):
sqrt=(np.sqrt(asqrt*10**(decimal1-decimal0)))*(2**96)
sqrtA=np.sqrt(asqrtA*10**(decimal1-decimal0))*(2**96)
sqrtB=np.sqrt(asqrtB*10**(decimal1-decimal0))*(2**96)
if (sqrtA > sqrtB):
(sqrtA,sqrtB)=(sqrtB,sqrtA)
if sqrt<=sqrtA:
amount0=get_amount0(sqrtA,sqrtB,liquidity,decimal0)
return amount0,0
elif sqrt<sqrtB and sqrt>sqrtA:
amount0=get_amount0(sqrt,sqrtB,liquidity,decimal0)
amount1=get_amount1(sqrtA,sqrt,liquidity,decimal1)
return amount0,amount1
else:
amount1=get_amount1(sqrtA,sqrtB,liquidity,decimal1)
return 0,amount1
'''get_liquidity function'''
#Use 'get_liquidity' function to calculate liquidity as a function of amounts and price range
def get_liquidity0(sqrtA,sqrtB,amount0,decimals):
if (sqrtA > sqrtB):
(sqrtA,sqrtB)=(sqrtB,sqrtA)
liquidity=amount0/((2**96*(sqrtB-sqrtA)/sqrtB/sqrtA)/10**decimals)
return liquidity
def get_liquidity1(sqrtA,sqrtB,amount1,decimals):
if (sqrtA > sqrtB):
(sqrtA,sqrtB)=(sqrtB,sqrtA)
liquidity=amount1/((sqrtB-sqrtA)/2**96/10**decimals)
return liquidity
def get_liquidity(asqrt,asqrtA,asqrtB,amount0,amount1,decimal0,decimal1):
sqrt=(np.sqrt(asqrt*10**(decimal1-decimal0)))*(2**96)
sqrtA=np.sqrt(asqrtA*10**(decimal1-decimal0))*(2**96)
sqrtB=np.sqrt(asqrtB*10**(decimal1-decimal0))*(2**96)
if (sqrtA > sqrtB):
(sqrtA,sqrtB)=(sqrtB,sqrtA)
if sqrt<=sqrtA:
liquidity0=get_liquidity0(sqrtA,sqrtB,amount0,decimal0)
return liquidity0
elif sqrt<sqrtB and sqrt>sqrtA:
liquidity0=get_liquidity0(sqrt,sqrtB,amount0,decimal0)
liquidity1=get_liquidity1(sqrtA,sqrt,amount1,decimal1)
liquidity=liquidity0 if liquidity0<liquidity1 else liquidity1
return liquidity
else:
liquidity1=get_liquidity1(sqrtA,sqrtB,amount1,decimal1)
return liquidity1