# Path: cryptofolio/api/API.py
# class API:
#     def __init__(self, exchange=None):
#         self.exchange = exchange
#         self.refreshBalances()
# 
#     def refreshBalances(self):
#         self.balances = self.__getBalances()
# 
#     def __getBalances(self):
#         balances = {}
#         error = None
# 
#         if self.exchange:
#             try:
#                 key, secret, passphrase = ('', '', '')
# 
#                 if self.exchange.key:
#                     key = self.exchange.key
# 
#                 if self.exchange.secret:
#                     secret = self.exchange.secret
# 
#                 if self.exchange.passphrase:
#                     passphrase = self.exchange.passphrase
# 
#                 api = None
# 
#                 if self.exchange.exchange.label == Config.BINANCE:
#                     api = Binance(key, secret)
#                 elif self.exchange.exchange.label == Config.BITTREX:
#                     api = Bittrex(key, secret)
#                 elif self.exchange.exchange.label == Config.COINBASE:
#                     api = Coinbase(key, secret)
#                 elif self.exchange.exchange.label == Config.POLONIEX:
#                     api = Poloniex(key, secret)
#                 elif self.exchange.exchange.label == Config.COINBASEPRO:
#                     api = CoinbasePro(key, secret, passphrase)
#                 elif self.exchange.exchange.label == Config.KRAKEN:
#                     api = Kraken(key, secret)
#                 elif self.exchange.exchange.label == Config.KUCOIN:
#                     api = Kucoin(key, secret)
# 
#                 if api:
#                     new_balances = api.getBalances()
#                     for key in new_balances:
#                         if key in balances:
#                             balances[key] += new_balances[key]
#                         else:
#                             balances[key] = new_balances[key]
#                 else:
#                     error = "Exchange %s is not defined!" % (
#                         self.exchange.exchange.label)
# 
#             except ExchangeException as e:
#                 error = e.message
# 
#         return (balances, error)
# 
#     def getBalances(self):
#         return self.balances
# 
# Path: cryptofolio/api/Config.py
# def get_default_fiat_name():
#     return get_configured_fiat_names()[0]
# 
# Path: cryptofolio/api/BalanceFromAddress.py
# class BalanceFromAddress:
#     def __init__(self):
#         self.logger = Logger(__name__)
#         self.handlers = {
#             'BTC': Blockchain(),
#             'ETH': Etherscan(),
#             'LTC': Chainz(),
#             'BCH': Blockchair(),
#             'XRP': Ripple()
#         }
# 
#     def getSingleBalance(self, currency, address):
#         if currency in self.handlers:
#             return self.handlers[currency].getBalance(address)
#         return 0.0
# 
#     def getBalances(self, address_inputs):
#         result = {}
#         for i, a in enumerate(address_inputs):
#             result[a.address] = self.getSingleBalance(a.currency, a.address)
#             # poor man rate limiter
#             if (i + 1) % 5 == 0:
#                 sleep(1)
#         return result
# 
#     def getSupportedCurrencies():
#         supported_currencies = ['BTC', 'ETH', 'LTC', 'BCH', 'XRP']
#         return zip(supported_currencies, supported_currencies)


from django.contrib.auth.models import User
from django.db import models
from encrypted_model_fields.fields import EncryptedCharField
from .api.API import API
from .api.Config import get_default_fiat_name
from .api.BalanceFromAddress import BalanceFromAddress
    def __str__(self):
        return "%s %s %s %s" % (self.user.username, self.timestamp,
                                self.currency, self.amount)


class AddressInput(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now=True)
    currency = models.CharField(max_length=10, default='BTC')
    address = models.CharField(max_length=100)
    amount = models.FloatField(default=None, blank=True, null=True)

    def __str__(self):
        return "%s %s %s %s %s" % (self.user.username, self.timestamp,
                                   self.currency, self.address, self.amount)

class Investment(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    amount = models.FloatField(default=None, blank=True, null=True)
    fiat = models.CharField(max_length=10, default=get_default_fiat_name())
    timestamp = models.DateTimeField(auto_now=True)

    def __str__(self):
        return "%s %s %s %s" % (self.user.username, self.amount, self.fiat.name,
                                self.timestamp)

class TimeSeries(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now=True)
    amount = models.FloatField(default=None, blank=True, null=True)

# Path: cryptofolio/api/API.py
# class API:
#     def __init__(self, exchange=None):
#         self.exchange = exchange
#         self.refreshBalances()
# 
#     def refreshBalances(self):
#         self.balances = self.__getBalances()
# 
#     def __getBalances(self):
#         balances = {}
#         error = None
# 
#         if self.exchange:
#             try:
#                 key, secret, passphrase = ('', '', '')
# 
#                 if self.exchange.key:
#                     key = self.exchange.key
# 
#                 if self.exchange.secret:
#                     secret = self.exchange.secret
# 
#                 if self.exchange.passphrase:
#                     passphrase = self.exchange.passphrase
# 
#                 api = None
# 
#                 if self.exchange.exchange.label == Config.BINANCE:
#                     api = Binance(key, secret)
#                 elif self.exchange.exchange.label == Config.BITTREX:
#                     api = Bittrex(key, secret)
#                 elif self.exchange.exchange.label == Config.COINBASE:
#                     api = Coinbase(key, secret)
#                 elif self.exchange.exchange.label == Config.POLONIEX:
#                     api = Poloniex(key, secret)
#                 elif self.exchange.exchange.label == Config.COINBASEPRO:
#                     api = CoinbasePro(key, secret, passphrase)
#                 elif self.exchange.exchange.label == Config.KRAKEN:
#                     api = Kraken(key, secret)
#                 elif self.exchange.exchange.label == Config.KUCOIN:
#                     api = Kucoin(key, secret)
# 
#                 if api:
#                     new_balances = api.getBalances()
#                     for key in new_balances:
#                         if key in balances:
#                             balances[key] += new_balances[key]
#                         else:
#                             balances[key] = new_balances[key]
#                 else:
#                     error = "Exchange %s is not defined!" % (
#                         self.exchange.exchange.label)
# 
#             except ExchangeException as e:
#                 error = e.message
# 
#         return (balances, error)
# 
#     def getBalances(self):
#         return self.balances
# 
# Path: cryptofolio/api/Config.py
# def get_default_fiat_name():
#     return get_configured_fiat_names()[0]
# 
# Path: cryptofolio/api/BalanceFromAddress.py
# class BalanceFromAddress:
#     def __init__(self):
#         self.logger = Logger(__name__)
#         self.handlers = {
#             'BTC': Blockchain(),
#             'ETH': Etherscan(),
#             'LTC': Chainz(),
#             'BCH': Blockchair(),
#             'XRP': Ripple()
#         }
# 
#     def getSingleBalance(self, currency, address):
#         if currency in self.handlers:
#             return self.handlers[currency].getBalance(address)
#         return 0.0
# 
#     def getBalances(self, address_inputs):
#         result = {}
#         for i, a in enumerate(address_inputs):
#             result[a.address] = self.getSingleBalance(a.currency, a.address)
#             # poor man rate limiter
#             if (i + 1) % 5 == 0:
#                 sleep(1)
#         return result
# 
#     def getSupportedCurrencies():
#         supported_currencies = ['BTC', 'ETH', 'LTC', 'BCH', 'XRP']
#         return zip(supported_currencies, supported_currencies)


# Path: cryptofolio/models.py
from django.contrib.auth.models import User
from django.db import models
from encrypted_model_fields.fields import EncryptedCharField
from .api.API import API
from .api.Config import get_default_fiat_name
from .api.BalanceFromAddress import BalanceFromAddress

    def __str__(self):
        return "%s %s %s %s" % (self.user.username, self.timestamp,
                                self.currency, self.amount)


class AddressInput(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now=True)
    currency = models.CharField(max_length=10, default='BTC')
    address = models.CharField(max_length=100)
    amount = models.FloatField(default=None, blank=True, null=True)

    def __str__(self):
        return "%s %s %s %s %s" % (self.user.username, self.timestamp,
                                   self.currency, self.address, self.amount)

class Investment(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    amount = models.FloatField(default=None, blank=True, null=True)
    fiat = models.CharField(max_length=10, default=get_default_fiat_name())
    timestamp = models.DateTimeField(auto_now=True)

    def __str__(self):
        return "%s %s %s %s" % (self.user.username, self.amount, self.fiat.name,
                                self.timestamp)

class TimeSeries(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now=True)
    amount = models.FloatField(default=None, blank=True, null=True)

    fiat = models.CharField(max_length=10, default=get_default_fiat_name())