forked from 0xbigz/drift-v2-streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
374 lines (323 loc) · 11.7 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
import asyncio
import datetime
import json
from math import exp
import os
from urllib.request import urlopen
from anchorpy import Provider
from anchorpy import Wallet
from dotenv import load_dotenv
from driftpy.constants.config import configs
from driftpy.drift_client import AccountSubscriptionConfig
from driftpy.drift_client import DriftClient
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
from plotly.subplots import make_subplots
from solana.rpc.async_api import AsyncClient
from solders.keypair import Keypair
import streamlit as st
# from network import show_network
from tabs.api import show_api
from tabs.competition import competitions
from tabs.fee_income import fee_income_page
from tabs.fees import fee_page
from tabs.funding_history import funding_history
from tabs.gpt import gpt_page
from tabs.if_stakers import insurance_fund_page
from tabs.imf import imf_page
from tabs.liquidity import mm_page
from tabs.logs import log_page
from tabs.mm_program import mm_program_page
from tabs.openbookv2 import tab_openbookv2
from tabs.orders import orders_page
from tabs.overview_markets import show_overview_markets
from tabs.perpLP import perp_lp_page
from tabs.pid import show_pid_positions
from tabs.refs import ref_page
from tabs.simulations import sim_page
from tabs.superstake import super_stake
from tabs.tradeflow import trade_flow_analysis
from tabs.useractivity import show_user_activity
from tabs.userdataraw import userdataraw
from tabs.userhealth import show_user_health
from tabs.usermap import usermap_page
from tabs.userperf import show_user_perf
from tabs.users_in_market import users_in_market_page
from tabs.userstats import show_user_stats
from tabs.userstatus import userstatus_page
from tabs.uservolume import show_user_volume
from tabs.vamm import vamm
from tabs.vaults import vaults
# import ssl
# import urllib.request
# ssl._create_default_https_context = ssl._create_unverified_context
load_dotenv()
try:
from tabs.backtester import backtester_page
except:
pass
def main():
current_time = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
if st.sidebar.button("Clear Cache"):
st.cache_data.clear()
env = st.sidebar.radio("env", ("mainnet-beta", "devnet"))
url = "🤫"
if env == "devnet":
url = "https://api." + env + ".solana.com"
else:
url = "🤫"
rpc = st.sidebar.text_input("rpc", url)
if env == "mainnet-beta" and (rpc == "🤫" or rpc == ""):
rpc = os.environ["ANCHOR_PROVIDER_URL"]
def query_string_callback():
st.query_params.from_dict({"tab": st.session_state.query_key})
query_p = st.query_params
query_tab = query_p.get("tab", "Welcome")
tab_options = (
"Welcome",
"Overview-Markets",
"Overview-Users",
"Simulations",
"Logs",
"Fee-Income",
"Fee-Schedule",
"Insurance-Fund",
"Perp-LPs",
"User-Activity",
"User-Performance",
"User-Health",
"User-Volume",
"User-Stats",
"DLOB",
"Refs",
"MM",
"Trade Flow",
"Drift-GPT",
"IMF",
"Network",
"User-Status",
"Config",
"API",
"SuperStake",
"vAMM",
"FundingHistory",
"UserDataRaw",
"Vaults",
"DriftDraw",
"Openbookv2",
"UserMap",
"UsersInMarket",
"Backtester",
"MM (legacy)",
# 'Social', 'PlatyPerps'
)
query_index = 0
for idx, x in enumerate(tab_options):
if x.lower() == query_tab.lower():
query_index = idx
tab = st.sidebar.radio(
"Select Tab:",
tab_options,
query_index,
on_change=query_string_callback,
key="query_key",
)
if env == "mainnet-beta":
config = configs["mainnet"]
else:
config = configs[env]
kp = Keypair() # random wallet
wallet = Wallet(kp)
connection = AsyncClient(rpc)
provider = Provider(connection, wallet)
clearing_house: DriftClient = DriftClient(
provider.connection,
provider.wallet,
env.split("-")[0],
account_subscription=AccountSubscriptionConfig("cached"),
)
# st.write(clearing_house.__dict__.keys())
clearing_house.time = current_time
st.title(f"Drift v2: {tab}")
if tab.lower() == "welcome":
st.warning(
"DISCLAIMER: INFORMATIONAL PURPOSES ONLY. USE EXPERIMENTAL SOFTWARE AT YOUR OWN RISK."
)
st.markdown(
"## welcome to the [Drift v2](app.drift.trade) streamlit dashboard!"
)
st.metric(
"protocol has been live for:",
str(
int(
(
datetime.datetime.now() - pd.to_datetime("2022-11-05")
).total_seconds()
/ (60 * 60 * 24)
)
)
+ " days",
)
st.markdown(
"""
On this site, did you know you can...
- explore all perp/spot market stats in [Overview](/?tab=Overview).
- track the Insurance Fund and balances in [Insurance Fund](/?tab=Insurance-Fund).
- inspect historical price impact and market maker leaderboards in [MM](/?tab=MM).
- view a taker trader breakdown in [Trade Flow](/?tab=Trade-Flow).
- talk to an AI at [Drift-GPT](/?tab=Drift-GPT).
this entire website is [open source](https://github.com/0xbigz/drift-v2-streamlit) and you can run it locally:
```
git clone https://github.com/0xbigz/drift-v2-streamlit.git
cd drift-v2-streamlit/
python3.10 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
streamlit run app.py
```
(this app uses streamlit==1.27, see `requirements.txt`)
"""
)
elif tab.lower() == "overview-markets":
loop = asyncio.new_event_loop()
loop.run_until_complete(show_overview_markets(clearing_house))
elif tab.lower() == "overview":
loop = asyncio.new_event_loop()
loop.run_until_complete(show_pid_positions(clearing_house))
elif tab.lower() == "config":
with st.expander(f"pid={clearing_house.program_id} config"):
st.json(config.__dict__)
if os.path.exists("gpt_database.csv"):
gpt_database = pd.read_csv("gpt_database.csv").to_csv().encode("utf-8")
st.download_button(
label="share browser statistics [bytes=" + str(len(gpt_database)) + "]",
data=gpt_database,
file_name="gpt_database.csv",
mime="text/csv",
)
elif tab.lower() == "logs":
loop = asyncio.new_event_loop()
loop.run_until_complete(log_page(rpc, clearing_house))
elif tab.lower() == "simulations":
sim_page()
elif tab.lower() == "fee-schedule":
loop = asyncio.new_event_loop()
loop.run_until_complete(fee_page(clearing_house))
elif tab.lower() == "fee-income":
loop = asyncio.new_event_loop()
loop.run_until_complete(fee_income_page(clearing_house))
elif tab.lower() == "refs":
loop = asyncio.new_event_loop()
loop.run_until_complete(ref_page(clearing_house))
elif tab.lower() == "insurance-fund":
loop = asyncio.new_event_loop()
loop.run_until_complete(insurance_fund_page(clearing_house, env))
elif tab.lower() == "user-performance":
loop = asyncio.new_event_loop()
loop.run_until_complete(show_user_perf(clearing_house))
elif tab.lower() == "user-activity":
loop = asyncio.new_event_loop()
loop.run_until_complete(show_user_activity(clearing_house))
elif tab.lower() == "user-health":
loop = asyncio.new_event_loop()
loop.run_until_complete(show_user_health(clearing_house))
elif tab.lower() == "user-status":
loop = asyncio.new_event_loop()
loop.run_until_complete(userstatus_page(clearing_house))
elif tab.lower() == "perp-lps":
loop = asyncio.new_event_loop()
loop.run_until_complete(perp_lp_page(clearing_house, env))
elif tab.lower() == "user-stats":
loop = asyncio.new_event_loop()
loop.run_until_complete(show_user_stats(clearing_house))
elif tab.lower() == "api":
loop = asyncio.new_event_loop()
loop.run_until_complete(show_api(clearing_house))
elif tab.lower() == "dlob":
orders_page(clearing_house)
elif tab.lower() == "user-volume":
loop = asyncio.new_event_loop()
loop.run_until_complete(show_user_volume(clearing_house))
elif tab.lower() == "vamm":
loop = asyncio.new_event_loop()
loop.run_until_complete(vamm(clearing_house))
# elif tab.lower() == 'network':
# loop = asyncio.new_event_loop()
# loop.run_until_complete(show_network(clearing_house))
elif tab.lower() == "mm":
loop = asyncio.new_event_loop()
loop.run_until_complete(mm_program_page(clearing_house, env))
elif tab.lower() == "mm (legacy)":
loop = asyncio.new_event_loop()
loop.run_until_complete(mm_page(clearing_house))
elif tab.lower() == "trade flow":
trade_flow_analysis()
elif tab.lower() == "imf":
loop = asyncio.new_event_loop()
loop.run_until_complete(imf_page(clearing_house))
elif tab.lower() == "social":
repo = "https://github.com/drift-labs/protocol-v2"
st.markdown(
"["
+ repo
+ "]("
+ repo
+ ") | [@driftprotocol](https://twitter.com/@driftprotocol)"
)
tweets = {
"cindy": "https://twitter.com/cindyleowtt/status/1569713537454579712",
"0xNineteen": "https://twitter.com/0xNineteen/status/1571926865681711104",
}
st.header("twitter:")
st.table(pd.Series(tweets))
elif tab.lower() == "platyperps":
loop = asyncio.new_event_loop()
loop.run_until_complete(show_platyperps(clearing_house))
elif tab.lower() == "drift-gpt":
loop = asyncio.new_event_loop()
loop.run_until_complete(gpt_page(clearing_house))
elif tab.lower() == "superstake":
loop = asyncio.new_event_loop()
loop.run_until_complete(super_stake(clearing_house))
elif tab.lower() == "userdataraw":
loop = asyncio.new_event_loop()
loop.run_until_complete(userdataraw(clearing_house))
elif tab.lower() == "fundinghistory":
loop = asyncio.new_event_loop()
loop.run_until_complete(funding_history(clearing_house, env))
elif tab.lower() == "vaults":
loop = asyncio.new_event_loop()
loop.run_until_complete(vaults(clearing_house, env))
elif tab.lower() == "driftdraw":
loop = asyncio.new_event_loop()
loop.run_until_complete(competitions(clearing_house, env))
elif tab.lower() == "openbookv2":
loop = asyncio.new_event_loop()
loop.run_until_complete(tab_openbookv2(clearing_house, env))
elif tab.lower() == "usermap":
# loop = asyncio.new_event_loop()
usermap_page(clearing_house, env)
elif tab.lower() == "usersinmarket":
loop = asyncio.new_event_loop()
loop.run_until_complete(users_in_market_page(clearing_house, env))
# users_in_market_page
elif tab.lower() == "backtester":
try:
backtester_page(clearing_house, env)
except:
pass
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
if __name__ == "__main__":
try:
st.set_page_config("Drift v2", layout="wide", page_icon="👾")
except:
pass
main()