diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 5ef4888e0..1f9573122 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1161,17 +1161,18 @@ class FreqtradeBot: return False def apply_fee_conditional(self, trade: Trade, trade_base_currency: str, - amount: float, fee: float) -> float: + amount: float, fee_abs: float) -> float: """ Applies the fee to amount (either from Order or from Trades). Can eat into dust if more than the required asset is available. """ - if fee != 0 and self.wallets.get_free(trade_base_currency) >= amount: + self.wallets.update() + if fee_abs != 0 and self.wallets.get_free(trade_base_currency) >= amount: # Eat into dust if we own more than base currency logger.info(f"Fee amount for {trade} was in base currency - " - f"Eating Fee {fee} into dust.") - elif fee != 0: - real_amount = self.exchange.amount_to_precision(trade.pair, amount - fee) + f"Eating Fee {fee_abs} into dust.") + elif fee_abs != 0: + real_amount = self.exchange.amount_to_precision(trade.pair, amount - fee_abs) logger.info(f"Applying fee on amount for {trade} " f"(from {amount} to {real_amount}).") return real_amount @@ -1203,7 +1204,7 @@ class FreqtradeBot: if trade_base_currency == fee_currency: # Apply fee to amount return self.apply_fee_conditional(trade, trade_base_currency, - amount=order_amount, fee=fee_cost) + amount=order_amount, fee_abs=fee_cost) return order_amount return self.fee_detection_from_trades(trade, order, order_amount) @@ -1245,6 +1246,6 @@ class FreqtradeBot: if fee_abs != 0: return self.apply_fee_conditional(trade, trade_base_currency, - amount=amount, fee=fee_abs) + amount=amount, fee_abs=fee_abs) else: return amount diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 4a28147fc..e69c88a13 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -3305,6 +3305,30 @@ def test_get_real_amount_quote(default_conf, trades_for_order, buy_order_fee, fe caplog) +def test_get_real_amount_quote_dust(default_conf, trades_for_order, buy_order_fee, fee, caplog, mocker): + mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order) + walletmock = mocker.patch('freqtrade.wallets.Wallets.update') + mocker.patch('freqtrade.wallets.Wallets.get_free', return_value=8.1122) + amount = sum(x['amount'] for x in trades_for_order) + trade = Trade( + pair='LTC/ETH', + amount=amount, + exchange='binance', + open_rate=0.245441, + fee_open=fee.return_value, + fee_close=fee.return_value, + open_order_id="123456" + ) + freqtrade = get_patched_freqtradebot(mocker, default_conf) + + walletmock.reset_mock() + # Amount is kept as is + assert freqtrade.get_real_amount(trade, buy_order_fee) == amount + assert walletmock.call_count == 1 + assert log_has_re(r'Fee amount for Trade.* was in base currency ' + '- Eating Fee 0.008 into dust', caplog) + + def test_get_real_amount_no_trade(default_conf, buy_order_fee, caplog, mocker, fee): mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[])