Move some Agora functions to money/markets

This commit is contained in:
Mark Veidemanis 2022-03-19 15:43:51 +00:00
parent 45392455fa
commit b1fd9a3197
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
5 changed files with 92 additions and 91 deletions

View File

@ -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

View File

@ -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)

View File

@ -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}"

View File

@ -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)

View File

@ -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)