Skip to content

Commit

Permalink
Merge pull request #96 from Benjtalkshow/fetching_price_from_endpoint
Browse files Browse the repository at this point in the history
Draft PR for Add fetching price from endpoint #72
  • Loading branch information
djeck1432 authored Oct 26, 2024
2 parents cb41db2 + eb800dc commit 311421e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions web_app/api/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ async def get_dashboard(wallet_id: str) -> DashboardResponse:
# Fetch zkLend position for the wallet ID
zklend_position = await DashboardMixin.get_zklend_position(contract_address)

# Fetch current prices
current_prices = await DashboardMixin.get_current_prices()

# Fetch balances (assuming you have a method for this)
wallet_balances = await DashboardMixin.get_wallet_balances(wallet_id)
return DashboardResponse(
balances=wallet_balances,
multipliers={"ETH": first_opened_position["multiplier"]},
start_dates={"ETH": first_opened_position["created_at"]},
zklend_position=zklend_position,
current_prices=current_prices,
)
34 changes: 32 additions & 2 deletions web_app/contract_tools/mixins/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
from typing import Dict
from decimal import Decimal

from web_app.contract_tools.blockchain_call import StarknetClient
from web_app.contract_tools.constants import TokenParams
Expand All @@ -18,12 +19,41 @@
# "https://cloud.argent-api.com/v1/tokens/defi/decomposition/{wallet_id}?chain=starknet"
ARGENT_X_POSITION_URL = "https://cloud.argent-api.com/v1/tokens/defi/"

# New constant for AVNU price endpoint
AVNU_PRICE_URL = "https://starknet.impulse.avnu.fi/v1/tokens/short"

class DashboardMixin:
"""
Mixin class for dashboard related methods.
"""

@classmethod
async def get_current_prices(cls) -> Dict[str, str]:
"""
Fetch current token prices from AVNU API.
:return: Returns dictionary mapping token symbols to their current prices.
"""
prices = {}

response = await APIRequest(base_url=AVNU_PRICE_URL).fetch("")
if not response:
return prices

for token_data in response:
try:
address = token_data.get("address")
current_price = token_data.get("currentPrice")
if address and current_price is not None:
symbol = TokenParams.get_token_symbol(address)
if symbol:
prices[symbol] = str(Decimal(current_price))
except AttributeError as e:
print(f"AttributeError while parsing price for {address}: {str(e)}")
except TypeError as e:
print(f"TypeError while parsing price for {address}: {str(e)}")

return prices

@classmethod
async def get_wallet_balances(cls, holder_address: str) -> Dict[str, str]:
"""
Expand All @@ -47,7 +77,7 @@ async def get_wallet_balances(cls, holder_address: str) -> Dict[str, str]:
)

return wallet_balances

@classmethod
async def get_zklend_position(cls, contract_address: str) -> ZkLendPositionResponse:
"""
Expand Down Expand Up @@ -76,4 +106,4 @@ def _get_products(cls, dapps: list) -> list[dict]:
:param dapps: List of dapps
:return: List of positions
"""
return [product for dapp in dapps for product in dapp.get("products", [])]
return [product for dapp in dapps for product in dapp.get("products", [])]

0 comments on commit 311421e

Please sign in to comment.