-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathposition.py
128 lines (105 loc) · 3.54 KB
/
position.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
125
126
127
128
import numpy as np
from uniswap_simulator.liquidity_amounts import liquidity_for_amounts, amounts_for_liquidity
class Position:
def __init__(self, price, lower, upper, fee):
self._price_sqrt = np.sqrt(price)
self._lower_sqrt = np.sqrt(lower)
self._upper_sqrt = np.sqrt(upper)
self._fee = fee
self._liquidity = np.zeros_like(price)
self._earned = None
def reset(self, price):
self._price_sqrt = np.sqrt(price)
self._liquidity = np.zeros_like(price)
self._earned = None
@property
def _price(self):
return np.square(self._price_sqrt)
@property
def lower(self):
return np.square(self._lower_sqrt)
@property
def upper(self):
return np.square(self._upper_sqrt)
@property
def fee(self):
return self._fee
@property
def amounts(self):
return self._earned + amounts_for_liquidity(
self._price_sqrt,
self._lower_sqrt,
self._upper_sqrt,
self._liquidity
)
@property
def collectable(self):
return self._earned
def update(self, price):
# If price movement is less than fee, it's not guaranteed that the AMM will
# be arb'd to match new price
should_update = np.any((
price / self._price > 1 / (1 - self._fee),
price / self._price < 1 - self._fee
), axis=0)
price[~should_update] = self._price[~should_update]
price_sqrt = np.sqrt(price)
amounts_previous = amounts_for_liquidity(
self._price_sqrt.clip(min=self._lower_sqrt, max=self._upper_sqrt),
self._lower_sqrt,
self._upper_sqrt,
self._liquidity
)
amounts_current = amounts_for_liquidity(
price_sqrt.clip(min=self._lower_sqrt, max=self._upper_sqrt),
self._lower_sqrt,
self._upper_sqrt,
self._liquidity
)
diff = amounts_current - amounts_previous
mask = diff[..., 0] > 0
if self._earned is None:
self._earned = np.zeros_like(diff)
self._earned[mask, 0] += diff[mask, 0] * self._fee
self._earned[~mask, 1] += diff[~mask, 1] * self._fee
self._price_sqrt = price_sqrt
return self.amounts
def mint(self, amount0, amount1):
liquidity = liquidity_for_amounts(
self._price_sqrt,
self._lower_sqrt,
self._upper_sqrt,
amount0,
amount1
)
self._liquidity += liquidity
return amounts_for_liquidity(
self._price_sqrt,
self._lower_sqrt,
self._upper_sqrt,
liquidity
)
def burn(self, fraction=1.0):
liquidity_to_burn = self._liquidity * fraction
self._liquidity -= liquidity_to_burn
burned = amounts_for_liquidity(
self._price_sqrt,
self._lower_sqrt,
self._upper_sqrt,
liquidity_to_burn
)
earned = self._earned * fraction
self._earned -= earned
return burned + earned
def burn_at(self, mask, fraction=1.0):
liquidity_to_burn = self._liquidity * fraction * mask
self._liquidity -= liquidity_to_burn
burned = amounts_for_liquidity(
self._price_sqrt,
self._lower_sqrt,
self._upper_sqrt,
liquidity_to_burn
)
earned = self._earned * fraction * mask[..., np.newaxis]
self._earned -= earned
return burned + earned