From 7c697d4ded67357909ca6ae1285979b96d6b2f2c Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 4 Jul 2024 16:38:12 +0200 Subject: [PATCH] fix: Validate trades for fee We can't rely on the "trades" subarray from the order to contain everything we need As such, we need to ensure that required properties are present and not None. closes #10398 --- freqtrade/freqtradebot.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index b3ff4923d..5a33e9fa6 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -2370,6 +2370,18 @@ class FreqtradeBot(LoggingMixin): trade, order, order_obj, order_amount, order.get("trades", []) ) + def _trades_valid_for_fee(self, trades: List[Dict[str, Any]]) -> bool: + """ + Check if trades are valid for fee detection. + :return: True if trades are valid for fee detection, False otherwise + """ + if not trades: + return False + # We expect amount and cost to be present in all trade objects. + if any(trade.get("amount") is None or trade.get("cost") is None for trade in trades): + return False + return True + def fee_detection_from_trades( self, trade: Trade, order: Dict, order_obj: Order, order_amount: float, trades: List ) -> Optional[float]: @@ -2377,7 +2389,7 @@ class FreqtradeBot(LoggingMixin): fee-detection fallback to Trades. Either uses provided trades list or the result of fetch_my_trades to get correct fee. """ - if not trades: + if not self._trades_valid_for_fee(trades): trades = self.exchange.get_trades_for_order( self.exchange.get_order_id_conditional(order), trade.pair, order_obj.order_date )