Skip to content

Commit

Permalink
Merge pull request #385 from sheddiboo/feat/user-collateral-endpoint
Browse files Browse the repository at this point in the history
add user collateral endpoint
  • Loading branch information
djeck1432 authored Jan 24, 2025
2 parents 3d8bf91 + 067fb6d commit 0e4dd93
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
63 changes: 63 additions & 0 deletions apps/sdk/api/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from typing import Dict
from fastapi import APIRouter, HTTPException
import pandas as pd
import json
from pathlib import Path
from sdk.schemas.schemas import UserCollateralResponse

router = APIRouter(
prefix="/user",
tags=["user"],
responses={404: {"description": "Not found"}},
)


@router.get("/debt", response_model=UserCollateralResponse)
async def get_user_debt(wallet_id: str, protocol_name: str) -> UserCollateralResponse:
"""
Get user's collateral information for a specific protocol.
Args:
wallet_id (str): The wallet ID of the user
protocol_name (str): The name of the protocol (e.g., 'zkLend')
Returns:
UserCollateralResponse: User's collateral information
Raises:
HTTPException: If user or protocol not found
"""
try:
data_path = "apps/sdk/mock_data.csv"
df = pd.read_csv(data_path)

user_data = df[
(df['user'] == wallet_id) &
(df['protocol_id'] == protocol_name)
]

if user_data.empty:
raise HTTPException(
status_code=404,
detail=f"No data found for wallet {wallet_id} in protocol {protocol_name}"
)
latest_entry = user_data.sort_values('timestamp', ascending=False).iloc[0]

try:
collateral = json.loads(latest_entry['collateral'].replace("'", '"'))
if not collateral:
collateral = {}
except (json.JSONDecodeError, AttributeError):
collateral = {}

return UserCollateralResponse(
wallet_id=wallet_id,
protocol_name=protocol_name,
collateral=collateral
)

except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Internal server error: {str(e)}"
)
14 changes: 13 additions & 1 deletion apps/sdk/schemas/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ class UserLoanByWalletResponse(BaseModel):
collateral: Dict[str, str]
debt: Dict[str, str]
deposit: Dict[str, str]



class UserCollateralResponse(BaseModel):
""" Base class for UserCollateralResponse
Attributes:
wallet_id: The unique identifier of the user's wallet address.
protocol_name: The name of the loan protocol (e.g., zkLend, Nostra).
collateral: A dictionary mapping token addresses to collateral values.
"""
wallet_id: str
protocol_name: str
collateral: Dict[str, float]

0 comments on commit 0e4dd93

Please sign in to comment.