From b1fd9a3197c28b35f8a168edec977c9e30e20d90 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 19 Mar 2022 15:43:51 +0000 Subject: [PATCH] Move some Agora functions to money/markets --- handler/markets.py | 38 +++++++++++++++++++++ handler/money.py | 20 +++++++++++ handler/sources/agora.py | 64 ++--------------------------------- handler/tests/test_agora.py | 30 ---------------- handler/tests/test_markets.py | 31 +++++++++++++++++ 5 files changed, 92 insertions(+), 91 deletions(-) diff --git a/handler/markets.py b/handler/markets.py index 1770634..6006abc 100644 --- a/handler/markets.py +++ b/handler/markets.py @@ -223,3 +223,41 @@ class Markets(util.Base): provider = ad[4] ad_id = ad[1] self.agora.create_ad(asset, countrycode, currency, provider, payment_details=False, visible=False, edit=True, ad_id=ad_id) + + def format_ad(self, asset, currency, payment_details_text): + """ + Format the ad. + """ + ad = settings.Platform.Ad + + # Substitute the currency + ad = ad.replace("$CURRENCY$", currency) + + # Substitute the asset + ad = ad.replace("$ASSET$", asset) + + # Substitute the payment details + ad = ad.replace("$PAYMENT$", payment_details_text) + + # Strip extra tabs + ad = ad.replace("\\t", "\t") + return ad + + def format_payment_details(self, currency, payment_details): + """ + Format the payment details. + """ + payment = settings.Platform.PaymentDetails + + payment_text = "" + for field, value in payment_details.items(): + formatted_name = field.replace("_", " ") + formatted_name = formatted_name.capitalize() + payment_text += f"* {formatted_name}: **{value}**" + if field != list(payment_details.keys())[-1]: # No trailing newline + payment_text += "\n" + + payment = payment.replace("$PAYMENT$", payment_text) + payment = payment.replace("$CURRENCY$", currency) + + return payment diff --git a/handler/money.py b/handler/money.py index c94e03f..0a47bd6 100644 --- a/handler/money.py +++ b/handler/money.py @@ -71,6 +71,26 @@ class Money(util.Base): max_local = max_usd * rates[currency] return (min_local, max_local) + def get_minmax(self, asset, currency): + rates = self.get_rates_all() + if currency not in rates and not currency == "USD": + self.log.error(f"Can't create ad without rates: {currency}") + return + if asset == "XMR": + min_usd = float(settings.Agora.MinUSDXMR) + max_usd = float(settings.Agora.MaxUSDXMR) + elif asset == "BTC": + min_usd = float(settings.Agora.MinUSDBTC) + max_usd = float(settings.Agora.MaxUSDBTC) + if currency == "USD": + min_amount = min_usd + max_amount = max_usd + else: + min_amount = rates[currency] * min_usd + max_amount = rates[currency] * max_usd + + return (min_amount, max_amount) + def to_usd(self, amount, currency): if currency == "USD": return float(amount) diff --git a/handler/sources/agora.py b/handler/sources/agora.py index f6db3fc..27cab21 100644 --- a/handler/sources/agora.py +++ b/handler/sources/agora.py @@ -450,64 +450,6 @@ class Agora(util.Base): return_ids.append(rtrn["success"]) return all(return_ids) - def format_ad(self, asset, currency, payment_details_text): - """ - Format the ad. - """ - ad = settings.Platform.Ad - - # Substitute the currency - ad = ad.replace("$CURRENCY$", currency) - - # Substitute the asset - ad = ad.replace("$ASSET$", asset) - - # Substitute the payment details - ad = ad.replace("$PAYMENT$", payment_details_text) - - # Strip extra tabs - ad = ad.replace("\\t", "\t") - return ad - - def format_payment_details(self, currency, payment_details): - """ - Format the payment details. - """ - payment = settings.Platform.PaymentDetails - - payment_text = "" - for field, value in payment_details.items(): - formatted_name = field.replace("_", " ") - formatted_name = formatted_name.capitalize() - payment_text += f"* {formatted_name}: **{value}**" - if field != list(payment_details.keys())[-1]: # No trailing newline - payment_text += "\n" - - payment = payment.replace("$PAYMENT$", payment_text) - payment = payment.replace("$CURRENCY$", currency) - - return payment - - def get_minmax(self, asset, currency): - rates = self.money.get_rates_all() - if currency not in rates and not currency == "USD": - self.log.error(f"Can't create ad without rates: {currency}") - return - if asset == "XMR": - min_usd = float(settings.Agora.MinUSDXMR) - max_usd = float(settings.Agora.MaxUSDXMR) - elif asset == "BTC": - min_usd = float(settings.Agora.MinUSDBTC) - max_usd = float(settings.Agora.MaxUSDBTC) - if currency == "USD": - min_amount = min_usd - max_amount = max_usd - else: - min_amount = rates[currency] * min_usd - max_amount = rates[currency] * max_usd - - return (min_amount, max_amount) - @util.handle_exceptions def create_ad(self, asset, countrycode, currency, provider, payment_details, visible=None, edit=False, ad_id=None): """ @@ -526,9 +468,9 @@ class Agora(util.Base): """ if payment_details: - payment_details_text = self.format_payment_details(currency, payment_details) - ad_text = self.format_ad(asset, currency, payment_details_text) - min_amount, max_amount = self.get_minmax(asset, currency) + payment_details_text = self.markets.format_payment_details(currency, payment_details) + ad_text = self.markets.format_ad(asset, currency, payment_details_text) + min_amount, max_amount = self.money.get_minmax(asset, currency) price_formula = f"coingecko{asset.lower()}usd*usd{currency.lower()}*{settings.Agora.Margin}" diff --git a/handler/tests/test_agora.py b/handler/tests/test_agora.py index cbdaff8..3641e87 100644 --- a/handler/tests/test_agora.py +++ b/handler/tests/test_agora.py @@ -229,33 +229,3 @@ class TestAgora(TestCase): # Test specifying rates= lookup_rates_return = self.agora.money.lookup_rates(enum_ads_return, rates=cg_prices) self.assertCountEqual(lookup_rates_return, expected_return) - - def test_format_ad(self): - settings.settings.Platform.Ad = """* Set **Country of recipient's bank** to **"United Kingdom"** -$PAYMENT$ -* Set **Company name** to **"PATHOGEN LIMITED"**""" - payment_details = {"sort_code": "02-03-04", "account_number": "0023-0045"} - payment_details_text = self.agora.format_payment_details("GBP", payment_details) - ad_text = self.agora.format_ad("XMR", "GBP", payment_details_text) - expected = """* Set **Country of recipient's bank** to **"United Kingdom"** -* Company name: **PATHOGEN LIMITED** -* Sort code: **02-03-04** -* Account number: **0023-0045** -* Please send in **GBP** -* If you are asked for address information, please use **24 Holborn Viaduct, London, England, EC1A 2BN** -* The post code is **EC1A 2BN** -* Set **Company name** to **"PATHOGEN LIMITED"**""" - print("EXPECT", ad_text) - self.assertEqual(ad_text, expected) - - def test_format_payment_details(self): - payment_details = {"sort_code": "02-03-04", "account_number": "0023-0045"} - payment_details_text = self.agora.format_payment_details("GBP", payment_details) - - expected = """* Company name: **PATHOGEN LIMITED** -* Sort code: **02-03-04** -* Account number: **0023-0045** -* Please send in **GBP** -* If you are asked for address information, please use **24 Holborn Viaduct, London, England, EC1A 2BN** -* The post code is **EC1A 2BN**""" - self.assertEqual(payment_details_text, expected) diff --git a/handler/tests/test_markets.py b/handler/tests/test_markets.py index 85bc8cd..c622a64 100644 --- a/handler/tests/test_markets.py +++ b/handler/tests/test_markets.py @@ -2,6 +2,7 @@ from unittest import TestCase from tests.common import fake_public_ads, expected_to_update from markets import Markets from sources.agora import Agora +import settings class TestMarkets(TestCase): @@ -36,3 +37,33 @@ class TestMarkets(TestCase): res_both = self.markets.get_new_ad_equations(fake_public_ads, ["XMR", "BTC"]) self.assertCountEqual(res_both, expected_to_update) + + def test_format_ad(self): + settings.settings.Platform.Ad = """* Set **Country of recipient's bank** to **"United Kingdom"** +$PAYMENT$ +* Set **Company name** to **"PATHOGEN LIMITED"**""" + payment_details = {"sort_code": "02-03-04", "account_number": "0023-0045"} + payment_details_text = self.markets.format_payment_details("GBP", payment_details) + ad_text = self.markets.format_ad("XMR", "GBP", payment_details_text) + expected = """* Set **Country of recipient's bank** to **"United Kingdom"** +* Company name: **PATHOGEN LIMITED** +* Sort code: **02-03-04** +* Account number: **0023-0045** +* Please send in **GBP** +* If you are asked for address information, please use **24 Holborn Viaduct, London, England, EC1A 2BN** +* The post code is **EC1A 2BN** +* Set **Company name** to **"PATHOGEN LIMITED"**""" + print("EXPECT", ad_text) + self.assertEqual(ad_text, expected) + + def test_format_payment_details(self): + payment_details = {"sort_code": "02-03-04", "account_number": "0023-0045"} + payment_details_text = self.markets.format_payment_details("GBP", payment_details) + + expected = """* Company name: **PATHOGEN LIMITED** +* Sort code: **02-03-04** +* Account number: **0023-0045** +* Please send in **GBP** +* If you are asked for address information, please use **24 Holborn Viaduct, London, England, EC1A 2BN** +* The post code is **EC1A 2BN**""" + self.assertEqual(payment_details_text, expected)