Refactor project structure to organize APIs #24
@@ -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.
|
||||
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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user