Fix get balance functionality

This commit is contained in:
Anton 2018-05-15 00:31:56 +03:00
parent 6cc8017943
commit f175f48418
3 changed files with 55 additions and 67 deletions

View File

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

View File

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

View File

@ -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):
""" """
@ -626,7 +619,7 @@ def test_telegram_balance_handle(default_conf, update, mocker) -> None:
assert '*Currency*: USDT' in result assert '*Currency*: 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(