From 859c1d20ef8beadfa9e4f0733091c808ed165157 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 22 Feb 2022 20:10:47 +0000 Subject: [PATCH] Move rates and margin functions into Money library --- handler/agora.py | 35 ++--------------------------------- handler/money.py | 33 +++++++++++++++++++++++++++++++++ handler/revolut.py | 2 +- handler/transactions.py | 8 ++++---- 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index bd6b929..ddd0e98 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -442,37 +442,6 @@ class Agora(object): return_ids.append(rtrn["success"]) return all(return_ids) - # TODO: move to money - 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 - - # TODO: move to money - 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) - @util.handle_exceptions def create_ad(self, asset, countrycode, currency, provider, edit=False, ad_id=None): """ @@ -502,7 +471,7 @@ class Agora(object): # Substitute the asset ad = ad.replace("$ASSET$", asset) - rates = self.get_rates_all() + rates = self.money.get_rates_all() if asset == "XMR": min_usd = float(settings.Agora.MinUSDXMR) max_usd = float(settings.Agora.MaxUSDXMR) @@ -721,5 +690,5 @@ class Agora(object): if currency == "USD": return float(amount) else: - rates = self.get_rates_all() + rates = self.money.get_rates_all() return float(amount) / rates[currency] diff --git a/handler/money.py b/handler/money.py index 5e1a4e0..0cb4b00 100644 --- a/handler/money.py +++ b/handler/money.py @@ -5,6 +5,10 @@ from twisted.logger import Logger 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. @@ -37,3 +41,32 @@ class Money(object): 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) diff --git a/handler/revolut.py b/handler/revolut.py index 4e138b5..bd901d5 100644 --- a/handler/revolut.py +++ b/handler/revolut.py @@ -184,7 +184,7 @@ class Revolut(object): return False def get_total_usd(self): - rates = self.agora.get_rates_all() + rates = self.money.get_rates_all() accounts = self.accounts() if not accounts: return False diff --git a/handler/transactions.py b/handler/transactions.py index f5baf00..1ab50b6 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -194,7 +194,7 @@ class Transactions(object): if currency == "USD": amount_usd = amount else: - rates = self.agora.get_rates_all() + rates = self.money.get_rates_all() amount_usd = amount / rates[currency] # Amount is reliable here as it is checked by find_trade, so no need for stored_trade["amount"] if float(amount_usd) > float(settings.Agora.AcceptableAltLookupUSD): @@ -230,7 +230,7 @@ class Transactions(object): if looked_up_without_reference: return # If the amount does not match exactly, get the min and max values for our given acceptable margins for trades - min_amount, max_amount = self.agora.get_acceptable_margins(currency, amount) + min_amount, max_amount = self.money.get_acceptable_margins(currency, amount) self.log.info( "Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}", min_amount=min_amount, @@ -510,7 +510,7 @@ class Transactions(object): total_usd = total_usd_agora + total_usd_revolut # Convert the total USD price to GBP and SEK - rates = self.agora.get_rates_all() + rates = self.money.get_rates_all() price_sek = rates["SEK"] * total_usd price_usd = total_usd price_gbp = rates["GBP"] * total_usd @@ -599,7 +599,7 @@ class Transactions(object): if dash is False: return False - rates = self.agora.get_rates_all() + rates = self.money.get_rates_all() cumul_usd = 0 for contact_id, contact in dash.items(): # We need created at in order to look up the historical prices