refactor _rpc_balance
This commit is contained in:
parent
f1a370b3b9
commit
112998c205
@ -3,9 +3,9 @@ This module contains class to define a RPC communications
|
||||
"""
|
||||
import logging
|
||||
from abc import abstractmethod
|
||||
from datetime import date, datetime, timedelta
|
||||
from datetime import timedelta, datetime, date
|
||||
from decimal import Decimal
|
||||
from typing import Any, Dict, List, Tuple
|
||||
from typing import Dict, Any, List
|
||||
|
||||
import arrow
|
||||
import sqlalchemy as sql
|
||||
@ -252,7 +252,7 @@ class RPC(object):
|
||||
'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 """
|
||||
output = []
|
||||
total = 0.0
|
||||
@ -269,22 +269,25 @@ class RPC(object):
|
||||
rate = self._freqtrade.exchange.get_ticker(coin + '/BTC', False)['bid']
|
||||
est_btc: float = rate * balance['total']
|
||||
total = total + est_btc
|
||||
output.append(
|
||||
{
|
||||
'currency': coin,
|
||||
'available': balance['free'],
|
||||
'balance': balance['total'],
|
||||
'pending': balance['used'],
|
||||
'est_btc': est_btc
|
||||
}
|
||||
)
|
||||
output.append({
|
||||
'currency': coin,
|
||||
'available': balance['free'],
|
||||
'balance': balance['total'],
|
||||
'pending': balance['used'],
|
||||
'est_btc': est_btc,
|
||||
})
|
||||
if total == 0.0:
|
||||
raise RPCException('all balances are zero')
|
||||
|
||||
fiat = self._freqtrade.fiat_converter
|
||||
symbol = fiat_display_currency
|
||||
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]:
|
||||
""" Handler for start """
|
||||
|
@ -253,10 +253,9 @@ class Telegram(RPC):
|
||||
def _balance(self, bot: Bot, update: Update) -> None:
|
||||
""" Handler for /balance """
|
||||
try:
|
||||
currencys, total, symbol, value = \
|
||||
self._rpc_balance(self._config['fiat_display_currency'])
|
||||
result = self._rpc_balance(self._config['fiat_display_currency'])
|
||||
output = ''
|
||||
for currency in currencys:
|
||||
for currency in result['currencies']:
|
||||
output += "*{currency}:*\n" \
|
||||
"\t`Available: {available: .8f}`\n" \
|
||||
"\t`Balance: {balance: .8f}`\n" \
|
||||
@ -264,8 +263,8 @@ class Telegram(RPC):
|
||||
"\t`Est. BTC: {est_btc: .8f}`\n".format(**currency)
|
||||
|
||||
output += "\n*Estimated Value*:\n" \
|
||||
"\t`BTC: {0: .8f}`\n" \
|
||||
"\t`{1}: {2: .2f}`\n".format(total, symbol, value)
|
||||
"\t`BTC: {total: .8f}`\n" \
|
||||
"\t`{symbol}: {value: .2f}`\n".format(**result)
|
||||
self._send_msg(output, bot=bot)
|
||||
except RPCException as e:
|
||||
self._send_msg(str(e), bot=bot)
|
||||
|
@ -325,16 +325,17 @@ def test_rpc_balance_handle(default_conf, mocker):
|
||||
freqtradebot = FreqtradeBot(default_conf)
|
||||
rpc = RPC(freqtradebot)
|
||||
|
||||
output, total, symbol, value = rpc._rpc_balance(default_conf['fiat_display_currency'])
|
||||
assert prec_satoshi(total, 12)
|
||||
assert prec_satoshi(value, 180000)
|
||||
assert 'USD' in symbol
|
||||
assert len(output) == 1
|
||||
assert 'BTC' in output[0]['currency']
|
||||
assert prec_satoshi(output[0]['available'], 10)
|
||||
assert prec_satoshi(output[0]['balance'], 12)
|
||||
assert prec_satoshi(output[0]['pending'], 2)
|
||||
assert prec_satoshi(output[0]['est_btc'], 12)
|
||||
result = rpc._rpc_balance(default_conf['fiat_display_currency'])
|
||||
assert prec_satoshi(result['total'], 12)
|
||||
assert prec_satoshi(result['value'], 180000)
|
||||
assert 'USD' == result['symbol']
|
||||
assert result['currencies'] == [{
|
||||
'currency': 'BTC',
|
||||
'available': 10.0,
|
||||
'balance': 12.0,
|
||||
'pending': 2.0,
|
||||
'est_btc': 12.0,
|
||||
}]
|
||||
|
||||
|
||||
def test_rpc_start(mocker, default_conf) -> None:
|
||||
|
Loading…
Reference in New Issue
Block a user