refactor _rpc_balance

This commit is contained in:
gcarq 2018-06-22 04:08:51 +02:00
parent f1a370b3b9
commit 112998c205
3 changed files with 31 additions and 28 deletions

View File

@ -3,9 +3,9 @@ This module contains class to define a RPC communications
""" """
import logging import logging
from abc import abstractmethod from abc import abstractmethod
from datetime import date, datetime, timedelta from datetime import timedelta, datetime, date
from decimal import Decimal from decimal import Decimal
from typing import Any, Dict, List, Tuple from typing import Dict, Any, List
import arrow import arrow
import sqlalchemy as sql import sqlalchemy as sql
@ -252,7 +252,7 @@ class RPC(object):
'best_rate': round(bp_rate * 100, 2), 'best_rate': round(bp_rate * 100, 2),
} }
def _rpc_balance(self, fiat_display_currency: str) -> Tuple[List[Dict], float, str, float]: def _rpc_balance(self, fiat_display_currency: str) -> Dict:
""" Returns current account balance per crypto """ """ Returns current account balance per crypto """
output = [] output = []
total = 0.0 total = 0.0
@ -269,22 +269,25 @@ class RPC(object):
rate = self._freqtrade.exchange.get_ticker(coin + '/BTC', False)['bid'] rate = self._freqtrade.exchange.get_ticker(coin + '/BTC', False)['bid']
est_btc: float = rate * balance['total'] est_btc: float = rate * balance['total']
total = total + est_btc total = total + est_btc
output.append( output.append({
{ 'currency': coin,
'currency': coin, 'available': balance['free'],
'available': balance['free'], 'balance': balance['total'],
'balance': balance['total'], 'pending': balance['used'],
'pending': balance['used'], 'est_btc': est_btc,
'est_btc': est_btc })
}
)
if total == 0.0: if total == 0.0:
raise RPCException('all balances are zero') raise RPCException('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)
return output, total, symbol, value return {
'currencies': output,
'total': total,
'symbol': symbol,
'value': value,
}
def _rpc_start(self) -> Dict[str, str]: def _rpc_start(self) -> Dict[str, str]:
""" Handler for start """ """ Handler for start """

View File

@ -253,10 +253,9 @@ class Telegram(RPC):
def _balance(self, bot: Bot, update: Update) -> None: def _balance(self, bot: Bot, update: Update) -> None:
""" Handler for /balance """ """ Handler for /balance """
try: try:
currencys, total, symbol, value = \ result = self._rpc_balance(self._config['fiat_display_currency'])
self._rpc_balance(self._config['fiat_display_currency'])
output = '' output = ''
for currency in currencys: for currency in result['currencies']:
output += "*{currency}:*\n" \ output += "*{currency}:*\n" \
"\t`Available: {available: .8f}`\n" \ "\t`Available: {available: .8f}`\n" \
"\t`Balance: {balance: .8f}`\n" \ "\t`Balance: {balance: .8f}`\n" \
@ -264,8 +263,8 @@ class Telegram(RPC):
"\t`Est. BTC: {est_btc: .8f}`\n".format(**currency) "\t`Est. BTC: {est_btc: .8f}`\n".format(**currency)
output += "\n*Estimated Value*:\n" \ output += "\n*Estimated Value*:\n" \
"\t`BTC: {0: .8f}`\n" \ "\t`BTC: {total: .8f}`\n" \
"\t`{1}: {2: .2f}`\n".format(total, symbol, value) "\t`{symbol}: {value: .2f}`\n".format(**result)
self._send_msg(output, bot=bot) self._send_msg(output, bot=bot)
except RPCException as e: except RPCException as e:
self._send_msg(str(e), bot=bot) self._send_msg(str(e), bot=bot)

View File

@ -325,16 +325,17 @@ def test_rpc_balance_handle(default_conf, mocker):
freqtradebot = FreqtradeBot(default_conf) freqtradebot = FreqtradeBot(default_conf)
rpc = RPC(freqtradebot) rpc = RPC(freqtradebot)
output, total, symbol, value = rpc._rpc_balance(default_conf['fiat_display_currency']) result = rpc._rpc_balance(default_conf['fiat_display_currency'])
assert prec_satoshi(total, 12) assert prec_satoshi(result['total'], 12)
assert prec_satoshi(value, 180000) assert prec_satoshi(result['value'], 180000)
assert 'USD' in symbol assert 'USD' == result['symbol']
assert len(output) == 1 assert result['currencies'] == [{
assert 'BTC' in output[0]['currency'] 'currency': 'BTC',
assert prec_satoshi(output[0]['available'], 10) 'available': 10.0,
assert prec_satoshi(output[0]['balance'], 12) 'balance': 12.0,
assert prec_satoshi(output[0]['pending'], 2) 'pending': 2.0,
assert prec_satoshi(output[0]['est_btc'], 12) 'est_btc': 12.0,
}]
def test_rpc_start(mocker, default_conf) -> None: def test_rpc_start(mocker, default_conf) -> None: