From bbbacab3da9c1e467122025304b8b954594429fb Mon Sep 17 00:00:00 2001 From: Berack96 Date: Thu, 9 Oct 2025 17:00:33 +0200 Subject: [PATCH] fix bug conversione delle valute fiat in stablecoin in BinanceWrapper --- src/app/api/markets/binance.py | 17 +++++++++++------ tests/api/test_binance.py | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/app/api/markets/binance.py b/src/app/api/markets/binance.py index a29a674..18b5f35 100644 --- a/src/app/api/markets/binance.py +++ b/src/app/api/markets/binance.py @@ -25,6 +25,12 @@ def extract_price(kline_data: list[Any]) -> Price: price.set_timestamp(timestamp_ms=timestamp) return price + +# Add here eventual other fiat not supported by Binance +FIAT_TO_STABLECOIN = { + "USD": "USDT", +} + class BinanceWrapper(MarketWrapper): """ Wrapper per le API autenticate di Binance.\n @@ -36,16 +42,15 @@ class BinanceWrapper(MarketWrapper): def __init__(self, currency: str = "USD"): """ Inizializza il wrapper di Binance con le credenziali API e la valuta di riferimento. - Se viene fornita una valuta fiat come "USD", questa viene automaticamente convertita in una stablecoin Tether ("USDT") per compatibilità con Binance, - poiché Binance non supporta direttamente le valute fiat per il trading di criptovalute. - Tutti i prezzi e volumi restituiti saranno quindi denominati nella stablecoin (ad esempio, "USDT") e non nella valuta fiat originale. - Args: - currency (str): Valuta in cui restituire i prezzi. Se "USD" viene fornito, verrà utilizzato "USDT". Default è "USD". + Alcune valute fiat non sono supportate direttamente da Binance (es. "USD"). + Infatti, se viene fornita una valuta fiat come "USD", questa viene automaticamente convertita in una stablecoin Tether ("USDT") per compatibilità con Binance. + Args: + currency (str): Valuta in cui restituire i prezzi. Se "USD" viene fornito, verrà utilizzato "USDT". Default è "USD". """ api_key = os.getenv("BINANCE_API_KEY") api_secret = os.getenv("BINANCE_API_SECRET") - self.currency = f"{currency}T" + self.currency = currency if currency not in FIAT_TO_STABLECOIN else FIAT_TO_STABLECOIN[currency] self.client = Client(api_key=api_key, api_secret=api_secret) def __format_symbol(self, asset_id: str) -> str: diff --git a/tests/api/test_binance.py b/tests/api/test_binance.py index fde26ac..46c6c2b 100644 --- a/tests/api/test_binance.py +++ b/tests/api/test_binance.py @@ -51,3 +51,18 @@ class TestBinance: assert entry.close > 0 assert entry.high > 0 assert entry.timestamp != '' + + def test_binance_fiat_conversion(self): + market = BinanceWrapper(currency="USD") + assert market.currency == "USDT" + product = market.get_product("BTC") + assert product is not None + assert product.symbol == "BTC" + assert product.price > 0 + + market = BinanceWrapper(currency="EUR") + assert market.currency == "EUR" + product = market.get_product("BTC") + assert product is not None + assert product.symbol == "BTC" + assert product.price > 0