From 6e8903833ccc0d2050ce4899fe245614a8385bc1 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 22 Feb 2022 20:06:56 +0000 Subject: [PATCH] Move lookup rates into new Money library --- handler/agora.py | 22 +-------------------- handler/app.py | 2 ++ handler/money.py | 39 +++++++++++++++++++++++++++++++++++++ handler/tests/test_agora.py | 12 ++++++++---- 4 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 handler/money.py diff --git a/handler/agora.py b/handler/agora.py index ed08e87..bd6b929 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -311,26 +311,6 @@ class Agora(object): to_return.append(to_append) return to_return - # TODO: move to money library - 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 run_cheat_in_thread(self, assets=None): """ Update prices in another thread. @@ -395,7 +375,7 @@ class Agora(object): ads_list = self.enum_public_ads(asset, currency, providers) if not ads_list: continue - ads = self.lookup_rates(ads_list, rates=rates) + ads = self.money.lookup_rates(ads_list, rates=rates) if not ads: continue if currency in public_ads: diff --git a/handler/app.py b/handler/app.py index 44583ab..c604ed8 100755 --- a/handler/app.py +++ b/handler/app.py @@ -18,6 +18,7 @@ from transactions import Transactions from irc import bot from notify import Notify from markets import Markets +from money import Money init_map = None @@ -82,6 +83,7 @@ if __name__ == "__main__": "revolut": Revolut(), "tx": Transactions(), "webapp": WebApp(), + "money": Money(), } for classname, object_instance in init_map.items(): # notify, Notify diff --git a/handler/money.py b/handler/money.py new file mode 100644 index 0000000..5e1a4e0 --- /dev/null +++ b/handler/money.py @@ -0,0 +1,39 @@ +# Twisted/Klein imports +from twisted.logger import Logger + +# Other library imports +from pycoingecko import CoinGeckoAPI + + +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]) diff --git a/handler/tests/test_agora.py b/handler/tests/test_agora.py index c9118c5..ed170f2 100644 --- a/handler/tests/test_agora.py +++ b/handler/tests/test_agora.py @@ -6,6 +6,7 @@ from copy import deepcopy from tests.common import fake_public_ads, cg_prices, expected_to_update from agora import Agora from markets import Markets +from money import Money import util @@ -22,7 +23,10 @@ class TestAgora(TestCase): def setUp(self): self.markets = Markets() self.agora = Agora() + self.money = Money() setattr(self.agora, "markets", self.markets) + setattr(self.money, "markets", self.markets) + setattr(self.agora, "money", self.money) self.all_providers = [ "XOOM", @@ -177,8 +181,8 @@ class TestAgora(TestCase): util.last_online_recent.return_value = True # Override get_price - self.agora.cg.get_price = MagicMock() - self.agora.cg.get_price.return_value = cg_prices + self.money.cg.get_price = MagicMock() + self.money.cg.get_price.return_value = cg_prices enum_ads_return = self.agora.enum_public_ads("XMR", "USD", self.all_providers) @@ -191,7 +195,7 @@ class TestAgora(TestCase): ad.append(margin) expected_return.append(ad) - lookup_rates_return = self.agora.lookup_rates(enum_ads_return) + lookup_rates_return = self.agora.money.lookup_rates(enum_ads_return) # TODO: do this properly self.assertCountEqual(lookup_rates_return, expected_return) def test_lookup_rates_not_usd(self): @@ -219,5 +223,5 @@ class TestAgora(TestCase): ad.append(margin) expected_return.append(ad) # Test specifying rates= - lookup_rates_return = self.agora.lookup_rates(enum_ads_return, rates=cg_prices) + lookup_rates_return = self.agora.money.lookup_rates(enum_ads_return, rates=cg_prices) self.assertCountEqual(lookup_rates_return, expected_return)