chore: improve typing for balance endpoint

This commit is contained in:
Matthias
2024-08-15 07:29:19 +02:00
parent 1b0ba0fa68
commit 646ed50f37
4 changed files with 15 additions and 7 deletions

View File

@@ -88,7 +88,7 @@ from freqtrade.exchange.exchange_utils_timeframe import (
timeframe_to_seconds, timeframe_to_seconds,
) )
from freqtrade.exchange.exchange_ws import ExchangeWS from freqtrade.exchange.exchange_ws import ExchangeWS
from freqtrade.exchange.types import OHLCVResponse, OrderBook, Ticker, Tickers from freqtrade.exchange.types import CcxtBalances, OHLCVResponse, OrderBook, Ticker, Tickers
from freqtrade.misc import ( from freqtrade.misc import (
chunks, chunks,
deep_merge_dicts, deep_merge_dicts,
@@ -1663,7 +1663,7 @@ class Exchange:
return order return order
@retrier @retrier
def get_balances(self) -> dict: def get_balances(self) -> CcxtBalances:
try: try:
balances = self._api.fetch_balance() balances = self._api.fetch_balance()
# Remove additional info from ccxt results # Remove additional info from ccxt results

View File

@@ -12,7 +12,7 @@ from freqtrade.enums import MarginMode, TradingMode
from freqtrade.exceptions import DDosProtection, OperationalException, TemporaryError from freqtrade.exceptions import DDosProtection, OperationalException, TemporaryError
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.exchange.common import retrier from freqtrade.exchange.common import retrier
from freqtrade.exchange.types import Tickers from freqtrade.exchange.types import CcxtBalances, Tickers
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -57,7 +57,7 @@ class Kraken(Exchange):
return super().get_tickers(symbols=symbols, cached=cached) return super().get_tickers(symbols=symbols, cached=cached)
@retrier @retrier
def get_balances(self) -> dict: def get_balances(self) -> CcxtBalances:
if self._config["dry_run"]: if self._config["dry_run"]:
return {} return {}

View File

@@ -25,6 +25,14 @@ class OrderBook(TypedDict):
nonce: Optional[int] nonce: Optional[int]
class CcxtBalance(TypedDict):
free: float
used: float
total: float
CcxtBalances = Dict[str, CcxtBalance]
Tickers = Dict[str, Ticker] Tickers = Dict[str, Ticker]
# pair, timeframe, candleType, OHLCV, drop last?, # pair, timeframe, candleType, OHLCV, drop last?,

View File

@@ -139,9 +139,9 @@ class Wallets:
if isinstance(balances[currency], dict): if isinstance(balances[currency], dict):
self._wallets[currency] = Wallet( self._wallets[currency] = Wallet(
currency, currency,
balances[currency].get("free"), balances[currency].get("free", 0),
balances[currency].get("used"), balances[currency].get("used", 0),
balances[currency].get("total"), balances[currency].get("total", 0),
) )
# Remove currencies no longer in get_balances output # Remove currencies no longer in get_balances output
for currency in deepcopy(self._wallets): for currency in deepcopy(self._wallets):