diff --git a/.env.example b/.env.example index fb9ebc39..8b0a538d 100644 --- a/.env.example +++ b/.env.example @@ -21,8 +21,5 @@ DASHBOARD_PORT=8502 AIRFLOW_PORT=8080 AIRFLOW_BASE_URL= -# dashboard -LEGACY_DB_LOCATION= - # snaxchain api key CONDUIT_API_KEY= diff --git a/.streamlit/config.toml b/.streamlit/config.toml deleted file mode 100644 index 63e44944..00000000 --- a/.streamlit/config.toml +++ /dev/null @@ -1,6 +0,0 @@ -[theme] -primaryColor="#00D1FF" -backgroundColor="#06061B" -secondaryBackgroundColor="#0B0B22" -textColor="#FEFEFF" -font="sans serif" diff --git a/.streamlit/secrets_example.toml b/.streamlit/secrets_example.toml deleted file mode 100644 index 9f2b47e8..00000000 --- a/.streamlit/secrets_example.toml +++ /dev/null @@ -1,11 +0,0 @@ -[database] -DB_NAME = 'analytics' -DB_USER = 'analytics' -DB_PASS = 'analytics' -DB_HOST = 'db' -DB_PORT = '5432' -DB_ENV = 'prod' - -[settings] -SHOW_TESTNETS = 'false' -IS_CLOUD = 'false' diff --git a/README.md b/README.md index 09b569b4..a8d09b3a 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ A collection of services to index, store, and transform data related to Syntheti At a high level, the data stack contains a set of services to listen to onchain events, store that event data in a Postgres database, then transform and aggregate that data into a format that is useful for analytics. Read more about each service: -* [**Database**](./postgres/) - A Postgres database used to store raw and transformed data. -* [**Indexers**](./indexers/) - Blockchain indexers using Subsquid archives to index Synthetix smart contracts. These indexers are used to populate a Postgres database with raw event log data. -* [**Extractors**](./extractors/) - Scripts that extract blockchain data using `eth_call` RPC calls and [cryo](https://github.com/paradigmxyz/cryo). Data is stored in the a `parquet-data` directory, and will be imported into the Postgres database using the Transformers service. -* [**Transformers**](./transformers/) - Services that transform raw event log data into a format that is more useful for querying. These services are used to populate a Postgres database with transformed data using [dbt](https://www.getdbt.com/). -* [**Dashboard**](./dashboard/) - A collection of dashboards built using [streamlit](https://streamlit.io/) and connected directly to the Postgres database. + +- [**Database**](./postgres/) - A Postgres database used to store raw and transformed data. +- [**Indexers**](./indexers/) - Blockchain indexers using Subsquid archives to index Synthetix smart contracts. These indexers are used to populate a Postgres database with raw event log data. +- [**Extractors**](./extractors/) - Scripts that extract blockchain data using `eth_call` RPC calls and [cryo](https://github.com/paradigmxyz/cryo). Data is stored in the a `parquet-data` directory, and will be imported into the Postgres database using the Transformers service. +- [**Transformers**](./transformers/) - Services that transform raw event log data into a format that is more useful for querying. These services are used to populate a Postgres database with transformed data using [dbt](https://www.getdbt.com/). ## Usage @@ -25,16 +25,14 @@ The services are all managed using [docker compose](https://docs.docker.com/comp - `PG_PASSWORD`: The password for the admin user of the Postgres database. - `READONLY_PASSWORD`: The password for a configured read-only user, used for dashboards. Change this password and run `make reset-pw` to update the user's password. - `DB_PORT`: The port that will be used to expose the Postgres database. If left blank, the database will only be exposed to the docker network. -- `DASHBOARD_PORT`: The port used to expose the streamlit dashboards. -- `LEGACY_DB_LOCATION`: The location of a legacy SQLite database. This database is used in Optimism Mainnet dashboards summarizing activity on the legacy perps V2 contracts. ### Start Indexers -Once you have configured your environment, run `docker compose up -d --build` to build and run the services in detached mode. By default, the service will start a Postgres database, indexers for each network, and a streamlit dashboard on startup. Each indexer will write data to a database corresponding with the network name (ex. `base_mainnet`). You can view the logs for each service using `docker compose logs -f `. +Once you have configured your environment, run `docker compose up -d --build` to build and run the services in detached mode. By default, the service will start a Postgres database, indexers for each network, and an instance of Airflow. Each indexer will write data to a database corresponding with the network name (ex. `base_mainnet`). You can view the logs for each service using `docker compose logs -f `. -The dashboard service relies on transformed data in the `analytics` database. To populate this database, you must run the transformers. +See the [data tools](https://github.com/Synthetixio/data-tools) for dashboards that consume this data. -### Run Extractors +## Run Extractors To populate the `parquet-data` directory with data, you must run the extractors. These scripts will run the `eth_call` RPC method for each of the specified calls in the `extractors/main.py` file. To run the extractors, use the following command: @@ -55,4 +53,4 @@ make import # import the data from the parquet files into the database make dbt # run dbt for each network ``` -You should see output confirming that dbt has run for each network, and created a set of tables and views in the `analytics` database. The running dashboard service will automatically detect these tables and views and populate the dashboards with data. To view the dashboards, visit `http://localhost:` in your browser. +You should see output confirming that dbt has run for each network, and created a set of tables and views in the `analytics` database. diff --git a/dashboard/.dockerignore b/dashboard/.dockerignore deleted file mode 100644 index f6f3f702..00000000 --- a/dashboard/.dockerignore +++ /dev/null @@ -1,4 +0,0 @@ -.streamlit/secrets.toml -.env -/env -__pycache__/ diff --git a/dashboard/About.py b/dashboard/About.py deleted file mode 100644 index 3efe1bcb..00000000 --- a/dashboard/About.py +++ /dev/null @@ -1,77 +0,0 @@ -import streamlit as st -from st_pages import Page, show_pages, add_page_title - -# page setup -PAGE_PREFIX = "dashboard/" if st.secrets.settings.IS_CLOUD == "true" else "" -SHOW_TESTNETS = True if st.secrets.settings.SHOW_TESTNETS == "true" else False - -st.set_page_config( - page_title="Synthetix Dashboards", - page_icon=f"{PAGE_PREFIX}static/favicon.ico", - layout="wide", -) - -hide_footer = """ - -""" -st.markdown(hide_footer, unsafe_allow_html=True) - -st.markdown( - """ - -# Synthetix Stats - -Discover the latest insights into the Synthetix ecosystem. - -## Base Andromeda (V3) - -Andromeda, the latest Synthetix V3 deployment on the Base network, allows liquidity provision with USDC and seamless trading of perps using USDC collateral. For details, explore our [blog post](https://blog.synthetix.io/what-is-the-andromeda-release/). - -### Dashboards: -- **Perps Stats**: Insights on volume, fees, and more across all markets. -- **Perps Markets**: Insights for each perps market. -- **Perps Monitor**: A consolidated view of all perps markets. -- **Core Stats**: Key metrics of the Core system, including debt, collateral, and LP performance. -- **Spot Markets**: Analysis of the USDC wrapper contracts. -- **Perps Integrators**: Activity overview by integrator. -- **Perps Accounts**: View recent activity by specific accounts. -- **Perps Keepers**: Track community keepers' settlement transactions. - -## Optimism (V2) - -Synthetix V2 offers perps trading on the Optimism network. These dashboards provide insights into the Perps V2 markets. - -### Dashboards: -- **Perps Stats**: Insights on volume, fees, and more across all markets. -- **Perps Markets**: Insights for each perps market. -- **Perps Monitor**: A consolidated view of all perps markets. -- **Perps Integrators**: Activity overview by integrator. - -## Additional Resources -- V3 details: [documentation](https://docs.synthetix.io/v/v3/) -- Updates: [blog](https://blog.synthetix.io/) -- Community: [discord](https://discord.com/invite/AEdUHzt) -""" -) - -home_page = [ - Page(f"{PAGE_PREFIX}About.py", "About"), - Page(f"{PAGE_PREFIX}pages/Milestones.py", "Milestones"), -] -mainnet_pages = [ - Page(f"{PAGE_PREFIX}pages/All_Chains.py", "All Chains"), - Page(f"{PAGE_PREFIX}pages/Ethereum_Mainnet.py", "Ethereum"), - Page(f"{PAGE_PREFIX}pages/Base_Mainnet.py", "Base"), - Page(f"{PAGE_PREFIX}pages/Arbitrum_Mainnet.py", "Arbitrum"), - Page(f"{PAGE_PREFIX}pages/Optimism_Mainnet.py", "Optimism"), -] -testnet_pages = [ - Page(f"{PAGE_PREFIX}pages/Base_Sepolia.py", "Base_Sepolia"), - Page(f"{PAGE_PREFIX}pages/Arbitrum_Sepolia.py", "Arbitrum_Sepolia"), -] - -# pages to show -pages_to_show = home_page + mainnet_pages + (testnet_pages if SHOW_TESTNETS else []) -show_pages(pages_to_show) diff --git a/dashboard/Dockerfile b/dashboard/Dockerfile deleted file mode 100644 index 078ba9c0..00000000 --- a/dashboard/Dockerfile +++ /dev/null @@ -1,18 +0,0 @@ -# Base image -FROM python:3.11-slim - -# Create app directory -WORKDIR /app - -# Copy requirements file -COPY requirements.txt . - -# Install dependencies -RUN pip install --upgrade pip -RUN pip install --no-cache-dir -r requirements.txt - -# Copy app source code -COPY . . - -# Command to run the service -CMD ["streamlit", "run", "About.py"] diff --git a/dashboard/README.md b/dashboard/README.md deleted file mode 100644 index a60e20ba..00000000 --- a/dashboard/README.md +++ /dev/null @@ -1,20 +0,0 @@ -# Dashboard - -The dashboard service provides a suite of dashboards using [streamlit](https://streamlit.io/). The dashboards are designed to use the data marts created by the [transformers](../transformers/) service. - -## Usage - -The service runs as a docker container, and is started along with the other services in the `docker compose` setup. The dashboard is available at the port set by the `DASHBOARD_PORT` environment variable, which defaults to `8501`. - -## Secrets - -The `secrets_default.toml` file contains default environment variables for this services. To override the defaults, create a `secrets.toml` file at `.streamlit/secrets.toml` and add the environment variables you want to override. Then, update the `docker-compose.yml` file to mount the secrets file as a volume. Learn more about streamlit secrets [here](https://docs.streamlit.io/library/advanced-features/secrets-management). - -## Development - -The dashboard service consists of a page in the `pages` directory which corresponds to a network (OP mainnet, Base Sepolia, Base Mainnet, etc.) Each page is set up to import a set of modules from the `modules` directory. To add new modules: - -1. Create a new python file in the `modules` directory for the corresponding network -1. Import the module in the `pages` file for the corresponding network -1. Add the module to the `pages` dictionary for that page - diff --git a/dashboard/modules/all_chains/core_stats.py b/dashboard/modules/all_chains/core_stats.py deleted file mode 100644 index e42b2d08..00000000 --- a/dashboard/modules/all_chains/core_stats.py +++ /dev/null @@ -1,205 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_area, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "24h", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - df_collateral = pd.read_sql_query( - f""" - SELECT - ts, - CONCAT(coalesce(tk.token_symbol, collateral_type), ' (Arbitrum)') as label, - collateral_value, - debt, - hourly_pnl, - rewards_usd, - hourly_issuance, - cumulative_issuance, - cumulative_pnl, - apr_{resolution} as apr, - apr_{resolution}_pnl as apr_pnl, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_core_apr_arbitrum_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.arbitrum_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - - UNION ALL - - SELECT - ts, - CONCAT(coalesce(tk.token_symbol, collateral_type), ' (Base)') as label, - collateral_value, - debt, - hourly_pnl, - rewards_usd, - hourly_issuance, - cumulative_issuance, - cumulative_pnl, - apr_{resolution} as apr, - apr_{resolution}_pnl as apr_pnl, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_core_apr_base_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.base_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - - ORDER BY ts - """, - db, - ) - - df_chain = pd.read_sql_query( - f""" - with arbitrum as ( - select - ts, - label, - sum(collateral_value) as collateral_value, - sum(cumulative_pnl) as cumulative_pnl - from ( - SELECT - ts, - 'Arbitrum' as label, - collateral_value, - cumulative_pnl - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_core_apr_arbitrum_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.arbitrum_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ) as a - group by ts, label - ), - base as ( - select - ts, - label, - sum(collateral_value) as collateral_value, - sum(cumulative_pnl) as cumulative_pnl - from ( - SELECT - ts, - 'Base' as label, - collateral_value, - cumulative_pnl - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_core_apr_base_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.base_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ) as b - group by ts, label - ) - - select * from arbitrum - union all - select * from base - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "collateral": df_collateral, - "chain": df_chain, - } - - -def make_charts(data, filters): - resolution = filters["resolution"] - return { - "tvl_collateral": chart_area( - data["collateral"], - "ts", - ["collateral_value"], - "TVL by Collateral", - "label", - ), - "pnl_collateral": chart_area( - data["collateral"], - "ts", - ["cumulative_pnl"], - "Cumulative Pnl by Collateral", - "label", - ), - "tvl_chain": chart_area( - data["chain"], - "ts", - ["collateral_value"], - "TVL by Chain", - "label", - ), - "pnl_chain": chart_area( - data["chain"], - "ts", - ["cumulative_pnl"], - "Cumulative Pnl by Chain", - "label", - ), - "apr": chart_lines( - data["collateral"], - "ts", - "apr", - f"APR - {resolution} average", - y_format="%", - color="label", - ), - } - - -def main(): - ## title - st.markdown("## V3: All Chains") - - ## inputs - with st.expander("Filters"): - filters["resolution"] = st.radio( - "Resolution", - ["28d", "7d", "24h"], - ) - - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data, filters) - - ## display - st.plotly_chart(charts["apr"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["tvl_chain"], use_container_width=True) - st.plotly_chart(charts["tvl_collateral"], use_container_width=True) - - with col2: - st.plotly_chart(charts["pnl_chain"], use_container_width=True) - st.plotly_chart(charts["pnl_collateral"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/all_chains/perp_stats.py b/dashboard/modules/all_chains/perp_stats.py deleted file mode 100644 index 0fb75a5c..00000000 --- a/dashboard/modules/all_chains/perp_stats.py +++ /dev/null @@ -1,156 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_stats = pd.read_sql_query( - f""" - with base as ( - SELECT - ts, - 'Base (V3)' as label, - volume, - trades, - exchange_fees as fees, - liquidated_accounts as liquidations - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_stats_{resolution}_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ), - optimism as ( - SELECT - ts, - 'Optimism (V2)' as label, - volume, - trades, - exchange_fees + liquidation_fees as fees, - liquidations - FROM {st.secrets.database.DB_ENV}_optimism_mainnet.fct_v2_stats_{resolution}_optimism_mainnet - where ts >= '{filters["start_date"]}' - and ts <= '{filters["end_date"]}' - ), - arbitrum as ( - SELECT - ts, - 'Arbitrum (V3)' as label, - volume, - trades, - exchange_fees as fees, - liquidated_accounts as liquidations - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_stats_{resolution}_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - - ) - select * from base - union all - select * from optimism - union all - select * from arbitrum - order by ts - """, - db, - ) - - db.close() - - return { - "stats": df_stats, - } - - -def make_charts(data): - return { - "volume": chart_bars( - data["stats"], - "ts", - ["volume"], - "Volume", - "label", - ), - "fees": chart_bars( - data["stats"], - "ts", - ["fees"], - "Exchange Fees", - "label", - ), - "trades": chart_bars( - data["stats"], - "ts", - ["trades"], - "Trades", - "label", - y_format="#", - ), - "liquidations": chart_bars( - data["stats"], - "ts", - ["liquidations"], - "Liquidations", - "label", - y_format="#", - ), - } - - -def main(): - ## title - st.markdown("## Perps: All Chains") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - - with col2: - st.plotly_chart(charts["fees"], use_container_width=True) - st.plotly_chart(charts["liquidations"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_mainnet/core_stats.py b/dashboard/modules/arbitrum_mainnet/core_stats.py deleted file mode 100644 index 44b0623e..00000000 --- a/dashboard/modules/arbitrum_mainnet/core_stats.py +++ /dev/null @@ -1,217 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "24h", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # get account data - df_account_delegation = pd.read_sql_query( - f""" - SELECT * FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_core_account_delegation_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_apr = pd.read_sql_query( - f""" - SELECT - ts, - coalesce(tk.token_symbol, collateral_type) as collateral_type, - collateral_value, - debt, - hourly_pnl, - rewards_usd, - hourly_issuance, - cumulative_issuance, - cumulative_pnl, - apr_{resolution} as apr, - apr_{resolution}_pnl as apr_pnl, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_core_apr_arbitrum_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.arbitrum_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - and pool_id = 1 - ORDER BY ts - """, - db, - ) - - df_apr_token = pd.read_sql_query( - f""" - SELECT - ts, - coalesce(tk.token_symbol, collateral_type) as collateral_type, - apr.reward_token, - concat(coalesce(tk.token_symbol, collateral_type), ' : ', apr.reward_token) as token_pair, - collateral_value, - rewards_usd, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_core_apr_rewards_arbitrum_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.arbitrum_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - and pool_id = 1 - and apr.reward_token is not null - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "account_delegation": df_account_delegation, - "apr": df_apr, - "apr_token": df_apr_token, - } - - -def make_charts(data, filters): - resolution = filters["resolution"] - return { - "tvl": chart_lines( - data["apr"], - "ts", - ["collateral_value"], - "TVL", - "collateral_type", - ), - "debt": chart_lines( - data["apr"], - "ts", - ["debt"], - "Debt", - "collateral_type", - ), - "hourly_issuance": chart_bars( - data["apr"], - "ts", - ["hourly_issuance"], - "Hourly Issuance", - "collateral_type", - ), - "issuance": chart_lines( - data["apr"], - "ts", - ["cumulative_issuance"], - "Issuance", - "collateral_type", - ), - "pnl": chart_lines( - data["apr"], - "ts", - ["cumulative_pnl"], - "Pnl", - "collateral_type", - ), - "hourly_pnl": chart_bars( - data["apr"], - "ts", - ["hourly_pnl"], - "Hourly Pnl", - "collateral_type", - ), - "hourly_rewards": chart_bars( - data["apr"], - "ts", - ["rewards_usd"], - "Hourly Rewards", - "collateral_type", - ), - "hourly_rewards_token": chart_bars( - data["apr_token"], - "ts", - ["rewards_usd"], - "Hourly Rewards (Collateral : Reward)", - "token_pair", - ), - "apr": chart_lines( - data["apr"], - "ts", - "apr", - f"APR - {resolution} average", - y_format="%", - color="collateral_type", - ), - "apr_token": chart_lines( - data["apr_token"], - "ts", - "apr_rewards", - f"Reward APR by Token - {resolution} average", - y_format="%", - color="token_pair", - ), - } - - -def main(): - ## title - st.markdown("## V3 Core") - - ## inputs - with st.expander("Filters"): - filters["resolution"] = st.radio( - "Resolution", - ["28d", "7d", "24h"], - ) - - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data, filters) - - ## display - st.plotly_chart(charts["apr"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["tvl"], use_container_width=True) - st.plotly_chart(charts["hourly_pnl"], use_container_width=True) - st.plotly_chart(charts["hourly_issuance"], use_container_width=True) - st.plotly_chart(charts["hourly_rewards"], use_container_width=True) - st.plotly_chart(charts["apr_token"], use_container_width=True) - - with col2: - st.plotly_chart(charts["debt"], use_container_width=True) - st.plotly_chart(charts["pnl"], use_container_width=True) - st.plotly_chart(charts["issuance"], use_container_width=True) - st.plotly_chart(charts["hourly_rewards_token"], use_container_width=True) - - st.markdown("## Top Delegators") - st.dataframe( - data["account_delegation"] - .sort_values("amount_delegated", ascending=False) - .head(25) - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_mainnet/perp_account.py b/dashboard/modules/arbitrum_mainnet/perp_account.py deleted file mode 100644 index 3683e9c0..00000000 --- a/dashboard/modules/arbitrum_mainnet/perp_account.py +++ /dev/null @@ -1,309 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "account_id": "NULL", - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=60) -def fetch_data(filters): - # get filters - account_id = filters["account_id"] - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # read data - df_order = pd.read_sql_query( - f""" - SELECT distinct account_id, sender FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_orders_arbitrum_mainnet - """, - db, - ) - df_order_expired = pd.read_sql_query( - f""" - SELECT - block_timestamp, - cast(account_id as text) as account_id, - market_id, - acceptable_price, - commitment_time - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_previous_order_expired_arbitrum_mainnet - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - df_trade = pd.read_sql_query( - f""" - SELECT - ts, - cast(account_id as text) as account_id, - market_id, - market_symbol, - position_size, - notional_position_size, - trade_size, - notional_trade_size, - fill_price, - total_fees, - accrued_funding, - tracking_code - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_trades_arbitrum_mainnet - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_transfer = pd.read_sql_query( - f""" - SELECT - block_timestamp, - cast(account_id as text) as account_id, - synth_market_id, - amount_delta - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_collateral_modified_arbitrum_mainnet - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - df_interest = pd.read_sql_query( - f""" - SELECT - block_timestamp, - transaction_hash, - cast(account_id as text) as account_id, - interest - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_interest_charged_arbitrum_mainnet - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - - df_account_liq = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - total_reward - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_liq_account_arbitrum_mainnet - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_hourly = pd.read_sql_query( - f""" - SELECT - ts, - cumulative_volume, - cumulative_fees - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_account_stats_hourly_arbitrum_mainnet - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - order by ts - """, - db, - ) - - df_accounts = df_order[["account_id", "sender"]].drop_duplicates() - df_accounts.columns = ["id", "owner"] - - # one basic transform - df_transfer["amount_delta"] = df_transfer["amount_delta"] / 1e18 - - db.close() - - return { - "accounts": df_accounts, - "order_expired": df_order_expired, - "interest": df_interest, - "trade": df_trade, - "transfer": df_transfer, - "account_liq": df_account_liq, - "hourly": df_hourly, - } - - -def make_charts(data): - return { - "cumulative_volume": chart_lines( - data["hourly"], "ts", ["cumulative_volume"], "Cumulative Volume" - ), - "cumulative_fees": chart_lines( - data["hourly"], "ts", ["cumulative_fees"], "Cumulative Fees" - ), - } - - -def main(): - st.markdown("## V3 Perps Accounts") - data = fetch_data(filters) - - ## date filter - with st.expander("Date filter"): - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## account lookup - with st.expander("Look up accounts by address"): - address = st.text_input("Enter an address to look up associated accounts") - - df_accounts = data["accounts"] - account_numbers = df_accounts[(df_accounts["owner"] == address)]["id"].unique() - - # account_numbers - if len(account_numbers) > 0: - st.dataframe(account_numbers, hide_index=True) - - ## account select - accounts = data["accounts"]["id"].unique() - accounts = sorted(list([int(_) for _ in accounts])) - filters["account_id"] = st.selectbox("Select account", accounts, index=0) - data = fetch_data(filters) - - ## do some lighter transforms - df_open_positions = ( - data["trade"] - .sort_values("ts") - .groupby(["account_id", "market_id"]) - .last() - .reset_index() - ) - df_open_positions = df_open_positions[df_open_positions["position_size"].abs() > 0] - - ## make the charts - charts = make_charts(data) - - ## display - # Open positions - df_open_account = df_open_positions[ - df_open_positions["account_id"] == filters["account_id"] - ] - - last_liq = ( - data["account_liq"] - .loc[data["account_liq"]["account_id"] == filters["account_id"], "ts"] - .max() - ) - - # this is a hack to handle the case where there are no liquidations - last_liq = last_liq if pd.isna(last_liq) == False else "2023-01-01 00:00:00+00:00" - - df_open_account = df_open_account.loc[ - df_open_account["ts"] > last_liq, - ["account_id", "market_symbol", "position_size", "notional_position_size"], - ] - - st.markdown( - """ - ### Open Positions - """ - ) - if len(df_open_account) > 0: - df_open_account - else: - st.markdown( - """ - No open positions - """ - ) - - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - pass - - # Recent trades - st.markdown( - """ - ### Recent Trades - """ - ) - - st.dataframe( - data["trade"].sort_values("ts", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Recent transfers - st.markdown( - """ - ### Recent Transfers - """ - ) - - st.dataframe( - data["transfer"].sort_values("block_timestamp", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Account liquidations table - st.markdown( - """ - ### Liquidations - """ - ) - - st.dataframe( - data["account_liq"].sort_values("ts", ascending=False).head(25), - use_container_width=True, - hide_index=True, - ) - - # Expired orders table - st.markdown( - """ - ### Expired Orders - """ - ) - - st.dataframe( - data["order_expired"], - use_container_width=True, - hide_index=True, - ) - - # Interest charged table - st.markdown( - """ - ### Interest Charged - """ - ) - - st.dataframe( - data["interest"], - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_mainnet/perp_integrators.py b/dashboard/modules/arbitrum_mainnet/perp_integrators.py deleted file mode 100644 index 31e21bac..00000000 --- a/dashboard/modules/arbitrum_mainnet/perp_integrators.py +++ /dev/null @@ -1,175 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # get account data - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - tracking_code, - accounts, - volume, - volume_share, - trades, - trades_share, - exchange_fees, - exchange_fees_share, - referral_fees, - referral_fees_share - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_tracking_stats_{resolution}_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "stats": df_stats, - } - - -def make_charts(data): - return { - "accounts": chart_bars( - data["stats"], - "ts", - ["accounts"], - "Accounts", - color="tracking_code", - y_format="#", - ), - "volume": chart_bars( - data["stats"], - "ts", - ["volume"], - "Volume", - color="tracking_code", - ), - "volume_pct": chart_bars( - data["stats"], - "ts", - ["volume_share"], - "Volume %", - color="tracking_code", - y_format="%", - ), - "trades": chart_bars( - data["stats"], - "ts", - ["trades"], - "Trades", - color="tracking_code", - y_format="#", - ), - "trades_pct": chart_bars( - data["stats"], - "ts", - ["trades_share"], - "Trades %", - color="tracking_code", - y_format="%", - ), - "exchange_fees": chart_bars( - data["stats"], - "ts", - ["exchange_fees"], - "Exchange Fees", - color="tracking_code", - ), - "exchange_fees_pct": chart_bars( - data["stats"], - "ts", - ["exchange_fees_share"], - "Exchange Fees %", - color="tracking_code", - y_format="%", - ), - "referral_fees": chart_bars( - data["stats"], - "ts", - ["referral_fees"], - "Referral Fees", - color="tracking_code", - ), - "referral_fees_pct": chart_bars( - data["stats"], - "ts", - ["referral_fees_share"], - "Referral Fees %", - color="tracking_code", - y_format="%", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Integrators") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["referral_fees"], use_container_width=True) - st.plotly_chart(charts["accounts"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["volume_pct"], use_container_width=True) - st.plotly_chart(charts["trades_pct"], use_container_width=True) - st.plotly_chart(charts["exchange_fees_pct"], use_container_width=True) - st.plotly_chart(charts["referral_fees_pct"], use_container_width=True) - pass - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_mainnet/perp_keepers.py b/dashboard/modules/arbitrum_mainnet/perp_keepers.py deleted file mode 100644 index f8ae7efb..00000000 --- a/dashboard/modules/arbitrum_mainnet/perp_keepers.py +++ /dev/null @@ -1,146 +0,0 @@ -import streamlit as st -import pandas as pd -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_lines, chart_many_bars, export_data -from utils import get_connection - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_keeper = pd.read_sql_query( - f""" - SELECT - ts, - keeper, - trades, - trades_pct, - amount_settled, - amount_settled_pct, - settlement_rewards, - settlement_rewards_pct - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_keeper_stats_{resolution}_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "keeper": df_keeper, - } - - -## charts -def make_charts(data): - df = data["keeper"] - - return { - "trades": chart_many_bars( - df, - "ts", - ["trades"], - "Orders Settled", - "keeper", - y_format="#", - ), - "trades_pct": chart_many_bars( - df, - "ts", - ["trades_pct"], - "Orders Settled %", - "keeper", - y_format="%", - ), - "amount_settled": chart_many_bars( - df, - "ts", - ["amount_settled"], - "Notional Size Settled", - "keeper", - ), - "amount_settled_pct": chart_many_bars( - df, - "ts", - ["amount_settled_pct"], - "Notional Size Settled %", - "keeper", - y_format="%", - ), - "settlement_rewards": chart_many_bars( - df, - "ts", - ["settlement_rewards"], - "Settlement Rewards", - "keeper", - ), - "settlement_rewards_pct": chart_many_bars( - df, - "ts", - ["settlement_rewards_pct"], - "Settlement Rewards %", - "keeper", - y_format="%", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Keepers") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["amount_settled"], use_container_width=True) - st.plotly_chart(charts["settlement_rewards"], use_container_width=True) - - with col2: - st.plotly_chart(charts["trades_pct"], use_container_width=True) - st.plotly_chart(charts["amount_settled_pct"], use_container_width=True) - st.plotly_chart(charts["settlement_rewards_pct"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_mainnet/perp_markets.py b/dashboard/modules/arbitrum_mainnet/perp_markets.py deleted file mode 100644 index 8dd426b8..00000000 --- a/dashboard/modules/arbitrum_mainnet/perp_markets.py +++ /dev/null @@ -1,170 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import chart_lines, chart_bars, chart_oi, export_data -from utils import get_connection - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # read data - df_market_history = pd.read_sql_query( - f""" - SELECT - ts, - market_id, - market_symbol, - funding_rate, - interest_rate, - funding_rate_apr, - long_rate_apr, - short_rate_apr, - price, - skew, - size_usd, - short_oi_pct, - long_oi_pct - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_market_history_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - volume, - trades, - exchange_fees, - liquidations - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_market_stats_daily_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "market_history": df_market_history, - "stats": df_stats, - } - - -## charts -def make_charts(data, asset): - df_market = data["market_history"][data["market_history"]["market_symbol"] == asset] - df_stats = data["stats"][data["stats"]["market_symbol"] == asset] - - return { - "rates": chart_lines( - df_market, - "ts", - ["funding_rate_apr", "interest_rate", "long_rate_apr", "short_rate_apr"], - "Rates", - smooth=True, - y_format="%", - ), - "price": chart_lines( - df_market, - "ts", - ["price"], - "Price", - smooth=True, - ), - "volume": chart_bars( - df_stats, - "ts", - ["volume"], - "Volume", - ), - "exchange_fees": chart_bars( - df_stats, - "ts", - ["exchange_fees"], - "Exchange Fees", - ), - "skew": chart_lines( - df_market, - "ts", - ["skew"], - "Market Skew", - y_format="#", - ), - "oi": chart_lines( - df_market, - "ts", - ["size_usd"], - "Open Interest: Total", - ), - "oi_pct": chart_oi( - df_market, - "ts", - "Open Interest: Long vs Short", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Markets") - - ## inputs - with st.expander("Filters") as expander: - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## market filter - assets = sorted( - data["market_history"]["market_symbol"].unique(), - key=lambda x: (x != "ETH", x != "BTC", x), - ) - asset = st.selectbox("Select asset", assets, index=0) - - ## make the charts - charts = make_charts(data, asset) - - ## display - st.plotly_chart(charts["price"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["oi"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - - with col2: - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["oi_pct"], use_container_width=True) - st.plotly_chart(charts["rates"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_mainnet/perp_monitor.py b/dashboard/modules/arbitrum_mainnet/perp_monitor.py deleted file mode 100644 index b9738ac0..00000000 --- a/dashboard/modules/arbitrum_mainnet/perp_monitor.py +++ /dev/null @@ -1,290 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_many_bars, chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=60) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_order_expired = pd.read_sql_query( - f""" - SELECT - block_number, - block_timestamp, - cast(account_id as text) as account_id, - market_id, - acceptable_price, - commitment_time, - tracking_code - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_previous_order_expired_arbitrum_mainnet - WHERE date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - ORDER BY block_timestamp - """, - db, - ) - df_trade = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - market_symbol, - position_size, - trade_size, - notional_trade_size, - fill_price, - total_fees, - accrued_funding, - tracking_code, - transaction_hash - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_trades_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_account_liq = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - total_reward - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_liq_account_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_skew = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - skew, - skew * price as skew_usd - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_market_history_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - # hourly data - df_market = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - volume, - trades, - exchange_fees, - liquidations - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_market_stats_{resolution}_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - liquidated_accounts, - liquidation_rewards - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_stats_{resolution}_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - # transform data - current_skew = ( - df_skew.groupby("market_symbol") - .tail(1) - .sort_values("skew_usd", ascending=False) - ) - current_skew["side"] = current_skew["skew"].apply( - lambda x: "Long" if x > 0 else ("Short" if x < 0 else "Neutral") - ) - - db.close() - - return { - "order_expired": df_order_expired, - "trade": df_trade, - "account_liq": df_account_liq, - "market": df_market, - "stats": df_stats, - "skew": df_skew, - "current_skew": current_skew, - } - - -def make_charts(data): - return { - "volume": chart_many_bars( - data["market"], - "ts", - ["volume"], - "Volume", - "market_symbol", - ), - "exchange_fees": chart_many_bars( - data["market"], - "ts", - ["exchange_fees"], - "Exchange Fees", - "market_symbol", - ), - "trades": chart_many_bars( - data["market"], - "ts", - ["trades"], - "Trades", - "market_symbol", - y_format="#", - ), - "position_liquidations": chart_many_bars( - data["market"], - "ts", - ["liquidations"], - "Position Liquidations", - "market_symbol", - y_format="#", - ), - "account_liquidations": chart_bars( - data["stats"], - "ts", - ["liquidated_accounts"], - "Account Liquidations", - y_format="#", - ), - "liquidation_rewards": chart_bars( - data["stats"], - "ts", - ["liquidation_rewards"], - "Liquidation Rewards", - ), - "skew": chart_lines( - data["skew"], - "ts", - ["skew_usd"], - "Market Skew", - "market_symbol", - ), - "current_skew": chart_bars( - data["current_skew"], - ["skew_usd"], - "side", - "Current Market Skew", - "market_symbol", - column=True, - x_format="$", - y_format="#", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Monitor") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["account_liquidations"], use_container_width=True) - - with col2: - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["position_liquidations"], use_container_width=True) - st.plotly_chart(charts["liquidation_rewards"], use_container_width=True) - - st.plotly_chart(charts["current_skew"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - - # Recent trades - st.markdown( - """ - ### Recent Trades - """ - ) - - st.dataframe( - data["trade"].sort_values("ts", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Account liquidations table - st.markdown( - """ - ### Accounts Liquidated - """ - ) - - st.dataframe( - data["account_liq"].sort_values("ts", ascending=False).head(25), - use_container_width=True, - hide_index=True, - ) - - # Expired orders table - st.markdown( - """ - ### Expired Orders - """ - ) - - st.dataframe( - data["order_expired"].sort_values("block_timestamp", ascending=False), - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_mainnet/perp_stats.py b/dashboard/modules/arbitrum_mainnet/perp_stats.py deleted file mode 100644 index 35b09db2..00000000 --- a/dashboard/modules/arbitrum_mainnet/perp_stats.py +++ /dev/null @@ -1,142 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - volume, - trades, - exchange_fees, - liquidated_accounts, - liquidation_rewards, - cumulative_exchange_fees, - cumulative_volume - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_perp_stats_{resolution}_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "stats": df_stats, - } - - -def make_charts(data): - return { - "volume": chart_bars(data["stats"], "ts", ["volume"], "Volume"), - "cumulative_volume": chart_lines( - data["stats"], - "ts", - ["cumulative_volume"], - "Cumulative Volume", - smooth=True, - ), - "cumulative_fees": chart_lines( - data["stats"], - "ts", - ["cumulative_exchange_fees"], - "Cumulative Fees", - smooth=True, - ), - "fees": chart_bars( - data["stats"], - "ts", - ["exchange_fees"], - "Exchange Fees", - ), - "trades": chart_bars( - data["stats"], - "ts", - ["trades"], - "Trades", - y_format="#", - ), - "account_liquidations": chart_bars( - data["stats"], - "ts", - ["liquidated_accounts"], - "Account Liquidations", - y_format="#", - ), - "liquidation_rewards": chart_bars( - data["stats"], - "ts", - ["liquidation_rewards"], - "Liquidation Rewards", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Stats") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - st.plotly_chart(charts["account_liquidations"], use_container_width=True) - st.plotly_chart(charts["liquidation_rewards"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["fees"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - pass - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_mainnet/spot_markets.py b/dashboard/modules/arbitrum_mainnet/spot_markets.py deleted file mode 100644 index 76c52fd4..00000000 --- a/dashboard/modules/arbitrum_mainnet/spot_markets.py +++ /dev/null @@ -1,147 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(settings): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # get account data - df_wrapper = pd.read_sql_query( - f""" - SELECT - ts, - block_number, - tx_hash, - synth_market_id, - amount_wrapped - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_spot_wrapper_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_atomics = pd.read_sql_query( - f""" - SELECT - ts, - block_number, - tx_hash, - synth_market_id, - amount, - price - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_spot_atomics_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_synth_supply = pd.read_sql_query( - f""" - SELECT - ts, - synth_market_id, - supply - FROM {st.secrets.database.DB_ENV}_arbitrum_mainnet.fct_synth_supply_arbitrum_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "synth_supply": df_synth_supply, - "wrapper": df_wrapper, - "atomics": df_atomics, - } - - -def make_charts(data): - return { - "supply": chart_lines( - data["synth_supply"], - "ts", - ["supply"], - "Synth Supply", - "synth_market_id", - ), - } - - -def main(): - ## title - st.markdown( - """ - ## V3 Spot Market - """ - ) - - ## inputs - with st.expander("Filters") as expander: - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch the data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - - st.plotly_chart(charts["supply"], use_container_width=True) - - # Wrapper table - st.markdown( - """ - ### Wrapper - """ - ) - - st.dataframe( - data["wrapper"].sort_values("ts", ascending=False), - use_container_width=True, - hide_index=True, - ) - - # Atomics table - st.markdown( - """ - ### Atomic Transactions - """ - ) - - st.dataframe( - data["atomics"].sort_values("ts", ascending=False), - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_sepolia/core_stats.py b/dashboard/modules/arbitrum_sepolia/core_stats.py deleted file mode 100644 index e2199c0e..00000000 --- a/dashboard/modules/arbitrum_sepolia/core_stats.py +++ /dev/null @@ -1,179 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "24h", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # get account data - df_account_delegation = pd.read_sql_query( - f""" - SELECT * FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_core_account_delegation_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_apr = pd.read_sql_query( - f""" - SELECT - ts, - coalesce(tk.token_symbol, collateral_type) as collateral_type, - collateral_value, - debt, - hourly_pnl, - rewards_usd, - hourly_issuance, - cumulative_issuance, - cumulative_pnl, - apr_{resolution} as apr, - apr_{resolution}_pnl as apr_pnl, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_core_apr_arbitrum_sepolia apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.arbitrum_sepolia_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - and pool_id = 1 - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "account_delegation": df_account_delegation, - "apr": df_apr, - } - - -def make_charts(data, filters): - resolution = filters["resolution"] - return { - "tvl": chart_lines( - data["apr"], - "ts", - ["collateral_value"], - "TVL", - "collateral_type", - ), - "debt": chart_lines( - data["apr"], - "ts", - ["debt"], - "Debt", - "collateral_type", - ), - "hourly_issuance": chart_bars( - data["apr"], - "ts", - ["hourly_issuance"], - "Hourly Issuance", - "collateral_type", - ), - "issuance": chart_lines( - data["apr"], - "ts", - ["cumulative_issuance"], - "Issuance", - "collateral_type", - ), - "pnl": chart_lines( - data["apr"], - "ts", - ["cumulative_pnl"], - "Pnl", - "collateral_type", - ), - "hourly_pnl": chart_bars( - data["apr"], - "ts", - ["hourly_pnl"], - "Hourly Pnl", - "collateral_type", - ), - "hourly_rewards": chart_bars( - data["apr"], - "ts", - ["rewards_usd"], - "Hourly Rewards", - "collateral_type", - ), - "apr": chart_lines( - data["apr"], - "ts", - "apr", - f"APR - {resolution} average", - y_format="%", - color="collateral_type", - ), - } - - -def main(): - ## title - st.markdown("## V3 Core") - - ## inputs - with st.expander("Filters"): - filters["resolution"] = st.radio( - "Resolution", - ["28d", "7d", "24h"], - ) - - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data, filters) - - ## display - st.plotly_chart(charts["apr"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["tvl"], use_container_width=True) - st.plotly_chart(charts["hourly_pnl"], use_container_width=True) - st.plotly_chart(charts["hourly_issuance"], use_container_width=True) - st.plotly_chart(charts["hourly_rewards"], use_container_width=True) - - with col2: - st.plotly_chart(charts["debt"], use_container_width=True) - st.plotly_chart(charts["pnl"], use_container_width=True) - st.plotly_chart(charts["issuance"], use_container_width=True) - - st.markdown("## Top Delegators") - st.dataframe( - data["account_delegation"] - .sort_values("amount_delegated", ascending=False) - .head(25) - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_sepolia/perp_account.py b/dashboard/modules/arbitrum_sepolia/perp_account.py deleted file mode 100644 index 9a1bdc2e..00000000 --- a/dashboard/modules/arbitrum_sepolia/perp_account.py +++ /dev/null @@ -1,309 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "account_id": "NULL", - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=60) -def fetch_data(filters): - # get filters - account_id = filters["account_id"] - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # read data - df_order = pd.read_sql_query( - f""" - SELECT distinct account_id, sender FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_orders_arbitrum_sepolia - """, - db, - ) - df_order_expired = pd.read_sql_query( - f""" - SELECT - block_timestamp, - cast(account_id as text) as account_id, - market_id, - acceptable_price, - commitment_time - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_previous_order_expired_arbitrum_sepolia - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - df_trade = pd.read_sql_query( - f""" - SELECT - ts, - cast(account_id as text) as account_id, - market_id, - market_symbol, - position_size, - notional_position_size, - trade_size, - notional_trade_size, - fill_price, - total_fees, - accrued_funding, - tracking_code - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_trades_arbitrum_sepolia - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_transfer = pd.read_sql_query( - f""" - SELECT - block_timestamp, - cast(account_id as text) as account_id, - synth_market_id, - amount_delta - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_collateral_modified_arbitrum_sepolia - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - df_interest = pd.read_sql_query( - f""" - SELECT - block_timestamp, - transaction_hash, - cast(account_id as text) as account_id, - interest - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_interest_charged_arbitrum_sepolia - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - - df_account_liq = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - total_reward - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_liq_account_arbitrum_sepolia - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_hourly = pd.read_sql_query( - f""" - SELECT - ts, - cumulative_volume, - cumulative_fees - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_account_stats_hourly_arbitrum_sepolia - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - order by ts - """, - db, - ) - - df_accounts = df_order[["account_id", "sender"]].drop_duplicates() - df_accounts.columns = ["id", "owner"] - - # one basic transform - df_transfer["amount_delta"] = df_transfer["amount_delta"] / 1e18 - - db.close() - - return { - "accounts": df_accounts, - "order_expired": df_order_expired, - "interest": df_interest, - "trade": df_trade, - "transfer": df_transfer, - "account_liq": df_account_liq, - "hourly": df_hourly, - } - - -def make_charts(data): - return { - "cumulative_volume": chart_lines( - data["hourly"], "ts", ["cumulative_volume"], "Cumulative Volume" - ), - "cumulative_fees": chart_lines( - data["hourly"], "ts", ["cumulative_fees"], "Cumulative Fees" - ), - } - - -def main(): - st.markdown("## V3 Perps Accounts") - data = fetch_data(filters) - - ## date filter - with st.expander("Date filter"): - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## account lookup - with st.expander("Look up accounts by address"): - address = st.text_input("Enter an address to look up associated accounts") - - df_accounts = data["accounts"] - account_numbers = df_accounts[(df_accounts["owner"] == address)]["id"].unique() - - # account_numbers - if len(account_numbers) > 0: - st.dataframe(account_numbers, hide_index=True) - - ## account select - accounts = data["accounts"]["id"].unique() - accounts = sorted(list([int(_) for _ in accounts])) - filters["account_id"] = st.selectbox("Select account", accounts, index=0) - data = fetch_data(filters) - - ## do some lighter transforms - df_open_positions = ( - data["trade"] - .sort_values("ts") - .groupby(["account_id", "market_id"]) - .last() - .reset_index() - ) - df_open_positions = df_open_positions[df_open_positions["position_size"].abs() > 0] - - ## make the charts - charts = make_charts(data) - - ## display - # Open positions - df_open_account = df_open_positions[ - df_open_positions["account_id"] == filters["account_id"] - ] - - last_liq = ( - data["account_liq"] - .loc[data["account_liq"]["account_id"] == filters["account_id"], "ts"] - .max() - ) - - # this is a hack to handle the case where there are no liquidations - last_liq = last_liq if pd.isna(last_liq) == False else "2023-01-01 00:00:00+00:00" - - df_open_account = df_open_account.loc[ - df_open_account["ts"] > last_liq, - ["account_id", "market_symbol", "position_size", "notional_position_size"], - ] - - st.markdown( - """ - ### Open Positions - """ - ) - if len(df_open_account) > 0: - df_open_account - else: - st.markdown( - """ - No open positions - """ - ) - - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - pass - - # Recent trades - st.markdown( - """ - ### Recent Trades - """ - ) - - st.dataframe( - data["trade"].sort_values("ts", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Recent transfers - st.markdown( - """ - ### Recent Transfers - """ - ) - - st.dataframe( - data["transfer"].sort_values("block_timestamp", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Account liquidations table - st.markdown( - """ - ### Liquidations - """ - ) - - st.dataframe( - data["account_liq"].sort_values("ts", ascending=False).head(25), - use_container_width=True, - hide_index=True, - ) - - # Expired orders table - st.markdown( - """ - ### Expired Orders - """ - ) - - st.dataframe( - data["order_expired"], - use_container_width=True, - hide_index=True, - ) - - # Interest charged table - st.markdown( - """ - ### Interest Charged - """ - ) - - st.dataframe( - data["interest"], - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_sepolia/perp_integrators.py b/dashboard/modules/arbitrum_sepolia/perp_integrators.py deleted file mode 100644 index b1203549..00000000 --- a/dashboard/modules/arbitrum_sepolia/perp_integrators.py +++ /dev/null @@ -1,175 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # get account data - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - tracking_code, - accounts, - volume, - volume_share, - trades, - trades_share, - exchange_fees, - exchange_fees_share, - referral_fees, - referral_fees_share - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_tracking_stats_{resolution}_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "stats": df_stats, - } - - -def make_charts(data): - return { - "accounts": chart_bars( - data["stats"], - "ts", - ["accounts"], - "Accounts", - color="tracking_code", - y_format="#", - ), - "volume": chart_bars( - data["stats"], - "ts", - ["volume"], - "Volume", - color="tracking_code", - ), - "volume_pct": chart_bars( - data["stats"], - "ts", - ["volume_share"], - "Volume %", - color="tracking_code", - y_format="%", - ), - "trades": chart_bars( - data["stats"], - "ts", - ["trades"], - "Trades", - color="tracking_code", - y_format="#", - ), - "trades_pct": chart_bars( - data["stats"], - "ts", - ["trades_share"], - "Trades %", - color="tracking_code", - y_format="%", - ), - "exchange_fees": chart_bars( - data["stats"], - "ts", - ["exchange_fees"], - "Exchange Fees", - color="tracking_code", - ), - "exchange_fees_pct": chart_bars( - data["stats"], - "ts", - ["exchange_fees_share"], - "Exchange Fees %", - color="tracking_code", - y_format="%", - ), - "referral_fees": chart_bars( - data["stats"], - "ts", - ["referral_fees"], - "Referral Fees", - color="tracking_code", - ), - "referral_fees_pct": chart_bars( - data["stats"], - "ts", - ["referral_fees_share"], - "Referral Fees %", - color="tracking_code", - y_format="%", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Integrators") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["referral_fees"], use_container_width=True) - st.plotly_chart(charts["accounts"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["volume_pct"], use_container_width=True) - st.plotly_chart(charts["trades_pct"], use_container_width=True) - st.plotly_chart(charts["exchange_fees_pct"], use_container_width=True) - st.plotly_chart(charts["referral_fees_pct"], use_container_width=True) - pass - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_sepolia/perp_keepers.py b/dashboard/modules/arbitrum_sepolia/perp_keepers.py deleted file mode 100644 index 9f5842b1..00000000 --- a/dashboard/modules/arbitrum_sepolia/perp_keepers.py +++ /dev/null @@ -1,146 +0,0 @@ -import streamlit as st -import pandas as pd -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_lines, chart_many_bars, export_data -from utils import get_connection - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_keeper = pd.read_sql_query( - f""" - SELECT - ts, - keeper, - trades, - trades_pct, - amount_settled, - amount_settled_pct, - settlement_rewards, - settlement_rewards_pct - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_keeper_stats_{resolution}_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "keeper": df_keeper, - } - - -## charts -def make_charts(data): - df = data["keeper"] - - return { - "trades": chart_many_bars( - df, - "ts", - ["trades"], - "Orders Settled", - "keeper", - y_format="#", - ), - "trades_pct": chart_many_bars( - df, - "ts", - ["trades_pct"], - "Orders Settled %", - "keeper", - y_format="%", - ), - "amount_settled": chart_many_bars( - df, - "ts", - ["amount_settled"], - "Notional Size Settled", - "keeper", - ), - "amount_settled_pct": chart_many_bars( - df, - "ts", - ["amount_settled_pct"], - "Notional Size Settled %", - "keeper", - y_format="%", - ), - "settlement_rewards": chart_many_bars( - df, - "ts", - ["settlement_rewards"], - "Settlement Rewards", - "keeper", - ), - "settlement_rewards_pct": chart_many_bars( - df, - "ts", - ["settlement_rewards_pct"], - "Settlement Rewards %", - "keeper", - y_format="%", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Keepers") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["amount_settled"], use_container_width=True) - st.plotly_chart(charts["settlement_rewards"], use_container_width=True) - - with col2: - st.plotly_chart(charts["trades_pct"], use_container_width=True) - st.plotly_chart(charts["amount_settled_pct"], use_container_width=True) - st.plotly_chart(charts["settlement_rewards_pct"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_sepolia/perp_markets.py b/dashboard/modules/arbitrum_sepolia/perp_markets.py deleted file mode 100644 index 9b502837..00000000 --- a/dashboard/modules/arbitrum_sepolia/perp_markets.py +++ /dev/null @@ -1,170 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import chart_lines, chart_bars, chart_oi, export_data -from utils import get_connection - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # read data - df_market_history = pd.read_sql_query( - f""" - SELECT - ts, - market_id, - market_symbol, - funding_rate, - interest_rate, - funding_rate_apr, - long_rate_apr, - short_rate_apr, - price, - skew, - size_usd, - short_oi_pct, - long_oi_pct - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_market_history_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - volume, - trades, - exchange_fees, - liquidations - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_market_stats_daily_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "market_history": df_market_history, - "stats": df_stats, - } - - -## charts -def make_charts(data, asset): - df_market = data["market_history"][data["market_history"]["market_symbol"] == asset] - df_stats = data["stats"][data["stats"]["market_symbol"] == asset] - - return { - "rates": chart_lines( - df_market, - "ts", - ["funding_rate_apr", "interest_rate", "long_rate_apr", "short_rate_apr"], - "Rates", - smooth=True, - y_format="%", - ), - "price": chart_lines( - df_market, - "ts", - ["price"], - "Price", - smooth=True, - ), - "volume": chart_bars( - df_stats, - "ts", - ["volume"], - "Volume", - ), - "exchange_fees": chart_bars( - df_stats, - "ts", - ["exchange_fees"], - "Exchange Fees", - ), - "skew": chart_lines( - df_market, - "ts", - ["skew"], - "Market Skew", - y_format="#", - ), - "oi": chart_lines( - df_market, - "ts", - ["size_usd"], - "Open Interest: Total", - ), - "oi_pct": chart_oi( - df_market, - "ts", - "Open Interest: Long vs Short", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Markets") - - ## inputs - with st.expander("Filters") as expander: - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## market filter - assets = sorted( - data["market_history"]["market_symbol"].unique(), - key=lambda x: (x != "ETH", x != "BTC", x), - ) - asset = st.selectbox("Select asset", assets, index=0) - - ## make the charts - charts = make_charts(data, asset) - - ## display - st.plotly_chart(charts["price"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["oi"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - - with col2: - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["oi_pct"], use_container_width=True) - st.plotly_chart(charts["rates"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_sepolia/perp_monitor.py b/dashboard/modules/arbitrum_sepolia/perp_monitor.py deleted file mode 100644 index b155b548..00000000 --- a/dashboard/modules/arbitrum_sepolia/perp_monitor.py +++ /dev/null @@ -1,290 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_many_bars, chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=60) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_order_expired = pd.read_sql_query( - f""" - SELECT - block_number, - block_timestamp, - cast(account_id as text) as account_id, - market_id, - acceptable_price, - commitment_time, - tracking_code - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_previous_order_expired_arbitrum_sepolia - WHERE date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - ORDER BY block_timestamp - """, - db, - ) - df_trade = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - market_symbol, - position_size, - trade_size, - notional_trade_size, - fill_price, - total_fees, - accrued_funding, - tracking_code, - transaction_hash - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_trades_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_account_liq = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - total_reward - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_liq_account_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_skew = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - skew, - skew * price as skew_usd - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_market_history_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - # hourly data - df_market = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - volume, - trades, - exchange_fees, - liquidations - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_market_stats_{resolution}_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - liquidated_accounts, - liquidation_rewards - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_stats_{resolution}_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - # transform data - current_skew = ( - df_skew.groupby("market_symbol") - .tail(1) - .sort_values("skew_usd", ascending=False) - ) - current_skew["side"] = current_skew["skew"].apply( - lambda x: "Long" if x > 0 else ("Short" if x < 0 else "Neutral") - ) - - db.close() - - return { - "order_expired": df_order_expired, - "trade": df_trade, - "account_liq": df_account_liq, - "market": df_market, - "stats": df_stats, - "skew": df_skew, - "current_skew": current_skew, - } - - -def make_charts(data): - return { - "volume": chart_many_bars( - data["market"], - "ts", - ["volume"], - "Volume", - "market_symbol", - ), - "exchange_fees": chart_many_bars( - data["market"], - "ts", - ["exchange_fees"], - "Exchange Fees", - "market_symbol", - ), - "trades": chart_many_bars( - data["market"], - "ts", - ["trades"], - "Trades", - "market_symbol", - y_format="#", - ), - "position_liquidations": chart_many_bars( - data["market"], - "ts", - ["liquidations"], - "Position Liquidations", - "market_symbol", - y_format="#", - ), - "account_liquidations": chart_bars( - data["stats"], - "ts", - ["liquidated_accounts"], - "Account Liquidations", - y_format="#", - ), - "liquidation_rewards": chart_bars( - data["stats"], - "ts", - ["liquidation_rewards"], - "Liquidation Rewards", - ), - "skew": chart_lines( - data["skew"], - "ts", - ["skew_usd"], - "Market Skew", - "market_symbol", - ), - "current_skew": chart_bars( - data["current_skew"], - ["skew_usd"], - "side", - "Current Market Skew", - "market_symbol", - column=True, - x_format="$", - y_format="#", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Monitor") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["account_liquidations"], use_container_width=True) - - with col2: - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["position_liquidations"], use_container_width=True) - st.plotly_chart(charts["liquidation_rewards"], use_container_width=True) - - st.plotly_chart(charts["current_skew"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - - # Recent trades - st.markdown( - """ - ### Recent Trades - """ - ) - - st.dataframe( - data["trade"].sort_values("ts", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Account liquidations table - st.markdown( - """ - ### Accounts Liquidated - """ - ) - - st.dataframe( - data["account_liq"].sort_values("ts", ascending=False).head(25), - use_container_width=True, - hide_index=True, - ) - - # Expired orders table - st.markdown( - """ - ### Expired Orders - """ - ) - - st.dataframe( - data["order_expired"].sort_values("block_timestamp", ascending=False), - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_sepolia/perp_stats.py b/dashboard/modules/arbitrum_sepolia/perp_stats.py deleted file mode 100644 index 5a8fc4dd..00000000 --- a/dashboard/modules/arbitrum_sepolia/perp_stats.py +++ /dev/null @@ -1,142 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - volume, - trades, - exchange_fees, - liquidated_accounts, - liquidation_rewards, - cumulative_exchange_fees, - cumulative_volume - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_perp_stats_{resolution}_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "stats": df_stats, - } - - -def make_charts(data): - return { - "volume": chart_bars(data["stats"], "ts", ["volume"], "Volume"), - "cumulative_volume": chart_lines( - data["stats"], - "ts", - ["cumulative_volume"], - "Cumulative Volume", - smooth=True, - ), - "cumulative_fees": chart_lines( - data["stats"], - "ts", - ["cumulative_exchange_fees"], - "Cumulative Fees", - smooth=True, - ), - "fees": chart_bars( - data["stats"], - "ts", - ["exchange_fees"], - "Exchange Fees", - ), - "trades": chart_bars( - data["stats"], - "ts", - ["trades"], - "Trades", - y_format="#", - ), - "account_liquidations": chart_bars( - data["stats"], - "ts", - ["liquidated_accounts"], - "Account Liquidations", - y_format="#", - ), - "liquidation_rewards": chart_bars( - data["stats"], - "ts", - ["liquidation_rewards"], - "Liquidation Rewards", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Stats") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - st.plotly_chart(charts["account_liquidations"], use_container_width=True) - st.plotly_chart(charts["liquidation_rewards"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["fees"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - pass - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/arbitrum_sepolia/spot_markets.py b/dashboard/modules/arbitrum_sepolia/spot_markets.py deleted file mode 100644 index 9605b146..00000000 --- a/dashboard/modules/arbitrum_sepolia/spot_markets.py +++ /dev/null @@ -1,147 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(settings): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # get account data - df_wrapper = pd.read_sql_query( - f""" - SELECT - ts, - block_number, - tx_hash, - synth_market_id, - amount_wrapped - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_spot_wrapper_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_atomics = pd.read_sql_query( - f""" - SELECT - ts, - block_number, - tx_hash, - synth_market_id, - amount, - price - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_spot_atomics_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_synth_supply = pd.read_sql_query( - f""" - SELECT - ts, - synth_market_id, - supply - FROM {st.secrets.database.DB_ENV}_arbitrum_sepolia.fct_synth_supply_arbitrum_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "synth_supply": df_synth_supply, - "wrapper": df_wrapper, - "atomics": df_atomics, - } - - -def make_charts(data): - return { - "supply": chart_lines( - data["synth_supply"], - "ts", - ["supply"], - "Synth Supply", - "synth_market_id", - ), - } - - -def main(): - ## title - st.markdown( - """ - ## V3 Spot Market - """ - ) - - ## inputs - with st.expander("Filters") as expander: - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch the data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - - st.plotly_chart(charts["supply"], use_container_width=True) - - # Wrapper table - st.markdown( - """ - ### Wrapper - """ - ) - - st.dataframe( - data["wrapper"].sort_values("ts", ascending=False), - use_container_width=True, - hide_index=True, - ) - - # Atomics table - st.markdown( - """ - ### Atomic Transactions - """ - ) - - st.dataframe( - data["atomics"].sort_values("ts", ascending=False), - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_mainnet/core_stats.py b/dashboard/modules/base_mainnet/core_stats.py deleted file mode 100644 index a31cf842..00000000 --- a/dashboard/modules/base_mainnet/core_stats.py +++ /dev/null @@ -1,217 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "24h", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # get account data - df_account_delegation = pd.read_sql_query( - f""" - SELECT * FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_core_account_delegation_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_apr = pd.read_sql_query( - f""" - SELECT - ts, - coalesce(tk.token_symbol, collateral_type) as collateral_type, - collateral_value, - debt, - hourly_pnl, - rewards_usd, - hourly_issuance, - cumulative_issuance, - cumulative_pnl, - apr_{resolution} as apr, - apr_{resolution}_pnl as apr_pnl, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_core_apr_base_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.base_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - and pool_id = 1 - ORDER BY ts - """, - db, - ) - - df_apr_token = pd.read_sql_query( - f""" - SELECT - ts, - coalesce(tk.token_symbol, collateral_type) as collateral_type, - apr.reward_token, - concat(coalesce(tk.token_symbol, collateral_type), ' : ', apr.reward_token) as token_pair, - collateral_value, - rewards_usd, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_core_apr_rewards_base_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.base_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - and pool_id = 1 - and apr.reward_token is not null - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "account_delegation": df_account_delegation, - "apr": df_apr, - "apr_token": df_apr_token, - } - - -def make_charts(data, filters): - resolution = filters["resolution"] - return { - "tvl": chart_lines( - data["apr"], - "ts", - ["collateral_value"], - "TVL", - "collateral_type", - ), - "debt": chart_lines( - data["apr"], - "ts", - ["debt"], - "Debt", - "collateral_type", - ), - "hourly_issuance": chart_bars( - data["apr"], - "ts", - ["hourly_issuance"], - "Hourly Issuance", - "collateral_type", - ), - "issuance": chart_lines( - data["apr"], - "ts", - ["cumulative_issuance"], - "Issuance", - "collateral_type", - ), - "pnl": chart_lines( - data["apr"], - "ts", - ["cumulative_pnl"], - "Pnl", - "collateral_type", - ), - "hourly_pnl": chart_bars( - data["apr"], - "ts", - ["hourly_pnl"], - "Hourly Pnl", - "collateral_type", - ), - "hourly_rewards": chart_bars( - data["apr"], - "ts", - ["rewards_usd"], - "Hourly Rewards", - "collateral_type", - ), - "hourly_rewards_token": chart_bars( - data["apr_token"], - "ts", - ["rewards_usd"], - "Hourly Rewards (Collateral : Reward)", - "token_pair", - ), - "apr": chart_lines( - data["apr"], - "ts", - "apr", - f"APR - {resolution} average", - y_format="%", - color="collateral_type", - ), - "apr_token": chart_lines( - data["apr_token"], - "ts", - "apr_rewards", - f"Reward APR by Token - {resolution} average", - y_format="%", - color="token_pair", - ), - } - - -def main(): - ## title - st.markdown("## V3 Core") - - ## inputs - with st.expander("Filters"): - filters["resolution"] = st.radio( - "Resolution", - ["28d", "7d", "24h"], - ) - - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data, filters) - - ## display - st.plotly_chart(charts["apr"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["tvl"], use_container_width=True) - st.plotly_chart(charts["hourly_pnl"], use_container_width=True) - st.plotly_chart(charts["hourly_issuance"], use_container_width=True) - st.plotly_chart(charts["hourly_rewards"], use_container_width=True) - st.plotly_chart(charts["apr_token"], use_container_width=True) - - with col2: - st.plotly_chart(charts["debt"], use_container_width=True) - st.plotly_chart(charts["pnl"], use_container_width=True) - st.plotly_chart(charts["issuance"], use_container_width=True) - st.plotly_chart(charts["hourly_rewards_token"], use_container_width=True) - - st.markdown("## Top Delegators") - st.dataframe( - data["account_delegation"] - .sort_values("amount_delegated", ascending=False) - .head(25) - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_mainnet/perp_account.py b/dashboard/modules/base_mainnet/perp_account.py deleted file mode 100644 index 80bd5632..00000000 --- a/dashboard/modules/base_mainnet/perp_account.py +++ /dev/null @@ -1,309 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "account_id": "NULL", - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=60) -def fetch_data(filters): - # get filters - account_id = filters["account_id"] - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # read data - df_order = pd.read_sql_query( - f""" - SELECT distinct account_id, sender FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_orders_base_mainnet - """, - db, - ) - df_order_expired = pd.read_sql_query( - f""" - SELECT - block_timestamp, - cast(account_id as text) as account_id, - market_id, - acceptable_price, - commitment_time - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_previous_order_expired_base_mainnet - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - df_trade = pd.read_sql_query( - f""" - SELECT - ts, - cast(account_id as text) as account_id, - market_id, - market_symbol, - position_size, - notional_position_size, - trade_size, - notional_trade_size, - fill_price, - total_fees, - accrued_funding, - tracking_code - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_trades_base_mainnet - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_transfer = pd.read_sql_query( - f""" - SELECT - block_timestamp, - cast(account_id as text) as account_id, - synth_market_id, - amount_delta - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_collateral_modified_base_mainnet - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - df_interest = pd.read_sql_query( - f""" - SELECT - block_timestamp, - transaction_hash, - cast(account_id as text) as account_id, - interest - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_interest_charged_base_mainnet - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - - df_account_liq = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - total_reward - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_liq_account_base_mainnet - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_hourly = pd.read_sql_query( - f""" - SELECT - ts, - cumulative_volume, - cumulative_fees - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_account_stats_hourly_base_mainnet - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - order by ts - """, - db, - ) - - df_accounts = df_order[["account_id", "sender"]].drop_duplicates() - df_accounts.columns = ["id", "owner"] - - # one basic transform - df_transfer["amount_delta"] = df_transfer["amount_delta"] / 1e18 - - db.close() - - return { - "accounts": df_accounts, - "order_expired": df_order_expired, - "interest": df_interest, - "trade": df_trade, - "transfer": df_transfer, - "account_liq": df_account_liq, - "hourly": df_hourly, - } - - -def make_charts(data): - return { - "cumulative_volume": chart_lines( - data["hourly"], "ts", ["cumulative_volume"], "Cumulative Volume" - ), - "cumulative_fees": chart_lines( - data["hourly"], "ts", ["cumulative_fees"], "Cumulative Fees" - ), - } - - -def main(): - st.markdown("## V3 Perps Accounts") - data = fetch_data(filters) - - ## date filter - with st.expander("Date filter"): - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## account lookup - with st.expander("Look up accounts by address"): - address = st.text_input("Enter an address to look up associated accounts") - - df_accounts = data["accounts"] - account_numbers = df_accounts[(df_accounts["owner"] == address)]["id"].unique() - - # account_numbers - if len(account_numbers) > 0: - st.dataframe(account_numbers, hide_index=True) - - ## account select - accounts = data["accounts"]["id"].unique() - accounts = sorted(list([int(_) for _ in accounts])) - filters["account_id"] = st.selectbox("Select account", accounts, index=0) - data = fetch_data(filters) - - ## do some lighter transforms - df_open_positions = ( - data["trade"] - .sort_values("ts") - .groupby(["account_id", "market_id"]) - .last() - .reset_index() - ) - df_open_positions = df_open_positions[df_open_positions["position_size"].abs() > 0] - - ## make the charts - charts = make_charts(data) - - ## display - # Open positions - df_open_account = df_open_positions[ - df_open_positions["account_id"] == filters["account_id"] - ] - - last_liq = ( - data["account_liq"] - .loc[data["account_liq"]["account_id"] == filters["account_id"], "ts"] - .max() - ) - - # this is a hack to handle the case where there are no liquidations - last_liq = last_liq if pd.isna(last_liq) == False else "2023-01-01 00:00:00+00:00" - - df_open_account = df_open_account.loc[ - df_open_account["ts"] > last_liq, - ["account_id", "market_symbol", "position_size", "notional_position_size"], - ] - - st.markdown( - """ - ### Open Positions - """ - ) - if len(df_open_account) > 0: - df_open_account - else: - st.markdown( - """ - No open positions - """ - ) - - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - pass - - # Recent trades - st.markdown( - """ - ### Recent Trades - """ - ) - - st.dataframe( - data["trade"].sort_values("ts", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Recent transfers - st.markdown( - """ - ### Recent Transfers - """ - ) - - st.dataframe( - data["transfer"].sort_values("block_timestamp", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Account liquidations table - st.markdown( - """ - ### Liquidations - """ - ) - - st.dataframe( - data["account_liq"].sort_values("ts", ascending=False).head(25), - use_container_width=True, - hide_index=True, - ) - - # Expired orders table - st.markdown( - """ - ### Expired Orders - """ - ) - - st.dataframe( - data["order_expired"], - use_container_width=True, - hide_index=True, - ) - - # Interest charged table - st.markdown( - """ - ### Interest Charged - """ - ) - - st.dataframe( - data["interest"], - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_mainnet/perp_integrators.py b/dashboard/modules/base_mainnet/perp_integrators.py deleted file mode 100644 index c31f957c..00000000 --- a/dashboard/modules/base_mainnet/perp_integrators.py +++ /dev/null @@ -1,175 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # get account data - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - tracking_code, - accounts, - volume, - volume_share, - trades, - trades_share, - exchange_fees, - exchange_fees_share, - referral_fees, - referral_fees_share - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_tracking_stats_{resolution}_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "stats": df_stats, - } - - -def make_charts(data): - return { - "accounts": chart_bars( - data["stats"], - "ts", - ["accounts"], - "Accounts", - color="tracking_code", - y_format="#", - ), - "volume": chart_bars( - data["stats"], - "ts", - ["volume"], - "Volume", - color="tracking_code", - ), - "volume_pct": chart_bars( - data["stats"], - "ts", - ["volume_share"], - "Volume %", - color="tracking_code", - y_format="%", - ), - "trades": chart_bars( - data["stats"], - "ts", - ["trades"], - "Trades", - color="tracking_code", - y_format="#", - ), - "trades_pct": chart_bars( - data["stats"], - "ts", - ["trades_share"], - "Trades %", - color="tracking_code", - y_format="%", - ), - "exchange_fees": chart_bars( - data["stats"], - "ts", - ["exchange_fees"], - "Exchange Fees", - color="tracking_code", - ), - "exchange_fees_pct": chart_bars( - data["stats"], - "ts", - ["exchange_fees_share"], - "Exchange Fees %", - color="tracking_code", - y_format="%", - ), - "referral_fees": chart_bars( - data["stats"], - "ts", - ["referral_fees"], - "Referral Fees", - color="tracking_code", - ), - "referral_fees_pct": chart_bars( - data["stats"], - "ts", - ["referral_fees_share"], - "Referral Fees %", - color="tracking_code", - y_format="%", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Integrators") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["referral_fees"], use_container_width=True) - st.plotly_chart(charts["accounts"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["volume_pct"], use_container_width=True) - st.plotly_chart(charts["trades_pct"], use_container_width=True) - st.plotly_chart(charts["exchange_fees_pct"], use_container_width=True) - st.plotly_chart(charts["referral_fees_pct"], use_container_width=True) - pass - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_mainnet/perp_keepers.py b/dashboard/modules/base_mainnet/perp_keepers.py deleted file mode 100644 index 43431609..00000000 --- a/dashboard/modules/base_mainnet/perp_keepers.py +++ /dev/null @@ -1,146 +0,0 @@ -import streamlit as st -import pandas as pd -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_lines, chart_many_bars, export_data -from utils import get_connection - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_keeper = pd.read_sql_query( - f""" - SELECT - ts, - keeper, - trades, - trades_pct, - amount_settled, - amount_settled_pct, - settlement_rewards, - settlement_rewards_pct - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_keeper_stats_{resolution}_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "keeper": df_keeper, - } - - -## charts -def make_charts(data): - df = data["keeper"] - - return { - "trades": chart_many_bars( - df, - "ts", - ["trades"], - "Orders Settled", - "keeper", - y_format="#", - ), - "trades_pct": chart_many_bars( - df, - "ts", - ["trades_pct"], - "Orders Settled %", - "keeper", - y_format="%", - ), - "amount_settled": chart_many_bars( - df, - "ts", - ["amount_settled"], - "Notional Size Settled", - "keeper", - ), - "amount_settled_pct": chart_many_bars( - df, - "ts", - ["amount_settled_pct"], - "Notional Size Settled %", - "keeper", - y_format="%", - ), - "settlement_rewards": chart_many_bars( - df, - "ts", - ["settlement_rewards"], - "Settlement Rewards", - "keeper", - ), - "settlement_rewards_pct": chart_many_bars( - df, - "ts", - ["settlement_rewards_pct"], - "Settlement Rewards %", - "keeper", - y_format="%", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Keepers") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["amount_settled"], use_container_width=True) - st.plotly_chart(charts["settlement_rewards"], use_container_width=True) - - with col2: - st.plotly_chart(charts["trades_pct"], use_container_width=True) - st.plotly_chart(charts["amount_settled_pct"], use_container_width=True) - st.plotly_chart(charts["settlement_rewards_pct"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_mainnet/perp_markets.py b/dashboard/modules/base_mainnet/perp_markets.py deleted file mode 100644 index 9b9201cf..00000000 --- a/dashboard/modules/base_mainnet/perp_markets.py +++ /dev/null @@ -1,170 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import chart_lines, chart_bars, chart_oi, export_data -from utils import get_connection - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # read data - df_market_history = pd.read_sql_query( - f""" - SELECT - ts, - market_id, - market_symbol, - funding_rate, - interest_rate, - funding_rate_apr, - long_rate_apr, - short_rate_apr, - price, - skew, - size_usd, - short_oi_pct, - long_oi_pct - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_market_history_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - volume, - trades, - exchange_fees, - liquidations - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_market_stats_daily_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "market_history": df_market_history, - "stats": df_stats, - } - - -## charts -def make_charts(data, asset): - df_market = data["market_history"][data["market_history"]["market_symbol"] == asset] - df_stats = data["stats"][data["stats"]["market_symbol"] == asset] - - return { - "rates": chart_lines( - df_market, - "ts", - ["funding_rate_apr", "interest_rate", "long_rate_apr", "short_rate_apr"], - "Rates", - smooth=True, - y_format="%", - ), - "price": chart_lines( - df_market, - "ts", - ["price"], - "Price", - smooth=True, - ), - "volume": chart_bars( - df_stats, - "ts", - ["volume"], - "Volume", - ), - "exchange_fees": chart_bars( - df_stats, - "ts", - ["exchange_fees"], - "Exchange Fees", - ), - "skew": chart_lines( - df_market, - "ts", - ["skew"], - "Market Skew", - y_format="#", - ), - "oi": chart_lines( - df_market, - "ts", - ["size_usd"], - "Open Interest: Total", - ), - "oi_pct": chart_oi( - df_market, - "ts", - "Open Interest: Long vs Short", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Markets") - - ## inputs - with st.expander("Filters") as expander: - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## market filter - assets = sorted( - data["market_history"]["market_symbol"].unique(), - key=lambda x: (x != "ETH", x != "BTC", x), - ) - asset = st.selectbox("Select asset", assets, index=0) - - ## make the charts - charts = make_charts(data, asset) - - ## display - st.plotly_chart(charts["price"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["oi"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - - with col2: - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["oi_pct"], use_container_width=True) - st.plotly_chart(charts["rates"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_mainnet/perp_monitor.py b/dashboard/modules/base_mainnet/perp_monitor.py deleted file mode 100644 index 1ba286bd..00000000 --- a/dashboard/modules/base_mainnet/perp_monitor.py +++ /dev/null @@ -1,290 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_many_bars, chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=60) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_order_expired = pd.read_sql_query( - f""" - SELECT - block_number, - block_timestamp, - cast(account_id as text) as account_id, - market_id, - acceptable_price, - commitment_time, - tracking_code - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_previous_order_expired_base_mainnet - WHERE date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - ORDER BY block_timestamp - """, - db, - ) - df_trade = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - market_symbol, - position_size, - trade_size, - notional_trade_size, - fill_price, - total_fees, - accrued_funding, - tracking_code, - transaction_hash - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_trades_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_account_liq = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - total_reward - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_liq_account_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_skew = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - skew, - skew * price as skew_usd - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_market_history_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - # hourly data - df_market = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - volume, - trades, - exchange_fees, - liquidations - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_market_stats_{resolution}_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - liquidated_accounts, - liquidation_rewards - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_stats_{resolution}_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - # transform data - current_skew = ( - df_skew.groupby("market_symbol") - .tail(1) - .sort_values("skew_usd", ascending=False) - ) - current_skew["side"] = current_skew["skew"].apply( - lambda x: "Long" if x > 0 else ("Short" if x < 0 else "Neutral") - ) - - db.close() - - return { - "order_expired": df_order_expired, - "trade": df_trade, - "account_liq": df_account_liq, - "market": df_market, - "stats": df_stats, - "skew": df_skew, - "current_skew": current_skew, - } - - -def make_charts(data): - return { - "volume": chart_many_bars( - data["market"], - "ts", - ["volume"], - "Volume", - "market_symbol", - ), - "exchange_fees": chart_many_bars( - data["market"], - "ts", - ["exchange_fees"], - "Exchange Fees", - "market_symbol", - ), - "trades": chart_many_bars( - data["market"], - "ts", - ["trades"], - "Trades", - "market_symbol", - y_format="#", - ), - "position_liquidations": chart_many_bars( - data["market"], - "ts", - ["liquidations"], - "Position Liquidations", - "market_symbol", - y_format="#", - ), - "account_liquidations": chart_bars( - data["stats"], - "ts", - ["liquidated_accounts"], - "Account Liquidations", - y_format="#", - ), - "liquidation_rewards": chart_bars( - data["stats"], - "ts", - ["liquidation_rewards"], - "Liquidation Rewards", - ), - "skew": chart_lines( - data["skew"], - "ts", - ["skew_usd"], - "Market Skew", - "market_symbol", - ), - "current_skew": chart_bars( - data["current_skew"], - ["skew_usd"], - "side", - "Current Market Skew", - "market_symbol", - column=True, - x_format="$", - y_format="#", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Monitor") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["account_liquidations"], use_container_width=True) - - with col2: - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["position_liquidations"], use_container_width=True) - st.plotly_chart(charts["liquidation_rewards"], use_container_width=True) - - st.plotly_chart(charts["current_skew"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - - # Recent trades - st.markdown( - """ - ### Recent Trades - """ - ) - - st.dataframe( - data["trade"].sort_values("ts", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Account liquidations table - st.markdown( - """ - ### Accounts Liquidated - """ - ) - - st.dataframe( - data["account_liq"].sort_values("ts", ascending=False).head(25), - use_container_width=True, - hide_index=True, - ) - - # Expired orders table - st.markdown( - """ - ### Expired Orders - """ - ) - - st.dataframe( - data["order_expired"].sort_values("block_timestamp", ascending=False), - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_mainnet/perp_stats.py b/dashboard/modules/base_mainnet/perp_stats.py deleted file mode 100644 index aa4f7da6..00000000 --- a/dashboard/modules/base_mainnet/perp_stats.py +++ /dev/null @@ -1,174 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - volume, - trades, - exchange_fees, - liquidated_accounts, - liquidation_rewards, - cumulative_exchange_fees, - cumulative_volume - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_stats_{resolution}_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_buyback = pd.read_sql_query( - f""" - SELECT - ts, - snx_amount, - usd_amount, - cumulative_snx_amount, - cumulative_usd_amount - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_buyback_{resolution}_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "stats": df_stats, - "buyback": df_buyback, - } - - -def make_charts(data): - return { - "volume": chart_bars(data["stats"], "ts", ["volume"], "Volume"), - "cumulative_volume": chart_lines( - data["stats"], - "ts", - ["cumulative_volume"], - "Cumulative Volume", - smooth=True, - ), - "cumulative_fees": chart_lines( - data["stats"], - "ts", - ["cumulative_exchange_fees"], - "Cumulative Fees", - smooth=True, - ), - "fees": chart_bars( - data["stats"], - "ts", - ["exchange_fees"], - "Exchange Fees", - ), - "trades": chart_bars( - data["stats"], - "ts", - ["trades"], - "Trades", - y_format="#", - ), - "account_liquidations": chart_bars( - data["stats"], - "ts", - ["liquidated_accounts"], - "Account Liquidations", - y_format="#", - ), - "liquidation_rewards": chart_bars( - data["stats"], - "ts", - ["liquidation_rewards"], - "Liquidation Rewards", - ), - "buyback": chart_bars( - data["buyback"], - "ts", - ["snx_amount"], - "SNX Buyback", - y_format="#", - ), - "cumulative_buyback": chart_lines( - data["buyback"], - "ts", - ["cumulative_snx_amount"], - "Cumulative SNX Buyback", - y_format="#", - smooth=True, - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Stats") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - st.plotly_chart(charts["account_liquidations"], use_container_width=True) - st.plotly_chart(charts["liquidation_rewards"], use_container_width=True) - st.plotly_chart(charts["cumulative_buyback"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["fees"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["buyback"], use_container_width=True) - pass - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_mainnet/spot_markets.py b/dashboard/modules/base_mainnet/spot_markets.py deleted file mode 100644 index cc5f5997..00000000 --- a/dashboard/modules/base_mainnet/spot_markets.py +++ /dev/null @@ -1,147 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(settings): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # get account data - df_wrapper = pd.read_sql_query( - f""" - SELECT - ts, - block_number, - tx_hash, - synth_market_id, - amount_wrapped - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_spot_wrapper_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_atomics = pd.read_sql_query( - f""" - SELECT - ts, - block_number, - tx_hash, - synth_market_id, - amount, - price - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_spot_atomics_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_synth_supply = pd.read_sql_query( - f""" - SELECT - ts, - synth_market_id, - supply - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_synth_supply_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "synth_supply": df_synth_supply, - "wrapper": df_wrapper, - "atomics": df_atomics, - } - - -def make_charts(data): - return { - "supply": chart_lines( - data["synth_supply"], - "ts", - ["supply"], - "Synth Supply", - "synth_market_id", - ), - } - - -def main(): - ## title - st.markdown( - """ - ## V3 Spot Market - """ - ) - - ## inputs - with st.expander("Filters") as expander: - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch the data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - - st.plotly_chart(charts["supply"], use_container_width=True) - - # Wrapper table - st.markdown( - """ - ### Wrapper - """ - ) - - st.dataframe( - data["wrapper"].sort_values("ts", ascending=False), - use_container_width=True, - hide_index=True, - ) - - # Atomics table - st.markdown( - """ - ### Atomic Transactions - """ - ) - - st.dataframe( - data["atomics"].sort_values("ts", ascending=False), - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_sepolia/core_stats.py b/dashboard/modules/base_sepolia/core_stats.py deleted file mode 100644 index 6253559d..00000000 --- a/dashboard/modules/base_sepolia/core_stats.py +++ /dev/null @@ -1,178 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "24h", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # get account data - df_account_delegation = pd.read_sql_query( - f""" - SELECT * FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_core_account_delegation_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_apr = pd.read_sql_query( - f""" - SELECT - ts, - coalesce(tk.token_symbol, collateral_type) as collateral_type, - collateral_value, - debt, - hourly_pnl, - rewards_usd, - hourly_issuance, - cumulative_issuance, - cumulative_pnl, - apr_{resolution} as apr, - apr_{resolution}_pnl as apr_pnl, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_core_apr_base_sepolia apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.base_sepolia_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - and pool_id = 1 - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "account_delegation": df_account_delegation, - "apr": df_apr, - } - - -def make_charts(data, filters): - resolution = filters["resolution"] - return { - "tvl": chart_lines( - data["apr"], - "ts", - ["collateral_value"], - "TVL", - "collateral_type", - ), - "debt": chart_lines( - data["apr"], - "ts", - ["debt"], - "Debt", - "collateral_type", - ), - "hourly_issuance": chart_bars( - data["apr"], - "ts", - ["hourly_issuance"], - "Hourly Issuance", - "collateral_type", - ), - "issuance": chart_lines( - data["apr"], - "ts", - ["cumulative_issuance"], - "Issuance", - "collateral_type", - ), - "pnl": chart_lines( - data["apr"], - "ts", - ["cumulative_pnl"], - "Pnl", - "collateral_type", - ), - "hourly_pnl": chart_bars( - data["apr"], - "ts", - ["hourly_pnl"], - "Hourly Pnl", - "collateral_type", - ), - "hourly_rewards": chart_bars( - data["apr"], - "ts", - ["rewards_usd"], - "Hourly Rewards", - "collateral_type", - ), - "apr": chart_lines( - data["apr"], - "ts", - ["apr", "apr_pnl", "apr_rewards"], - f"APR - {resolution} average", - y_format="%", - ), - } - - -def main(): - ## title - st.markdown("## V3 Core") - - ## inputs - with st.expander("Filters"): - filters["resolution"] = st.radio( - "Resolution", - ["28d", "7d", "24h"], - ) - - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data, filters) - - ## display - st.plotly_chart(charts["apr"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["tvl"], use_container_width=True) - st.plotly_chart(charts["hourly_pnl"], use_container_width=True) - st.plotly_chart(charts["hourly_issuance"], use_container_width=True) - st.plotly_chart(charts["hourly_rewards"], use_container_width=True) - - with col2: - st.plotly_chart(charts["debt"], use_container_width=True) - st.plotly_chart(charts["pnl"], use_container_width=True) - st.plotly_chart(charts["issuance"], use_container_width=True) - - st.markdown("## Top Delegators") - st.dataframe( - data["account_delegation"] - .sort_values("amount_delegated", ascending=False) - .head(25) - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_sepolia/perp_account.py b/dashboard/modules/base_sepolia/perp_account.py deleted file mode 100644 index 3dec0318..00000000 --- a/dashboard/modules/base_sepolia/perp_account.py +++ /dev/null @@ -1,309 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "account_id": "NULL", - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=60) -def fetch_data(filters): - # get filters - account_id = filters["account_id"] - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # read data - df_order = pd.read_sql_query( - f""" - SELECT distinct account_id, sender FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_orders_base_sepolia - """, - db, - ) - df_order_expired = pd.read_sql_query( - f""" - SELECT - block_timestamp, - cast(account_id as text) as account_id, - market_id, - acceptable_price, - commitment_time - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_previous_order_expired_base_sepolia - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - df_trade = pd.read_sql_query( - f""" - SELECT - ts, - cast(account_id as text) as account_id, - market_id, - market_symbol, - position_size, - notional_position_size, - trade_size, - notional_trade_size, - fill_price, - total_fees, - accrued_funding, - tracking_code - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_trades_base_sepolia - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_transfer = pd.read_sql_query( - f""" - SELECT - block_timestamp, - cast(account_id as text) as account_id, - synth_market_id, - amount_delta - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_collateral_modified_base_sepolia - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - df_interest = pd.read_sql_query( - f""" - SELECT - block_timestamp, - transaction_hash, - cast(account_id as text) as account_id, - interest - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_interest_charged_base_sepolia - WHERE account_id = {account_id if account_id else 'NULL'} - and date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - """, - db, - ) - - df_account_liq = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - total_reward - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_liq_account_base_sepolia - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_hourly = pd.read_sql_query( - f""" - SELECT - ts, - cumulative_volume, - cumulative_fees - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_account_stats_hourly_base_sepolia - WHERE account_id = '{account_id}' - and ts >= '{start_date}' and ts <= '{end_date}' - order by ts - """, - db, - ) - - df_accounts = df_order[["account_id", "sender"]].drop_duplicates() - df_accounts.columns = ["id", "owner"] - - # one basic transform - df_transfer["amount_delta"] = df_transfer["amount_delta"] / 1e18 - - db.close() - - return { - "accounts": df_accounts, - "order_expired": df_order_expired, - "interest": df_interest, - "trade": df_trade, - "transfer": df_transfer, - "account_liq": df_account_liq, - "hourly": df_hourly, - } - - -def make_charts(data): - return { - "cumulative_volume": chart_lines( - data["hourly"], "ts", ["cumulative_volume"], "Cumulative Volume" - ), - "cumulative_fees": chart_lines( - data["hourly"], "ts", ["cumulative_fees"], "Cumulative Fees" - ), - } - - -def main(): - st.markdown("## V3 Perps Accounts") - data = fetch_data(filters) - - ## date filter - with st.expander("Date filter"): - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## account lookup - with st.expander("Look up accounts by address"): - address = st.text_input("Enter an address to look up associated accounts") - - df_accounts = data["accounts"] - account_numbers = df_accounts[(df_accounts["owner"] == address)]["id"].unique() - - # account_numbers - if len(account_numbers) > 0: - st.dataframe(account_numbers, hide_index=True) - - ## account select - accounts = data["accounts"]["id"].unique() - accounts = sorted(list([int(_) for _ in accounts])) - filters["account_id"] = st.selectbox("Select account", accounts, index=0) - data = fetch_data(filters) - - ## do some lighter transforms - df_open_positions = ( - data["trade"] - .sort_values("ts") - .groupby(["account_id", "market_id"]) - .last() - .reset_index() - ) - df_open_positions = df_open_positions[df_open_positions["position_size"].abs() > 0] - - ## make the charts - charts = make_charts(data) - - ## display - # Open positions - df_open_account = df_open_positions[ - df_open_positions["account_id"] == filters["account_id"] - ] - - last_liq = ( - data["account_liq"] - .loc[data["account_liq"]["account_id"] == filters["account_id"], "ts"] - .max() - ) - - # this is a hack to handle the case where there are no liquidations - last_liq = last_liq if pd.isna(last_liq) == False else "2023-01-01 00:00:00+00:00" - - df_open_account = df_open_account.loc[ - df_open_account["ts"] > last_liq, - ["account_id", "market_symbol", "position_size", "notional_position_size"], - ] - - st.markdown( - """ - ### Open Positions - """ - ) - if len(df_open_account) > 0: - df_open_account - else: - st.markdown( - """ - No open positions - """ - ) - - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - pass - - # Recent trades - st.markdown( - """ - ### Recent Trades - """ - ) - - st.dataframe( - data["trade"].sort_values("ts", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Recent transfers - st.markdown( - """ - ### Recent Transfers - """ - ) - - st.dataframe( - data["transfer"].sort_values("block_timestamp", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Account liquidations table - st.markdown( - """ - ### Liquidations - """ - ) - - st.dataframe( - data["account_liq"].sort_values("ts", ascending=False).head(25), - use_container_width=True, - hide_index=True, - ) - - # Expired orders table - st.markdown( - """ - ### Expired Orders - """ - ) - - st.dataframe( - data["order_expired"], - use_container_width=True, - hide_index=True, - ) - - # Interest charged table - st.markdown( - """ - ### Interest Charged - """ - ) - - st.dataframe( - data["interest"], - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_sepolia/perp_integrators.py b/dashboard/modules/base_sepolia/perp_integrators.py deleted file mode 100644 index 4d777d46..00000000 --- a/dashboard/modules/base_sepolia/perp_integrators.py +++ /dev/null @@ -1,175 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # get account data - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - tracking_code, - accounts, - volume, - volume_share, - trades, - trades_share, - exchange_fees, - exchange_fees_share, - referral_fees, - referral_fees_share - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_tracking_stats_{resolution}_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "stats": df_stats, - } - - -def make_charts(data): - return { - "accounts": chart_bars( - data["stats"], - "ts", - ["accounts"], - "Accounts", - color="tracking_code", - y_format="#", - ), - "volume": chart_bars( - data["stats"], - "ts", - ["volume"], - "Volume", - color="tracking_code", - ), - "volume_pct": chart_bars( - data["stats"], - "ts", - ["volume_share"], - "Volume %", - color="tracking_code", - y_format="%", - ), - "trades": chart_bars( - data["stats"], - "ts", - ["trades"], - "Trades", - color="tracking_code", - y_format="#", - ), - "trades_pct": chart_bars( - data["stats"], - "ts", - ["trades_share"], - "Trades %", - color="tracking_code", - y_format="%", - ), - "exchange_fees": chart_bars( - data["stats"], - "ts", - ["exchange_fees"], - "Exchange Fees", - color="tracking_code", - ), - "exchange_fees_pct": chart_bars( - data["stats"], - "ts", - ["exchange_fees_share"], - "Exchange Fees %", - color="tracking_code", - y_format="%", - ), - "referral_fees": chart_bars( - data["stats"], - "ts", - ["referral_fees"], - "Referral Fees", - color="tracking_code", - ), - "referral_fees_pct": chart_bars( - data["stats"], - "ts", - ["referral_fees_share"], - "Referral Fees %", - color="tracking_code", - y_format="%", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Integrators") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["referral_fees"], use_container_width=True) - st.plotly_chart(charts["accounts"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["volume_pct"], use_container_width=True) - st.plotly_chart(charts["trades_pct"], use_container_width=True) - st.plotly_chart(charts["exchange_fees_pct"], use_container_width=True) - st.plotly_chart(charts["referral_fees_pct"], use_container_width=True) - pass - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_sepolia/perp_keepers.py b/dashboard/modules/base_sepolia/perp_keepers.py deleted file mode 100644 index e22d74c0..00000000 --- a/dashboard/modules/base_sepolia/perp_keepers.py +++ /dev/null @@ -1,146 +0,0 @@ -import streamlit as st -import pandas as pd -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_lines, chart_many_bars, export_data -from utils import get_connection - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_keeper = pd.read_sql_query( - f""" - SELECT - ts, - keeper, - trades, - trades_pct, - amount_settled, - amount_settled_pct, - settlement_rewards, - settlement_rewards_pct - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_keeper_stats_{resolution}_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "keeper": df_keeper, - } - - -## charts -def make_charts(data): - df = data["keeper"] - - return { - "trades": chart_many_bars( - df, - "ts", - ["trades"], - "Orders Settled", - "keeper", - y_format="#", - ), - "trades_pct": chart_many_bars( - df, - "ts", - ["trades_pct"], - "Orders Settled %", - "keeper", - y_format="%", - ), - "amount_settled": chart_many_bars( - df, - "ts", - ["amount_settled"], - "Notional Size Settled", - "keeper", - ), - "amount_settled_pct": chart_many_bars( - df, - "ts", - ["amount_settled_pct"], - "Notional Size Settled %", - "keeper", - y_format="%", - ), - "settlement_rewards": chart_many_bars( - df, - "ts", - ["settlement_rewards"], - "Settlement Rewards", - "keeper", - ), - "settlement_rewards_pct": chart_many_bars( - df, - "ts", - ["settlement_rewards_pct"], - "Settlement Rewards %", - "keeper", - y_format="%", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Keepers") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["amount_settled"], use_container_width=True) - st.plotly_chart(charts["settlement_rewards"], use_container_width=True) - - with col2: - st.plotly_chart(charts["trades_pct"], use_container_width=True) - st.plotly_chart(charts["amount_settled_pct"], use_container_width=True) - st.plotly_chart(charts["settlement_rewards_pct"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_sepolia/perp_markets.py b/dashboard/modules/base_sepolia/perp_markets.py deleted file mode 100644 index 97e95f54..00000000 --- a/dashboard/modules/base_sepolia/perp_markets.py +++ /dev/null @@ -1,170 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import chart_lines, chart_bars, chart_oi, export_data -from utils import get_connection - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # read data - df_market_history = pd.read_sql_query( - f""" - SELECT - ts, - market_id, - market_symbol, - funding_rate, - interest_rate, - funding_rate_apr, - long_rate_apr, - short_rate_apr, - price, - skew, - size_usd, - short_oi_pct, - long_oi_pct - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_market_history_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - volume, - trades, - exchange_fees, - liquidations - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_market_stats_daily_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "market_history": df_market_history, - "stats": df_stats, - } - - -## charts -def make_charts(data, asset): - df_market = data["market_history"][data["market_history"]["market_symbol"] == asset] - df_stats = data["stats"][data["stats"]["market_symbol"] == asset] - - return { - "rates": chart_lines( - df_market, - "ts", - ["funding_rate_apr", "interest_rate", "long_rate_apr", "short_rate_apr"], - "Rates", - smooth=True, - y_format="%", - ), - "price": chart_lines( - df_market, - "ts", - ["price"], - "Price", - smooth=True, - ), - "volume": chart_bars( - df_stats, - "ts", - ["volume"], - "Volume", - ), - "exchange_fees": chart_bars( - df_stats, - "ts", - ["exchange_fees"], - "Exchange Fees", - ), - "skew": chart_lines( - df_market, - "ts", - ["skew"], - "Market Skew", - y_format="#", - ), - "oi": chart_lines( - df_market, - "ts", - ["size_usd"], - "Open Interest: Total", - ), - "oi_pct": chart_oi( - df_market, - "ts", - "Open Interest: Long vs Short", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Markets") - - ## inputs - with st.expander("Filters") as expander: - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## market filter - assets = sorted( - data["market_history"]["market_symbol"].unique(), - key=lambda x: (x != "ETH", x != "BTC", x), - ) - asset = st.selectbox("Select asset", assets, index=0) - - ## make the charts - charts = make_charts(data, asset) - - ## display - st.plotly_chart(charts["price"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["oi"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - - with col2: - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["oi_pct"], use_container_width=True) - st.plotly_chart(charts["rates"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_sepolia/perp_monitor.py b/dashboard/modules/base_sepolia/perp_monitor.py deleted file mode 100644 index e84d8099..00000000 --- a/dashboard/modules/base_sepolia/perp_monitor.py +++ /dev/null @@ -1,290 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_many_bars, chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=60) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_order_expired = pd.read_sql_query( - f""" - SELECT - block_number, - block_timestamp, - cast(account_id as text) as account_id, - market_id, - acceptable_price, - commitment_time, - tracking_code - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_previous_order_expired_base_sepolia - WHERE date(block_timestamp) >= '{start_date}' and date(block_timestamp) <= '{end_date}' - ORDER BY block_timestamp - """, - db, - ) - df_trade = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - market_symbol, - position_size, - trade_size, - notional_trade_size, - fill_price, - total_fees, - accrued_funding, - tracking_code, - transaction_hash - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_trades_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_account_liq = pd.read_sql_query( - f""" - SELECT - ts, - account_id, - total_reward - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_liq_account_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_skew = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - skew, - skew * price as skew_usd - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_market_history_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - # hourly data - df_market = pd.read_sql_query( - f""" - SELECT - ts, - market_symbol, - volume, - trades, - exchange_fees, - liquidations - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_market_stats_{resolution}_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - liquidated_accounts, - liquidation_rewards - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_stats_{resolution}_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - # transform data - current_skew = ( - df_skew.groupby("market_symbol") - .tail(1) - .sort_values("skew_usd", ascending=False) - ) - current_skew["side"] = current_skew["skew"].apply( - lambda x: "Long" if x > 0 else ("Short" if x < 0 else "Neutral") - ) - - db.close() - - return { - "order_expired": df_order_expired, - "trade": df_trade, - "account_liq": df_account_liq, - "market": df_market, - "stats": df_stats, - "skew": df_skew, - "current_skew": current_skew, - } - - -def make_charts(data): - return { - "volume": chart_many_bars( - data["market"], - "ts", - ["volume"], - "Volume", - "market_symbol", - ), - "exchange_fees": chart_many_bars( - data["market"], - "ts", - ["exchange_fees"], - "Exchange Fees", - "market_symbol", - ), - "trades": chart_many_bars( - data["market"], - "ts", - ["trades"], - "Trades", - "market_symbol", - y_format="#", - ), - "position_liquidations": chart_many_bars( - data["market"], - "ts", - ["liquidations"], - "Position Liquidations", - "market_symbol", - y_format="#", - ), - "account_liquidations": chart_bars( - data["stats"], - "ts", - ["liquidated_accounts"], - "Account Liquidations", - y_format="#", - ), - "liquidation_rewards": chart_bars( - data["stats"], - "ts", - ["liquidation_rewards"], - "Liquidation Rewards", - ), - "skew": chart_lines( - data["skew"], - "ts", - ["skew_usd"], - "Market Skew", - "market_symbol", - ), - "current_skew": chart_bars( - data["current_skew"], - ["skew_usd"], - "side", - "Current Market Skew", - "market_symbol", - column=True, - x_format="$", - y_format="#", - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Monitor") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["account_liquidations"], use_container_width=True) - - with col2: - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["position_liquidations"], use_container_width=True) - st.plotly_chart(charts["liquidation_rewards"], use_container_width=True) - - st.plotly_chart(charts["current_skew"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - - # Recent trades - st.markdown( - """ - ### Recent Trades - """ - ) - - st.dataframe( - data["trade"].sort_values("ts", ascending=False).head(50), - use_container_width=True, - hide_index=True, - ) - - # Account liquidations table - st.markdown( - """ - ### Accounts Liquidated - """ - ) - - st.dataframe( - data["account_liq"].sort_values("ts", ascending=False).head(25), - use_container_width=True, - hide_index=True, - ) - - # Expired orders table - st.markdown( - """ - ### Expired Orders - """ - ) - - st.dataframe( - data["order_expired"].sort_values("block_timestamp", ascending=False), - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_sepolia/perp_stats.py b/dashboard/modules/base_sepolia/perp_stats.py deleted file mode 100644 index 2b25402e..00000000 --- a/dashboard/modules/base_sepolia/perp_stats.py +++ /dev/null @@ -1,174 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "daily", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # read data - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - volume, - trades, - exchange_fees, - liquidated_accounts, - liquidation_rewards, - cumulative_exchange_fees, - cumulative_volume - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_perp_stats_{resolution}_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_buyback = pd.read_sql_query( - f""" - SELECT - ts, - snx_amount, - usd_amount, - cumulative_snx_amount, - cumulative_usd_amount - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_buyback_{resolution}_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "stats": df_stats, - "buyback": df_buyback, - } - - -def make_charts(data): - return { - "volume": chart_bars(data["stats"], "ts", ["volume"], "Volume"), - "cumulative_volume": chart_lines( - data["stats"], - "ts", - ["cumulative_volume"], - "Cumulative Volume", - smooth=True, - ), - "cumulative_fees": chart_lines( - data["stats"], - "ts", - ["cumulative_exchange_fees"], - "Cumulative Fees", - smooth=True, - ), - "fees": chart_bars( - data["stats"], - "ts", - ["exchange_fees"], - "Exchange Fees", - ), - "trades": chart_bars( - data["stats"], - "ts", - ["trades"], - "Trades", - y_format="#", - ), - "account_liquidations": chart_bars( - data["stats"], - "ts", - ["liquidated_accounts"], - "Account Liquidations", - y_format="#", - ), - "liquidation_rewards": chart_bars( - data["stats"], - "ts", - ["liquidation_rewards"], - "Liquidation Rewards", - ), - "buyback": chart_bars( - data["buyback"], - "ts", - ["snx_amount"], - "SNX Buyback", - y_format="#", - ), - "cumulative_buyback": chart_lines( - data["buyback"], - "ts", - ["cumulative_snx_amount"], - "Cumulative SNX Buyback", - y_format="#", - smooth=True, - ), - } - - -def main(): - ## title - st.markdown("## V3 Perps Stats") - - ## inputs - with st.expander("Filters") as expander: - # resolution - filters["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - st.plotly_chart(charts["account_liquidations"], use_container_width=True) - st.plotly_chart(charts["liquidation_rewards"], use_container_width=True) - st.plotly_chart(charts["cumulative_buyback"], use_container_width=True) - pass - - with col2: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["fees"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["buyback"], use_container_width=True) - pass - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/base_sepolia/spot_markets.py b/dashboard/modules/base_sepolia/spot_markets.py deleted file mode 100644 index 6fc289e3..00000000 --- a/dashboard/modules/base_sepolia/spot_markets.py +++ /dev/null @@ -1,147 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(settings): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - - # initialize connection - db = get_connection() - - # get account data - df_wrapper = pd.read_sql_query( - f""" - SELECT - ts, - block_number, - tx_hash, - synth_market_id, - amount_wrapped - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_spot_wrapper_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_atomics = pd.read_sql_query( - f""" - SELECT - ts, - block_number, - tx_hash, - synth_market_id, - amount, - price - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_spot_atomics_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_synth_supply = pd.read_sql_query( - f""" - SELECT - ts, - synth_market_id, - supply - FROM {st.secrets.database.DB_ENV}_base_sepolia.fct_synth_supply_base_sepolia - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - db.close() - - return { - "synth_supply": df_synth_supply, - "wrapper": df_wrapper, - "atomics": df_atomics, - } - - -def make_charts(data): - return { - "supply": chart_lines( - data["synth_supply"], - "ts", - ["supply"], - "Synth Supply", - "synth_market_id", - ), - } - - -def main(): - ## title - st.markdown( - """ - ## V3 Spot Market - """ - ) - - ## inputs - with st.expander("Filters") as expander: - # date filter - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - ## fetch the data - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data) - - ## display - - st.plotly_chart(charts["supply"], use_container_width=True) - - # Wrapper table - st.markdown( - """ - ### Wrapper - """ - ) - - st.dataframe( - data["wrapper"].sort_values("ts", ascending=False), - use_container_width=True, - hide_index=True, - ) - - # Atomics table - st.markdown( - """ - ### Atomic Transactions - """ - ) - - st.dataframe( - data["atomics"].sort_values("ts", ascending=False), - use_container_width=True, - hide_index=True, - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/eth_mainnet/core_stats.py b/dashboard/modules/eth_mainnet/core_stats.py deleted file mode 100644 index b9297afa..00000000 --- a/dashboard/modules/eth_mainnet/core_stats.py +++ /dev/null @@ -1,217 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import get_connection -from utils import chart_bars, chart_lines, export_data - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date() + timedelta(days=1), - "resolution": "24h", -} - - -## data -@st.cache_data(ttl=600) -def fetch_data(filters): - # get filters - start_date = filters["start_date"] - end_date = filters["end_date"] - resolution = filters["resolution"] - - # initialize connection - db = get_connection() - - # get account data - df_account_delegation = pd.read_sql_query( - f""" - SELECT * FROM {st.secrets.database.DB_ENV}_eth_mainnet.fct_core_account_delegation_eth_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - """, - db, - ) - - df_apr = pd.read_sql_query( - f""" - SELECT - ts, - coalesce(tk.token_symbol, collateral_type) as collateral_type, - collateral_value, - debt, - hourly_pnl, - rewards_usd, - hourly_issuance, - cumulative_issuance, - cumulative_pnl, - apr_{resolution} as apr, - apr_{resolution}_pnl as apr_pnl, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_eth_mainnet.fct_core_apr_eth_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.eth_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - and pool_id = 1 - ORDER BY ts - """, - db, - ) - - df_apr_token = pd.read_sql_query( - f""" - SELECT - ts, - coalesce(tk.token_symbol, collateral_type) as collateral_type, - apr.reward_token, - concat(coalesce(tk.token_symbol, collateral_type), ' : ', apr.reward_token) as token_pair, - collateral_value, - rewards_usd, - apr_{resolution}_rewards as apr_rewards - FROM {st.secrets.database.DB_ENV}_eth_mainnet.fct_core_apr_rewards_eth_mainnet apr - LEFT JOIN {st.secrets.database.DB_ENV}_seeds.eth_mainnet_tokens tk on lower(apr.collateral_type) = lower(tk.token_address) - WHERE ts >= '{start_date}' and ts <= '{end_date}' - and pool_id = 1 - and apr.reward_token is not null - ORDER BY ts - """, - db, - ) - - db.close() - - return { - "account_delegation": df_account_delegation, - "apr": df_apr, - "apr_token": df_apr_token, - } - - -def make_charts(data, filters): - resolution = filters["resolution"] - return { - "tvl": chart_lines( - data["apr"], - "ts", - ["collateral_value"], - "TVL", - "collateral_type", - ), - "debt": chart_lines( - data["apr"], - "ts", - ["debt"], - "Debt", - "collateral_type", - ), - "hourly_issuance": chart_bars( - data["apr"], - "ts", - ["hourly_issuance"], - "Hourly Issuance", - "collateral_type", - ), - "issuance": chart_lines( - data["apr"], - "ts", - ["cumulative_issuance"], - "Issuance", - "collateral_type", - ), - "pnl": chart_lines( - data["apr"], - "ts", - ["cumulative_pnl"], - "Pnl", - "collateral_type", - ), - "hourly_pnl": chart_bars( - data["apr"], - "ts", - ["hourly_pnl"], - "Hourly Pnl", - "collateral_type", - ), - "hourly_rewards": chart_bars( - data["apr"], - "ts", - ["rewards_usd"], - "Hourly Rewards", - "collateral_type", - ), - "hourly_rewards_token": chart_bars( - data["apr_token"], - "ts", - ["rewards_usd"], - "Hourly Rewards (Collateral : Reward)", - "token_pair", - ), - "apr": chart_lines( - data["apr"], - "ts", - "apr", - f"APR - {resolution} average", - y_format="%", - color="collateral_type", - ), - "apr_token": chart_lines( - data["apr_token"], - "ts", - "apr_rewards", - f"Reward APR by Token - {resolution} average", - y_format="%", - color="token_pair", - ), - } - - -def main(): - ## title - st.markdown("## V3 Core") - - ## inputs - with st.expander("Filters"): - filters["resolution"] = st.radio( - "Resolution", - ["28d", "7d", "24h"], - ) - - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - data = fetch_data(filters) - - ## make the charts - charts = make_charts(data, filters) - - ## display - st.plotly_chart(charts["apr"], use_container_width=True) - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["tvl"], use_container_width=True) - st.plotly_chart(charts["hourly_pnl"], use_container_width=True) - st.plotly_chart(charts["hourly_issuance"], use_container_width=True) - st.plotly_chart(charts["hourly_rewards"], use_container_width=True) - st.plotly_chart(charts["apr_token"], use_container_width=True) - - with col2: - st.plotly_chart(charts["debt"], use_container_width=True) - st.plotly_chart(charts["pnl"], use_container_width=True) - st.plotly_chart(charts["issuance"], use_container_width=True) - st.plotly_chart(charts["hourly_rewards_token"], use_container_width=True) - - st.markdown("## Top Delegators") - st.dataframe( - data["account_delegation"] - .sort_values("amount_delegated", ascending=False) - .head(25) - ) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/milestones/base_andromeda.py b/dashboard/modules/milestones/base_andromeda.py deleted file mode 100644 index dd50e97e..00000000 --- a/dashboard/modules/milestones/base_andromeda.py +++ /dev/null @@ -1,86 +0,0 @@ -import streamlit as st -import pandas as pd -from datetime import datetime, timedelta -from utils import get_connection -from .charts import chart_volume, chart_collateral - - -## data -@st.cache_data(ttl=600) -def fetch_data(): - # set filters - start_date = datetime.today().date() - timedelta(days=28) - end_date = datetime.today().date() + timedelta(days=1) - - # initialize connection - db = get_connection() - - # read data - df_stats = pd.read_sql_query( - f""" - SELECT - ts, - volume, - cumulative_volume - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_perp_stats_daily_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - df_collateral = pd.read_sql_query( - f""" - SELECT - ts, - pool_id, - collateral_type, - amount, - collateral_value - FROM {st.secrets.database.DB_ENV}_base_mainnet.fct_core_vault_collateral_base_mainnet - WHERE ts >= '{start_date}' and ts <= '{end_date}' - ORDER BY ts - """, - db, - ) - - # data transformations - # add 7 day rolling volume average for volume field - df_stats["volume_7d"] = df_stats["volume"].rolling(window=7).mean() - - db.close() - - return { - "stats": df_stats, - "collateral": df_collateral, - } - - -def make_charts(data): - return { - "volume": chart_volume(data["stats"]), - "collateral": chart_collateral(data["collateral"]), - } - - -def main(): - ## title - st.markdown( - """ -## **Scale Base Andromeda** - -Measuring progress toward Synthetix's milestones on Base. - -1. Perps to \$100M daily volume -2. \$10M LP collateral. - """ - ) - - ## fetch data - data = fetch_data() - - ## make the charts - charts = make_charts(data) - - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["collateral"], use_container_width=True) diff --git a/dashboard/modules/milestones/charts.py b/dashboard/modules/milestones/charts.py deleted file mode 100644 index 4403edcd..00000000 --- a/dashboard/modules/milestones/charts.py +++ /dev/null @@ -1,95 +0,0 @@ -from datetime import timedelta -from utils import categorical -import plotly.express as px -import plotly.graph_objects as go -from utils import chart_lines - - -def chart_volume(df): - fig = px.bar( - df, - x="ts", - y="volume", - title="Base Andromeda Perps Volume", - color_discrete_sequence=categorical, - template="plotly_dark", - ) - - # add the 7 day avg line - fig.add_trace( - go.Scatter( - x=df["ts"], - y=df["volume_7d"], - mode="lines", - line=dict(color=categorical[1]), - name="7d Avg Volume", - ) - ) - - # set the y axis max to 110 million - fig.update_yaxes(range=[0, 110000000]) - - # add a horizontal line at 100 million - fig.add_shape( - dict( - type="line", - x0=df["ts"].min() - timedelta(days=1), - x1=df["ts"].max() + timedelta(days=1), - y0=100000000, - y1=100000000, - line=dict(color="red", width=2), - ) - ) - - # remove axis labels - fig.update_xaxes( - title_text="", - automargin=True, - ) - fig.update_yaxes(title_text="") - fig.update_traces(hovertemplate=None) - fig.update_yaxes(tickprefix="$") - - # Update layout for dark theme readability - fig.update_layout( - hovermode="x unified", - legend=dict( - orientation="h", - yanchor="bottom", - y=1.02, - xanchor="right", - x=1, - title=None, - ), - font=dict( - family="sans-serif", - ), - ) - return fig - - -def chart_collateral(df): - fig = chart_lines( - df, - "ts", - ["collateral_value"], - "Base Andromeda LP Collateral", - smooth=True, - ) - - # set the y axis max to 11 million - fig.update_yaxes(range=[0, max(11000000, df["collateral_value"].max())]) - - # add a horizontal line at 10 million - fig.add_shape( - dict( - type="line", - x0=df["ts"].min() - timedelta(days=1), - x1=df["ts"].max() + timedelta(days=1), - y0=10000000, - y1=10000000, - line=dict(color="red", width=2), - ) - ) - - return fig diff --git a/dashboard/modules/op_mainnet/perp_integrators.py b/dashboard/modules/op_mainnet/perp_integrators.py deleted file mode 100644 index d71dafd2..00000000 --- a/dashboard/modules/op_mainnet/perp_integrators.py +++ /dev/null @@ -1,269 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_bars, chart_lines, export_data - - -## data -@st.cache_data(ttl=600) -def fetch_data(): - # initialize connection - conn = sqlite3.connect("/app/data/perps.db") - - # read data - df = pd.read_sql("SELECT * FROM v2_trades order by timestamp", conn) - - df["date"] = pd.to_datetime(df["timestamp"], unit="s") - return df - - -def make_daily_data(df): - # make daily data - keepers = [ - "kwenta", - "polynomial", - "decentrex", - "lyra", - "rage", - "dhedge", - "atlantean", - "valio", - "copin", - "dextoro", - "kwentapysdk", - ] - df["tracking_code"] = df["trackingCode"].apply( - lambda x: ( - x.capitalize() - if x.lower() in keepers - else "No tracking code" if x == "" else "Other" - ) - ) - df["day"] = df["date"].dt.floor("D") - - df_no_liq = df[df["orderType"] != "Liquidation"] - - df_daily_sum = ( - df_no_liq.groupby(["day", "tracking_code"])[ - [ - "volume", - "exchange_fees_paid", - ] - ] - .sum() - .reset_index() - ) - df_daily_sum.columns = ["day", "tracking_code", "volume", "fees"] - - df_daily_count = ( - df_no_liq.groupby(["day", "tracking_code"])[ - [ - "id", - "account", - ] - ] - .nunique() - .reset_index() - ) - df_daily_count.columns = ["day", "tracking_code", "trades", "traders"] - - # Unique cumulative traders - def cumulative_unique(df): - unique_accounts = set() - result = [] - - for account in df["account"]: - unique_accounts.add(account) - result.append(len(unique_accounts)) - - return pd.Series(result, index=df.index) - - df_no_liq["traders_cumulative"] = ( - df_no_liq.groupby("tracking_code") - .apply(cumulative_unique) - .reset_index(level=0, drop=True) - ) - df_traders = df_no_liq.drop_duplicates( - subset=["tracking_code", "day"], keep="last" - )[["tracking_code", "day", "traders_cumulative"]] - - # merge the datasets - df_daily = df_daily_sum.merge( - df_daily_count, how="left", on=["day", "tracking_code"] - ) - df_daily = df_daily.merge(df_traders, how="left", on=["day", "tracking_code"]) - - # add percentages - df_daily["volume_pct"] = df_daily.groupby("day")["volume"].transform( - lambda x: x / x.sum() - ) - df_daily["fees_pct"] = df_daily.groupby("day")["fees"].transform( - lambda x: x / x.sum() - ) - df_daily["trades_pct"] = df_daily.groupby("day")["trades"].transform( - lambda x: x / x.sum() - ) - - # add cumulative values - df_daily["volume_cumulative"] = df_daily.groupby("tracking_code")["volume"].cumsum() - df_daily["fees_cumulative"] = df_daily.groupby("tracking_code")["fees"].cumsum() - df_daily["trades_cumulative"] = df_daily.groupby("tracking_code")["trades"].cumsum() - return df_daily - - -def filter_data(df, start_date, end_date, assets): - start_date = pd.to_datetime(start_date) - end_date = pd.to_datetime(end_date).replace(hour=23, minute=59, second=59) - - df = df[ - (df["date"] >= start_date) - & (df["date"] <= end_date) - & (df["asset"].isin(assets)) - ] - df_daily = make_daily_data(df[df["asset"].isin(assets)]) - df_daily = df_daily[(df_daily["day"] >= start_date) & (df_daily["day"] <= end_date)] - return df, df_daily - - -## charts -def make_charts(df, df_daily): - return { - "fees": chart_bars( - df_daily, "day", ["fees"], "Fees Paid", color="tracking_code" - ), - "fees_pct": chart_bars( - df_daily, - "day", - ["fees_pct"], - "Fees Paid %", - color="tracking_code", - y_format="%", - ), - "fees_cumulative": chart_lines( - df_daily, - "day", - ["fees_cumulative"], - "Cumulative Fees Paid", - color="tracking_code", - smooth=True, - ), - "volume": chart_bars( - df_daily, "day", ["volume"], "Volume", color="tracking_code" - ), - "volume_pct": chart_bars( - df_daily, - "day", - ["volume_pct"], - "Volume %", - color="tracking_code", - y_format="%", - ), - "volume_cumulative": chart_lines( - df_daily, - "day", - ["volume_cumulative"], - "Cumulative Volume", - color="tracking_code", - smooth=True, - ), - "traders": chart_bars( - df_daily, - "day", - ["traders"], - "Traders", - color="tracking_code", - y_format="#", - ), - "traders_cumulative": chart_lines( - df_daily, - "day", - ["traders_cumulative"], - "Cumulative Traders", - color="tracking_code", - smooth=True, - y_format="#", - ), - "trades": chart_bars( - df_daily, - "day", - ["trades"], - "Trades", - color="tracking_code", - y_format="#", - ), - "trades_pct": chart_bars( - df_daily, - "day", - ["trades_pct"], - "Trades %", - color="tracking_code", - y_format="%", - ), - "trades_cumulative": chart_lines( - df_daily, - "day", - ["trades_cumulative"], - "Cumulative Trades", - color="tracking_code", - smooth=True, - y_format="#", - ), - } - - -def main(): - df = fetch_data() - - ## get list of assets sorted alphabetically - assets = df["asset"].unique() - assets.sort() - - ## inputs - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - start_date = st.date_input( - "Start", datetime.today().date() - timedelta(days=30) - ) - - with filt_col2: - end_date = st.date_input("End", datetime.today().date()) - - with st.expander("Filter markets"): - assets_filter = st.multiselect("Select markets", assets, default=assets) - - ## filter the data - df, df_daily = filter_data(df, start_date, end_date, assets_filter) - - ## make the charts - charts = make_charts(df, df_daily) - - ## display - - col1, col2 = st.columns(2) - with col1: - st.plotly_chart(charts["volume_cumulative"], use_container_width=True) - st.plotly_chart(charts["trades_cumulative"], use_container_width=True) - st.plotly_chart(charts["fees"], use_container_width=True) - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["traders"], use_container_width=True) - - with col2: - st.plotly_chart(charts["fees_cumulative"], use_container_width=True) - st.plotly_chart(charts["traders_cumulative"], use_container_width=True) - st.plotly_chart(charts["fees_pct"], use_container_width=True) - st.plotly_chart(charts["volume_pct"], use_container_width=True) - st.plotly_chart(charts["trades_pct"], use_container_width=True) - - ## export - exports = [ - { - "title": "Daily Data", - "df": df_daily, - } - ] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/op_mainnet/perp_integrators_db.py b/dashboard/modules/op_mainnet/perp_integrators_db.py deleted file mode 100644 index a009ee0e..00000000 --- a/dashboard/modules/op_mainnet/perp_integrators_db.py +++ /dev/null @@ -1,237 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_many_lines, chart_many_bars, export_data -from utils import get_connection, get_v2_markets - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=14), - "end_date": datetime.today().date(), -} - -## set default settings -settings = {"resolution": "daily"} - - -## data -@st.cache_data(ttl=1) -def fetch_data(filters, settings): - # get settings - resolution = settings["resolution"] - - # initialize connection - db = get_connection() - - df_integrator_stats_agg = pd.read_sql_query( - f""" - SELECT - ts, - case when tracking_code in (NULL, '', '`') then 'No tracking code' else tracking_code end as tracking_code, - exchange_fees, - exchange_fees_share, - volume, - volume_share, - trades, - trades_share, - traders, - cumulative_exchange_fees, - cumulative_volume, - cumulative_trades - FROM {st.secrets.database.DB_ENV}_optimism_mainnet.fct_v2_integrator_{resolution}_optimism_mainnet - where ts >= '{filters["start_date"]}' - and ts <= '{filters["end_date"]}' - order by ts - """, - db, - ) - - db.close() - - return { - "integrator_stats_agg": df_integrator_stats_agg, - } - - -## charts -@st.cache_data(ttl=300) -def make_charts(data, filters, settings): - df = data["integrator_stats_agg"] - return { - "volume": chart_many_bars( - df, - "ts", - ["volume"], - "Volume", - "tracking_code", - ), - "volume_pct": chart_many_bars( - df, - "ts", - ["volume_share"], - "Volume %", - "tracking_code", - y_format="%", - ), - "exchange_fees": chart_many_bars( - df, - "ts", - ["exchange_fees"], - "Exchange Fees", - "tracking_code", - ), - "exchange_fees_pct": chart_many_bars( - df, - "ts", - ["exchange_fees_share"], - "Exchange Fees %", - "tracking_code", - y_format="%", - ), - "trades": chart_many_bars( - df, - "ts", - ["trades"], - "Trades", - "tracking_code", - y_format="#", - ), - "trades_pct": chart_many_bars( - df, - "ts", - ["trades_share"], - "Trades %", - "tracking_code", - y_format="%", - ), - "traders": chart_many_bars( - df, - "ts", - ["traders"], - "Traders", - "tracking_code", - y_format="#", - ), - "cumulative_volume": chart_many_lines( - df, - "ts", - ["cumulative_volume"], - "Cumulative Volume", - "tracking_code", - ), - "cumulative_exchange_fees": chart_many_lines( - df, - "ts", - ["cumulative_exchange_fees"], - "Cumulative Exchange Fees", - "tracking_code", - ), - "cumulative_trades": chart_many_lines( - df, - "ts", - ["cumulative_trades"], - "Cumulative Trades", - "tracking_code", - ), - } - - -def main(): - ## inputs - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - with st.expander("Settings") as expander: - settings["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - ## refetch if filters changed - data = fetch_data(filters, settings) - - ## make the charts - charts = make_charts(data, filters, settings) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - st.plotly_chart(charts["traders"], use_container_width=True) - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - - with col2: - st.plotly_chart(charts["volume_pct"], use_container_width=True) - st.plotly_chart(charts["exchange_fees_pct"], use_container_width=True) - st.plotly_chart(charts["trades_pct"], use_container_width=True) - st.plotly_chart(charts["cumulative_exchange_fees"], use_container_width=True) - st.plotly_chart(charts["cumulative_trades"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) - - -## old -# def main(): -# df = fetch_data() - -# ## get list of assets sorted alphabetically -# assets = df["asset"].unique() -# assets.sort() - -# ## inputs -# filt_col1, filt_col2 = st.columns(2) -# with filt_col1: -# start_date = st.date_input( -# "Start", datetime.today().date() - timedelta(days=30) -# ) - -# with filt_col2: -# end_date = st.date_input("End", datetime.today().date()) - -# with st.expander("Filter markets"): -# assets_filter = st.multiselect("Select markets", assets, default=assets) - -# ## filter the data -# df, df_daily = filter_data(df, start_date, end_date, assets_filter) - -# ## make the charts -# charts = make_charts(df, df_daily) - -# ## display - -# col1, col2 = st.columns(2) -# with col1: -# st.plotly_chart(charts["volume_cumulative"], use_container_width=True) -# st.plotly_chart(charts["trades_cumulative"], use_container_width=True) -# st.plotly_chart(charts["fees"], use_container_width=True) -# st.plotly_chart(charts["volume"], use_container_width=True) -# st.plotly_chart(charts["trades"], use_container_width=True) -# st.plotly_chart(charts["traders"], use_container_width=True) - -# with col2: -# st.plotly_chart(charts["fees_cumulative"], use_container_width=True) -# st.plotly_chart(charts["traders_cumulative"], use_container_width=True) -# st.plotly_chart(charts["fees_pct"], use_container_width=True) -# st.plotly_chart(charts["volume_pct"], use_container_width=True) -# st.plotly_chart(charts["trades_pct"], use_container_width=True) - -# ## export -# exports = [ -# { -# "title": "Daily Data", -# "df": df_daily, -# } -# ] -# with st.expander("Exports"): -# for export in exports: -# export_data(export["title"], export["df"]) diff --git a/dashboard/modules/op_mainnet/perp_markets.py b/dashboard/modules/op_mainnet/perp_markets.py deleted file mode 100644 index cc71c8d0..00000000 --- a/dashboard/modules/op_mainnet/perp_markets.py +++ /dev/null @@ -1,171 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_bars, chart_lines, chart_oi, export_data - - -## data -@st.cache_data(ttl=600) -def fetch_data(): - # initialize connection - conn = sqlite3.connect("/app/data/perps.db") - - # read data - df_trade = pd.read_sql("SELECT * FROM v2_trades order by timestamp", conn) - df_funding = pd.read_sql("SELECT * FROM v2_funding_rates order by timestamp", conn) - df = pd.read_sql("SELECT * FROM v2_debt order by timestamp", conn) - - df["date"] = pd.to_datetime(df["timestamp"], unit="s") - df_trade["date"] = pd.to_datetime(df_trade["timestamp"], unit="s") - df_funding["date"] = pd.to_datetime(df_funding["timestamp"], unit="s") - return df, df_trade, df_funding - - -def make_daily_data(df): - # make daily data - df_daily = df.groupby(["date", "asset"])[ - [ - "cumulative_volume", - "staker_pnl", - "fees_paid", - "exchange_fees", - "keeper_fees", - "liq_fees", - ] - ].sum() - - df_daily = df_daily.reset_index() - df_daily["day"] = df_daily["date"].dt.floor("D") - df_daily = ( - df_daily.sort_values(["asset", "date"]) - .groupby(["day", "asset"]) - .last() - .reset_index() - ) - - # add some columns - df_daily["daily_volume"] = df_daily.groupby("asset")["cumulative_volume"].diff() - df_daily["daily_staker_pnl"] = df_daily.groupby("asset")["staker_pnl"].diff() - df_daily["daily_exchange_fees"] = df_daily.groupby("asset")["exchange_fees"].diff() - df_daily["daily_liq_fees"] = df_daily.groupby("asset")["liq_fees"].diff() - df_daily["protocol_fees"] = df_daily["exchange_fees"] + df_daily["liq_fees"] - return df_daily - - -def filter_data(df, df_trade, df_funding, start_date, end_date): - start_date = pd.to_datetime(start_date) - end_date = pd.to_datetime(end_date).replace(hour=23, minute=59, second=59) - - df = df[(df["date"] >= start_date) & (df["date"] <= end_date)] - df_daily = make_daily_data(df) - df_daily = df_daily[(df_daily["day"] >= start_date) & (df_daily["day"] <= end_date)] - df_trade = df_trade[ - (df_trade["date"] >= start_date) & (df_trade["date"] <= end_date) - ] - df_funding = df_funding[ - (df_funding["date"] >= start_date) & (df_funding["date"] <= end_date) - ] - return df, df_daily, df_trade, df_funding - - -## charts -def make_charts(df, df_daily, df_trade, df_funding, asset): - df = df[df["asset"] == asset] - df_daily = df_daily[df_daily["asset"] == asset] - df_trade = df_trade[df_trade["asset"] == asset] - df_funding = df_funding[df_funding["asset"] == asset] - - return { - "cumulative_volume": chart_lines( - df, "date", ["cumulative_volume"], "Cumulative Volume" - ), - "daily_volume": chart_bars(df_daily, "day", ["daily_volume"], "Daily Volume"), - "fees": chart_lines( - df, "date", ["liq_fees", "exchange_fees"], "Cumulative Fees" - ), - "daily_fees": chart_bars( - df_daily, - "day", - ["daily_exchange_fees", "daily_liq_fees"], - "Daily Fees", - ), - "pnl": chart_lines(df, "date", ["staker_pnl"], "Cumulative Staker Pnl"), - "daily_pnl": chart_bars( - df_daily, "day", ["daily_staker_pnl"], "Daily Staker Pnl" - ), - "skew": chart_lines(df_trade, "date", ["net_skew"], "Net Skew", y_format="#"), - "funding": chart_lines( - df_funding, "date", ["fundingRate"], "Funding Rate", y_format="%" - ), - "oi": chart_oi(df_trade, "date", f"Open Interest %"), - } - - -def main(): - df, df_trade, df_funding = fetch_data() - - ## get list of assets sorted alphabetically - assets = sorted( - df_trade["asset"].unique(), key=lambda x: (x != "ETH", x != "BTC", x) - ) - - ## inputs - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - start_date = st.date_input( - "Start", datetime.today().date() - timedelta(days=30) - ) - - with filt_col2: - end_date = st.date_input("End", datetime.today().date()) - - asset = st.selectbox("Select asset", assets, index=0) - - ## filter the data - df, df_daily, df_trade, df_funding = filter_data( - df, df_trade, df_funding, start_date, end_date - ) - - ## make the charts - charts = make_charts(df, df_daily, df_trade, df_funding, asset) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["pnl"], use_container_width=True) - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - st.plotly_chart(charts["fees"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - st.plotly_chart(charts["funding"], use_container_width=True) - - with col2: - st.plotly_chart(charts["daily_pnl"], use_container_width=True) - st.plotly_chart(charts["daily_volume"], use_container_width=True) - st.plotly_chart(charts["daily_fees"], use_container_width=True) - st.plotly_chart(charts["oi"], use_container_width=True) - - ## export - exports = [ - { - "title": "Daily Data", - "df": df_daily, - }, - { - "title": "Hourly Data", - "df": df, - }, - { - "title": "Trades", - "df": df_trade, - }, - { - "title": "Funding Rates", - "df": df_funding, - }, - ] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/op_mainnet/perp_markets_db.py b/dashboard/modules/op_mainnet/perp_markets_db.py deleted file mode 100644 index 6a7f2119..00000000 --- a/dashboard/modules/op_mainnet/perp_markets_db.py +++ /dev/null @@ -1,210 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_bars, chart_lines, chart_oi, export_data -from utils import get_connection, get_v2_markets - -## set default filters -filters = { - "market": "ETH", - "start_date": datetime.today().date() - timedelta(days=30), - "end_date": datetime.today().date(), -} - -## set default settings -settings = {"resolution": "daily"} - - -## data -@st.cache_data(ttl=300) -def fetch_data(filters, settings): - # get settings - resolution = settings["resolution"] - - # initialize connection - db = get_connection() - - df_markets = pd.read_sql_query( - f""" - SELECT distinct market FROM {st.secrets.database.DB_ENV}_optimism_mainnet.fct_v2_market_stats_optimism_mainnet - """, - db, - ) - - df_market_stats_agg = pd.read_sql_query( - f""" - SELECT - ts, - market, - exchange_fees, - liquidation_fees, - volume, - amount_liquidated, - cumulative_volume, - cumulative_exchange_fees, - cumulative_liquidation_fees, - cumulative_amount_liquidated, - long_oi_usd, - short_oi_usd, - total_oi_usd - FROM {st.secrets.database.DB_ENV}_optimism_mainnet.fct_v2_market_{resolution}_optimism_mainnet - where - market = '{filters["market"]}' - and ts >= '{filters["start_date"]}' - and ts <= '{filters["end_date"]}' - order by ts - """, - db, - ) - - df_market_stats = pd.read_sql_query( - f""" - SELECT - ts, - market, - skew, - funding_rate, - long_oi_pct, - short_oi_pct - FROM {st.secrets.database.DB_ENV}_optimism_mainnet.fct_v2_market_stats_optimism_mainnet - where - market = '{filters["market"]}' - and ts >= '{filters["start_date"]}' - and ts <= '{filters["end_date"]}' - order by ts - """, - db, - ) - - db.close() - - return { - "market_stats": df_market_stats, - "market_stats_agg": df_market_stats_agg, - } - - -## charts -@st.cache_data(ttl=300) -def make_charts(data, filters): - return { - "cumulative_volume": chart_lines( - data["market_stats_agg"], - "ts", - ["cumulative_volume"], - "Cumulative Volume", - smooth=True, - ), - "daily_volume": chart_bars( - data["market_stats_agg"], - "ts", - ["volume"], - "Daily Volume", - ), - "cumulative_fees": chart_lines( - data["market_stats_agg"], - "ts", - ["cumulative_exchange_fees", "cumulative_liquidation_fees"], - "Cumulative Fees", - smooth=True, - ), - "daily_fees": chart_bars( - data["market_stats_agg"], - "ts", - ["exchange_fees", "liquidation_fees"], - "Daily Fees", - ), - "cumulative_liquidation": chart_lines( - data["market_stats_agg"], - "ts", - ["cumulative_amount_liquidated"], - "Cumulative Amount Liquidated", - smooth=True, - ), - "daily_liquidation": chart_bars( - data["market_stats_agg"], - "ts", - ["amount_liquidated"], - "Daily Amount Liquidated", - ), - "skew": chart_lines( - data["market_stats"], - "ts", - ["skew"], - "Skew", - y_format="#", - ), - "funding_rate": chart_lines( - data["market_stats"], - "ts", - ["funding_rate"], - "Funding Rate", - y_format="%", - smooth=True, - ), - "oi_pct": chart_oi( - data["market_stats"], - "ts", - "Open Interest: Long vs Short", - ), - "oi_usd": chart_lines( - data["market_stats_agg"], - "ts", - ["long_oi_usd", "short_oi_usd", "total_oi_usd"], - "Open Interest (USD)", - smooth=True, - ), - } - - -def main(): - ## get list of markets - markets = get_v2_markets() - markets = sorted( - markets, - key=lambda x: (x != "ETH", x != "BTC", x), - ) - - ## inputs - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - filters["market"] = st.selectbox("Select asset", markets, index=0) - - with st.expander("Settings") as expander: - settings["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - ## refetch if filters changed - data = fetch_data(filters, settings) - - ## make the charts - charts = make_charts(data, filters) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - st.plotly_chart(charts["cumulative_liquidation"], use_container_width=True) - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - st.plotly_chart(charts["skew"], use_container_width=True) - st.plotly_chart(charts["oi_usd"], use_container_width=True) - - with col2: - st.plotly_chart(charts["daily_volume"], use_container_width=True) - st.plotly_chart(charts["daily_liquidation"], use_container_width=True) - st.plotly_chart(charts["daily_fees"], use_container_width=True) - st.plotly_chart(charts["funding_rate"], use_container_width=True) - st.plotly_chart(charts["oi_pct"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/op_mainnet/perp_monitor.py b/dashboard/modules/op_mainnet/perp_monitor.py deleted file mode 100644 index ce8199c1..00000000 --- a/dashboard/modules/op_mainnet/perp_monitor.py +++ /dev/null @@ -1,196 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_many_bars, export_data - - -## data -@st.cache_data(ttl=60) -def fetch_data(): - # initialize connection - conn = sqlite3.connect("/app/data/perps.db") - - # read data - df_trade = pd.read_sql("SELECT * FROM v2_trades order by timestamp", conn) - df = pd.read_sql("SELECT * FROM v2_debt order by timestamp", conn) - - df["date"] = pd.to_datetime(df["timestamp"], unit="s") - df_trade["date"] = pd.to_datetime(df_trade["timestamp"], unit="s") - - df["date_hour"] = df["date"].dt.floor("H") - df_trade["date_hour"] = df_trade["date"].dt.floor("H") - return df, df_trade - - -def make_hourly_data(df, df_trade): - # get hourly count of trades - df_trade["trades"] = df_trade["orderType"].apply( - lambda x: 1 if x != "Liquidation" else 0 - ) - df_trade["liquidations"] = df_trade["orderType"].apply( - lambda x: 1 if x == "Liquidation" else 0 - ) - - df_trade_hourly = ( - df_trade[ - [ - "asset", - "date_hour", - "trades", - "liquidations", - ] - ] - .groupby(["asset", "date_hour"]) - .sum() - .reset_index() - ) - - # make hourly data - df_hourly = df.groupby(["asset", "date_hour"])[ - [ - "cumulative_volume", - "staker_pnl", - "fees_paid", - "exchange_fees", - "keeper_fees", - "liq_fees", - ] - ].last() - - # add some columns - df_hourly["volume"] = df_hourly.groupby("asset")["cumulative_volume"].diff() - df_hourly["staker_pnl"] = df_hourly.groupby("asset")["staker_pnl"].diff() - df_hourly["exchange_fees"] = df_hourly.groupby("asset")["exchange_fees"].diff() - df_hourly["liq_fees"] = df_hourly.groupby("asset")["liq_fees"].diff() - df_hourly["protocol_fees"] = df_hourly["exchange_fees"] + df_hourly["liq_fees"] - - df_final = df_hourly.merge(df_trade_hourly, on=["asset", "date_hour"], how="left") - return df_final - - -def filter_data(df, df_trade, start_date, end_date, assets): - start_date = pd.to_datetime(start_date) - end_date = pd.to_datetime(end_date).replace(hour=23, minute=59, second=59) - - df = df[ - (df["date"] >= start_date) - & (df["date"] <= end_date) - & (df["asset"].isin(assets)) - ] - df_hourly = make_hourly_data( - df[df["asset"].isin(assets)], df_trade[df_trade["asset"].isin(assets)] - ) - df_hourly = df_hourly[ - (df_hourly["date_hour"] >= start_date) & (df_hourly["date_hour"] <= end_date) - ] - df_trade = df_trade[ - (df_trade["date"] >= start_date) & (df_trade["date"] <= end_date) - ] - return df, df_hourly, df_trade - - -## charts -def make_charts(df_hourly): - return { - "volume": chart_many_bars( - df_hourly, - "date_hour", - ["volume"], - "Volume", - "asset", - ), - "pnl": chart_many_bars( - df_hourly, - "date_hour", - ["staker_pnl"], - "Staker Pnl", - "asset", - ), - "exchange_fees": chart_many_bars( - df_hourly, - "date_hour", - ["exchange_fees"], - "Exchange Fees", - "asset", - ), - "liquidation_fees": chart_many_bars( - df_hourly, - "date_hour", - ["liq_fees"], - "Liquidation Fees", - "asset", - ), - "trades": chart_many_bars( - df_hourly, - "date_hour", - ["trades"], - "Trades", - "asset", - y_format="#", - ), - "liquidations": chart_many_bars( - df_hourly, - "date_hour", - ["liquidations"], - "Liquidations", - "asset", - y_format="#", - ), - } - - -def main(): - df, df_trade = fetch_data() - - ## get list of assets sorted by highest volume - assets = ( - df_trade.groupby("asset")["volume"] - .sum() - .sort_values(ascending=False) - .index.tolist() - ) - - ## inputs - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - start_date = st.date_input("Start", datetime.today().date() - timedelta(days=3)) - - with filt_col2: - end_date = st.date_input("End", datetime.today().date()) - - with st.expander("Filter markets"): - assets_filter = st.multiselect("Select markets", assets, default=assets) - - ## filter the data - df, df_hourly, df_trade = filter_data( - df, df_trade, start_date, end_date, assets_filter - ) - - ## make the charts - charts = make_charts(df_hourly) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - - with col2: - st.plotly_chart(charts["pnl"], use_container_width=True) - st.plotly_chart(charts["liquidation_fees"], use_container_width=True) - st.plotly_chart(charts["liquidations"], use_container_width=True) - - ## export - exports = [ - { - "title": "Hourly Data", - "df": df_hourly, - } - ] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/op_mainnet/perp_monitor_db.py b/dashboard/modules/op_mainnet/perp_monitor_db.py deleted file mode 100644 index ab1a9cfc..00000000 --- a/dashboard/modules/op_mainnet/perp_monitor_db.py +++ /dev/null @@ -1,168 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_many_bars, export_data -from utils import chart_bars, chart_many_bars, export_data -from utils import get_connection, get_v2_markets - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=3), - "end_date": datetime.today().date(), -} - -## set default settings -settings = {"resolution": "hourly"} - - -## data -@st.cache_data(ttl=1) -def fetch_data(filters, settings): - # get settings - resolution = settings["resolution"] - - # initialize connection - db = get_connection() - - df_market_stats_agg = pd.read_sql_query( - f""" - SELECT - ts, - market, - exchange_fees, - liquidation_fees, - volume, - amount_liquidated, - trades, - liquidations, - cumulative_volume, - cumulative_exchange_fees, - cumulative_liquidation_fees, - cumulative_amount_liquidated, - long_oi_usd, - short_oi_usd, - total_oi_usd - FROM {st.secrets.database.DB_ENV}_optimism_mainnet.fct_v2_market_{resolution}_optimism_mainnet - where ts >= '{filters["start_date"]}' - and ts <= '{filters["end_date"]}' - order by ts - """, - db, - ) - - db.close() - - return { - "market_stats_agg": df_market_stats_agg, - } - - -## charts -@st.cache_data(ttl=300) -def make_charts(data, filters, settings): - df = data["market_stats_agg"][ - data["market_stats_agg"]["market"].isin(filters["markets"]) - ] - - return { - "volume": chart_many_bars( - df, - "ts", - ["volume"], - "Volume", - "market", - ), - # "pnl": chart_many_bars( - # data["market_stats_agg"], - # "date_hour", - # ["staker_pnl"], - # "Staker Pnl", - # "market", - # ), - "exchange_fees": chart_many_bars( - df, - "ts", - ["exchange_fees"], - "Exchange Fees", - "market", - ), - "liquidation_fees": chart_many_bars( - df, - "ts", - ["liquidation_fees"], - "Liquidation Fees", - "market", - ), - "amount_liquidated": chart_many_bars( - df, - "ts", - ["amount_liquidated"], - "Amount Liquidated", - "market", - ), - "trades": chart_many_bars( - df, - "ts", - ["trades"], - "Trades", - "market", - ), - "liquidations": chart_many_bars( - df, - "ts", - ["liquidations"], - "Liquidations", - "market", - ), - } - - -def main(): - ## get list of markets - markets = get_v2_markets() - markets = sorted( - markets, - key=lambda x: (x != "ETH", x != "BTC", x), - ) - filters["markets"] = markets - - ## inputs - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - with st.expander("Filter markets"): - filters["markets"] = st.multiselect("Select markets", markets, default=markets) - - with st.expander("Settings") as expander: - settings["resolution"] = st.radio("Resolution", ["hourly", "daily"]) - - ## refetch if filters changed - data = fetch_data(filters, settings) - - ## make the charts - charts = make_charts(data, filters, settings) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["volume"], use_container_width=True) - st.plotly_chart(charts["exchange_fees"], use_container_width=True) - st.plotly_chart(charts["trades"], use_container_width=True) - - with col2: - st.plotly_chart(charts["amount_liquidated"], use_container_width=True) - st.plotly_chart(charts["liquidation_fees"], use_container_width=True) - st.plotly_chart(charts["liquidations"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/op_mainnet/perp_stats.py b/dashboard/modules/op_mainnet/perp_stats.py deleted file mode 100644 index d8f2e754..00000000 --- a/dashboard/modules/op_mainnet/perp_stats.py +++ /dev/null @@ -1,221 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_bars, chart_lines, export_data - - -def make_oi(df): - # add USD columns - df["long_oi_usd"] = df["long_oi"] * df["price"] - df["short_oi_usd"] = df["short_oi"] * df["price"] - - # Convert timestamp to datetime and sort - df["date"] = pd.to_datetime(df["timestamp"], unit="s") - df.sort_values(by="date", inplace=True) - - # Create a pivot table for long_oi and short_oi for each asset - pivot_long_oi = df.pivot_table( - index="date", columns="asset", values="long_oi_usd", aggfunc="last" - ) - pivot_short_oi = df.pivot_table( - index="date", columns="asset", values="short_oi_usd", aggfunc="last" - ) - - # Forward fill missing values - pivot_long_oi.ffill(inplace=True) - pivot_short_oi.ffill(inplace=True) - - # Calculate total Open Interest at each timestamp - total_oi = pivot_long_oi.sum(axis=1) + pivot_short_oi.sum(axis=1) - total_oi = total_oi.reset_index(name="total_oi") - - # Check if 'ETH' and 'BTC' are in the columns and calculate ETH and BTC Open Interest - eth_btc_oi_long = ( - pivot_long_oi[["ETH", "BTC"]].sum(axis=1) - if all(asset in pivot_long_oi.columns for asset in ["ETH", "BTC"]) - else pivot_long_oi[["ETH", "BTC"]].fillna(0).sum(axis=1) - ) - eth_btc_oi_short = ( - pivot_short_oi[["ETH", "BTC"]].sum(axis=1) - if all(asset in pivot_short_oi.columns for asset in ["ETH", "BTC"]) - else pivot_short_oi[["ETH", "BTC"]].fillna(0).sum(axis=1) - ) - eth_btc_oi = eth_btc_oi_long + eth_btc_oi_short - - # Calculate Other Assets Open Interest - other_assets = pivot_long_oi.columns.difference(["ETH", "BTC"]) - other_oi_long = pivot_long_oi[other_assets].sum(axis=1) - other_oi_short = pivot_short_oi[other_assets].sum(axis=1) - other_oi = other_oi_long + other_oi_short - - # Add these columns to the total_oi DataFrame - total_oi["eth_btc_oi"] = eth_btc_oi.values - total_oi["other_oi"] = other_oi.values - - total_oi = total_oi.set_index("date").resample("H").last().reset_index() - - # Display or export the summary - return total_oi - - -## data -@st.cache_data(ttl=600) -def fetch_data(): - # initialize connection - conn = sqlite3.connect("/app/data/perps.db") - - # read data - df_trade = pd.read_sql("SELECT * FROM v2_trades order by timestamp", conn) - df = pd.read_sql("SELECT * FROM v2_debt order by timestamp", conn) - - df["date"] = pd.to_datetime(df["timestamp"], unit="s") - df_trade["date"] = pd.to_datetime(df_trade["timestamp"], unit="s") - return df, df_trade - - -def make_daily_data(df): - # make daily data - df_daily = ( - df.groupby("date")[ - [ - "cumulative_volume", - "staker_pnl", - "fees_paid", - "exchange_fees", - "keeper_fees", - "liq_fees", - ] - ] - .sum() - .reset_index() - ) - - df_daily["day"] = df_daily["date"].dt.floor("D") - df_daily = df_daily.sort_values("date").groupby("day").last().reset_index() - - # add some columns - df_daily["daily_volume"] = df_daily["cumulative_volume"].diff() - df_daily["daily_staker_pnl"] = df_daily["staker_pnl"].diff() - df_daily["daily_exchange_fees"] = df_daily["exchange_fees"].diff() - df_daily["daily_liq_fees"] = df_daily["liq_fees"].diff() - df_daily["protocol_fees"] = df_daily["exchange_fees"] + df_daily["liq_fees"] - return df_daily - - -def filter_data(df, df_trade, start_date, end_date, assets): - start_date = pd.to_datetime(start_date) - end_date = pd.to_datetime(end_date).replace(hour=23, minute=59, second=59) - - df = df[ - (df["date"] >= start_date) - & (df["date"] <= end_date) - & (df["asset"].isin(assets)) - ] - df_daily = make_daily_data(df[df["asset"].isin(assets)]) - df_daily = df_daily[(df_daily["day"] >= start_date) & (df_daily["day"] <= end_date)] - df_trade = df_trade[ - (df_trade["date"] >= start_date) & (df_trade["date"] <= end_date) - ] - df_oi = make_oi(df_trade) - return df, df_daily, df_trade, df_oi - - -## charts -def make_charts(df, df_daily, df_trade, df_oi): - return { - "pnl": chart_lines( - df_daily, "date", ["staker_pnl"], "Cumulative Staker Pnl", smooth=True - ), - "daily_pnl": chart_bars( - df_daily, "day", ["daily_staker_pnl"], "Daily Staker Pnl" - ), - "cumulative_volume": chart_lines( - df_daily, "date", ["cumulative_volume"], "Cumulative Volume", smooth=True - ), - "daily_volume": chart_bars(df_daily, "day", ["daily_volume"], "Daily Volume"), - "fees": chart_lines( - df_daily, - "date", - ["liq_fees", "exchange_fees"], - "Cumulative Fees", - smooth=True, - ), - "daily_fees": chart_bars( - df_daily, "day", ["daily_liq_fees", "daily_exchange_fees"], "Daily Fees" - ), - "oi": chart_lines( - df_oi, - "date", - ["total_oi", "eth_btc_oi", "other_oi"], - "Open Interest ($)", - ), - } - - -def main(): - df, df_trade = fetch_data() - - ## get list of assets sorted by highest volume - assets = ( - df_trade.groupby("asset")["volume"] - .sum() - .sort_values(ascending=False) - .index.tolist() - ) - - ## inputs - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - start_date = st.date_input( - "Start", datetime.today().date() - timedelta(days=30) - ) - - with filt_col2: - end_date = st.date_input("End", datetime.today().date()) - - with st.expander("Filter markets"): - assets_filter = st.multiselect("Select markets", assets, default=assets) - - ## filter the data - df, df_daily, df_trade, df_oi = filter_data( - df, df_trade, start_date, end_date, assets_filter - ) - - ## make the charts - charts = make_charts(df, df_daily, df_trade, df_oi) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["pnl"], use_container_width=True) - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - st.plotly_chart(charts["fees"], use_container_width=True) - - with col2: - st.plotly_chart(charts["daily_pnl"], use_container_width=True) - st.plotly_chart(charts["daily_volume"], use_container_width=True) - st.plotly_chart(charts["daily_fees"], use_container_width=True) - - st.plotly_chart(charts["oi"], use_container_width=True) - - ## export - exports = [ - { - "title": "Daily Data", - "df": df_daily, - }, - { - "title": "Trades", - "df": df_trade, - }, - { - "title": "Open Interest", - "df": df_oi, - }, - ] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/modules/op_mainnet/perp_stats_db.py b/dashboard/modules/op_mainnet/perp_stats_db.py deleted file mode 100644 index d3dd56a4..00000000 --- a/dashboard/modules/op_mainnet/perp_stats_db.py +++ /dev/null @@ -1,152 +0,0 @@ -import streamlit as st -import pandas as pd -import sqlite3 -import plotly.express as px -from datetime import datetime, timedelta -from utils import chart_bars, chart_lines, chart_oi, export_data -from utils import get_connection - -## set default filters -filters = { - "start_date": datetime.today().date() - timedelta(days=30), - "end_date": datetime.today().date(), -} - -## set default settings -settings = {"resolution": "daily"} - - -## data -@st.cache_data(ttl=1) -def fetch_data(filters, settings): - # get settings - resolution = settings["resolution"] - - # initialize connection - db = get_connection() - - df_market_stats_agg = pd.read_sql_query( - f""" - SELECT - ts, - exchange_fees, - liquidation_fees, - volume, - amount_liquidated, - cumulative_volume, - cumulative_exchange_fees, - cumulative_liquidation_fees, - cumulative_amount_liquidated, - long_oi_usd, - short_oi_usd, - total_oi_usd, - eth_btc_oi_usd, - alt_oi_usd - FROM {st.secrets.database.DB_ENV}_optimism_mainnet.fct_v2_stats_{resolution}_optimism_mainnet - where ts >= '{filters["start_date"]}' - and ts <= '{filters["end_date"]}' - order by ts - """, - db, - ) - - db.close() - - return { - "market_stats_agg": df_market_stats_agg, - } - - -## charts -@st.cache_data(ttl=1) -def make_charts(data, filters, settings): - return { - "cumulative_volume": chart_lines( - data["market_stats_agg"], - "ts", - ["cumulative_volume"], - "Cumulative Volume", - smooth=True, - ), - "daily_volume": chart_bars( - data["market_stats_agg"], - "ts", - ["volume"], - f"{settings['resolution'].capitalize()} Volume", - ), - "cumulative_fees": chart_lines( - data["market_stats_agg"], - "ts", - ["cumulative_exchange_fees", "cumulative_liquidation_fees"], - "Cumulative Fees", - smooth=True, - ), - "daily_fees": chart_bars( - data["market_stats_agg"], - "ts", - ["exchange_fees", "liquidation_fees"], - f"{settings['resolution'].capitalize()} Fees", - ), - "cumulative_liquidation": chart_lines( - data["market_stats_agg"], - "ts", - ["cumulative_amount_liquidated"], - "Cumulative Amount Liquidated", - smooth=True, - ), - "daily_liquidation": chart_bars( - data["market_stats_agg"], - "ts", - ["amount_liquidated"], - f"{settings['resolution'].capitalize()} Amount Liquidated", - ), - "oi_usd": chart_lines( - data["market_stats_agg"], - "ts", - ["total_oi_usd", "eth_btc_oi_usd", "alt_oi_usd"], - "Open Interest (USD)", - smooth=True, - ), - } - - -def main(): - data = fetch_data(filters, settings) - - ## inputs - filt_col1, filt_col2 = st.columns(2) - with filt_col1: - filters["start_date"] = st.date_input("Start", filters["start_date"]) - - with filt_col2: - filters["end_date"] = st.date_input("End", filters["end_date"]) - - with st.expander("Settings") as expander: - settings["resolution"] = st.radio("Resolution", ["daily", "hourly"]) - - ## refetch if filters changed - data = fetch_data(filters, settings) - - ## make the charts - charts = make_charts(data, filters, settings) - - ## display - col1, col2 = st.columns(2) - - with col1: - st.plotly_chart(charts["cumulative_volume"], use_container_width=True) - st.plotly_chart(charts["cumulative_liquidation"], use_container_width=True) - st.plotly_chart(charts["cumulative_fees"], use_container_width=True) - - with col2: - st.plotly_chart(charts["daily_volume"], use_container_width=True) - st.plotly_chart(charts["daily_liquidation"], use_container_width=True) - st.plotly_chart(charts["daily_fees"], use_container_width=True) - - st.plotly_chart(charts["oi_usd"], use_container_width=True) - - ## export - exports = [{"title": export, "df": data[export]} for export in data.keys()] - with st.expander("Exports"): - for export in exports: - export_data(export["title"], export["df"]) diff --git a/dashboard/pages/All_Chains.py b/dashboard/pages/All_Chains.py deleted file mode 100644 index a533b24a..00000000 --- a/dashboard/pages/All_Chains.py +++ /dev/null @@ -1,20 +0,0 @@ -import streamlit as st -from modules.all_chains import ( - core_stats, - perp_stats, -) - -st.set_page_config(page_title="All Chains", layout="wide") - - -pages = { - "Core Stats": core_stats.main, - "Perps Stats": perp_stats.main, -} -state_page = None -state_page = st.sidebar.radio( - "All Chains", - tuple(pages.keys()), - index=tuple(pages.keys()).index(state_page) if state_page else 0, -) -pages[state_page]() diff --git a/dashboard/pages/Arbitrum_Mainnet.py b/dashboard/pages/Arbitrum_Mainnet.py deleted file mode 100644 index 5c7544fd..00000000 --- a/dashboard/pages/Arbitrum_Mainnet.py +++ /dev/null @@ -1,32 +0,0 @@ -import streamlit as st -from modules.arbitrum_mainnet import ( - core_stats, - spot_markets, - perp_stats, - perp_markets, - perp_monitor, - perp_integrators, - perp_account, - perp_keepers, -) - -st.set_page_config(page_title="Arbitrum Mainnet", layout="wide") - - -pages = { - "Perps Stats": perp_stats.main, - "Perps Markets": perp_markets.main, - "Perps Monitor": perp_monitor.main, - "Core Stats": core_stats.main, - "Spot Markets": spot_markets.main, - "Perps Integrators": perp_integrators.main, - "Perps Accounts": perp_account.main, - "Perps Keepers": perp_keepers.main, -} -state_page = None -state_page = st.sidebar.radio( - ":large_blue_circle: Arbitrum Mainnet", - tuple(pages.keys()), - index=tuple(pages.keys()).index(state_page) if state_page else 0, -) -pages[state_page]() diff --git a/dashboard/pages/Arbitrum_Sepolia.py b/dashboard/pages/Arbitrum_Sepolia.py deleted file mode 100644 index 2d98edb2..00000000 --- a/dashboard/pages/Arbitrum_Sepolia.py +++ /dev/null @@ -1,32 +0,0 @@ -import streamlit as st -from modules.arbitrum_sepolia import ( - core_stats, - spot_markets, - perp_account, - perp_integrators, - perp_keepers, - perp_markets, - perp_monitor, - perp_stats, -) - -st.set_page_config(page_title="Arbitrum Sepolia", layout="wide") - - -pages = { - "Perps Stats": perp_stats.main, - "Perps Markets": perp_markets.main, - "Perps Monitor": perp_monitor.main, - "Core Stats": core_stats.main, - "Spot Markets": spot_markets.main, - "Perps Integrators": perp_integrators.main, - "Perps Accounts": perp_account.main, - "Perps Keepers": perp_keepers.main, -} -state_page = None -state_page = st.sidebar.radio( - ":large_blue_circle: :test_tube: Arbitrum Sepolia", - tuple(pages.keys()), - index=tuple(pages.keys()).index(state_page) if state_page else 0, -) -pages[state_page]() diff --git a/dashboard/pages/Base_Mainnet.py b/dashboard/pages/Base_Mainnet.py deleted file mode 100644 index c37a1009..00000000 --- a/dashboard/pages/Base_Mainnet.py +++ /dev/null @@ -1,32 +0,0 @@ -import streamlit as st -from modules.base_mainnet import ( - perp_integrators, - perp_stats, - perp_monitor, - perp_markets, - spot_markets, - perp_account, - perp_keepers, - core_stats, -) - -st.set_page_config(page_title="Base Mainnet", layout="wide") - - -pages = { - "Perps Stats": perp_stats.main, - "Perps Markets": perp_markets.main, - "Perps Monitor": perp_monitor.main, - "Core Stats": core_stats.main, - "Spot Markets": spot_markets.main, - "Perps Integrators": perp_integrators.main, - "Perps Accounts": perp_account.main, - "Perps Keepers": perp_keepers.main, -} -state_page = None -state_page = st.sidebar.radio( - ":large_blue_circle: :rocket: Base Mainnet", - tuple(pages.keys()), - index=tuple(pages.keys()).index(state_page) if state_page else 0, -) -pages[state_page]() diff --git a/dashboard/pages/Base_Sepolia.py b/dashboard/pages/Base_Sepolia.py deleted file mode 100644 index 0fb9d7df..00000000 --- a/dashboard/pages/Base_Sepolia.py +++ /dev/null @@ -1,32 +0,0 @@ -import streamlit as st -from modules.base_sepolia import ( - perp_integrators, - perp_stats, - perp_monitor, - perp_markets, - spot_markets, - perp_account, - perp_keepers, - core_stats, -) - -st.set_page_config(page_title="Base Sepolia", layout="wide") - - -pages = { - "Perps Stats": perp_stats.main, - "Perps Markets": perp_markets.main, - "Perps Monitor": perp_monitor.main, - "Core Stats": core_stats.main, - "Spot Markets": spot_markets.main, - "Perps Integrators": perp_integrators.main, - "Perps Accounts": perp_account.main, - "Perps Keepers": perp_keepers.main, -} -state_page = None -state_page = st.sidebar.radio( - ":large_blue_circle: :test_tube: Base Sepolia", - tuple(pages.keys()), - index=tuple(pages.keys()).index(state_page) if state_page else 0, -) -pages[state_page]() diff --git a/dashboard/pages/Ethereum_Mainnet.py b/dashboard/pages/Ethereum_Mainnet.py deleted file mode 100644 index a051dc7e..00000000 --- a/dashboard/pages/Ethereum_Mainnet.py +++ /dev/null @@ -1,18 +0,0 @@ -import streamlit as st -from modules.eth_mainnet import ( - core_stats, -) - -st.set_page_config(page_title="Ethereum Mainnet", layout="wide") - - -pages = { - "Core Stats": core_stats.main, -} -state_page = None -state_page = st.sidebar.radio( - "Ethereum Mainnet", - tuple(pages.keys()), - index=tuple(pages.keys()).index(state_page) if state_page else 0, -) -pages[state_page]() diff --git a/dashboard/pages/Milestones.py b/dashboard/pages/Milestones.py deleted file mode 100644 index 5b612fc7..00000000 --- a/dashboard/pages/Milestones.py +++ /dev/null @@ -1,18 +0,0 @@ -import streamlit as st -from modules.milestones import ( - base_andromeda, -) - -st.set_page_config(page_title="Milestones", layout="wide") - - -pages = { - "Base Andromeda": base_andromeda.main, -} -state_page = None -state_page = st.sidebar.radio( - "Synthetix Milestones", - tuple(pages.keys()), - index=tuple(pages.keys()).index(state_page) if state_page else 0, -) -pages[state_page]() diff --git a/dashboard/pages/Optimism_Mainnet.py b/dashboard/pages/Optimism_Mainnet.py deleted file mode 100644 index 2d64618e..00000000 --- a/dashboard/pages/Optimism_Mainnet.py +++ /dev/null @@ -1,23 +0,0 @@ -import streamlit as st -from modules.op_mainnet import ( - perp_stats_db, - perp_markets_db, - perp_monitor_db, - perp_integrators_db, -) - -st.set_page_config(page_title="Optimism Mainnet", layout="wide") - -pages = { - "Perps V2 Stats": perp_stats_db.main, - "Perps V2 Markets": perp_markets_db.main, - "Perps V2 Monitor": perp_monitor_db.main, - "Perps V2 Integrators": perp_integrators_db.main, -} -state_page = None -state_page = st.sidebar.radio( - ":red_circle: Optimism Mainnet", - tuple(pages.keys()), - index=tuple(pages.keys()).index(state_page) if state_page else 0, -) -pages[state_page]() diff --git a/dashboard/requirements.txt b/dashboard/requirements.txt deleted file mode 100644 index 432779f2..00000000 --- a/dashboard/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -streamlit -st-pages==0.5.0 -sqlalchemy -psycopg2-binary -plotly -watchdog diff --git a/dashboard/static/favicon.ico b/dashboard/static/favicon.ico deleted file mode 100644 index 5845395c..00000000 Binary files a/dashboard/static/favicon.ico and /dev/null differ diff --git a/dashboard/utils/__init__.py b/dashboard/utils/__init__.py deleted file mode 100644 index 040acdb1..00000000 --- a/dashboard/utils/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from .charts import * -from .data import * diff --git a/dashboard/utils/charts.py b/dashboard/utils/charts.py deleted file mode 100644 index c5068d6a..00000000 --- a/dashboard/utils/charts.py +++ /dev/null @@ -1,282 +0,0 @@ -import plotly.express as px - -## color sets -sequential = [ - "#E5FAFF", - "#B7F2FF", - "#8AEAFF", - "#5CE1FF", - "#2FD9FF", - "#00D1FF", - "#00B0D6", - "#006D85", - "#004B5C", -] -categorical = ["#00D1FF", "#EB46FF", "#6B59FF", "#4FD1C5", "#1F68AC", "#FDE8FF"] - - -def set_axes(fig, x_format, y_format): - # format the y-axis - if y_format == "%": - fig.update_yaxes(tickformat=".2%") - elif y_format == "$": - fig.update_yaxes(tickprefix="$") - elif y_format == "#": - fig.update_yaxes(tickprefix=None) - - # format the y-axis - if x_format == "%": - fig.update_xaxes(tickformat=".2%") - elif x_format == "$": - fig.update_xaxes(tickprefix="$") - elif x_format == "#": - fig.update_xaxes(tickprefix=None) - - return fig - - -## charts -def chart_many_bars(df, x_col, y_cols, title, color=None, x_format="#", y_format="$"): - fig = px.bar( - df, - x=x_col, - y=y_cols, - title=title, - color=color, - color_discrete_sequence=categorical, - template="plotly_dark", - ) - - # remove axis labels - fig.update_xaxes( - title_text="", - automargin=True, - ) - fig.update_yaxes(title_text="") - - # format axes - fig = set_axes(fig, x_format, y_format) - - if y_format == "%": - fig.update_yaxes(tickformat=".2%") - elif y_format == "$": - fig.update_yaxes(tickprefix="$") - elif y_format == "#": - fig.update_yaxes(tickprefix=None) - - fig.update_layout( - font=dict( - family="sans-serif", - ), - ) - return fig - - -def chart_many_lines(df, x_col, y_cols, title, color=None, x_format="#", y_format="$"): - fig = px.line( - df, - x=x_col, - y=y_cols, - title=title, - color=color, - color_discrete_sequence=categorical, - template="plotly_dark", - ) - - # remove axis labels - fig.update_xaxes( - title_text="", - automargin=True, - ) - fig.update_yaxes(title_text="") - - # format axes - fig = set_axes(fig, x_format, y_format) - - if y_format == "%": - fig.update_yaxes(tickformat=".2%") - elif y_format == "$": - fig.update_yaxes(tickprefix="$") - elif y_format == "#": - fig.update_yaxes(tickprefix=None) - - fig.update_layout( - font=dict( - family="sans-serif", - ), - ) - return fig - - -def chart_bars( - df, x_col, y_cols, title, color=None, x_format="#", y_format="$", column=False -): - fig = px.bar( - df, - x=x_col, - y=y_cols, - title=title, - color=color, - color_discrete_sequence=categorical, - template="plotly_dark", - orientation="h" if column else "v", - ) - - # remove axis labels - fig.update_xaxes( - title_text="", - automargin=True, - ) - fig.update_yaxes(title_text="") - fig.update_traces(hovertemplate=None) - - # format the axis - fig = set_axes(fig, x_format, y_format) - - # Update layout for dark theme readability - fig.update_layout( - hovermode=f"{'y' if column else 'x'} unified", - legend=dict( - orientation="h", - yanchor="bottom", - y=1.02, - xanchor="right", - x=1, - title=None, - ), - font=dict( - family="sans-serif", - ), - ) - return fig - - -def chart_area( - df, x_col, y_cols, title, color=None, x_format="#", y_format="$", column=False -): - fig = px.area( - df, - x=x_col, - y=y_cols, - title=title, - color=color, - color_discrete_sequence=categorical, - template="plotly_dark", - orientation="h" if column else "v", - ) - - # remove axis labels - fig.update_xaxes( - title_text="", - automargin=True, - ) - fig.update_yaxes(title_text="") - fig.update_traces(hovertemplate=None) - - # format the axis - fig = set_axes(fig, x_format, y_format) - - # Update layout for dark theme readability - fig.update_layout( - hovermode=f"{'y' if column else 'x'} unified", - legend=dict( - orientation="h", - yanchor="bottom", - y=1.02, - xanchor="right", - x=1, - title=None, - ), - font=dict( - family="sans-serif", - ), - ) - return fig - - -def chart_lines( - df, x_col, y_cols, title, color=None, smooth=False, x_format="#", y_format="$" -): - fig = px.line( - df, - x=x_col, - y=y_cols, - title=title, - color=color, - color_discrete_sequence=categorical, - template="plotly_dark", - ) - - fig.update_traces( - hovertemplate=None, - line_shape=None if smooth else "hv", - ) - - fig = set_axes(fig, x_format, y_format) - - # remove axis labels - fig.update_xaxes( - title_text="", - automargin=True, - ) - fig.update_yaxes(title_text="") - - # format the y-axis - if y_format == "%": - fig.update_yaxes(tickformat=".2%") - elif y_format == "$": - fig.update_yaxes(tickprefix="$") - elif y_format == "#": - fig.update_yaxes(tickprefix=None) - - # Update layout for dark theme readability - fig.update_layout( - hovermode="x unified", - legend=dict( - orientation="h", - yanchor="bottom", - y=1.02, - xanchor="right", - x=1, - title=None, - ), - font=dict( - family="sans-serif", - ), - ) - return fig - - -def chart_oi(df, x_col, title): - fig = px.area( - df, - x=x_col, - y=["short_oi_pct", "long_oi_pct"], - line_shape="hv", - color_discrete_sequence=["red", "green"], - title=title, - ) - - # remove axis labels - fig.update_traces(hovertemplate=None) - fig.update_xaxes( - title_text="", - automargin=True, - ) - fig.update_yaxes(title_text="", tickformat=".0%") - - fig.update_layout( - hovermode="x unified", - legend=dict( - orientation="h", - yanchor="bottom", - y=1.02, - xanchor="right", - x=1, - title=None, - ), - font=dict( - family="sans-serif", - ), - ) - return fig diff --git a/dashboard/utils/data.py b/dashboard/utils/data.py deleted file mode 100644 index 3d4bea11..00000000 --- a/dashboard/utils/data.py +++ /dev/null @@ -1,54 +0,0 @@ -import os -import streamlit as st -import sqlalchemy -import pandas as pd - -# get the password from the environment -DB_NAME = st.secrets.database.DB_NAME -DB_USER = st.secrets.database.DB_USER -DB_PASS = st.secrets.database.DB_PASS -DB_HOST = st.secrets.database.DB_HOST -DB_PORT = st.secrets.database.DB_PORT - -# Database connection parameters -DEFAULT_DB_CONFIG = { - "dbname": DB_NAME, - "user": DB_USER, - "password": DB_PASS, - "host": DB_HOST, - "port": DB_PORT, -} - - -def get_connection(db_config=DEFAULT_DB_CONFIG): - connection_string = f"postgresql://{db_config['user']}:{db_config['password']}@{db_config['host']}:{db_config['port']}/{db_config['dbname']}" - engine = sqlalchemy.create_engine(connection_string) - conn = engine.connect() - return conn - - -def export_data(title, df): - csv = df.to_csv(index=False).encode("utf-8") - - st.write(f"### {title}") - st.download_button( - f"Download CSV", csv, "export.csv", "text/csv", key=f"{title}-csv" - ) - st.write(df.head(25)) - - -@st.cache_data(ttl=600) -def get_v2_markets(): - # initialize connection - db = get_connection() - - df_markets = pd.read_sql_query( - f""" - SELECT distinct market FROM optimism_mainnet.fct_v2_market_stats - """, - db, - ) - - db.close() - - return df_markets["market"].unique().tolist() diff --git a/docker-compose.yml b/docker-compose.yml index df619332..6b2746c0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -190,25 +190,6 @@ services: - ./extractors:/app - ./parquet-data:/parquet-data - dashboard: - image: ghcr.io/synthetixio/data/dashboard:${VERSION} - build: - context: ./dashboard - restart: always - networks: - - data - ports: - - "${DASHBOARD_PORT}:8501" - volumes: - - ${LEGACY_DB_LOCATION}:/app/data/perps.db - - .streamlit:/app/.streamlit # pass local secrets - - ./dashboard:/app - deploy: - resources: - limits: - cpus: "4.0" - memory: 12288M - scheduler: build: context: .