Skip to content

Commit

Permalink
fixes provided
Browse files Browse the repository at this point in the history
  • Loading branch information
iamnovichek committed Feb 1, 2025
1 parent a0259f8 commit 0826ca9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 35 deletions.
1 change: 1 addition & 0 deletions apps/dashboard_app/charts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"strk": "STRK",
}


@dataclass
class ChartsHeaders:
low_health_factor_loans: str = "Loans with low health factor"
Expand Down
77 changes: 53 additions & 24 deletions apps/dashboard_app/charts/main.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
"""
This module defines the Dashboard class for rendering a DeRisk dashboard using Streamlit.
"""
import plotly
import numpy as np
import pandas as pd
import plotly
import streamlit as st
from datetime import datetime, UTC
from data_handler.handlers.loan_states.abstractions import State
from shared.helpers import (
add_leading_zeros,
extract_token_addresses,
fetch_token_symbols_from_set_of_loan_addresses,
update_loan_data_with_symbols,
add_leading_zeros
)

from helpers.settings import COLLATERAL_TOKENS, DEBT_TOKENS, STABLECOIN_BUNDLE_NAME, TOKEN_SETTINGS
from helpers.settings import (
COLLATERAL_TOKENS,
DEBT_TOKENS,
STABLECOIN_BUNDLE_NAME,
TOKEN_SETTINGS,
)

from .constants import ChartsHeaders, CommonValues
from .main_chart_figure import get_main_chart_figure, get_specific_loan_usd_amounts, get_bar_chart_figures
from .main_chart_figure import (
get_bar_chart_figures,
get_main_chart_figure,
get_specific_loan_usd_amounts,
)
from .utils import (
get_protocol_data_mappings,
infer_protocol_name,
process_liquidity,
transform_loans_data,
transform_main_chart_data,
infer_protocol_name,

)


Expand All @@ -39,7 +46,15 @@ class Dashboard:
# "Nostra Mainnet",
]

def __init__(self, state: State, general_stats: dict, supply_stats: dict, collateral_stats: dict, debt_stats: dict, utilization_stats: dict):
def __init__(
self,
state: State,
general_stats: dict,
supply_stats: dict,
collateral_stats: dict,
debt_stats: dict,
utilization_stats: dict,
):
"""
Initialize the dashboard.
"""
Expand Down Expand Up @@ -167,12 +182,17 @@ def load_loans_with_low_health_factor_chart(self):
label="Select range of USD borrowings",
min_value=0,
max_value=int(loans_data[CommonValues.debt_usd.value].max()),
value=(0, int(loans_data[CommonValues.debt_usd.value].max()) or 1), # FIXME remove 1
value=(
0,
int(loans_data[CommonValues.debt_usd.value].max()) or 1,
), # FIXME remove 1
)

st.dataframe(
loans_data[
(loans_data[CommonValues.health_factor.value] > 0) # TODO: debug the negative HFs
(
loans_data[CommonValues.health_factor.value] > 0
) # TODO: debug the negative HFs
& loans_data[CommonValues.debt_usd.value].between(
debt_usd_lower_bound, debt_usd_upper_bound
)
Expand Down Expand Up @@ -203,8 +223,13 @@ def load_top_loans_chart(self):
st.subheader("Sorted by collateral")
st.dataframe(
loans_data[
(loans_data[CommonValues.health_factor.value] > 1) # TODO: debug the negative HFs
& (loans_data[CommonValues.standardized_health_factor.value] != float("inf"))
(
loans_data[CommonValues.health_factor.value] > 1
) # TODO: debug the negative HFs
& (
loans_data[CommonValues.standardized_health_factor.value]
!= float("inf")
)
]
.sort_values(CommonValues.collateral_usd.value, ascending=False)
.iloc[:20],
Expand All @@ -216,7 +241,8 @@ def load_top_loans_chart(self):
loans_data[
(loans_data[CommonValues.health_factor.value] > 1)
& (
loans_data[CommonValues.standardized_health_factor.value] != float("inf")
loans_data[CommonValues.standardized_health_factor.value]
!= float("inf")
) # TODO: debug the negative HFs
]
.sort_values(CommonValues.debt_usd.value, ascending=False)
Expand Down Expand Up @@ -277,13 +303,12 @@ def load_detail_loan_chart(self):
protocol = infer_protocol_name(protocol, valid_protocols)

loan = loans_data_main.loc[
(loans_data[CommonValues.user.value] == user) & (loans_data[CommonValues.protocol.value] == protocol),
(loans_data[CommonValues.user.value] == user)
& (loans_data[CommonValues.protocol.value] == protocol),
]

if loan.empty:
st.warning(
f"No loan found for user = {user} and protocol = {protocol}."
)
st.warning(f"No loan found for user = {user} and protocol = {protocol}.")
else:
(
collateral_usd_amounts,
Expand Down Expand Up @@ -341,6 +366,8 @@ def load_comparison_lending_protocols_chart(self):
for column, token_1, token_2 in zip(columns, tokens[:4], tokens[4:]):
with column:
for token in [token_1, token_2]:
if token == "WBTC":
token = "wBTC"
figure = plotly.express.pie(
self.collateral_stats.reset_index(),
values=f"{token} collateral",
Expand All @@ -352,17 +379,19 @@ def load_comparison_lending_protocols_chart(self):
for token in [token_1, token_2]:
figure = plotly.express.pie(
self.debt_stats.reset_index(),
values=f"{token} debt",
names=CommonValues.protocol.value,
values=f"{token} debt".lower(),
names=CommonValues.protocol.value.lower(),
title=f"{token} debt",
color_discrete_sequence=plotly.express.colors.sequential.Greens_r,
)
st.plotly_chart(figure, True)
for token in [token_1, token_2]:
if "dai" in token.lower():
continue
figure = plotly.express.pie(
self.supply_stats.reset_index(),
values=f"{token} supply",
names=CommonValues.protocol.value,
values=f"{token} supply".lower(),
names=CommonValues.protocol.value.lower(),
title=f"{token} supply",
color_discrete_sequence=plotly.express.colors.sequential.Blues_r,
)
Expand All @@ -389,7 +418,7 @@ def run(self):
# Load sidebar with protocol settings
self.load_sidebar()
self.load_main_chart()
# self.load_loans_with_low_health_factor_chart()
# self.load_top_loans_chart()
# self.load_detail_loan_chart()
self.load_loans_with_low_health_factor_chart()
self.load_top_loans_chart()
self.load_detail_loan_chart()
self.load_comparison_lending_protocols_chart()
38 changes: 28 additions & 10 deletions apps/dashboard_app/charts/main_chart_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import math
from decimal import Decimal

import pandas as pd
import plotly.express
import plotly.graph_objs
from shared.amms import SwapAmm
from shared.state import State
from shared.custom_types import Prices
from shared.state import State

from helpers.settings import TOKEN_SETTINGS
from helpers.tools import (
Expand All @@ -18,6 +19,7 @@
get_prices,
get_underlying_address,
)

from .constants import SUPPLY_STATS_TOKEN_SYMBOLS_MAPPING

AMMS = ("10kSwap", "MySwap", "SithSwap", "JediSwap")
Expand Down Expand Up @@ -213,20 +215,36 @@ def get_bar_chart_figures(
for column in supply_stats.columns:
if "protocol" in column.lower():
bar_chart_supply_stats[column] = supply_stats[column][0]
bar_chart_collateral_stats[column] = collateral_stats[column][0]
bar_chart_collateral_stats[column] = collateral_stats[column.capitalize()][
0
]
bar_chart_debt_stats[column] = debt_stats[column][0]
continue
elif "total" in column.lower():
continue
underlying_symbol = column.split(" ")[0]
underlying_address = underlying_symbols_to_addresses[SUPPLY_STATS_TOKEN_SYMBOLS_MAPPING[underlying_symbol]]
bar_chart_supply_stats[underlying_symbol] = supply_stats[column].loc[0] * Decimal(prices[underlying_address])
underlying_address = underlying_symbols_to_addresses[
SUPPLY_STATS_TOKEN_SYMBOLS_MAPPING[underlying_symbol]
]
bar_chart_supply_stats[underlying_symbol] = supply_stats[column].loc[
0
] * Decimal(prices[underlying_address])
# handling the specific case with WBTC token
if underlying_symbol == "wbtc":
bar_chart_collateral_stats[underlying_symbol] = (collateral_stats["wBTC collateral"].loc[0] * prices[underlying_address])
bar_chart_collateral_stats[underlying_symbol] = (
collateral_stats["wBTC collateral"].loc[0] * prices[underlying_address]
)
else:
bar_chart_collateral_stats[underlying_symbol] = (collateral_stats[SUPPLY_STATS_TOKEN_SYMBOLS_MAPPING[underlying_symbol] + " collateral"].loc[0] * prices[underlying_address])
bar_chart_debt_stats[underlying_symbol] = (debt_stats[underlying_symbol + " debt"].loc[0] * prices[underlying_address])
bar_chart_collateral_stats[underlying_symbol] = (
collateral_stats[
SUPPLY_STATS_TOKEN_SYMBOLS_MAPPING[underlying_symbol]
+ " collateral"
].loc[0]
* prices[underlying_address]
)
bar_chart_debt_stats[underlying_symbol] = (
debt_stats[underlying_symbol + " debt"].loc[0] * prices[underlying_address]
)

bar_chart_supply_stats = bar_chart_supply_stats.T
bar_chart_collateral_stats = bar_chart_collateral_stats.T
Expand All @@ -237,7 +255,7 @@ def get_bar_chart_figures(
plotly.graph_objs.Bar(
name="zkLend",
x=bar_chart_supply_stats.index,
y=bar_chart_supply_stats["zkLend"],
y=bar_chart_supply_stats[0],
marker=plotly.graph_objs.bar.Marker(color="#fff7bc"),
),
# TODO: add functionality for other protocols
Expand All @@ -249,7 +267,7 @@ def get_bar_chart_figures(
plotly.graph_objs.Bar(
name="zkLend",
x=bar_chart_collateral_stats.index,
y=bar_chart_collateral_stats["zkLend"],
y=bar_chart_collateral_stats[0],
marker=plotly.graph_objs.bar.Marker(color="#fff7bc"),
),
# TODO: add functionality for other protocols
Expand All @@ -261,7 +279,7 @@ def get_bar_chart_figures(
plotly.graph_objs.Bar(
name="zkLend",
x=bar_chart_debt_stats.index,
y=bar_chart_debt_stats["zkLend"],
y=bar_chart_debt_stats[0],
marker=plotly.graph_objs.bar.Marker(color="#fff7bc"),
),
# TODO: add functionality for other protocols
Expand Down
3 changes: 2 additions & 1 deletion apps/dashboard_app/charts/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
This moudel process and transform liquidity, loan, and chart data for protocols.
"""
import difflib
import asyncio
import difflib
import logging
import math
import time
Expand Down Expand Up @@ -306,6 +306,7 @@ def transform_main_chart_data(

return main_chart_data


def infer_protocol_name(input_protocol: str, valid_protocols: list[str]) -> str:
"""Find the closest matching protocol name from a list of valid protocols using fuzzy matching.
Expand Down

0 comments on commit 0826ca9

Please sign in to comment.