2018-11-17 20:16:32 +00:00
|
|
|
# pragma pylint: disable=W0603
|
|
|
|
""" Wallet """
|
2019-02-28 22:26:29 +00:00
|
|
|
|
2018-11-17 20:16:32 +00:00
|
|
|
import logging
|
2019-02-28 22:26:29 +00:00
|
|
|
from typing import Dict, NamedTuple
|
2018-11-17 20:16:32 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
2019-02-28 22:26:29 +00:00
|
|
|
from freqtrade import constants
|
2018-11-17 20:16:32 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-12-29 08:01:58 +00:00
|
|
|
# wallet data structure
|
2018-11-21 18:47:51 +00:00
|
|
|
class Wallet(NamedTuple):
|
2018-11-21 22:35:44 +00:00
|
|
|
currency: str
|
2018-11-21 18:47:51 +00:00
|
|
|
free: float = 0
|
|
|
|
used: float = 0
|
|
|
|
total: float = 0
|
|
|
|
|
|
|
|
|
2018-11-17 20:16:32 +00:00
|
|
|
class Wallets(object):
|
|
|
|
|
2019-02-28 22:26:29 +00:00
|
|
|
def __init__(self, config: dict, exchange: Exchange) -> None:
|
|
|
|
self._config = config
|
|
|
|
self._exchange = exchange
|
|
|
|
self._wallets: Dict[str, Wallet] = {}
|
|
|
|
|
2018-11-24 15:37:28 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def get_free(self, currency) -> float:
|
|
|
|
|
2019-02-28 22:26:29 +00:00
|
|
|
if self._config['dry_run']:
|
|
|
|
return self._config.get('dry_run_wallet', constants.DRY_RUN_WALLET)
|
2018-11-24 15:37:28 +00:00
|
|
|
|
2019-02-28 22:26:29 +00:00
|
|
|
balance = self._wallets.get(currency)
|
2018-11-26 01:18:29 +00:00
|
|
|
if balance and balance.free:
|
|
|
|
return balance.free
|
2018-11-23 09:17:10 +00:00
|
|
|
else:
|
2018-11-24 15:37:28 +00:00
|
|
|
return 0
|
2018-11-17 20:16:32 +00:00
|
|
|
|
2018-11-28 14:36:32 +00:00
|
|
|
def get_used(self, currency) -> float:
|
|
|
|
|
2019-02-28 22:26:29 +00:00
|
|
|
if self._config['dry_run']:
|
|
|
|
return self._config.get('dry_run_wallet', constants.DRY_RUN_WALLET)
|
2018-11-28 14:36:32 +00:00
|
|
|
|
2019-02-28 22:26:29 +00:00
|
|
|
balance = self._wallets.get(currency)
|
2018-11-28 14:36:32 +00:00
|
|
|
if balance and balance.used:
|
|
|
|
return balance.used
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def get_total(self, currency) -> float:
|
|
|
|
|
2019-02-28 22:26:29 +00:00
|
|
|
if self._config['dry_run']:
|
|
|
|
return self._config.get('dry_run_wallet', constants.DRY_RUN_WALLET)
|
2018-11-28 14:36:32 +00:00
|
|
|
|
2019-02-28 22:26:29 +00:00
|
|
|
balance = self._wallets.get(currency)
|
2018-11-28 14:36:32 +00:00
|
|
|
if balance and balance.total:
|
|
|
|
return balance.total
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2018-11-18 13:39:31 +00:00
|
|
|
def update(self) -> None:
|
2019-02-28 22:26:29 +00:00
|
|
|
|
|
|
|
balances = self._exchange.get_balances()
|
2018-11-17 20:16:32 +00:00
|
|
|
|
|
|
|
for currency in balances:
|
2019-02-28 22:26:29 +00:00
|
|
|
self._wallets[currency] = Wallet(
|
2018-11-21 18:47:51 +00:00
|
|
|
currency,
|
|
|
|
balances[currency].get('free', None),
|
|
|
|
balances[currency].get('used', None),
|
|
|
|
balances[currency].get('total', None)
|
|
|
|
)
|
2018-11-17 20:16:32 +00:00
|
|
|
|
2019-02-28 22:26:29 +00:00
|
|
|
logger.info('Wallets synced.')
|