Skip to content

Commit

Permalink
Merge branch 'master' into features/fetch_total_amount_of_collateral
Browse files Browse the repository at this point in the history
  • Loading branch information
Bigcheese-2 authored Feb 27, 2025
2 parents 8130115 + f74ddbe commit 9909667
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion apps/dashboard_app/charts/main_chart_figure.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
This module includes functions to generate financial charts using token price and liquidity data.
"""

from collections import defaultdict
import math
from decimal import Decimal
import pandas as pd
Expand Down Expand Up @@ -408,6 +408,7 @@ def display_user_history_chart(df: pd.DataFrame):
else:
st.error("No data found for this wallet ID.")

features/fetch_total_amount_of_collateral
def get_total_collateral_amount(df: pd.DataFrame) -> pd.DataFrame:
"""
Calculate the total collateral amount for each token address.
Expand Down Expand Up @@ -442,3 +443,67 @@ def get_total_collateral_amount(df: pd.DataFrame) -> pd.DataFrame:
result = get_total_collateral_amount(mock_data)
print("Total collateral amount per token address:")
print(result)

def get_total_debt_amount(debt_df: pd.DataFrame) -> pd.DataFrame:
"""
Computes the total debt amount per token from the provided DataFrame.
The function assumes that the DataFrame contains (according to the data.csv):
- A column representing token identifiers (e.g., 'Token' or 'Asset').
- A 'Debt (USD)' column containing numerical debt values.
It groups the data by token and sums the debt amounts accordingly.
Args:
debt_df (pd.DataFrame): DataFrame containing debt data with at least:
- A column for token identifiers.
- A 'Debt (USD)' column for debt values.
"""
total_debt = {}

for _, row in debt_df.iterrows():
debt_entry = row.get("Debt")
if isinstance(debt_entry, dict):
for token, amount in debt_entry.items():
total_debt[token] = total_debt.get(token, 0) + amount
else:
# If the 'Debt' field is not a dict, skip this row or handle accordingly.
continue

result_df = pd.DataFrame(list(total_debt.items()), columns=["token", "total_debt"])
return result_df

def get_total_deposit_amount(df: pd.DataFrame) -> dict:
"""
A dataframe with a "Collateral" column containing deposit amount in the format:
"TOKEN: amount, TOKEN: amount, ...".
This function sums up the deposit amounts per token across all rows.
Args: Pandas DataFrame with a "Collateral" column.
Returns: A dictionary containing the total deposit amount per token.
"""
totals = defaultdict(float)

for _, row in df.iterrows():
collateral = row.get("Collateral", "")

if pd.isna(collateral) or not collateral.strip():
continue

tokens = collateral.split(',')
for token_entry in tokens:
token_entry = token_entry.strip()
if not token_entry or ":" not in token_entry:
continue

token, amount_str = token_entry.split(":", 1)
token = token.strip()
try:
amount = float(amount_str.strip())
except ValueError:

continue

totals[token] += amount

return dict(totals)

0 comments on commit 9909667

Please sign in to comment.