forked from DefiLab-xyz/uniswap-v3-backtest-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliquidity.py
68 lines (48 loc) · 2.18 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
import numpy as np
def get_amount0(sqrtA, sqrtB, liquidity, decimals):
if (sqrtA > sqrtB):
(sqrtA, sqrtB) = (sqrtB, sqrtA)
return ((liquidity*2**96*(sqrtB-sqrtA)/sqrtB/sqrtA)/10**decimals)
def get_amount1(sqrtA, sqrtB, liquidity, decimals):
if (sqrtA > sqrtB):
(sqrtA, sqrtB) = (sqrtB, sqrtA)
return liquidity*(sqrtB-sqrtA)/2**96/10**decimals
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
def get_liquidity0(sqrtA, sqrtB, amount0, decimals):
if (sqrtA > sqrtB):
(sqrtA, sqrtB) = (sqrtB, sqrtA)
return amount0/((2**96*(sqrtB-sqrtA)/sqrtB/sqrtA)/10**decimals)
def get_liquidity1(sqrtA, sqrtB, amount1, decimals):
if (sqrtA > sqrtB):
(sqrtA, sqrtB) = (sqrtB, sqrtA)
return amount1/((sqrtB-sqrtA)/2**96/10**decimals)
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:
return get_liquidity0(sqrtA, sqrtB, amount0, decimal0)
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:
return get_liquidity1(sqrtA, sqrtB, amount1, decimal1)