Provide the provider to market and money functions

This commit is contained in:
Mark Veidemanis 2022-04-15 14:53:39 +01:00
parent c173e9d232
commit 7cfeb95b10
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 33 additions and 20 deletions

View File

@ -11,19 +11,28 @@ class Markets(util.Base):
Markets handler for generic market functions. Markets handler for generic market functions.
""" """
def get_all_assets(self): def get_all_assets(self, platform):
if platform == "agora":
assets = loads(settings.Agora.AssetList) assets = loads(settings.Agora.AssetList)
elif platform == "lbtc":
assets = loads(settings.LocalBitcoins.AssetList)
return assets return assets
def get_all_providers(self): def get_all_providers(self, platform):
if platform == "agora":
providers = loads(settings.Agora.ProviderList) providers = loads(settings.Agora.ProviderList)
elif platform == "lbtc":
providers = loads(settings.LocalBitcoins.ProviderList)
return providers return providers
def get_all_currencies(self): 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)])) currencies = list(set([x[0] for x in loads(settings.Agora.DistList)]))
return currencies 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. Update all our prices.
:param public_ads: dictionary of public ads keyed by currency :param public_ads: dictionary of public ads keyed by currency
@ -31,6 +40,10 @@ class Markets(util.Base):
:return: list of ads to modify :return: list of ads to modify
:rtype: list :rtype: list
""" """
if platform == "agora":
username = settings.Agora.Username
elif platform == "lbtc":
username = settings.LocalBitcoins.Username
to_update = [] to_update = []
# NOTES: # NOTES:
@ -40,9 +53,9 @@ class Markets(util.Base):
# (asset, currency, provider) # (asset, currency, provider)
if not assets: if not assets:
assets = self.get_all_assets() assets = self.get_all_assets(platform)
currencies = self.get_all_currencies() currencies = self.get_all_currencies(platform)
providers = self.get_all_providers() providers = self.get_all_providers(platform)
brute = [(asset, currency, provider) for asset in assets for currency in currencies for provider in providers] brute = [(asset, currency, provider) for asset in assets for currency in currencies for provider in providers]
for asset, currency, provider in brute: for asset, currency, provider in brute:
@ -62,7 +75,7 @@ class Markets(util.Base):
# Filter provider # Filter provider
public_ads_filtered = [ad for ad in public_ads_filtered if ad[3] == 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: if not our_ads:
continue continue
new_margin = self.autoprice(public_ads_filtered, currency) new_margin = self.autoprice(public_ads_filtered, currency)

View File

@ -29,7 +29,7 @@ class Money(util.Base):
if not rates: if not rates:
rates = self.cg.get_price( rates = self.cg.get_price(
ids=["monero", "bitcoin"], 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 # Set the price based on the asset
for ad in ads: for ad in ads:

View File

@ -341,7 +341,7 @@ class Agora(util.Base):
return False return False
# Get the ads to update # 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) self.slow_ad_update(to_update)
# TODO: make generic and move to markets # TODO: make generic and move to markets
@ -359,12 +359,12 @@ class Agora(util.Base):
} }
if not assets: if not assets:
assets = self.markets.get_all_assets() assets = self.markets.get_all_assets("agora")
# Get all currencies we have ads for, deduplicated # Get all currencies we have ads for, deduplicated
if not currencies: if not currencies:
currencies = self.markets.get_all_currencies() currencies = self.markets.get_all_currencies("agora")
if not providers: 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 # 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) rates = self.money.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=currencies)
for asset in assets: for asset in assets:

View File

@ -64,18 +64,18 @@ class TestMarkets(TestCase):
self.assertEqual(margin, expected_margin) self.assertEqual(margin, expected_margin)
def test_get_new_ad_equation(self): 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) 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"] expected_xmr_to_update = [x for x in expected_to_update if x[2] == "XMR"]
self.assertCountEqual(res_xmr, expected_xmr_to_update) 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"] expected_btc_to_update = [x for x in expected_to_update if x[2] == "BTC"]
self.assertCountEqual(res_btc, expected_btc_to_update) 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) self.assertCountEqual(res_both, expected_to_update)
def test_format_ad(self): def test_format_ad(self):