-
Notifications
You must be signed in to change notification settings - Fork 7
/
UNI_v3_funcs.py
124 lines (75 loc) · 3.39 KB
/
UNI_v3_funcs.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 14 18:53:09 2021
@author: JNP
"""
'''liquitidymath'''
'''Python library to emulate the calculations done in liquiditymath.sol of UNI_V3 peryphery contract'''
'''get_amounts function'''
#Use 'get_amounts' function to calculate amounts as a function of liquitidy and price range
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(tick,tickA,tickB,liquidity,decimal0,decimal1):
sqrt=(1.0001**(tick/2)*(2**96))
sqrtA=(1.0001**(tickA/2)*(2**96))
sqrtB=(1.0001**(tickB/2)*(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 token amounts relation'''
#Use this formula to calculate amount of t0 based on amount of t1 (required before calculate liquidity)
#relation = t1/t0
def amounts_relation (tick,tickA,tickB,decimals0,decimals1):
sqrt=(1.0001**tick/10**(decimals1-decimals0))**(1/2)
sqrtA=(1.0001**tickA/10**(decimals1-decimals0))**(1/2)
sqrtB=(1.0001**tickB/10**(decimals1-decimals0))**(1/2)
if sqrt==sqrtA or sqrt==sqrtB:
relation=0
relation=(sqrt-sqrtA)/((1/sqrt)-(1/sqrtB))
return relation
'''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(tick,tickA,tickB,amount0,amount1,decimal0,decimal1):
sqrt=(1.0001**(tick/2)*(2**96))
sqrtA=(1.0001**(tickA/2)*(2**96))
sqrtB=(1.0001**(tickB/2)*(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