# Twisted/Klein imports from twisted.logger import Logger # Other library imports from pycoingecko import CoinGeckoAPI # Project imports from settings import settings class Money(object): """ Generic class for handling money-related matters that aren't Revolut or Agora. """ def __init__(self): """ Initialise the Money object. Set the logger. Initialise the CoinGecko API. """ self.log = Logger("money") self.cg = CoinGeckoAPI() def lookup_rates(self, ads, rates=None): """ Lookup the rates for a list of public ads. """ if not rates: rates = self.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=self.markets.get_all_currencies()) # Set the price based on the asset for ad in ads: if ad[4] == "XMR": coin = "monero" elif ad[4] == "BTC": coin = "bitcoin" # No s here currency = ad[5] base_currency_price = rates[coin][currency.lower()] price = float(ad[2]) rate = round(price / base_currency_price, 2) ad.append(rate) return sorted(ads, key=lambda x: x[2]) def get_rates_all(self): """ Get all rates that pair with USD. :return: dictionary of USD/XXX rates :rtype: dict """ rates = self.cr.get_rates("USD") return rates def get_acceptable_margins(self, currency, amount): """ Get the minimum and maximum amounts we would accept a trade for. :param currency: currency code :param amount: amount :return: (min, max) :rtype: tuple """ rates = self.get_rates_all() if currency == "USD": min_amount = amount - float(settings.Agora.AcceptableUSDMargin) max_amount = amount + float(settings.Agora.AcceptableUSDMargin) return (min_amount, max_amount) amount_usd = amount / rates[currency] min_usd = amount_usd - float(settings.Agora.AcceptableUSDMargin) max_usd = amount_usd + float(settings.Agora.AcceptableUSDMargin) min_local = min_usd * rates[currency] max_local = max_usd * rates[currency] return (min_local, max_local) def to_usd(self, amount, currency): if currency == "USD": return float(amount) else: rates = self.get_rates_all() return float(amount) / rates[currency]