-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
546 lines (495 loc) · 18.3 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
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 - Current
token_price = requests.get("https://api.flipsidecrypto.com/api/v2/queries/d539a543-59c8-48e9-a60a-bc3b54998eea/data/latest")
prices = token_price.json()
price = json_normalize(prices)
## Import Price Data - Last 14 Days
tp = requests.get("https://api.flipsidecrypto.com/api/v2/queries/ca9e5cd1-410d-4577-a84c-4958a18c547c/data/latest")
p = tp.json()
pp = json_normalize(p)
## Import Pool Depth Information
poold = requests.get("https://api.flipsidecrypto.com/api/v2/queries/a7caa828-df39-4216-9a4f-cbfc9048c26b/data/latest")
pool_depth = poold.json()
pool_depth = json_normalize(pool_depth)
app_ui = ui.page_fixed(
ui.tags.head(
ui.tags.style(
(Path(__file__).parent / "style.css").read_text(),
),
ui.tags.script(
(Path(__file__).parent / "column-height.js").read_text(),
),
),
## Title / Heading
ui.tags.div({"class": "head"},
ui.row(
ui.column(
1,
ui.tags.div({"class": "title_icon"})
),
ui.column(
10,
ui.tags.h1({"class": "title"}, "Osmosis Swap Simulator")
),
ui.column(
1,
),
),
),
## Line Break
ui.row(
ui.hr()
),
## Row With Main Content
ui.row(
ui.tags.div({"class": "double-col"}, {"id": "group"},
## Pool Information Column
ui.column(
6,
ui.tags.div({"class": "col-right"},
ui.row(
ui.tags.h5({"class": "heading"}, "Pool Information"),
),
ui.row(
ui.tags.p({"class": "instructions"}, "Search: click dropdown > backspace/ delete > type")
),
# Pool Info Input Values
ui.row(
ui.column(
3,
),
ui.column(
6,
ui.input_select("pool", "Pool", pool_id['POOL_NAME']),
),
ui.column(
3,
),
),
# Pool Fee Input Values
ui.row(
ui.column(
3,
),
ui.column(
6,
ui.input_numeric("pool_fee", "Percentage Swap Fee", value=0.2),
),
ui.column(
3,
),
),
## Token 1 & Token 2 Labels
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,
),
),
## Output the Tokens in the Pool
ui.row(
ui.column(
3,
),
ui.column(
3,
ui.output_text_verbatim("symbol0"),
),
ui.column(
3,
ui.output_text_verbatim("symbol1"),
),
ui.column(
3,
),
),
## Get Current Token Price
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "Token Price"),
),
ui.column(
3,
ui.output_text_verbatim("price0"),
),
ui.column(
3,
ui.output_text_verbatim("price1"),
),
ui.column(
3,
),
),
## Last block updates
ui.row(
ui.column(
12,
ui.output_text_verbatim("last_time"),
),
),
## Last block
ui.row(
ui.column(
12,
ui.output_text_verbatim("last_block"),
),
),
## Line Break
ui.row(
ui.hr()
),
## Section Title
ui.row(
ui.tags.h5({"class": "heading"}, "Swap Parameters"),
),
ui.row(
ui.tags.p({"class": "instructions"}, "Type token symbol and amount in boxes below")
),
## Input Token In
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "Token In"),
),
ui.column(
3,
ui.input_text("t_in", None, value = 'ATOM'),
),
ui.column(
3,
ui.input_numeric("t_in_amt", None, value = 1),
),
ui.column(
3,
),
),
## Token Out
## Input Number Of Tokens
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "Token Out"),
),
ui.column(
3,
ui.output_text_verbatim("t_out"),
),
## Need to change this to output
ui.column(
3,
ui.output_text_verbatim("t_out_amt"),
),
ui.column(
3,
),
),
## Slippage Losses
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "Slippage"),
),
ui.column(
3,
ui.output_text_verbatim("t_slip"),
),
ui.column(
3,
ui.output_text_verbatim("usd_slip"),
),
ui.column(
3,
),
),
## Fees Paid
ui.row(
ui.column(
3,
ui.tags.h6({"class": "col-label"}, "Fees Paid"),
),
ui.column(
3,
ui.output_text_verbatim("fee_paid"),
),
ui.column(
3,
ui.output_text_verbatim("usd_fee"),
),
ui.column(
3,
),
),
),
),
## Right Hand Column
ui.column(
6,
ui.tags.div({"class": "col-left"},
## Pool Price
ui.row(
ui.tags.h5({"class": "pool-price"}, "Pool Price"),
),
ui.row(
ui.output_text_verbatim("pool_price"),
),
## Chart of Pool Price
ui.row(
ui.output_plot("rel_pool_price")
),
## Pool Depth
ui.row(
ui.tags.h5({"class": "pool-depth"}, "Pool Depth"),
),
ui.row(
ui.output_plot("rel_pool_depth")
),
),
),
),
),
## Row with bottom columns
ui.row({"class":"bottom"},
## DYOR
ui.column(
12,
ui.row(
ui.tags.h6({"class": "paragraph"}, "IMPORTANT: This calculator is not investment advice, use it at your own risk and always DYOR. Questions? Find me on", ui.tags.a("Twitter", href="https://twitter.com/web3_analyst"), "and let's chat!"),
),
),
),
)
def server(input: Inputs, output: Outputs, session: Session):
## This Function Grabs The Current / Live Price
@reactive.Calc
async def get_price():
if input.pool() == "":
return ""
rel_price = 0
## Grab the asset symbols from the pool ID
asset0 = pool_id.loc[int(input.pool()), "SYMBOL_1"]
asset1 = pool_id.loc[int(input.pool()), "SYMBOL_2"]
## Get the token price for each token
price0 = sum(price.loc[price["SYMBOL"] == asset0, "PRICE"])
price1 = sum(price.loc[price["SYMBOL"] == asset1, "PRICE"])
if int(price0) < int(price1):
rel_price = price1/price0
else:
rel_price = price0/price1
## Grab the asset in & the amount, calculate the expected token amount
if input.t_in() == asset0:
if int(price0) < int(price1):
out_exp_amt = input.t_in_amt()*(1 / rel_price)
else:
out_exp_amt = input.t_in_amt()*rel_price
else:
if int(price0) < int(price1):
out_exp_amt = input.t_in_amt()*rel_price
else:
out_exp_amt = input.t_in_amt()*(1 / rel_price)
## Slippage calculations
t_in = input.t_in()
t_in_amt = input.t_in_amt()
amt0 = pool_depth.loc[pool_depth["POOL_ID"] == int(input.pool())+1, "TOKEN_0_AMOUNT"]
amt1 = pool_depth.loc[pool_depth["POOL_ID"] == int(input.pool())+1, "TOKEN_1_AMOUNT"]
time = pool_depth.loc[int(input.pool()), "BLOCK_TIMESTAMP"]
block = pool_depth.loc[int(input.pool()), "BLOCK_ID"]
if input.t_in() == asset1:
t_out_amt1 = amt1.values - ((amt0.values*amt1.values)/(amt0.values+input.t_in_amt()))
fee = t_out_amt1*input.pool_fee()/100
t_out_amt = t_out_amt1 - fee
usd_out_amt = price0*1
usd_fee = fee*price0
else:
t_out_amt1 = amt0.values - ((amt0.values*amt1.values)/(amt1.values+input.t_in_amt()))
fee = t_out_amt1*input.pool_fee()/100
t_out_amt = t_out_amt1 - fee
usd_out_amt = price1*1
usd_fee = fee*price1
return price0, price1, rel_price, asset0, asset1, t_out_amt1, t_out_amt, usd_out_amt, time, block, fee, usd_fee, out_exp_amt
@output
@render.text
async def symbol0():
symbol1 = pool_id.loc[int(input.pool()), "SYMBOL_1"]
return f"{symbol1}"
@output
@render.text
async def symbol1():
symbol2 = pool_id.loc[int(input.pool()), "SYMBOL_2"]
return f"{symbol2}"
@output
@render.text
async def price0():
prices = await get_price()
return f"${round(prices[0], 3)}"
@output
@render.text
async def price1():
prices = await get_price()
return f"${round(prices[1], 3)}"
@output
@render.text
async def pool_price():
prices = await get_price()
if prices[0] < prices[1]:
return f"{round(prices[2], 3)} {prices[3]} per {prices[4]}"
else:
return f"{round(prices[2], 3)} {prices[4]} per {prices[3]}"
## Output token
@output
@render.text
async def t_out():
asset0 = pool_id.loc[int(input.pool()), "SYMBOL_1"]
asset1 = pool_id.loc[int(input.pool()), "SYMBOL_2"]
if input.t_in() == asset0:
return asset1
else:
return asset0
## Calculate the amount of token received
@output
@render.text
async def t_out_amt():
prices = await get_price()
return f"{round(prices[6][0], 3)}"
## Slippage Output
@output
@render.text
async def t_slip():
prices = await get_price()
return f"{round(abs(prices[12]-prices[6][0]), 3)}"
@output
@render.text
async def usd_slip():
prices = await get_price()
return f"${round(prices[7]*(abs(prices[12]-prices[6][0])), 3)}"
## Recency Outputs
@output
@render.text
async def last_time():
prices = await get_price()
return f"Last Pool Update: {prices[8]} UTC"
@output
@render.text
async def last_block():
prices = await get_price()
return f"Last Block: {prices[9]}"
## Output fees paid
@output
@render.text
async def fee_paid():
prices = await get_price()
return f"{round(prices[10][0], 5)}"
@output
@render.text
async def usd_fee():
prices = await get_price()
return f"${round(prices[11][0], 5)}"
## Creates Relative Price Plot
@output
@render.plot()
async def rel_pool_price():
asset0 = pool_id.loc[int(input.pool()), "SYMBOL_1"]
asset1 = pool_id.loc[int(input.pool()), "SYMBOL_2"]
## Mean Price
price0 = pp.loc[pp["SYMBOL"] == asset0, "MEAN_PRICE"]
price1 = pp.loc[pp["SYMBOL"] == asset1, "MEAN_PRICE"]
## Min Price
min0 = pp.loc[pp["SYMBOL"] == asset0, "MIN_PRICE"]
min1 = pp.loc[pp["SYMBOL"] == asset1, "MIN_PRICE"]
if price0.iloc[0] < price1.iloc[1]:
rel_min = min1.values / min0.values
else:
rel_min = min0.values / min1.values
# Max Price
max0 = pp.loc[pp["SYMBOL"] == asset0, "MAX_PRICE"]
max1 = pp.loc[pp["SYMBOL"] == asset1, "MAX_PRICE"]
if price0.iloc[0] < price1.iloc[1]:
rel_max = max1.values / max0.values
else:
rel_max = max0.values / max1.values
date = pp.loc[pp["SYMBOL"]== asset0, "DATE"]
x_d = date.values
if price0.iloc[0] < price1.iloc[1]:
rel_price = price1.values/price0.values
else:
rel_price = price0.values/price1.values
err = rel_max - rel_min
fig, ax=plt.subplots()
fig.patch.set_facecolor('#140e35')
ax.errorbar(x_d, rel_price, err, fmt='o', color='#e13cbd')
# Rotates and right-aligns the x labels so they don't crowd each other.
for label in ax.get_xticklabels():
label.set(rotation=30, horizontalalignment='right')
for label in ax.xaxis.get_ticklabels()[::2]:
label.set_visible(False)
ax.set_facecolor("#140e35")
## Hide top & right Axes
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(True)
ax.spines['bottom'].set_visible(True)
## Recolor showing Axes
ax.spines['bottom'].set_color('#d2d0ff')
ax.spines['left'].set_color('#d2d0ff')
ax.tick_params(axis='x', colors='#d2d0ff')
ax.tick_params(axis='y', colors='#d2d0ff')
return fig
## Creates Pool Depth Plot
@output
@render.plot()
async def rel_pool_depth():
if input.pool() == "":
return ""
asset0 = pool_id.loc[int(input.pool()), "SYMBOL_1"]
asset1 = pool_id.loc[int(input.pool()), "SYMBOL_2"]
amt0 = pool_depth.loc[pool_depth["POOL_ID"] == int(input.pool())+1, "TOKEN_0_AMOUNT"]
amt1 = pool_depth.loc[pool_depth["POOL_ID"] == int(input.pool())+1, "TOKEN_1_AMOUNT"]
## Create xy = k curve
k = amt0*amt1
amt0_new = []
amt1_new = np.linspace(amt1/2, amt1+amt1/2, 200)
for i in range(0, len(amt1_new)):
amt0_new.append(k/amt1_new[i])
fig, ax=plt.subplots()
fig.patch.set_facecolor('#140e35')
ax.plot(amt0_new, amt1_new, '#e13cbd', amt0, amt1, 'o')
plt.xlabel(asset0)
plt.ylabel(asset1)
ax.set_facecolor("#140e35")
## Hide Axes
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(True)
ax.spines['bottom'].set_visible(True)
## Recolor showing Axes
ax.spines['bottom'].set_color('#d2d0ff')
ax.spines['left'].set_color('#d2d0ff')
ax.tick_params(axis='x', colors='#d2d0ff')
ax.tick_params(axis='y', colors='#d2d0ff')
ax.yaxis.label.set_color('#d2d0ff')
ax.xaxis.label.set_color('#d2d0ff')
return fig
www_dir = Path(__file__).parent
app = App(app_ui, server, debug=True, static_assets=www_dir)