From 7cfeb95b10b89df33dae654ddf006ee0341f17f6 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 15 Apr 2022 14:53:39 +0100 Subject: [PATCH] Provide the provider to market and money functions --- handler/markets.py | 35 ++++++++++++++++++++++++----------- handler/money.py | 2 +- handler/sources/agora.py | 8 ++++---- handler/tests/test_markets.py | 8 ++++---- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/handler/markets.py b/handler/markets.py index e0dddca..b9106c7 100644 --- a/handler/markets.py +++ b/handler/markets.py @@ -11,19 +11,28 @@ class Markets(util.Base): Markets handler for generic market functions. """ - def get_all_assets(self): - assets = loads(settings.Agora.AssetList) + def get_all_assets(self, platform): + if platform == "agora": + assets = loads(settings.Agora.AssetList) + elif platform == "lbtc": + assets = loads(settings.LocalBitcoins.AssetList) return assets - def get_all_providers(self): - providers = loads(settings.Agora.ProviderList) + def get_all_providers(self, platform): + if platform == "agora": + providers = loads(settings.Agora.ProviderList) + elif platform == "lbtc": + providers = loads(settings.LocalBitcoins.ProviderList) return providers - def get_all_currencies(self): - currencies = list(set([x[0] for x in loads(settings.Agora.DistList)])) + 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)])) return currencies - def get_new_ad_equations(self, public_ads, assets=None): + def get_new_ad_equations(self, platform, public_ads, assets=None): """ Update all our prices. :param public_ads: dictionary of public ads keyed by currency @@ -31,6 +40,10 @@ class Markets(util.Base): :return: list of ads to modify :rtype: list """ + if platform == "agora": + username = settings.Agora.Username + elif platform == "lbtc": + username = settings.LocalBitcoins.Username to_update = [] # NOTES: @@ -40,9 +53,9 @@ class Markets(util.Base): # (asset, currency, provider) if not assets: - assets = self.get_all_assets() - currencies = self.get_all_currencies() - providers = self.get_all_providers() + assets = self.get_all_assets(platform) + currencies = self.get_all_currencies(platform) + providers = self.get_all_providers(platform) brute = [(asset, currency, provider) for asset in assets for currency in currencies for provider in providers] for asset, currency, provider in brute: @@ -62,7 +75,7 @@ class Markets(util.Base): # Filter provider public_ads_filtered = [ad for ad in public_ads_filtered if ad[3] == provider] - our_ads = [ad for ad in public_ads_filtered if ad[1] == settings.Agora.Username] + our_ads = [ad for ad in public_ads_filtered if ad[1] == username] if not our_ads: continue new_margin = self.autoprice(public_ads_filtered, currency) diff --git a/handler/money.py b/handler/money.py index f3e398a..642e1ad 100644 --- a/handler/money.py +++ b/handler/money.py @@ -29,7 +29,7 @@ class Money(util.Base): if not rates: rates = self.cg.get_price( ids=["monero", "bitcoin"], - vs_currencies=self.markets.get_all_currencies(), + vs_currencies=self.markets.get_all_currencies("agora"), ) # Set the price based on the asset for ad in ads: diff --git a/handler/sources/agora.py b/handler/sources/agora.py index 1399eea..b05999f 100644 --- a/handler/sources/agora.py +++ b/handler/sources/agora.py @@ -341,7 +341,7 @@ class Agora(util.Base): return False # Get the ads to update - to_update = self.markets.get_new_ad_equations(public_ads, assets) + to_update = self.markets.get_new_ad_equations("agora", public_ads, assets) self.slow_ad_update(to_update) # TODO: make generic and move to markets @@ -359,12 +359,12 @@ class Agora(util.Base): } if not assets: - assets = self.markets.get_all_assets() + assets = self.markets.get_all_assets("agora") # Get all currencies we have ads for, deduplicated if not currencies: - currencies = self.markets.get_all_currencies() + currencies = self.markets.get_all_currencies("agora") if not providers: - providers = self.markets.get_all_providers() + providers = self.markets.get_all_providers("agora") # We want to get the ads for each of these currencies and return the result rates = self.money.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=currencies) for asset in assets: diff --git a/handler/tests/test_markets.py b/handler/tests/test_markets.py index 1fbb4bf..0fa616b 100644 --- a/handler/tests/test_markets.py +++ b/handler/tests/test_markets.py @@ -64,18 +64,18 @@ class TestMarkets(TestCase): self.assertEqual(margin, expected_margin) def test_get_new_ad_equation(self): - to_update = self.markets.get_new_ad_equations(fake_public_ads) + to_update = self.markets.get_new_ad_equations("agora", fake_public_ads) self.assertCountEqual(to_update, expected_to_update) - res_xmr = self.markets.get_new_ad_equations(fake_public_ads, ["XMR"]) + res_xmr = self.markets.get_new_ad_equations("agora", fake_public_ads, ["XMR"]) expected_xmr_to_update = [x for x in expected_to_update if x[2] == "XMR"] self.assertCountEqual(res_xmr, expected_xmr_to_update) - res_btc = self.markets.get_new_ad_equations(fake_public_ads, ["BTC"]) + res_btc = self.markets.get_new_ad_equations("agora", fake_public_ads, ["BTC"]) expected_btc_to_update = [x for x in expected_to_update if x[2] == "BTC"] self.assertCountEqual(res_btc, expected_btc_to_update) - res_both = self.markets.get_new_ad_equations(fake_public_ads, ["XMR", "BTC"]) + res_both = self.markets.get_new_ad_equations("agora", fake_public_ads, ["XMR", "BTC"]) self.assertCountEqual(res_both, expected_to_update) def test_format_ad(self):