2021-12-24 02:23:38 +00:00
|
|
|
# Twisted/Klein imports
|
|
|
|
from twisted.logger import Logger
|
2021-12-27 19:30:03 +00:00
|
|
|
from twisted.internet.task import LoopingCall
|
2021-12-24 02:23:38 +00:00
|
|
|
|
|
|
|
# Other library imports
|
2021-12-26 21:36:25 +00:00
|
|
|
from json import loads
|
2021-12-24 02:23:38 +00:00
|
|
|
from forex_python.converter import CurrencyRates
|
2021-12-24 17:26:31 +00:00
|
|
|
from agoradesk_py.agoradesk import AgoraDesk
|
2021-12-24 02:23:38 +00:00
|
|
|
|
|
|
|
# Project imports
|
|
|
|
from settings import settings
|
|
|
|
|
|
|
|
|
|
|
|
class Agora(object):
|
|
|
|
"""
|
|
|
|
AgoraDesk API handler.
|
|
|
|
"""
|
|
|
|
|
2021-12-27 20:59:24 +00:00
|
|
|
def __init__(self):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
|
|
|
Initialise the AgoraDesk and CurrencyRates APIs.
|
|
|
|
Initialise the last_dash storage for detecting new trades.
|
|
|
|
"""
|
2021-12-24 02:23:38 +00:00
|
|
|
self.log = Logger("agora")
|
2021-12-24 17:26:31 +00:00
|
|
|
self.agora = AgoraDesk(settings.Agora.Token)
|
2021-12-24 02:23:38 +00:00
|
|
|
self.cr = CurrencyRates()
|
2021-12-28 01:00:02 +00:00
|
|
|
|
|
|
|
# Cache for detecting new trades
|
2021-12-27 19:30:03 +00:00
|
|
|
self.last_dash = set()
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2021-12-28 01:00:02 +00:00
|
|
|
# Cache for detecting new messages
|
|
|
|
self.last_messages = {}
|
|
|
|
|
2021-12-27 20:59:24 +00:00
|
|
|
def set_irc(self, irc):
|
|
|
|
self.irc = irc
|
|
|
|
|
2021-12-28 15:28:28 +00:00
|
|
|
def set_tx(self, tx):
|
|
|
|
self.tx = tx
|
|
|
|
|
2021-12-27 19:30:03 +00:00
|
|
|
def setup_loop(self):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2021-12-29 14:25:07 +00:00
|
|
|
Set up the LoopingCall to get all active trades and messages.
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2021-12-29 14:25:07 +00:00
|
|
|
self.lc_dash = LoopingCall(self.dashboard)
|
|
|
|
self.lc_dash.start(int(settings.Agora.RefreshSec))
|
2021-12-27 19:30:03 +00:00
|
|
|
|
2021-12-29 14:25:07 +00:00
|
|
|
def dashboard(self, send_irc=True, formatted=False):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2021-12-29 14:25:07 +00:00
|
|
|
Returns a dict for the dashboard.
|
|
|
|
Calls hooks to parse dashboard info and get all contact messages.
|
|
|
|
:return: dict of dashboard info
|
|
|
|
:rtype: dict
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2021-12-27 19:30:03 +00:00
|
|
|
dash = self.agora.dashboard_seller()
|
2021-12-29 14:25:07 +00:00
|
|
|
dash_tmp = {}
|
2021-12-28 14:09:33 +00:00
|
|
|
if "data" not in dash["response"].keys():
|
|
|
|
self.log.error("Data not in dashboard response: {content}", content=dash)
|
|
|
|
return False
|
2021-12-27 19:30:03 +00:00
|
|
|
if dash["response"]["data"]["contact_count"] > 0:
|
|
|
|
for contact in dash["response"]["data"]["contact_list"]:
|
|
|
|
contact_id = contact["data"]["contact_id"]
|
2021-12-29 14:25:07 +00:00
|
|
|
dash_tmp[contact_id] = contact
|
|
|
|
fmt = self.dashboard_hook(dash_tmp)
|
|
|
|
self.get_all_messages_hook(dash_tmp)
|
|
|
|
if formatted:
|
|
|
|
return fmt
|
2021-12-27 19:30:03 +00:00
|
|
|
return dash_tmp
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2021-12-29 14:25:07 +00:00
|
|
|
def dashboard_hook(self, dash, send_irc=True):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
|
|
|
Get information about our open trades.
|
2021-12-28 01:00:02 +00:00
|
|
|
Post new trades to IRC and cache trades for the future.
|
2021-12-28 11:26:51 +00:00
|
|
|
:return: human readable list of strings about our trades or False
|
|
|
|
:rtype: list or bool
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2021-12-27 19:30:03 +00:00
|
|
|
dash_tmp = []
|
2021-12-28 01:00:02 +00:00
|
|
|
current_trades = []
|
2021-12-29 14:25:07 +00:00
|
|
|
for contact_id, contact in dash.items():
|
2021-12-29 14:41:05 +00:00
|
|
|
reference = self.tx.tx_to_ref(contact_id)
|
|
|
|
if not reference:
|
|
|
|
reference = "not_set"
|
|
|
|
current_trades.append(reference)
|
2021-12-29 14:25:07 +00:00
|
|
|
buyer = contact["data"]["buyer"]["username"]
|
|
|
|
amount = contact["data"]["amount"]
|
|
|
|
amount_xmr = contact["data"]["amount_xmr"]
|
|
|
|
currency = contact["data"]["currency"]
|
|
|
|
if not contact["data"]["is_selling"]:
|
|
|
|
continue
|
2021-12-29 14:41:05 +00:00
|
|
|
if reference not in self.last_dash:
|
2021-12-29 14:25:07 +00:00
|
|
|
self.tx.new_trade(contact_id, buyer, currency, amount, amount_xmr)
|
|
|
|
if send_irc:
|
|
|
|
|
2021-12-29 14:41:05 +00:00
|
|
|
self.irc.client.msg(self.irc.client.channel, f"AUTO {reference}: {buyer} {amount}{currency} {amount_xmr}XMR")
|
2021-12-29 14:32:06 +00:00
|
|
|
|
2021-12-29 14:41:05 +00:00
|
|
|
dash_tmp.append(f"{reference}: {buyer} {amount}{currency} {amount_xmr}XMR")
|
|
|
|
self.last_dash.add(reference)
|
2021-12-29 14:25:07 +00:00
|
|
|
|
|
|
|
# Purge old trades from cache
|
2021-12-29 14:41:05 +00:00
|
|
|
for ref in list(self.last_dash): # We're removing from the list on the fly
|
|
|
|
if ref not in current_trades:
|
|
|
|
self.last_dash.remove(ref)
|
2021-12-27 19:03:01 +00:00
|
|
|
return dash_tmp
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2021-12-28 13:42:31 +00:00
|
|
|
def dashboard_release_urls(self):
|
|
|
|
"""
|
|
|
|
Get information about our open trades.
|
|
|
|
Post new trades to IRC and cache trades for the future.
|
|
|
|
:return: human readable list of strings about our trades or False
|
|
|
|
:rtype: list or bool
|
|
|
|
"""
|
|
|
|
dash = self.agora.dashboard_seller()
|
|
|
|
dash_tmp = []
|
|
|
|
if "data" not in dash["response"]:
|
2021-12-28 14:09:33 +00:00
|
|
|
self.log.error("Data not in dashboard response: {content}", content=dash)
|
2021-12-28 13:42:31 +00:00
|
|
|
return False
|
|
|
|
if dash["response"]["data"]["contact_count"] > 0:
|
|
|
|
for contact in dash["response"]["data"]["contact_list"]:
|
|
|
|
contact_id = contact["data"]["contact_id"]
|
|
|
|
buyer = contact["data"]["buyer"]["username"]
|
|
|
|
amount = contact["data"]["amount"]
|
|
|
|
amount_xmr = contact["data"]["amount_xmr"]
|
|
|
|
currency = contact["data"]["currency"]
|
|
|
|
release_url = contact["actions"]["release_url"]
|
|
|
|
if not contact["data"]["is_selling"]:
|
|
|
|
continue
|
2021-12-29 14:41:05 +00:00
|
|
|
reference = self.tx.tx_to_ref(contact_id)
|
|
|
|
if not reference:
|
|
|
|
reference = "not_set"
|
|
|
|
dash_tmp.append(f"{reference}: {buyer} {amount}{currency} {amount_xmr}XMR {release_url}")
|
2021-12-28 13:42:31 +00:00
|
|
|
|
|
|
|
return dash_tmp
|
|
|
|
|
2021-12-28 01:00:02 +00:00
|
|
|
def get_messages(self, contact_id, send_irc=True):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
|
|
|
Get messages for a certain trade ID.
|
2021-12-28 01:00:02 +00:00
|
|
|
Post new messages to IRC and cache messages for the future.
|
2021-12-27 21:33:49 +00:00
|
|
|
:param contact_id: trade/contact ID
|
|
|
|
:return: list of messages
|
|
|
|
:rtype: list
|
|
|
|
"""
|
2021-12-26 21:36:25 +00:00
|
|
|
messages = self.agora.contact_messages(contact_id)
|
2021-12-27 19:03:01 +00:00
|
|
|
messages_tmp = []
|
2021-12-29 14:41:05 +00:00
|
|
|
reference = self.tx.tx_to_ref(contact_id)
|
|
|
|
if not reference:
|
|
|
|
reference = "not_set"
|
2021-12-27 19:03:01 +00:00
|
|
|
for message in messages["response"]["data"]["message_list"]:
|
|
|
|
messages_tmp.append(f"({message['sender']['username']}): {message['msg']}")
|
2021-12-28 01:00:02 +00:00
|
|
|
if send_irc:
|
2021-12-29 14:41:05 +00:00
|
|
|
if reference in self.last_messages:
|
|
|
|
if not len(self.last_messages[reference]) == len(messages_tmp):
|
|
|
|
difference = set(messages_tmp) ^ self.last_messages[reference]
|
2021-12-28 01:00:02 +00:00
|
|
|
for x in difference:
|
2021-12-29 14:41:05 +00:00
|
|
|
self.irc.client.msg(self.irc.client.channel, f"AUTO {reference}: {x}")
|
2021-12-28 01:00:02 +00:00
|
|
|
else:
|
2021-12-29 14:41:05 +00:00
|
|
|
self.last_messages[reference] = messages_tmp
|
2021-12-28 01:00:02 +00:00
|
|
|
for x in messages_tmp:
|
2021-12-29 14:41:05 +00:00
|
|
|
self.irc.client.msg(self.irc.client.channel, f"NEW {reference}: {x}")
|
2021-12-27 19:03:01 +00:00
|
|
|
return messages_tmp
|
|
|
|
|
2021-12-28 13:42:31 +00:00
|
|
|
def get_all_messages(self, send_irc=True):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
|
|
|
Get all messages for all open trades.
|
2021-12-29 14:25:07 +00:00
|
|
|
Wrapper for passing in dashboard.
|
2021-12-27 21:33:49 +00:00
|
|
|
:return: dict of lists keyed by trade/contact ID
|
|
|
|
:rtype: dict with lists
|
|
|
|
"""
|
2021-12-29 14:25:07 +00:00
|
|
|
dash = self.dashboard()
|
2021-12-28 14:09:33 +00:00
|
|
|
if dash is None:
|
|
|
|
return False
|
2021-12-29 14:25:07 +00:00
|
|
|
return self.get_all_messages_hook(dash, send_irc=send_irc)
|
|
|
|
|
|
|
|
def get_all_messages_hook(self, dash, send_irc=True):
|
|
|
|
"""
|
|
|
|
Get all messages for all open trades.
|
|
|
|
:return: dict of lists keyed by trade/contact ID
|
|
|
|
:rtype: dict with lists
|
|
|
|
"""
|
2021-12-27 19:03:01 +00:00
|
|
|
messages_tmp = {}
|
|
|
|
for contact_id in dash:
|
2021-12-29 14:41:05 +00:00
|
|
|
reference = self.tx.tx_to_ref(contact_id)
|
|
|
|
if not reference:
|
|
|
|
reference = "not_set"
|
2021-12-28 13:42:31 +00:00
|
|
|
messages = self.get_messages(contact_id, send_irc=send_irc)
|
2021-12-29 14:41:05 +00:00
|
|
|
messages_tmp[reference] = messages
|
2021-12-27 19:03:01 +00:00
|
|
|
|
2021-12-28 01:00:02 +00:00
|
|
|
# Purge old trades from cache
|
2021-12-29 14:41:05 +00:00
|
|
|
for ref in list(self.last_messages): # We're removing from the list on the fly
|
|
|
|
if ref not in messages_tmp:
|
|
|
|
del self.last_messages[ref]
|
2021-12-28 01:00:02 +00:00
|
|
|
|
2021-12-27 19:03:01 +00:00
|
|
|
return messages_tmp
|
2021-12-24 02:23:38 +00:00
|
|
|
|
|
|
|
def get_ads(self):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
|
|
|
Get information about our ads.
|
|
|
|
:return: data about our open ads
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
2021-12-26 21:36:25 +00:00
|
|
|
ads = self.agora.ads()
|
|
|
|
return ads
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2021-12-24 17:26:31 +00:00
|
|
|
def get_rates_all(self):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
|
|
|
Get all rates that pair with USD.
|
|
|
|
:return: dictionary of USD/XXX rates
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
2021-12-24 17:26:31 +00:00
|
|
|
rates = self.cr.get_rates("USD")
|
|
|
|
return rates
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2021-12-28 23:56:32 +00:00
|
|
|
def get_acceptable_margins(self, currency, amount):
|
|
|
|
"""
|
|
|
|
Get the minimum and maximum amounts we would accept a trade for.
|
|
|
|
:param currency: currency code
|
|
|
|
:param amount: amount
|
|
|
|
:return: (min, max)
|
|
|
|
:rtype: tuple
|
|
|
|
"""
|
|
|
|
rates = self.get_rates_all()
|
|
|
|
if currency == "USD":
|
|
|
|
min_amount = amount - float(settings.Agora.AcceptableUSDMargin)
|
|
|
|
max_amount = amount + float(settings.Agora.AcceptableUSDMargin)
|
|
|
|
return (min_amount, max_amount)
|
|
|
|
amount_usd = amount / rates[currency]
|
|
|
|
min_usd = amount_usd - float(settings.Agora.AcceptableUSDMargin)
|
|
|
|
max_usd = amount_usd + float(settings.Agora.AcceptableUSDMargin)
|
|
|
|
min_local = min_usd * rates[currency]
|
|
|
|
max_local = max_usd * rates[currency]
|
|
|
|
return (min_local, max_local)
|
|
|
|
|
2021-12-24 17:26:31 +00:00
|
|
|
def create_ad(self, countrycode, currency):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
|
|
|
Post an ad in a country with a given currency.
|
|
|
|
Convert the min and max amounts from settings to the given currency with CurrencyRates.
|
|
|
|
:param countrycode: country code
|
|
|
|
:param currency: currency code
|
|
|
|
:type countrycode: string
|
|
|
|
:type currency: string
|
|
|
|
:return: data about created object or error
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
2021-12-24 17:26:31 +00:00
|
|
|
rates = self.get_rates_all()
|
2021-12-27 19:03:01 +00:00
|
|
|
if currency == "USD":
|
|
|
|
min_amount = float(settings.Agora.MinUSD)
|
|
|
|
max_amount = float(settings.Agora.MaxUSD)
|
|
|
|
else:
|
|
|
|
min_amount = rates[currency] * float(settings.Agora.MinUSD)
|
|
|
|
max_amount = rates[currency] * float(settings.Agora.MaxUSD)
|
2021-12-26 23:12:02 +00:00
|
|
|
price_formula = f"coingeckoxmrusd*usd{currency.lower()}*{settings.Agora.Margin}"
|
2021-12-26 21:36:25 +00:00
|
|
|
# price_formula = f"coingeckoxmrusd*{settings.Agora.Margin}"
|
2021-12-27 13:50:32 +00:00
|
|
|
ad = settings.Agora.Ad
|
|
|
|
ad = ad.replace("\\t", "\t")
|
2021-12-26 21:36:25 +00:00
|
|
|
ad = self.agora.ad_create(
|
|
|
|
country_code=countrycode,
|
|
|
|
currency=currency,
|
|
|
|
trade_type="ONLINE_SELL",
|
|
|
|
asset="XMR",
|
|
|
|
price_equation=price_formula,
|
|
|
|
track_max_amount=False,
|
|
|
|
require_trusted_by_advertiser=False,
|
|
|
|
# verified_email_required = False,
|
|
|
|
online_provider="REVOLUT",
|
|
|
|
msg=settings.Agora.Ad,
|
|
|
|
min_amount=min_amount,
|
|
|
|
max_amount=max_amount,
|
|
|
|
payment_method_details=settings.Agora.PaymentMethodDetails,
|
|
|
|
# require_feedback_score = 0,
|
|
|
|
account_info=settings.Agora.PaymentDetails,
|
|
|
|
)
|
|
|
|
return ad
|
2021-12-24 02:23:38 +00:00
|
|
|
|
|
|
|
def dist_countries(self):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
|
|
|
Distribute our advert into all countries listed in the config.
|
|
|
|
:return: True or False
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2021-12-24 02:23:38 +00:00
|
|
|
for currency, countrycode in loads(settings.Agora.DistList):
|
2021-12-24 17:26:31 +00:00
|
|
|
rtrn = self.create_ad(countrycode, currency)
|
2021-12-24 02:23:38 +00:00
|
|
|
if not rtrn:
|
|
|
|
return False
|
|
|
|
return True
|