Merge pull request #678 from arudov/fix/get-balance
Fixed bot crash while requesting the current balance
This commit is contained in:
		| @@ -245,35 +245,34 @@ class RPC(object): | |||||||
|         """ |         """ | ||||||
|         :return: current account balance per crypto |         :return: current account balance per crypto | ||||||
|         """ |         """ | ||||||
|         balances = [ |  | ||||||
|             c for c in exchange.get_balances() |  | ||||||
|             if c['Balance'] or c['Available'] or c['Pending'] |  | ||||||
|         ] |  | ||||||
|         if not balances: |  | ||||||
|             return True, '`All balances are zero.`' |  | ||||||
|  |  | ||||||
|         output = [] |         output = [] | ||||||
|         total = 0.0 |         total = 0.0 | ||||||
|         for currency in balances: |         for coin, balance in exchange.get_balances().items(): | ||||||
|             coin = currency['Currency'] |             if not balance['total']: | ||||||
|  |                 continue | ||||||
|  |  | ||||||
|  |             rate = None | ||||||
|             if coin == 'BTC': |             if coin == 'BTC': | ||||||
|                 currency["Rate"] = 1.0 |                 rate = 1.0 | ||||||
|             else: |             else: | ||||||
|                 if coin == 'USDT': |                 if coin == 'USDT': | ||||||
|                     currency["Rate"] = 1.0 / exchange.get_ticker('BTC/USDT', False)['bid'] |                     rate = 1.0 / exchange.get_ticker('BTC/USDT', False)['bid'] | ||||||
|                 else: |                 else: | ||||||
|                     currency["Rate"] = exchange.get_ticker(coin + '/BTC', False)['bid'] |                     rate = exchange.get_ticker(coin + '/BTC', False)['bid'] | ||||||
|             currency['BTC'] = currency["Rate"] * currency["Balance"] |             est_btc: float = rate * balance['total'] | ||||||
|             total = total + currency['BTC'] |             total = total + est_btc | ||||||
|             output.append( |             output.append( | ||||||
|                 { |                 { | ||||||
|                     'currency': currency['Currency'], |                     'currency': coin, | ||||||
|                     'available': currency['Available'], |                     'available': balance['free'], | ||||||
|                     'balance': currency['Balance'], |                     'balance': balance['total'], | ||||||
|                     'pending': currency['Pending'], |                     'pending': balance['used'], | ||||||
|                     'est_btc': currency['BTC'] |                     'est_btc': est_btc | ||||||
|                 } |                 } | ||||||
|             ) |             ) | ||||||
|  |         if total == 0.0: | ||||||
|  |             return True, '`All balances are zero.`' | ||||||
|  |  | ||||||
|         fiat = self.freqtrade.fiat_converter |         fiat = self.freqtrade.fiat_converter | ||||||
|         symbol = fiat_display_currency |         symbol = fiat_display_currency | ||||||
|         value = fiat.convert_amount(total, 'BTC', symbol) |         value = fiat.convert_amount(total, 'BTC', symbol) | ||||||
|   | |||||||
| @@ -264,17 +264,15 @@ class Telegram(RPC): | |||||||
|         (currencys, total, symbol, value) = result |         (currencys, total, symbol, value) = result | ||||||
|         output = '' |         output = '' | ||||||
|         for currency in currencys: |         for currency in currencys: | ||||||
|             output += """*Currency*: {currency} |             output += "*{currency}:*\n" \ | ||||||
|     *Available*: {available} |                       "\t`Available: {available: .8f}`\n" \ | ||||||
|     *Balance*: {balance} |                       "\t`Balance: {balance: .8f}`\n" \ | ||||||
|     *Pending*: {pending} |                       "\t`Pending: {pending: .8f}`\n" \ | ||||||
|     *Est. BTC*: {est_btc: .8f} |                       "\t`Est. BTC: {est_btc: .8f}`\n".format(**currency) | ||||||
|     """.format(**currency) |  | ||||||
|  |  | ||||||
|         output += """*Estimated Value*: |         output += "\n*Estimated Value*:\n" \ | ||||||
|     *BTC*: {0: .8f} |                   "\t`BTC: {0: .8f}`\n" \ | ||||||
|     *{1}*: {2: .2f} |                   "\t`{1}: {2: .2f}`\n".format(total, symbol, value) | ||||||
|     """.format(total, symbol, value) |  | ||||||
|         self.send_msg(output) |         self.send_msg(output) | ||||||
|  |  | ||||||
|     @authorized_only |     @authorized_only | ||||||
|   | |||||||
| @@ -288,22 +288,18 @@ def test_rpc_balance_handle(default_conf, mocker): | |||||||
|     """ |     """ | ||||||
|     Test rpc_balance() method |     Test rpc_balance() method | ||||||
|     """ |     """ | ||||||
|     mock_balance = [ |     mock_balance = { | ||||||
|         { |         'BTC': { | ||||||
|             'Currency': 'BTC', |             'free': 10.0, | ||||||
|             'Balance': 10.0, |             'total': 12.0, | ||||||
|             'Available': 12.0, |             'used': 2.0, | ||||||
|             'Pending': 0.0, |  | ||||||
|             'CryptoAddress': 'XXXX', |  | ||||||
|         }, |         }, | ||||||
|         { |         'ETH': { | ||||||
|             'Currency': 'ETH', |             'free': 0.0, | ||||||
|             'Balance': 0.0, |             'total': 0.0, | ||||||
|             'Available': 0.0, |             'used': 0.0, | ||||||
|             'Pending': 0.0, |         } | ||||||
|             'CryptoAddress': 'XXXX', |  | ||||||
|     } |     } | ||||||
|     ] |  | ||||||
|  |  | ||||||
|     patch_get_signal(mocker, (True, False)) |     patch_get_signal(mocker, (True, False)) | ||||||
|     mocker.patch.multiple( |     mocker.patch.multiple( | ||||||
| @@ -324,15 +320,15 @@ def test_rpc_balance_handle(default_conf, mocker): | |||||||
|     (error, res) = rpc.rpc_balance(default_conf['fiat_display_currency']) |     (error, res) = rpc.rpc_balance(default_conf['fiat_display_currency']) | ||||||
|     assert not error |     assert not error | ||||||
|     (trade, x, y, z) = res |     (trade, x, y, z) = res | ||||||
|     assert prec_satoshi(x, 10) |     assert prec_satoshi(x, 12) | ||||||
|     assert prec_satoshi(z, 150000) |     assert prec_satoshi(z, 180000) | ||||||
|     assert 'USD' in y |     assert 'USD' in y | ||||||
|     assert len(trade) == 1 |     assert len(trade) == 1 | ||||||
|     assert 'BTC' in trade[0]['currency'] |     assert 'BTC' in trade[0]['currency'] | ||||||
|     assert prec_satoshi(trade[0]['available'], 12) |     assert prec_satoshi(trade[0]['available'], 10) | ||||||
|     assert prec_satoshi(trade[0]['balance'], 10) |     assert prec_satoshi(trade[0]['balance'], 12) | ||||||
|     assert prec_satoshi(trade[0]['pending'], 0) |     assert prec_satoshi(trade[0]['pending'], 2) | ||||||
|     assert prec_satoshi(trade[0]['est_btc'], 10) |     assert prec_satoshi(trade[0]['est_btc'], 12) | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_rpc_start(mocker, default_conf) -> None: | def test_rpc_start(mocker, default_conf) -> None: | ||||||
|   | |||||||
| @@ -554,36 +554,29 @@ def test_telegram_balance_handle(default_conf, update, mocker) -> None: | |||||||
|     """ |     """ | ||||||
|     Test _balance() method |     Test _balance() method | ||||||
|     """ |     """ | ||||||
|     mock_balance = [ |  | ||||||
|         { |     mock_balance = { | ||||||
|             'Currency': 'BTC', |         'BTC': { | ||||||
|             'Balance': 10.0, |             'total': 12.0, | ||||||
|             'Available': 12.0, |             'free': 12.0, | ||||||
|             'Pending': 0.0, |             'used': 0.0 | ||||||
|             'CryptoAddress': 'XXXX', |  | ||||||
|         }, |         }, | ||||||
|         { |         'ETH': { | ||||||
|             'Currency': 'ETH', |             'total': 0.0, | ||||||
|             'Balance': 0.0, |             'free': 0.0, | ||||||
|             'Available': 0.0, |             'used': 0.0 | ||||||
|             'Pending': 0.0, |  | ||||||
|             'CryptoAddress': 'XXXX', |  | ||||||
|         }, |         }, | ||||||
|         { |         'USDT': { | ||||||
|             'Currency': 'USDT', |             'total': 10000.0, | ||||||
|             'Balance': 10000.0, |             'free': 10000.0, | ||||||
|             'Available': 0.0, |             'used': 0.0 | ||||||
|             'Pending': 0.0, |  | ||||||
|             'CryptoAddress': 'XXXX', |  | ||||||
|         }, |         }, | ||||||
|         { |         'LTC': { | ||||||
|             'Currency': 'LTC', |             'total': 10.0, | ||||||
|             'Balance': 10.0, |             'free': 10.0, | ||||||
|             'Available': 10.0, |             'used': 0.0 | ||||||
|             'Pending': 0.0, |         } | ||||||
|             'CryptoAddress': 'XXXX', |  | ||||||
|     } |     } | ||||||
|     ] |  | ||||||
|  |  | ||||||
|     def mock_ticker(symbol, refresh): |     def mock_ticker(symbol, refresh): | ||||||
|         """ |         """ | ||||||
| @@ -621,12 +614,12 @@ def test_telegram_balance_handle(default_conf, update, mocker) -> None: | |||||||
|     telegram._balance(bot=MagicMock(), update=update) |     telegram._balance(bot=MagicMock(), update=update) | ||||||
|     result = msg_mock.call_args_list[0][0][0] |     result = msg_mock.call_args_list[0][0][0] | ||||||
|     assert msg_mock.call_count == 1 |     assert msg_mock.call_count == 1 | ||||||
|     assert '*Currency*: BTC' in result |     assert '*BTC:*' in result | ||||||
|     assert '*Currency*: ETH' not in result |     assert '*ETH:*' not in result | ||||||
|     assert '*Currency*: USDT' in result |     assert '*USDT:*' in result | ||||||
|     assert 'Balance' in result |     assert 'Balance:' in result | ||||||
|     assert 'Est. BTC' in result |     assert 'Est. BTC:' in result | ||||||
|     assert '*BTC*:  12.00000000' in result |     assert 'BTC:  14.00000000' in result | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_zero_balance_handle(default_conf, update, mocker) -> None: | def test_zero_balance_handle(default_conf, update, mocker) -> None: | ||||||
| @@ -636,7 +629,7 @@ def test_zero_balance_handle(default_conf, update, mocker) -> None: | |||||||
|     patch_get_signal(mocker, (True, False)) |     patch_get_signal(mocker, (True, False)) | ||||||
|     patch_coinmarketcap(mocker, value={'price_usd': 15000.0}) |     patch_coinmarketcap(mocker, value={'price_usd': 15000.0}) | ||||||
|     mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock()) |     mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock()) | ||||||
|     mocker.patch('freqtrade.freqtradebot.exchange.get_balances', return_value=[]) |     mocker.patch('freqtrade.freqtradebot.exchange.get_balances', return_value={}) | ||||||
|  |  | ||||||
|     msg_mock = MagicMock() |     msg_mock = MagicMock() | ||||||
|     mocker.patch.multiple( |     mocker.patch.multiple( | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user