2022-02-09 09:01:18 +00:00
|
|
|
# Other library imports
|
|
|
|
from json import loads
|
|
|
|
|
2022-02-07 13:24:09 +00:00
|
|
|
# Project imports
|
|
|
|
from settings import settings
|
2022-03-05 21:52:31 +00:00
|
|
|
import util
|
2022-02-07 13:24:09 +00:00
|
|
|
|
|
|
|
|
2022-03-05 21:52:31 +00:00
|
|
|
class Markets(util.Base):
|
2022-02-07 13:24:09 +00:00
|
|
|
""" "
|
|
|
|
Markets handler for generic market functions.
|
|
|
|
"""
|
|
|
|
|
2022-04-15 14:06:28 +00:00
|
|
|
def get_all_assets(self, platform):
|
2022-04-15 14:12:31 +00:00
|
|
|
sets = util.get_settings(platform)
|
2022-04-15 14:06:28 +00:00
|
|
|
assets = loads(sets.AssetList)
|
2022-02-09 09:01:18 +00:00
|
|
|
return assets
|
|
|
|
|
2022-04-15 13:53:39 +00:00
|
|
|
def get_all_providers(self, platform):
|
2022-04-15 14:12:31 +00:00
|
|
|
sets = util.get_settings(platform)
|
2022-04-15 14:06:28 +00:00
|
|
|
providers = loads(sets.ProviderList)
|
2022-02-09 09:01:18 +00:00
|
|
|
return providers
|
|
|
|
|
2022-04-15 13:53:39 +00:00
|
|
|
def get_all_currencies(self, platform):
|
2022-04-15 14:12:31 +00:00
|
|
|
sets = util.get_settings(platform)
|
2022-04-15 14:06:28 +00:00
|
|
|
currencies = list(set([x[0] for x in loads(sets.DistList)]))
|
2022-02-09 09:01:18 +00:00
|
|
|
return currencies
|
|
|
|
|
2022-04-15 13:53:39 +00:00
|
|
|
def get_new_ad_equations(self, platform, public_ads, assets=None):
|
2022-02-09 09:01:18 +00:00
|
|
|
"""
|
|
|
|
Update all our prices.
|
|
|
|
:param public_ads: dictionary of public ads keyed by currency
|
|
|
|
:type public_ads: dict
|
|
|
|
:return: list of ads to modify
|
|
|
|
:rtype: list
|
|
|
|
"""
|
2022-04-15 14:12:31 +00:00
|
|
|
sets = util.get_settings(platform)
|
2022-04-15 14:06:28 +00:00
|
|
|
username = sets.Username
|
|
|
|
min_margin = sets.MinMargin
|
|
|
|
max_margin = sets.MaxMargin
|
|
|
|
|
2022-02-09 09:01:18 +00:00
|
|
|
to_update = []
|
|
|
|
|
|
|
|
# NOTES:
|
|
|
|
# Get all ads for each currency, with all the payment methods.
|
|
|
|
# Create a function to, in turn, filter these so it contains only one payment method. Run autoprice on this.
|
|
|
|
# Append all results to to_update. Repeat for remaining payment methods, then call slow update.
|
|
|
|
|
|
|
|
# (asset, currency, provider)
|
|
|
|
if not assets:
|
2022-04-15 13:53:39 +00:00
|
|
|
assets = self.get_all_assets(platform)
|
|
|
|
currencies = self.get_all_currencies(platform)
|
|
|
|
providers = self.get_all_providers(platform)
|
2022-04-18 16:21:58 +00:00
|
|
|
sinks_currencies = self.sinks.currencies
|
|
|
|
supported_currencies = [currency for currency in currencies if currency in sinks_currencies]
|
|
|
|
currencies = supported_currencies
|
2022-02-09 09:01:18 +00:00
|
|
|
|
|
|
|
brute = [(asset, currency, provider) for asset in assets for currency in currencies for provider in providers]
|
|
|
|
for asset, currency, provider in brute:
|
|
|
|
# Filter currency
|
|
|
|
try:
|
|
|
|
public_ads_currency = public_ads[currency]
|
|
|
|
except KeyError:
|
2022-02-13 22:20:09 +00:00
|
|
|
# self.log.error("Error getting public ads for currency {currency}", currency=currency)
|
2022-04-04 13:19:37 +00:00
|
|
|
if currency == "GBP":
|
2022-02-09 14:57:11 +00:00
|
|
|
self.log.error("Error getting public ads for currency USD, aborting")
|
|
|
|
break
|
2022-02-09 09:01:18 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Filter asset
|
|
|
|
public_ads_filtered = [ad for ad in public_ads_currency if ad[4] == asset]
|
|
|
|
|
|
|
|
# Filter provider
|
|
|
|
public_ads_filtered = [ad for ad in public_ads_filtered if ad[3] == provider]
|
|
|
|
|
2022-04-15 13:53:39 +00:00
|
|
|
our_ads = [ad for ad in public_ads_filtered if ad[1] == username]
|
2022-02-09 09:01:18 +00:00
|
|
|
if not our_ads:
|
2022-04-19 20:02:53 +00:00
|
|
|
self.log.warning(f"No ads found in public listing for {asset} {currency} {provider}")
|
2022-02-09 09:01:18 +00:00
|
|
|
continue
|
2022-04-15 13:58:18 +00:00
|
|
|
new_margin = self.autoprice(username, min_margin, max_margin, public_ads_filtered, currency)
|
2022-02-13 22:20:09 +00:00
|
|
|
# self.log.info("New rate for {currency}: {rate}", currency=currency, rate=new_margin)
|
2022-04-18 16:21:58 +00:00
|
|
|
if platform == "agora":
|
|
|
|
new_formula = f"coingecko{asset.lower()}usd*usd{currency.lower()}*{new_margin}"
|
|
|
|
elif platform == "lbtc":
|
|
|
|
new_formula = f"btc_in_usd*{new_margin}"
|
2022-02-09 09:01:18 +00:00
|
|
|
for ad in our_ads:
|
|
|
|
ad_id = ad[0]
|
|
|
|
asset = ad[4]
|
|
|
|
our_margin = ad[5]
|
|
|
|
if new_margin != our_margin:
|
|
|
|
to_update.append([ad_id, new_formula, asset, currency, False])
|
|
|
|
|
|
|
|
return to_update
|
|
|
|
|
2022-04-15 13:58:18 +00:00
|
|
|
def autoprice(self, username, min_margin, max_margin, ads, currency):
|
2022-02-07 13:24:09 +00:00
|
|
|
"""
|
|
|
|
Helper function to automatically adjust the price up/down in certain markets
|
|
|
|
in order to gain the most profits and sales.
|
|
|
|
:param ads: list of ads
|
|
|
|
:type ads: list of lists
|
|
|
|
:param currency: currency of the ads
|
|
|
|
:type currency: string
|
|
|
|
:return: the rate we should use for this currency
|
|
|
|
:rtype: float
|
|
|
|
"""
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Autoprice starting for {x}", x=currency)
|
2022-02-07 13:24:09 +00:00
|
|
|
# Find cheapest ad
|
|
|
|
# Filter by 3rd index on each ad list to find the cheapest
|
2022-02-11 10:45:23 +00:00
|
|
|
min_margin_ad = min(ads, key=lambda x: x[6])
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Minimum margin ad: {x}", x=min_margin_ad)
|
2022-02-07 13:24:09 +00:00
|
|
|
|
|
|
|
# Find second cheapest that is not us
|
|
|
|
# Remove results from ads that are us
|
2022-04-15 13:58:18 +00:00
|
|
|
ads_without_us = [ad for ad in ads if not ad[1] == username]
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Ads without us: {x}", x=ads_without_us)
|
2022-02-07 13:24:09 +00:00
|
|
|
# Find ads above our min that are not us
|
2022-04-15 13:58:18 +00:00
|
|
|
ads_above_our_min_not_us = [ad for ad in ads_without_us if ad[6] > float(min_margin)]
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Ads above our min not us: {x}", x=ads_above_our_min_not_us)
|
2022-02-07 13:24:09 +00:00
|
|
|
# Check that this list without us is not empty
|
|
|
|
if ads_without_us:
|
|
|
|
# Find the cheapest from these
|
2022-02-11 10:45:23 +00:00
|
|
|
min_margin_ad_not_us = min(ads_without_us, key=lambda x: x[6])
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Min margin ad not us: {x}", x=min_margin_ad_not_us)
|
2022-02-07 13:24:09 +00:00
|
|
|
# Lowball the lowest ad that is not ours
|
2022-02-11 10:45:23 +00:00
|
|
|
lowball_lowest_not_ours = min_margin_ad_not_us[6] # - 0.005
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Lowball lowest not ours: {x}", x=lowball_lowest_not_ours)
|
2022-02-07 13:24:09 +00:00
|
|
|
|
|
|
|
# Check if the username field of the cheapest ad matches ours
|
2022-04-15 13:58:18 +00:00
|
|
|
if min_margin_ad[1] == username:
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("We are the cheapest for: {x}", x=currency)
|
2022-02-07 13:24:09 +00:00
|
|
|
# We are the cheapest!
|
|
|
|
# Are all of the ads ours?
|
2022-04-15 13:58:18 +00:00
|
|
|
all_ads_ours = all([ad[1] == username for ad in ads])
|
2022-02-07 13:24:09 +00:00
|
|
|
if all_ads_ours:
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("All ads are ours for: {x}", x=currency)
|
2022-02-07 13:24:09 +00:00
|
|
|
# Now we know it's safe to return the maximum value
|
2022-04-15 13:58:18 +00:00
|
|
|
return float(max_margin)
|
2022-02-07 13:24:09 +00:00
|
|
|
else:
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("All ads are NOT ours for: {x}", x=currency)
|
2022-02-07 13:24:09 +00:00
|
|
|
# All the ads are not ours, but we are first...
|
|
|
|
# Check if the lowballed, lowest (that is not ours) ad's margin
|
|
|
|
# is less than our minimum
|
2022-04-15 13:58:18 +00:00
|
|
|
if lowball_lowest_not_ours < float(min_margin):
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Lowball lowest not ours less than MinMargin")
|
2022-04-15 13:58:18 +00:00
|
|
|
return float(min_margin)
|
|
|
|
elif lowball_lowest_not_ours > float(max_margin):
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Lowball lowest not ours more than MaxMargin")
|
2022-04-15 13:58:18 +00:00
|
|
|
return float(max_margin)
|
2022-02-07 13:24:09 +00:00
|
|
|
else:
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Returning lowballed figure: {x}", x=lowball_lowest_not_ours)
|
2022-02-07 13:24:09 +00:00
|
|
|
return lowball_lowest_not_ours
|
|
|
|
else:
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("We are NOT the cheapest for: {x}", x=currency)
|
2022-02-07 13:24:09 +00:00
|
|
|
# We are not the cheapest :(
|
|
|
|
# Check if this list is empty
|
|
|
|
if not ads_above_our_min_not_us:
|
|
|
|
# Return the maximum margin?
|
2022-04-15 13:58:18 +00:00
|
|
|
return float(max_margin)
|
2022-02-07 13:24:09 +00:00
|
|
|
# Find cheapest ad above our min that is not us
|
|
|
|
cheapest_ad = min(ads_above_our_min_not_us, key=lambda x: x[4])
|
2022-02-11 10:45:23 +00:00
|
|
|
cheapest_ad_margin = cheapest_ad[6] # - 0.005
|
2022-04-15 13:58:18 +00:00
|
|
|
if cheapest_ad_margin > float(max_margin):
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Cheapest ad not ours more than MaxMargin")
|
2022-04-15 13:58:18 +00:00
|
|
|
return float(max_margin)
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.debug("Cheapest ad above our min that is not us: {x}", x=cheapest_ad)
|
2022-02-07 13:24:09 +00:00
|
|
|
return cheapest_ad_margin
|
2022-02-22 20:15:42 +00:00
|
|
|
|
2022-04-15 14:06:28 +00:00
|
|
|
def create_distribution_list(self, platform, filter_asset=None):
|
2022-02-22 20:15:42 +00:00
|
|
|
"""
|
|
|
|
Create a list for distribution of ads.
|
|
|
|
:return: generator of asset, countrycode, currency, provider
|
|
|
|
:rtype: generator of tuples
|
|
|
|
"""
|
2022-04-15 14:12:31 +00:00
|
|
|
sets = util.get_settings(platform)
|
2022-04-15 14:06:28 +00:00
|
|
|
|
2022-02-22 20:15:42 +00:00
|
|
|
# Iterate providers like REVOLUT, NATIONAL_BANK
|
2022-04-15 14:06:28 +00:00
|
|
|
for provider in loads(sets.ProviderList):
|
2022-02-22 20:15:42 +00:00
|
|
|
# Iterate assets like XMR, BTC
|
2022-04-15 14:06:28 +00:00
|
|
|
for asset in loads(sets.AssetList):
|
2022-02-22 20:15:42 +00:00
|
|
|
# Iterate pairs of currency and country like EUR, GB
|
2022-04-15 14:06:28 +00:00
|
|
|
for currency, countrycode in loads(sets.DistList):
|
2022-02-22 20:15:42 +00:00
|
|
|
if filter_asset:
|
|
|
|
if asset == filter_asset:
|
|
|
|
yield (asset, countrycode, currency, provider)
|
2022-03-22 19:28:21 +00:00
|
|
|
else:
|
|
|
|
yield (asset, countrycode, currency, provider)
|
2022-03-08 20:42:47 +00:00
|
|
|
|
2022-04-18 16:21:58 +00:00
|
|
|
def get_valid_account_details(self, platform):
|
2022-03-08 21:27:58 +00:00
|
|
|
currencies = self.sinks.currencies
|
|
|
|
account_info = self.sinks.account_info
|
2022-04-18 16:21:58 +00:00
|
|
|
all_currencies = self.get_all_currencies(platform)
|
2022-03-08 21:27:58 +00:00
|
|
|
supported_currencies = [currency for currency in currencies if currency in all_currencies]
|
|
|
|
currency_account_info_map = {}
|
|
|
|
for currency in supported_currencies:
|
|
|
|
for bank, accounts in account_info.items():
|
|
|
|
for account in accounts:
|
|
|
|
if account["currency"] == currency:
|
|
|
|
currency_account_info_map[currency] = account["account_number"]
|
2022-04-18 16:21:58 +00:00
|
|
|
currency_account_info_map[currency]["bank"] = bank.split("_")[0]
|
2022-03-08 21:27:58 +00:00
|
|
|
return (supported_currencies, currency_account_info_map)
|
|
|
|
|
2022-04-20 18:07:22 +00:00
|
|
|
def get_matching_account_details(self, platform, currency):
|
|
|
|
supported_currencies, currency_account_info_map = self.get_valid_account_details(platform)
|
|
|
|
if currency not in supported_currencies:
|
|
|
|
return False
|
|
|
|
return currency_account_info_map[currency]
|
|
|
|
|
2022-04-15 13:42:03 +00:00
|
|
|
def _distribute_account_details(self, platform, currencies=None, account_info=None):
|
2022-03-08 20:42:47 +00:00
|
|
|
"""
|
|
|
|
Distribute account details for ads.
|
|
|
|
We will disable ads we can't support.
|
|
|
|
"""
|
2022-04-15 13:42:03 +00:00
|
|
|
if platform == "agora":
|
|
|
|
caller = self.agora
|
|
|
|
elif platform == "lbtc":
|
|
|
|
caller = self.lbtc
|
|
|
|
|
2022-03-08 20:42:47 +00:00
|
|
|
if not currencies:
|
|
|
|
currencies = self.sinks.currencies
|
|
|
|
if not account_info:
|
|
|
|
account_info = self.sinks.account_info
|
2022-04-19 20:02:53 +00:00
|
|
|
supported_currencies, currency_account_info_map = self.get_valid_account_details(platform)
|
2022-03-08 20:42:47 +00:00
|
|
|
|
|
|
|
# not_supported = [currency for currency in all_currencies if currency not in supported_currencies]
|
|
|
|
|
2022-04-15 13:42:03 +00:00
|
|
|
our_ads = caller.enum_ads()
|
2022-03-08 20:42:47 +00:00
|
|
|
|
|
|
|
supported_ads = [ad for ad in our_ads if ad[3] in supported_currencies]
|
|
|
|
|
|
|
|
not_supported_ads = [ad for ad in our_ads if ad[3] not in supported_currencies]
|
|
|
|
|
|
|
|
for ad in supported_ads:
|
|
|
|
asset = ad[0]
|
|
|
|
countrycode = ad[2]
|
|
|
|
currency = ad[3]
|
|
|
|
provider = ad[4]
|
|
|
|
payment_details = currency_account_info_map[currency]
|
|
|
|
ad_id = ad[1]
|
2022-04-15 13:42:03 +00:00
|
|
|
caller.create_ad(
|
2022-04-12 21:06:28 +00:00
|
|
|
asset,
|
|
|
|
countrycode,
|
|
|
|
currency,
|
|
|
|
provider,
|
|
|
|
payment_details,
|
|
|
|
visible=True,
|
|
|
|
edit=True,
|
|
|
|
ad_id=ad_id,
|
|
|
|
)
|
2022-03-08 20:42:47 +00:00
|
|
|
|
|
|
|
for ad in not_supported_ads:
|
|
|
|
asset = ad[0]
|
|
|
|
countrycode = ad[2]
|
|
|
|
currency = ad[3]
|
|
|
|
provider = ad[4]
|
|
|
|
ad_id = ad[1]
|
2022-04-15 13:42:03 +00:00
|
|
|
caller.create_ad(
|
2022-04-12 21:06:28 +00:00
|
|
|
asset,
|
|
|
|
countrycode,
|
|
|
|
currency,
|
|
|
|
provider,
|
|
|
|
payment_details=False,
|
|
|
|
visible=False,
|
|
|
|
edit=True,
|
|
|
|
ad_id=ad_id,
|
|
|
|
)
|
2022-03-19 15:43:51 +00:00
|
|
|
|
2022-04-15 13:42:03 +00:00
|
|
|
def distribute_account_details(self, currencies=None, account_info=None):
|
|
|
|
"""
|
|
|
|
Helper to distribute the account details for all platforms.
|
|
|
|
"""
|
|
|
|
platforms = ("agora", "lbtc")
|
|
|
|
for platform in platforms:
|
2022-04-19 20:02:53 +00:00
|
|
|
self._distribute_account_details(platform, currencies=currencies, account_info=account_info)
|
2022-04-15 13:42:03 +00:00
|
|
|
|
2022-03-19 15:43:51 +00:00
|
|
|
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
|
|
|
|
|
2022-04-20 19:46:02 +00:00
|
|
|
def format_payment_details(self, currency, payment_details, real=False):
|
2022-03-19 15:43:51 +00:00
|
|
|
"""
|
|
|
|
Format the payment details.
|
|
|
|
"""
|
2022-04-20 19:46:02 +00:00
|
|
|
if real:
|
|
|
|
payment = settings.Platform.PaymentDetailsReal
|
|
|
|
else:
|
|
|
|
payment = settings.Platform.PaymentDetails
|
2022-03-19 15:43:51 +00:00
|
|
|
|
|
|
|
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
|