-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
421 lines (387 loc) · 12.4 KB
/
app.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import matplotlib.pyplot as plt
import numpy as np
import requests
import json
from shiny import ui, render, App, reactive, Inputs, Outputs, Session
from pandas import json_normalize
from pathlib import Path
# Get Liquidity Pool Data
pool_ids = requests.get("https://node-api.flipsidecrypto.com/api/v2/queries/355406c5-ed57-430d-8940-1bf4d11a64cc/data/latest")
pools = pool_ids.json()
pool_id = json_normalize(pools)
# Import Price Data For Last Month
last30_prices = requests.get("https://node-api.flipsidecrypto.com/api/v2/queries/e35fa300-acdb-44d0-a532-5f0ea2daeeee/data/latest")
price_json = last30_prices.json()
prices = json_normalize(price_json)
app_ui = ui.page_fixed(
ui.tags.head(
ui.tags.style(
(Path(__file__).parent / "style.css").read_text(),
),
),
ui.row(
ui.column(
12,
ui.row(
ui.tags.h2({"class": "title"}, "Osmosis Impermanent Loss Calculator"),
),
ui.row(
ui.tags.strong("How To Use:"),
ui.tags.p("Fill out all the outlined boxes, using negative numbers to denote a decrease in token price. Other values will auto-populate based off the selected pool and other input values. On-chain pool and token pricing data is sourced from Flipside Crypto."),
),
ui.row(
ui.tags.strong("Important:"),
ui.tags.p("This calculator is not meant to be investment or financial advice."),
),
ui.row(
ui.hr()
),
ui.row(
ui.tags.h5({"class": "heading"}, "Input Values"),
),
ui.row(
ui.column(
3
),
ui.column(
3,
ui.input_select("pool", "Select A Pool", pool_id['POOL_ID']),
),
ui.column(
3,
ui.input_numeric("fee", "Percent Pool Fee", value = 0.2, step=0.05)
),
ui.column(
3,
),
),
ui.row(
ui.column(
3,
),
ui.column(
3,
ui.tags.h6({"class": "output_text"}, "Token 1"),
),
ui.column(
3,
ui.tags.h6({"class": "output_text"}, "Token 2"),
),
ui.column(
3,
ui.tags.h6({"class": "output_text"}, "Total"),
),
),
ui.row(
ui.column(
3,
),
ui.column(
3,
ui.output_text_verbatim("symbol1"),
),
ui.column(
3,
ui.output_text_verbatim("symbol2"),
),
ui.column(
3,
),
),
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "# Tokens"),
),
ui.column(
3,
ui.input_numeric("t1", None, value = 1),
),
ui.column(
3,
ui.input_numeric("t2", None, value = 1),
),
ui.column(
3,
),
),
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "% Price Change"),
),
ui.column(
3,
ui.input_numeric("rt1", None, value = -2.0),
),
ui.column(
3,
ui.input_numeric("rt2", None, value = 1),
),
ui.column(
3,
),
),
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "Price (30 Day Avg)"),
),
ui.column(
3,
ui.output_text_verbatim("mov_avg1"),
),
ui.column(
3,
ui.output_text_verbatim("mov_avg2"),
),
ui.column(
3,
),
),
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "USD Value"),
),
ui.column(
3,
ui.output_text_verbatim("usd_val1"),
),
ui.column(
3,
ui.output_text_verbatim("usd_val2"),
),
ui.column(
3,
ui.output_text_verbatim("usd_valt"),
),
),
ui.row(
ui.hr()
),
ui.row(
ui.tags.h5({"class": "heading"}, "Future Values"),
),
ui.row(
ui.column(
3,
),
ui.column(
3,
ui.tags.h6({"class": "output_text"}, "Token 1"),
),
ui.column(
3,
ui.tags.h6({"class": "output_text"}, "Token 2"),
),
ui.column(
3,
ui.tags.h6({"class": "output_text"}, "Total")
),
),
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "# Tokens")
),
ui.column(
3,
ui.output_text_verbatim("f_t1"),
),
ui.column(
3,
ui.output_text_verbatim("f_t2"),
),
ui.column(
3,
),
),
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "USD Value")
),
ui.column(
3,
ui.output_text_verbatim("f_lp1"),
),
ui.column(
3,
ui.output_text_verbatim("f_lp2"),
),
ui.column(
3,
ui.output_text_verbatim("f_lpt")
),
),
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "Hodl Value")
),
ui.column(
3,
ui.output_text_verbatim("f_h1"),
),
ui.column(
3,
ui.output_text_verbatim("f_h2"),
),
ui.column(
3,
ui.output_text_verbatim("f_ht")
),
),
ui.row(
ui.column(
12,
ui.tags.textarea("Powered By Flipside \nMade With ❤️ By @web3_analyst")
),
),
),
),
)
def server(input: Inputs, output: Outputs, session: Session):
@reactive.Calc
async def moving_avg():
if input.pool() == "":
return ""
asset1 = pool_id.loc[int(input.pool()), "ASSET_1"]
asset2 = pool_id.loc[int(input.pool()), "ASSET_2"]
t1a = sum(prices.loc[prices["CURRENCY"] == asset1, "PRICE"]) / 720
t2a = sum(prices.loc[prices["CURRENCY"] == asset2, "PRICE"]) / 720
return t1a, t2a
@reactive.Calc
async def end_values():
moving = await moving_avg()
if input.pool() == "":
return ""
## LP values
lp_val_entry = (input.t1()*moving[0] + input.t2()*moving[1]) / 2
## 50 / 50 token-split on entry
t1_lp = ((input.t1()*moving[0] + input.t2()*moving[1]) / 2) / moving[0]
t2_lp = ((input.t1()*moving[0] + input.t2()*moving[1]) / 2) / moving[1]
# Price Ratios
pr_sup = moving[0] / moving[1]
pr_new = (moving[0]+moving[0]*input.rt1()/100)/(moving[1]+moving[1]*input.rt2()/100)
k = pr_sup / pr_new
il = 2*np.sqrt(k) / (1+k) - 1
# Output Price
p1 = moving[0]+input.rt1()/100*moving[0] # Output Price
p2 = moving[1]+input.rt2()/100*moving[1]
## Hodl Values
t1_hodl = input.t1() * moving[0] * input.rt1() / 100
t2_hodl = input.t2() * moving[1] * input.rt2() / 100
t2_hodl_total = t1_hodl + t2_hodl
return t1_hodl, t2_hodl, t2_hodl_total, lp_val_entry, il, p1, p2
@reactive.Calc
async def usd_values():
moving = await moving_avg()
v1 = moving[0]*input.t1()
v2 = moving[1]*input.t2()
vt = moving[0]*input.t1()+moving[1]*input.t2()
return v1, v2, vt
@reactive.Calc
async def f_lpo():
moving = await moving_avg()
end = await end_values()
tot = moving[0]*input.t1()+moving[1]*input.t2()
amt = tot-(tot*(end[4]))
return amt
@output
@render.text
def token1():
token1 = pool_id.loc[int(input.pool()), "TOKEN_1"]
return f"{token1}"
@output
@render.text
def token2():
token2 = pool_id.loc[int(input.pool()), "TOKEN_2"]
return f"{token2}"
@output
@render.text
def symbol1():
symbol1 = pool_id.loc[int(input.pool()), "SYMBOL_1"]
return f"{symbol1}"
@output
@render.text
def symbol2():
symbol2 = pool_id.loc[int(input.pool()), "SYMBOL_2"]
return f"{symbol2}"
@output
@render.text
async def mov_avg1():
moving = await moving_avg()
return f"${round(moving[0], 3)}"
@output
@render.text
async def mov_avg2():
moving = await moving_avg()
return f"${round(moving[1], 3)}"
@output
@render.text
async def usd_val1():
moving = await moving_avg()
return f"${round(moving[0]*input.t1(), 3)}"
@output
@render.text
async def usd_val2():
moving = await moving_avg()
return f"${round(moving[1]*input.t2(), 3)}"
@output
@render.text
async def usd_valt():
moving = await moving_avg()
return f"${round(moving[0]*input.t1()+moving[1]*input.t2(), 3)}"
@output
@render.text
async def f_lp1():
moving = await usd_values()
end = await end_values()
return f"${round((moving[2]+end[2])/2+(moving[2]+end[2])/2*end[4], 3)}"
@output
@render.text
async def f_lp2():
moving = await usd_values()
end = await end_values()
return f"${round((moving[2]+end[2])/2+(moving[2]+end[2])/2*end[4], 3)}"
@output
@render.text
async def f_lpt():
moving = await usd_values()
end = await end_values()
return f"${round((moving[2]+end[2])+(moving[2]+end[2])*end[4], 3)}"
@output
@render.text
async def f_t1():
moving = await usd_values()
end = await end_values()
return f"{round(((moving[2]+end[2])/2+(moving[2]+end[2])/2*end[4])/end[5], 3)}"
@output
@render.text
async def f_t2():
moving = await usd_values()
end = await end_values()
return f"{round(((moving[2]+end[2])/2+(moving[2]+end[2])/2*end[4])/end[6], 3)}"
#((moving[2]+end[2])/2+(moving[2]+end[2])/2*end[4])
@output
@render.text
async def f_h1():
start = await usd_values()
end = await end_values()
return f"${round(start[0]+end[0], 3)}"
@output
@render.text
async def f_h2():
start = await usd_values()
end = await end_values()
return f"${round(start[1]+end[1], 3)}"
@output
@render.text
async def f_ht():
start = await usd_values()
end = await end_values()
return f"${round(start[2]+end[2], 3)}"
www_dir = Path(__file__).parent
app = App(app_ui, server, debug=True, static_assets=www_dir)