-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from sheddiboo/feat/user-collateral-endpoint
add user collateral endpoint
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters