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
This commit is contained in:
Matthias 2024-07-04 16:38:12 +02:00
parent d1a0f5a4e1
commit 7c697d4ded

View File

@ -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
)