feat: add Bitfinex price fetcher for BTC/EUR

This commit is contained in:
counterweight 2025-12-22 15:42:59 +01:00
parent 3f3822f7c0
commit b077c93351
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C

38
backend/price_fetcher.py Normal file
View file

@ -0,0 +1,38 @@
"""Bitfinex price fetcher for BTC/EUR pair."""
from datetime import UTC, datetime
import httpx
BITFINEX_TICKER_URL = "https://api-pub.bitfinex.com/v2/ticker/tBTCEUR"
LAST_PRICE_INDEX = 6
async def fetch_btc_eur_price() -> tuple[float, datetime]:
"""
Fetch the current BTC/EUR price from Bitfinex.
Returns:
Tuple of (price, timestamp) where timestamp is the current UTC time.
Raises:
httpx.HTTPStatusError: If the API returns a non-2xx status code.
httpx.RequestError: If a network error occurs.
ValueError: If the response format is unexpected.
"""
async with httpx.AsyncClient() as client:
response = await client.get(BITFINEX_TICKER_URL)
response.raise_for_status()
data = response.json()
# Bitfinex ticker response is an array:
# [BID, BID_SIZE, ASK, ASK_SIZE, DAILY_CHANGE, DAILY_CHANGE_RELATIVE,
# LAST_PRICE, VOLUME, HIGH, LOW]
if not isinstance(data, list) or len(data) <= LAST_PRICE_INDEX:
raise ValueError(f"Unexpected response format: {data}")
price = float(data[LAST_PRICE_INDEX])
timestamp = datetime.now(UTC)
return price, timestamp