Add test for apply_fee_conditional

This commit is contained in:
Matthias 2020-05-03 11:13:59 +02:00
parent 38c4949360
commit 58168336e1
2 changed files with 32 additions and 7 deletions

View File

@ -1161,17 +1161,18 @@ class FreqtradeBot:
return False return False
def apply_fee_conditional(self, trade: Trade, trade_base_currency: str, 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). Applies the fee to amount (either from Order or from Trades).
Can eat into dust if more than the required asset is available. 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 # Eat into dust if we own more than base currency
logger.info(f"Fee amount for {trade} was in base currency - " logger.info(f"Fee amount for {trade} was in base currency - "
f"Eating Fee {fee} into dust.") f"Eating Fee {fee_abs} into dust.")
elif fee != 0: elif fee_abs != 0:
real_amount = self.exchange.amount_to_precision(trade.pair, amount - fee) real_amount = self.exchange.amount_to_precision(trade.pair, amount - fee_abs)
logger.info(f"Applying fee on amount for {trade} " logger.info(f"Applying fee on amount for {trade} "
f"(from {amount} to {real_amount}).") f"(from {amount} to {real_amount}).")
return real_amount return real_amount
@ -1203,7 +1204,7 @@ class FreqtradeBot:
if trade_base_currency == fee_currency: if trade_base_currency == fee_currency:
# Apply fee to amount # Apply fee to amount
return self.apply_fee_conditional(trade, trade_base_currency, 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 order_amount
return self.fee_detection_from_trades(trade, order, order_amount) return self.fee_detection_from_trades(trade, order, order_amount)
@ -1245,6 +1246,6 @@ class FreqtradeBot:
if fee_abs != 0: if fee_abs != 0:
return self.apply_fee_conditional(trade, trade_base_currency, return self.apply_fee_conditional(trade, trade_base_currency,
amount=amount, fee=fee_abs) amount=amount, fee_abs=fee_abs)
else: else:
return amount return amount

View File

@ -3305,6 +3305,30 @@ def test_get_real_amount_quote(default_conf, trades_for_order, buy_order_fee, fe
caplog) 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): 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=[]) mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[])