-
Notifications
You must be signed in to change notification settings - Fork 6
/
libmodel.py
407 lines (345 loc) · 13.6 KB
/
libmodel.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
from collections import defaultdict
from math import log, floor, sqrt
class LendingAMM:
def __init__(self, p_base, A, fee=0, dynamic_fee_multiplier=0.0, use_po_fee=1, po_fee_delay=1):
self.p_base = p_base
self.p_oracle = p_base
self.prev_p_oracle = p_base
self.old_dfee = 0
self.A = A
self.bands_x = defaultdict(float)
self.bands_y = defaultdict(float)
self.active_band = 0
self.fee = fee
self.dynamic_fee_multiplier = dynamic_fee_multiplier
self.use_po_fee = use_po_fee
self.po_fee_delay = po_fee_delay
self.oracle_history = []
# Deposit:
# - above active band - only in y,
# - below active band - only in x
# - transform band prices: p_band = p_oracle**3 / p_base**2
# - add transformation to get_band, get_band_n, deposit_range
# - add y0 ("invariant") changing with p_oracle
# - get_price depending on current state (band, x[band], y[band])
# - trade cross-bands: given dx or dy, calculate the destination band / move
# - fees:
# - collect fees separately for the protocol (wohoo), compensate traded bands
# - reduced_input *= (1 - fee), calc output for reduced_input, split fee * input across bands touched
def set_p_oracle(self, p):
if self.use_po_fee:
if len(self.oracle_history) >= 1:
self.prev_p_oracle = self.oracle_history[-1]
else:
self.prev_p_oracle = self.p_oracle
self.oracle_history.append(p)
else:
self.prev_p_oracle = p
self.p_oracle = p
def dynamic_fee(self, n_band, new=True):
p_up = self.p_up(n_band)
p_down = self.p_down(n_band)
diff1 = p_up - self.p_oracle
diff2 = self.p_oracle - p_down
fee = self.fee
if self.use_po_fee:
if new:
r = min(self.prev_p_oracle, self.p_oracle) / max(self.prev_p_oracle, self.p_oracle)
fee = (self.old_dfee + (1 - r**3)) * (self.po_fee_delay - 1) / self.po_fee_delay
self.old_dfee = fee
fee = max(fee, self.fee)
else:
fee = max(self.old_dfee, self.fee)
if self.dynamic_fee_multiplier > 0:
if (p_down > self.p_oracle) or (p_up < self.p_oracle):
# Fee if not current band
fee = max(fee, min([abs(diff1), abs(diff2)]) / (2 * self.p_oracle) * self.dynamic_fee_multiplier)
# No addition if in the band
return fee
def p_down(self, n_band):
"""
Lower price for the band at the current p_oracle
"""
k = (self.A - 1) / self.A # equal to (p_down / p_up)
p_base = self.p_base * k ** n_band
return self.p_oracle**3 / p_base**2
def p_up(self, n_band):
"""
Upper price for the band at the current p_oracle
"""
k = (self.A - 1) / self.A # equal to (p_down / p_up)
p_base = self.p_base * k ** (n_band + 1)
return self.p_oracle**3 / p_base**2
def p_top(self, n):
k = (self.A - 1) / self.A # equal to (p_down / p_up)
# Prices which show start and end of band when p_oracle = p
return self.p_base * k ** n
def p_bottom(self, n):
k = (self.A - 1) / self.A # equal to (p_down / p_up)
return self.p_top(n) * k
def get_band_n(self, p):
"""
Rounds correct way for both higher and lower prices
"""
k = (self.A - 1) / self.A # equal to (p_down / p_up)
return floor(log(p / self.p_base) / log(k))
def deposit_range(self, amount, p1, p2):
assert p1 <= self.p_oracle and p2 <= self.p_oracle
n1 = self.get_band_n(p1)
n2 = self.get_band_n(p2)
n1, n2 = sorted([n1, n2])
y = amount / (n2 - n1 + 1)
self.min_band = min(n1, n2)
self.max_band = max(n1, n2)
for i in range(n1, n2 + 1):
assert self.bands_x[i] == 0
self.bands_y[i] += y
def deposit_nrange(self, amount, p, dn):
n_top = self.get_band_n(self.p_oracle) + 1
assert p <= self.p_oracle
n1 = max(self.get_band_n(p), n_top)
n2 = n1 + dn - 1
y = amount / dn
self.min_band = n1
self.max_band = n2
for i in range(n1, n2 + 1):
assert self.bands_x[i] == 0
self.bands_y[i] += y
def get_y0(self, n=None):
A = self.A
if n is None:
n = self.active_band
x = self.bands_x[n]
y = self.bands_y[n]
p_o = self.p_oracle
p_top = self.p_top(n)
# solve:
# p_o * A * y0**2 - y0 * (p_top/p_o * (A-1) * x + p_o**2/p_top * A * y) - xy = 0
b = p_top / p_o * (A-1) * x + p_o**2 / p_top * A * y
a = p_o * A
D = b**2 + 4 * a * x * y
return (b + sqrt(D)) / (2 * a)
def get_f(self, y0=None, n=None):
if y0 is None:
y0 = self.get_y0()
if n is None:
n = self.active_band
p_top = self.p_top(n)
p_oracle = self.p_oracle
return y0 * p_oracle**2 / p_top * self.A
def get_g(self, y0=None, n=None):
if y0 is None:
y0 = self.get_y0()
if n is None:
n = self.active_band
p_top = self.p_top(n)
p_oracle = self.p_oracle
return y0 * p_top / p_oracle * (self.A - 1)
def get_p(self, y0=None):
x = self.bands_x[self.active_band]
y = self.bands_y[self.active_band]
if x == 0 and y == 0:
return (self.p_up(self.active_band) * self.p_down(self.active_band)) ** 0.5
if y0 is None:
y0 = self.get_y0()
return (self.get_f(y0) + x) / (self.get_g(y0) + y)
def trade_to_price(self, price):
"""
Not the method to be present in real smart contract, for simulations only
"""
if self.bands_x[self.active_band] == 0 and self.bands_y[self.active_band] == 0:
# If current band is empty - steps are determined by whether current price is higher or lower than
# boundaries
if price > self.p_up(self.active_band):
bstep = 1
elif price < self.p_down(self.active_band):
bstep = -1
else:
return 0, 0
else:
current_price = self.get_p()
if price > current_price:
bstep = 1 # going up: sell
elif price < current_price:
bstep = -1 # going down: buy
else:
return 0, 0
dx = 0
dy = 0
original_price = price
while True:
n = self.active_band
y0 = self.get_y0()
g = self.get_g(y0)
f = self.get_f(y0)
x = self.bands_x[n]
y = self.bands_y[n]
# (f + x)(g + y) = const = p_oracle * A**2 * y0**2 = I
Inv = (f + x) * (g + y)
# p = (f + x) / (g + y) => p * (g + y)**2 = I or (f + x)**2 / p = I
price = original_price
if x == 0 and y == 0:
if price >= self.p_down(n) and price <= self.p_up(n):
break
self.active_band += bstep
continue
fee = self.dynamic_fee(n, new=False)
p_c_d = self.p_down(n)
p_c_u = self.p_up(n)
if bstep == 1: # up
price = price * (1 - fee)
if price < p_c_d:
break
# reduce y, increase x, go up
y_dest = (Inv / price)**0.5 - g
x_old = self.bands_x[n]
if y_dest >= 0:
# End the cycle
self.bands_y[n] = y_dest
self.bands_x[n] = Inv / (g + y_dest) - f
delta_x = self.bands_x[n] - x_old
self.bands_x[n] += fee * delta_x
dx += self.bands_x[n] - x
dy += self.bands_y[n] - y
break
else:
self.bands_y[n] = 0
self.bands_x[n] = Inv / g - f
delta_x = self.bands_x[n] - x_old
self.bands_x[n] += fee * delta_x
self.active_band += 1
else: # down
price = price * (1 + fee)
if price > p_c_u:
break
# increase y, reduce x, go down
x_dest = (Inv * price)**0.5 - f
y_old = self.bands_y[n]
if x_dest >= 0:
# End the cycle
self.bands_x[n] = x_dest
self.bands_y[n] = Inv / (f + x_dest) - g
delta_y = self.bands_y[n] - y_old
self.bands_y[n] += fee * delta_y
dx += self.bands_x[n] - x
dy += self.bands_y[n] - y
break
else:
self.bands_x[n] = 0
self.bands_y[n] = Inv / f - g
delta_y = self.bands_y[n] - y_old
self.bands_y[n] += fee * delta_y
self.active_band -= 1
dx += self.bands_x[n] - x
dy += self.bands_y[n] - y
if abs(n) > 1000:
raise Exception("We should not be here ever")
return dx, dy
def get_y_up(self, n):
"""
Measure the amount of y in the band n if we adiabatically trade near p_oracle on the way up
"""
x = self.bands_x[n]
y = self.bands_y[n]
p_o = self.p_oracle
p_o_up = self.p_top(n)
p_o_down = p_o_up * (self.A - 1) / self.A
p_current_mid = p_o**3 / p_o_down**2 * (self.A - 1) / self.A
sqrt_band_ratio = sqrt(self.A / (self.A - 1))
if x == 0 or y == 0:
if x == 0 and y == 0:
return 0
if p_o > p_o_up:
# all to y at constant p_o, then to target currency adiabatically
y_equiv = y
if y == 0:
y_equiv = x / p_current_mid
return y_equiv
elif p_o < p_o_down:
x_equiv = x
if x == 0:
x_equiv = y * p_current_mid
return x_equiv * sqrt_band_ratio / p_o_up
y0 = self.get_y0(n)
g = self.get_g(y0, n)
f = self.get_f(y0, n)
# (f + x)(g + y) = const = p_top * A**2 * y0**2 = I
Inv = (f + x) * (g + y)
# p = (f + x) / (g + y) => p * (g + y)**2 = I or (f + x)**2 / p = I
# First, "trade" in this band to p_oracle
x_o = 0
y_o = 0
if p_o > p_o_up: # p_o < p_current_down, all to y
# x_o = 0
y_o = max(Inv / f, g) - g
return y_o
elif p_o < p_o_down: # p_o > p_current_up, all to x
# y_o = 0
x_o = max(Inv / g, f) - f
return x_o * sqrt_band_ratio / p_o_up
else:
# y_o__ = max(sqrt(Inv / p_o), g) - g
# x_o__ = max(Inv / (g + y_o__), f) - f
# y_o = self.A * y0 * (1 - p_o_down / p_o)
# x_o = self.A * y0 * p_o * (1 - p_o / p_o_up)
# print('p_o =', p_o, 'p_o_up =', p_o_up, 'p_o_down =', p_o_down)
# print('x', x_o__, '->', x_o)
# print('y', y_o__, '->', y_o)
# print()
y_o = self.A * y0 * (1 - p_o_down / p_o)
x_o = max(Inv / (g + y_o), f) - f
# Now adiabatic conversion from definitely in-band
return y_o + x_o / sqrt(p_o_up * p_o)
def get_x_down(self, n):
"""
Measure the amount of x in the band n if we adiabatically trade near p_oracle on the way up
"""
x = self.bands_x[n]
y = self.bands_y[n]
p_o = self.p_oracle
p_o_up = self.p_top(n)
p_o_down = p_o_up * (self.A - 1) / self.A
p_current_mid = p_o**3 / p_o_down**2 * (self.A - 1) / self.A
sqrt_band_ratio = sqrt(self.A / (self.A - 1))
if x == 0 or y == 0:
if x == 0 and y == 0:
return 0
if p_o > p_o_up:
# all to y at constant p_o, then to target currency adiabatically
y_equiv = y
if y == 0:
y_equiv = x / p_current_mid
return y_equiv * p_o_up / sqrt_band_ratio
elif p_o < p_o_down:
x_equiv = x
if x == 0:
x_equiv = y * p_current_mid
return x_equiv
y0 = self.get_y0(n)
g = self.get_g(y0, n)
f = self.get_f(y0, n)
# (f + x)(g + y) = const = p_top * A**2 * y0**2 = I
Inv = (f + x) * (g + y)
# p = (f + x) / (g + y) => p * (g + y)**2 = I or (f + x)**2 / p = I
# First, "trade" in this band to p_oracle
x_o = 0
y_o = 0
if p_o > p_o_up: # p_o < p_current_down, all to y
# x_o = 0
y_o = max(Inv / f, g) - g
return y_o * p_o_up / sqrt_band_ratio
elif p_o < p_o_down: # p_o > p_current_up, all to x
# y_o = 0
x_o = max(Inv / g, f) - f
return x_o
else:
# y_o = max(sqrt(Inv / p_o), g) - g
# x_o = max(Inv / (g + y_o), f) - f
y_o = self.A * y0 * (1 - p_o_down / p_o)
x_o = max(Inv / (g + y_o), f) - f
# Now adiabatic conversion from definitely in-band
return x_o + y_o * sqrt(p_o_down * p_o)
def get_all_y(self):
return sum(self.get_y_up(i) for i in range(-500, 500))
def get_all_x(self):
return sum(self.get_x_down(i) for i in range(-500, 500))