Skip to content

Commit

Permalink
Add comments + typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
CBoYXD committed Jan 18, 2025
1 parent 04a5276 commit 5fae4aa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
23 changes: 15 additions & 8 deletions web_app/api/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,10 @@ async def get_add_deposit_data(position_id: UUID, amount: str, token_symbol: str
"""
Prepare data for adding an extra deposit to a position.
Args:
position_id: UUID of the position
amount: Amount of tokens to deposit
token_symbol: Symbol of the token being deposited
Returns:
Dict containing deposit data with token address and amount
:param position_id: UUID of the position
:param amount: Amount of tokens to deposit
:param token_symbol: Symbol of the token being deposited
:return: Dict containing deposit data with token address and amount
"""
if not amount:
raise HTTPException(status_code=400, detail="Amount is required")
Expand Down Expand Up @@ -242,6 +239,10 @@ async def add_extra_deposit(position_id: UUID, data: AddPositionDepositData):
Add extra deposit to a user position.
All deposits are now handled through ExtraDeposit table,
regardless of token type.
:param position_id: UUID of the position
:param data: Deposit data to create extra deposit
:return Dict containing detail
"""
if not data.amount:
raise HTTPException(status_code=400, detail="Amount is required")
Expand Down Expand Up @@ -296,5 +297,11 @@ async def get_user_positions(wallet_id: str, start: Optional[int] = None) -> lis
response_model=UserPositionExtraDepositsResponse,
summary="Get all extra positions for a user",
)
async def get_list_of_deposited_tokens(position_id: UUID) -> dict[str, dict | list]:
async def get_list_of_deposited_tokens(position_id: UUID) -> dict[str, dict | list[dict]]:
"""
Get position and extra position by position id
:param position_id: UUID of the position
:return Dict containing main position and extra positions
"""
return position_db_connector.get_all_extra_deposit_positions(position_id)
8 changes: 3 additions & 5 deletions web_app/contract_tools/mixins/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async def get_current_prices(cls) -> Dict[str, Decimal]:
)
symbol = TokenParams.get_token_symbol(address_with_leading_zero)
if symbol:
# Convert to Decimal for precise calculations
prices[symbol] = Decimal(str(current_price))
except (AttributeError, TypeError, ValueError) as e:
logger.debug(f"Error parsing price for {address}: {str(e)}")
Expand Down Expand Up @@ -108,11 +109,8 @@ async def get_current_position_sum(cls, position: dict) -> Decimal:
"""
Calculate the total position value including extra deposits.
Args:
position: Position object containing base amount and token information
Returns:
Decimal representing total position value including extra deposits
:param position: Position object containing base amount and token information
:return: Decimal representing total position value including extra deposits
"""
total_amount = Decimal(position["amount"])
extra_deposits = position_db_connector.get_extra_deposits_data(position["id"])
Expand Down
18 changes: 15 additions & 3 deletions web_app/db/crud/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def get_positions_by_wallet_id(
.all()
)

# Convert positions to a list of dictionaries
positions_dict = [
self._position_to_dict(position) for position in positions
]
Expand Down Expand Up @@ -132,6 +133,7 @@ def get_all_positions_by_wallet_id(
.all()
)

# Convert positions to a list of dictionaries
return [self._position_to_dict(position) for position in positions]

except SQLAlchemyError as e:
Expand Down Expand Up @@ -181,6 +183,7 @@ def create_position(
logger.error(f"User with wallet ID {wallet_id} not found")
return None

# Check if a position with status 'pending' already exists for this user
with self.Session() as session:
existing_position = (
session.query(Position)
Expand All @@ -199,6 +202,7 @@ def create_position(
session.refresh(existing_position)
return existing_position

# Create a new position since none with 'pending' status exists
position = Position(
user_id=user.id,
token_symbol=token_symbol,
Expand Down Expand Up @@ -304,7 +308,6 @@ def get_total_amounts_for_open_positions(self) -> dict[str, Decimal]:
"""
with self.Session() as db:
try:

token_amounts = (
db.query(
Position.token_symbol,
Expand Down Expand Up @@ -402,6 +405,7 @@ def get_all_liquidated_positions(self) -> list[dict]:
.all()
)

# Convert ORM objects to dictionaries for return
return [
{
"user_id": position.user_id,
Expand Down Expand Up @@ -468,7 +472,9 @@ def add_extra_deposit_to_position(
def get_extra_deposits_data(self, position_id: UUID) -> dict[str, str]:
"""
Get all extra deposits for a position.
Returns a dictionary of token_symbol: amount pairs.
:param position_id: UUID of the position
:return: a dictionary of token_symbol: amount pairs.
"""
with self.Session() as db:
deposits = (
Expand Down Expand Up @@ -519,8 +525,14 @@ def get_current_position_sum(
return Decimal(0)

def get_all_extra_deposit_positions(
self, position_id
self, position_id: UUID
) -> dict[str, dict | list[dict]]:
"""
Get all extra deposits by position id + main deposit
:param position_id: UUID of the position
:return: dict with main position and extra_deposits
"""
with self.Session() as db:
extra_deposits = (
db.query(ExtraDeposit)
Expand Down

0 comments on commit 5fae4aa

Please sign in to comment.