From 72ce2704a69e0f6699f1e431040d0ffb8ec32699 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 15 Apr 2022 15:06:28 +0100 Subject: [PATCH] Account for LBTC in lookup_rates --- handler/markets.py | 45 ++++++++++++++++++------------------- handler/money.py | 4 ++-- handler/sources/agora.py | 2 +- handler/tests/test_agora.py | 4 ++-- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/handler/markets.py b/handler/markets.py index fb5e6be..4b903e1 100644 --- a/handler/markets.py +++ b/handler/markets.py @@ -11,25 +11,25 @@ class Markets(util.Base): Markets handler for generic market functions. """ - def get_all_assets(self, platform): + def get_settings(self, platform): if platform == "agora": - assets = loads(settings.Agora.AssetList) + return settings.Agora elif platform == "lbtc": - assets = loads(settings.LocalBitcoins.AssetList) + return settings.LocalBitcoins + + def get_all_assets(self, platform): + sets = self.get_settings(platform) + assets = loads(sets.AssetList) return assets def get_all_providers(self, platform): - if platform == "agora": - providers = loads(settings.Agora.ProviderList) - elif platform == "lbtc": - providers = loads(settings.LocalBitcoins.ProviderList) + sets = self.get_settings(platform) + providers = loads(sets.ProviderList) return providers def get_all_currencies(self, platform): - if platform == "agora": - currencies = list(set([x[0] for x in loads(settings.Agora.DistList)])) - elif platform == "lbtc": - currencies = list(set([x[0] for x in loads(settings.Agora.DistList)])) + sets = self.get_settings(platform) + currencies = list(set([x[0] for x in loads(sets.DistList)])) return currencies def get_new_ad_equations(self, platform, public_ads, assets=None): @@ -40,14 +40,11 @@ class Markets(util.Base): :return: list of ads to modify :rtype: list """ - if platform == "agora": - username = settings.Agora.Username - min_margin = settings.Agora.MinMargin - max_margin = settings.Agora.MaxMargin - elif platform == "lbtc": - username = settings.LocalBitcoins.Username - min_margin = settings.LocalBitcoins.MinMargin - max_margin = settings.LocalBitcoins.MaxMargin + sets = self.get_settings(platform) + username = sets.Username + min_margin = sets.MinMargin + max_margin = sets.MaxMargin + to_update = [] # NOTES: @@ -167,18 +164,20 @@ class Markets(util.Base): # self.log.debug("Cheapest ad above our min that is not us: {x}", x=cheapest_ad) return cheapest_ad_margin - def create_distribution_list(self, filter_asset=None): + def create_distribution_list(self, platform, filter_asset=None): """ Create a list for distribution of ads. :return: generator of asset, countrycode, currency, provider :rtype: generator of tuples """ + sets = self.get_settings(platform) + # Iterate providers like REVOLUT, NATIONAL_BANK - for provider in loads(settings.Agora.ProviderList): + for provider in loads(sets.ProviderList): # Iterate assets like XMR, BTC - for asset in loads(settings.Agora.AssetList): + for asset in loads(sets.AssetList): # Iterate pairs of currency and country like EUR, GB - for currency, countrycode in loads(settings.Agora.DistList): + for currency, countrycode in loads(sets.DistList): if filter_asset: if asset == filter_asset: yield (asset, countrycode, currency, provider) diff --git a/handler/money.py b/handler/money.py index 642e1ad..8ba1c0b 100644 --- a/handler/money.py +++ b/handler/money.py @@ -22,14 +22,14 @@ class Money(util.Base): self.cr = CurrencyRates() self.cg = CoinGeckoAPI() - def lookup_rates(self, ads, rates=None): + def lookup_rates(self, platform, 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("agora"), + vs_currencies=self.markets.get_all_currencies(platform), ) # Set the price based on the asset for ad in ads: diff --git a/handler/sources/agora.py b/handler/sources/agora.py index b05999f..af6e062 100644 --- a/handler/sources/agora.py +++ b/handler/sources/agora.py @@ -378,7 +378,7 @@ class Agora(util.Base): ads_list = self.enum_public_ads(asset, currency, providers) if not ads_list: continue - ads = self.money.lookup_rates(ads_list, rates=rates) + ads = self.money.lookup_rates("agora", ads_list, rates=rates) if not ads: continue self.write_to_es_ads("ads", ads) diff --git a/handler/tests/test_agora.py b/handler/tests/test_agora.py index 225c866..5cef9a5 100644 --- a/handler/tests/test_agora.py +++ b/handler/tests/test_agora.py @@ -206,7 +206,7 @@ class TestAgora(TestCase): ad.append(margin) expected_return.append(ad) - lookup_rates_return = self.agora.money.lookup_rates(enum_ads_return) # TODO: do this properly + lookup_rates_return = self.agora.money.lookup_rates("agora", enum_ads_return) # TODO: do this properly self.assertCountEqual(lookup_rates_return, expected_return) def test_lookup_rates_not_usd(self): @@ -234,5 +234,5 @@ class TestAgora(TestCase): ad.append(margin) expected_return.append(ad) # Test specifying rates= - lookup_rates_return = self.agora.money.lookup_rates(enum_ads_return, rates=cg_prices) + lookup_rates_return = self.agora.money.lookup_rates("agora", enum_ads_return, rates=cg_prices) self.assertCountEqual(lookup_rates_return, expected_return)