2021-12-24 02:23:38 +00:00
|
|
|
# Twisted/Klein imports
|
2021-12-27 19:30:03 +00:00
|
|
|
from twisted.internet.task import LoopingCall
|
2022-01-12 19:55:50 +00:00
|
|
|
from twisted.internet.threads import deferToThread
|
2021-12-24 02:23:38 +00:00
|
|
|
|
|
|
|
# Other library imports
|
2021-12-26 21:36:25 +00:00
|
|
|
from json import loads
|
2022-03-14 21:19:26 +00:00
|
|
|
from lib.agoradesk_py import AgoraDesk
|
2022-01-27 13:45:08 +00:00
|
|
|
from time import sleep
|
2022-02-02 13:36:18 +00:00
|
|
|
from pyotp import TOTP
|
2022-02-25 00:06:10 +00:00
|
|
|
from datetime import datetime
|
2021-12-24 02:23:38 +00:00
|
|
|
|
|
|
|
# Project imports
|
|
|
|
from settings import settings
|
2022-02-22 19:53:22 +00:00
|
|
|
import util
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2022-01-27 12:08:26 +00:00
|
|
|
|
2022-03-05 21:52:31 +00:00
|
|
|
class Agora(util.Base):
|
2021-12-24 02:23:38 +00:00
|
|
|
"""
|
|
|
|
AgoraDesk API handler.
|
|
|
|
"""
|
|
|
|
|
2021-12-27 20:59:24 +00:00
|
|
|
def __init__(self):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2022-03-22 15:35:22 +00:00
|
|
|
Initialise the AgoraDesk API.
|
2021-12-27 21:33:49 +00:00
|
|
|
Initialise the last_dash storage for detecting new trades.
|
|
|
|
"""
|
2022-04-18 16:22:33 +00:00
|
|
|
self.platform = "agora"
|
2022-03-05 21:52:31 +00:00
|
|
|
super().__init__()
|
2021-12-24 17:26:31 +00:00
|
|
|
self.agora = AgoraDesk(settings.Agora.Token)
|
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 = {}
|
|
|
|
|
2022-01-27 16:52:32 +00:00
|
|
|
# Assets that cheat has been run on
|
|
|
|
self.cheat_run_on = []
|
|
|
|
|
2022-03-14 21:19:26 +00:00
|
|
|
def setup_loop(self): # TODO:: move to main sources
|
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
|
|
|
"""
|
2022-01-01 16:34:32 +00:00
|
|
|
self.lc_dash = LoopingCall(self.loop_check)
|
2021-12-29 14:25:07 +00:00
|
|
|
self.lc_dash.start(int(settings.Agora.RefreshSec))
|
2022-01-11 21:40:55 +00:00
|
|
|
if settings.Agora.Cheat == "1":
|
2022-02-09 14:57:11 +00:00
|
|
|
self.lc_cheat = LoopingCall(self.run_cheat_in_thread)
|
2022-01-11 21:40:55 +00:00
|
|
|
self.lc_cheat.start(int(settings.Agora.CheatSec))
|
2022-01-11 21:40:05 +00:00
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2022-01-09 22:11:39 +00:00
|
|
|
def wrap_dashboard(self):
|
2022-01-27 11:36:17 +00:00
|
|
|
dash = self.agora.dashboard_seller()
|
2022-01-27 16:52:32 +00:00
|
|
|
if dash is None:
|
|
|
|
return False
|
2022-01-27 12:08:26 +00:00
|
|
|
if dash is False:
|
2022-01-26 19:46:23 +00:00
|
|
|
return False
|
2022-02-01 20:49:54 +00:00
|
|
|
if dash["response"] is None:
|
|
|
|
return False
|
2021-12-29 14:25:07 +00:00
|
|
|
dash_tmp = {}
|
2022-01-27 16:52:32 +00:00
|
|
|
if not dash.items():
|
|
|
|
return False
|
2021-12-28 14:09:33 +00:00
|
|
|
if "data" not in dash["response"].keys():
|
2022-03-05 21:52:31 +00:00
|
|
|
self.log.error(f"Data not in dashboard response: {dash}")
|
2022-01-09 22:11:39 +00:00
|
|
|
return dash_tmp
|
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
|
2022-01-09 22:11:39 +00:00
|
|
|
return dash_tmp
|
|
|
|
|
|
|
|
def loop_check(self):
|
|
|
|
"""
|
|
|
|
Calls hooks to parse dashboard info and get all contact messages.
|
|
|
|
"""
|
|
|
|
dash_tmp = self.wrap_dashboard()
|
2021-12-31 18:21:56 +00:00
|
|
|
|
2022-01-01 16:34:32 +00:00
|
|
|
# Call dashboard hooks
|
|
|
|
self.dashboard_hook(dash_tmp)
|
|
|
|
|
|
|
|
# Get recent messages
|
2022-01-27 11:36:17 +00:00
|
|
|
self.get_recent_messages()
|
2021-12-27 19:30:03 +00:00
|
|
|
return dash_tmp
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2022-01-01 16:34:32 +00:00
|
|
|
def get_dashboard(self):
|
|
|
|
"""
|
|
|
|
Get dashboard helper for IRC only.
|
|
|
|
"""
|
2022-01-09 22:11:39 +00:00
|
|
|
dash = self.wrap_dashboard()
|
|
|
|
rtrn = []
|
2022-01-27 12:08:26 +00:00
|
|
|
if dash is False:
|
|
|
|
return False
|
2022-01-09 22:11:39 +00:00
|
|
|
for contact_id, contact in dash.items():
|
|
|
|
reference = self.tx.tx_to_ref(contact_id)
|
|
|
|
buyer = contact["data"]["buyer"]["username"]
|
|
|
|
amount = contact["data"]["amount"]
|
2022-01-21 13:54:47 +00:00
|
|
|
asset = contact["data"]["advertisement"]["asset"]
|
|
|
|
if asset == "XMR":
|
2022-01-21 13:25:33 +00:00
|
|
|
amount_crypto = contact["data"]["amount_xmr"]
|
2022-01-21 13:54:47 +00:00
|
|
|
elif asset == "BTC":
|
2022-01-21 13:25:33 +00:00
|
|
|
amount_crypto = contact["data"]["amount_btc"]
|
2022-01-09 22:11:39 +00:00
|
|
|
currency = contact["data"]["currency"]
|
2022-02-17 12:18:05 +00:00
|
|
|
provider = contact["data"]["advertisement"]["payment_method"]
|
2022-01-09 22:11:39 +00:00
|
|
|
if not contact["data"]["is_selling"]:
|
|
|
|
continue
|
2022-02-17 12:18:05 +00:00
|
|
|
rtrn.append(f"{reference}: {buyer} {amount}{currency} {provider} {amount_crypto}{asset}")
|
2022-01-09 22:11:39 +00:00
|
|
|
return rtrn
|
2022-01-01 16:34:32 +00:00
|
|
|
|
|
|
|
def dashboard_hook(self, dash):
|
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-27 21:33:49 +00:00
|
|
|
"""
|
2021-12-28 01:00:02 +00:00
|
|
|
current_trades = []
|
2022-01-21 09:17:18 +00:00
|
|
|
if not dash:
|
|
|
|
return
|
2022-01-05 19:24:15 +00:00
|
|
|
if not dash.items():
|
|
|
|
return
|
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)
|
2021-12-29 18:40:44 +00:00
|
|
|
if reference:
|
|
|
|
current_trades.append(reference)
|
2021-12-29 14:25:07 +00:00
|
|
|
buyer = contact["data"]["buyer"]["username"]
|
|
|
|
amount = contact["data"]["amount"]
|
2022-01-21 13:54:47 +00:00
|
|
|
asset = contact["data"]["advertisement"]["asset"]
|
2022-02-17 12:18:05 +00:00
|
|
|
provider = contact["data"]["advertisement"]["payment_method"]
|
2022-01-21 13:54:47 +00:00
|
|
|
if asset == "XMR":
|
2022-01-21 13:26:58 +00:00
|
|
|
amount_crypto = contact["data"]["amount_xmr"]
|
2022-01-21 13:54:47 +00:00
|
|
|
elif asset == "BTC":
|
2022-01-21 13:26:58 +00:00
|
|
|
amount_crypto = contact["data"]["amount_btc"]
|
2021-12-29 14:25:07 +00:00
|
|
|
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:
|
2022-04-12 21:06:28 +00:00
|
|
|
reference = self.tx.new_trade(
|
|
|
|
"agora",
|
|
|
|
asset,
|
|
|
|
contact_id,
|
|
|
|
buyer,
|
|
|
|
currency,
|
|
|
|
amount,
|
|
|
|
amount_crypto,
|
|
|
|
provider,
|
|
|
|
)
|
2021-12-29 18:40:44 +00:00
|
|
|
if reference:
|
|
|
|
if reference not in current_trades:
|
|
|
|
current_trades.append(reference)
|
2022-01-01 16:34:32 +00:00
|
|
|
# Let us know there is a new trade
|
2022-02-17 12:18:05 +00:00
|
|
|
self.irc.sendmsg(f"AUTO {reference}: {buyer} {amount}{currency} {provider} {amount_crypto}{asset}")
|
2022-01-01 16:34:32 +00:00
|
|
|
# Note that we have seen this reference
|
2021-12-29 14:41:05 +00:00
|
|
|
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-29 18:40:44 +00:00
|
|
|
if reference and reference not in current_trades:
|
|
|
|
current_trades.append(reference)
|
2021-12-29 15:03:47 +00:00
|
|
|
self.tx.cleanup(current_trades)
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2021-12-30 22:41:28 +00:00
|
|
|
def get_recent_messages(self, send_irc=True):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2021-12-30 22:41:28 +00:00
|
|
|
Get recent messages.
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2021-12-30 22:41:28 +00:00
|
|
|
messages_tmp = {}
|
2022-01-27 11:36:17 +00:00
|
|
|
messages = self.agora.recent_messages()
|
2022-01-27 12:08:26 +00:00
|
|
|
if messages is False:
|
|
|
|
return False
|
2021-12-30 22:41:28 +00:00
|
|
|
if not messages["success"]:
|
2021-12-30 14:15:24 +00:00
|
|
|
return False
|
2022-01-12 19:17:53 +00:00
|
|
|
if "data" not in messages["response"]:
|
2022-03-05 21:52:31 +00:00
|
|
|
self.log.error(f"Data not in messages response: {messages['response']}")
|
2022-01-11 21:16:49 +00:00
|
|
|
return False
|
2021-12-30 22:41:28 +00:00
|
|
|
open_tx = self.tx.get_ref_map().keys()
|
2021-12-27 19:03:01 +00:00
|
|
|
for message in messages["response"]["data"]["message_list"]:
|
2021-12-30 22:41:28 +00:00
|
|
|
contact_id = message["contact_id"]
|
|
|
|
username = message["sender"]["username"]
|
|
|
|
msg = message["msg"]
|
|
|
|
if contact_id not in open_tx:
|
|
|
|
continue
|
|
|
|
reference = self.tx.tx_to_ref(contact_id)
|
|
|
|
if reference in messages_tmp:
|
|
|
|
messages_tmp[reference].append([username, msg])
|
2021-12-28 01:00:02 +00:00
|
|
|
else:
|
2021-12-30 22:41:28 +00:00
|
|
|
messages_tmp[reference] = [[username, msg]]
|
2021-12-27 19:03:01 +00:00
|
|
|
|
2021-12-30 22:41:28 +00:00
|
|
|
# Send new messages on IRC
|
|
|
|
if send_irc:
|
|
|
|
for user, message in messages_tmp[reference]:
|
|
|
|
if reference in self.last_messages:
|
|
|
|
if not [user, message] in self.last_messages[reference]:
|
2022-01-01 16:39:36 +00:00
|
|
|
self.irc.sendmsg(f"AUTO {reference}: ({user}) {message}")
|
2021-12-30 22:41:28 +00:00
|
|
|
# Append sent messages to last_messages so we don't send them again
|
|
|
|
self.last_messages[reference].append([user, message])
|
|
|
|
else:
|
|
|
|
self.last_messages[reference] = [[user, message]]
|
|
|
|
for x in messages_tmp[reference]:
|
2022-01-01 16:39:36 +00:00
|
|
|
self.irc.sendmsg(f"NEW {reference}: ({user}) {message}")
|
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
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2021-12-30 14:15:24 +00:00
|
|
|
def enum_ad_ids(self, page=0):
|
|
|
|
ads = self.agora._api_call(api_method="ads", query_values={"page": page})
|
2022-01-27 12:08:26 +00:00
|
|
|
if ads is False:
|
|
|
|
return False
|
2021-12-30 14:15:24 +00:00
|
|
|
ads_total = []
|
|
|
|
if not ads["success"]:
|
|
|
|
return False
|
|
|
|
for ad in ads["response"]["data"]["ad_list"]:
|
|
|
|
ads_total.append(ad["data"]["ad_id"])
|
2021-12-31 00:39:52 +00:00
|
|
|
if "pagination" in ads["response"]:
|
|
|
|
if "next" in ads["response"]["pagination"]:
|
|
|
|
page += 1
|
2022-01-27 12:08:26 +00:00
|
|
|
ads_iter = self.enum_ad_ids(page)
|
|
|
|
if ads_iter is None:
|
|
|
|
return False
|
|
|
|
if ads_iter is False:
|
|
|
|
return False
|
|
|
|
for ad in ads_iter:
|
2021-12-31 00:39:52 +00:00
|
|
|
ads_total.append(ad)
|
|
|
|
return ads_total
|
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2022-01-29 09:02:27 +00:00
|
|
|
def enum_ads(self, requested_asset=None, page=0):
|
2022-01-27 16:52:32 +00:00
|
|
|
query_values = {"page": page}
|
2022-01-29 09:02:27 +00:00
|
|
|
if requested_asset:
|
|
|
|
query_values["asset"] = requested_asset
|
2022-01-27 16:52:32 +00:00
|
|
|
ads = self.agora._api_call(api_method="ads", query_values=query_values)
|
2022-01-27 12:08:26 +00:00
|
|
|
if ads is False:
|
|
|
|
return False
|
2021-12-31 00:39:52 +00:00
|
|
|
ads_total = []
|
|
|
|
if not ads["success"]:
|
|
|
|
return False
|
|
|
|
for ad in ads["response"]["data"]["ad_list"]:
|
2022-01-29 09:02:27 +00:00
|
|
|
asset = ad["data"]["asset"]
|
|
|
|
ad_id = ad["data"]["ad_id"]
|
|
|
|
country = ad["data"]["countrycode"]
|
|
|
|
currency = ad["data"]["currency"]
|
|
|
|
provider = ad["data"]["online_provider"]
|
|
|
|
ads_total.append([asset, ad_id, country, currency, provider])
|
2021-12-31 00:39:52 +00:00
|
|
|
if "pagination" in ads["response"]:
|
|
|
|
if "next" in ads["response"]["pagination"]:
|
|
|
|
page += 1
|
2022-01-29 09:02:27 +00:00
|
|
|
ads_iter = self.enum_ads(requested_asset, page)
|
2022-01-27 12:08:26 +00:00
|
|
|
if ads_iter is None:
|
|
|
|
return False
|
|
|
|
if ads_iter is False:
|
|
|
|
return False
|
|
|
|
for ad in ads_iter:
|
2022-01-29 09:02:27 +00:00
|
|
|
ads_total.append([ad[0], ad[1], ad[2], ad[3], ad[4]])
|
2021-12-30 14:15:24 +00:00
|
|
|
return ads_total
|
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2022-02-11 10:45:23 +00:00
|
|
|
def enum_public_ads(self, asset, currency, providers=None, page=0):
|
|
|
|
to_return = []
|
|
|
|
if asset == "XMR":
|
|
|
|
coin = "monero"
|
|
|
|
elif asset == "BTC":
|
|
|
|
coin = "bitcoins"
|
2022-02-02 13:36:18 +00:00
|
|
|
if not providers:
|
|
|
|
providers = ["REVOLUT"]
|
2022-01-25 11:48:12 +00:00
|
|
|
# buy-monero-online, buy-bitcoin-online
|
|
|
|
# Work around Agora weirdness calling it bitcoins
|
2022-04-12 21:06:28 +00:00
|
|
|
ads = self.agora._api_call(
|
|
|
|
api_method=f"buy-{coin}-online/{currency}",
|
|
|
|
query_values={"page": page},
|
|
|
|
)
|
2022-03-08 21:01:21 +00:00
|
|
|
# with open("pub.json", "a") as f:
|
|
|
|
# import json
|
|
|
|
# f.write(json.dumps([page, currency, asset, ads])+"\n")
|
|
|
|
# f.close()
|
2022-01-27 13:45:08 +00:00
|
|
|
if ads is None:
|
|
|
|
return False
|
2022-01-27 12:08:26 +00:00
|
|
|
if ads is False:
|
2022-01-27 16:52:32 +00:00
|
|
|
return False
|
2022-02-21 10:01:38 +00:00
|
|
|
if ads["response"] is None:
|
|
|
|
return False
|
2022-01-27 16:52:32 +00:00
|
|
|
if "data" not in ads["response"]:
|
2022-01-09 14:34:37 +00:00
|
|
|
return False
|
|
|
|
for ad in ads["response"]["data"]["ad_list"]:
|
2022-02-02 13:36:18 +00:00
|
|
|
if ad["data"]["online_provider"] not in providers:
|
|
|
|
continue
|
2022-01-29 09:02:27 +00:00
|
|
|
date_last_seen = ad["data"]["profile"]["last_online"]
|
|
|
|
# Check if this person was seen recently
|
2022-02-22 19:53:22 +00:00
|
|
|
if not util.last_online_recent(date_last_seen):
|
2022-01-29 09:02:27 +00:00
|
|
|
continue
|
|
|
|
ad_id = ad["data"]["ad_id"]
|
|
|
|
username = ad["data"]["profile"]["username"]
|
|
|
|
temp_price = ad["data"]["temp_price"]
|
|
|
|
provider = ad["data"]["online_provider"]
|
2022-02-11 10:45:23 +00:00
|
|
|
if ad["data"]["currency"] != currency:
|
|
|
|
continue
|
|
|
|
to_append = [ad_id, username, temp_price, provider, asset, currency]
|
|
|
|
if to_append not in to_return:
|
|
|
|
to_return.append(to_append)
|
|
|
|
# yield [ad_id, username, temp_price, provider, asset, currency]
|
2022-01-09 14:34:37 +00:00
|
|
|
if "pagination" in ads["response"]:
|
|
|
|
if "next" in ads["response"]["pagination"]:
|
|
|
|
page += 1
|
2022-02-11 10:45:23 +00:00
|
|
|
ads_iter = self.enum_public_ads(asset, currency, providers, page)
|
2022-01-27 12:08:26 +00:00
|
|
|
if ads_iter is None:
|
|
|
|
return False
|
|
|
|
if ads_iter is False:
|
|
|
|
return False
|
|
|
|
for ad in ads_iter:
|
2022-02-11 10:45:23 +00:00
|
|
|
to_append = [ad[0], ad[1], ad[2], ad[3], ad[4], ad[5]]
|
|
|
|
if to_append not in to_return:
|
|
|
|
to_return.append(to_append)
|
|
|
|
return to_return
|
2022-01-09 14:34:37 +00:00
|
|
|
|
2022-02-09 09:01:18 +00:00
|
|
|
def run_cheat_in_thread(self, assets=None):
|
2022-01-12 19:55:50 +00:00
|
|
|
"""
|
|
|
|
Update prices in another thread.
|
|
|
|
"""
|
2022-02-09 09:01:18 +00:00
|
|
|
if not assets:
|
2022-01-27 16:52:32 +00:00
|
|
|
all_assets = loads(settings.Agora.AssetList)
|
|
|
|
assets_not_run = set(all_assets) ^ set(self.cheat_run_on)
|
|
|
|
if not assets_not_run:
|
|
|
|
self.cheat_run_on = []
|
|
|
|
asset = list(all_assets).pop()
|
|
|
|
self.cheat_run_on.append(asset)
|
|
|
|
else:
|
|
|
|
asset = assets_not_run.pop()
|
|
|
|
self.cheat_run_on.append(asset)
|
2022-02-09 09:01:18 +00:00
|
|
|
deferToThread(self.update_prices, [asset])
|
2022-01-27 16:52:32 +00:00
|
|
|
return asset
|
|
|
|
else:
|
2022-02-09 09:01:18 +00:00
|
|
|
deferToThread(self.update_prices, assets)
|
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2022-02-09 09:01:18 +00:00
|
|
|
def update_prices(self, assets=None):
|
|
|
|
# Get all public ads for the given assets
|
|
|
|
public_ads = self.get_all_public_ads(assets)
|
2022-02-13 09:56:22 +00:00
|
|
|
if not public_ads:
|
|
|
|
return False
|
2022-02-09 09:01:18 +00:00
|
|
|
|
|
|
|
# Get the ads to update
|
2022-04-15 13:53:39 +00:00
|
|
|
to_update = self.markets.get_new_ad_equations("agora", public_ads, assets)
|
2022-02-09 09:01:18 +00:00
|
|
|
self.slow_ad_update(to_update)
|
2022-01-12 19:55:50 +00:00
|
|
|
|
2022-02-15 21:30:45 +00:00
|
|
|
# TODO: make generic and move to markets
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2022-02-12 15:12:52 +00:00
|
|
|
def get_all_public_ads(self, assets=None, currencies=None, providers=None):
|
2022-02-07 17:42:11 +00:00
|
|
|
"""
|
|
|
|
Get all public ads for our listed currencies.
|
|
|
|
:return: dict of public ads keyed by currency
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
|
|
|
public_ads = {}
|
|
|
|
crypto_map = {
|
|
|
|
"XMR": "monero",
|
|
|
|
"BTC": "bitcoin",
|
|
|
|
}
|
|
|
|
|
2022-02-09 09:01:18 +00:00
|
|
|
if not assets:
|
2022-04-18 16:22:33 +00:00
|
|
|
assets = self.markets.get_all_assets(self.platform)
|
2022-02-07 17:42:11 +00:00
|
|
|
# Get all currencies we have ads for, deduplicated
|
2022-02-12 15:12:52 +00:00
|
|
|
if not currencies:
|
2022-04-18 16:22:33 +00:00
|
|
|
currencies = self.markets.get_all_currencies(self.platform)
|
2022-02-12 15:12:52 +00:00
|
|
|
if not providers:
|
2022-04-18 16:22:33 +00:00
|
|
|
providers = self.markets.get_all_providers(self.platform)
|
2022-04-20 11:55:08 +00:00
|
|
|
sinks_currencies = self.sinks.currencies
|
|
|
|
supported_currencies = [currency for currency in currencies if currency in sinks_currencies]
|
|
|
|
currencies = supported_currencies
|
2022-02-07 17:42:11 +00:00
|
|
|
# We want to get the ads for each of these currencies and return the result
|
2022-03-19 15:38:24 +00:00
|
|
|
rates = self.money.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=currencies)
|
2022-02-09 09:01:18 +00:00
|
|
|
for asset in assets:
|
2022-02-07 17:42:11 +00:00
|
|
|
for currency in currencies:
|
|
|
|
cg_asset_name = crypto_map[asset]
|
|
|
|
try:
|
2022-02-11 10:45:23 +00:00
|
|
|
rates[cg_asset_name][currency.lower()]
|
2022-02-07 17:42:11 +00:00
|
|
|
except KeyError:
|
2022-02-13 22:18:19 +00:00
|
|
|
# self.log.error("Error getting public ads for currency {currency}", currency=currency)
|
2022-02-07 17:42:11 +00:00
|
|
|
continue
|
2022-02-11 10:45:23 +00:00
|
|
|
ads_list = self.enum_public_ads(asset, currency, providers)
|
2022-02-13 09:56:22 +00:00
|
|
|
if not ads_list:
|
|
|
|
continue
|
2022-04-18 16:22:33 +00:00
|
|
|
ads = self.money.lookup_rates(self.platform, ads_list, rates=rates)
|
2022-02-07 17:42:11 +00:00
|
|
|
if not ads:
|
|
|
|
continue
|
2022-02-24 23:44:10 +00:00
|
|
|
self.write_to_es_ads("ads", ads)
|
2022-02-07 17:42:11 +00:00
|
|
|
if currency in public_ads:
|
2022-02-09 09:01:18 +00:00
|
|
|
for ad in list(ads):
|
|
|
|
if ad not in public_ads[currency]:
|
|
|
|
public_ads[currency].append(ad)
|
2022-02-07 17:42:11 +00:00
|
|
|
else:
|
|
|
|
public_ads[currency] = ads
|
2022-02-13 09:56:22 +00:00
|
|
|
|
2022-02-07 17:42:11 +00:00
|
|
|
return public_ads
|
|
|
|
|
2022-02-24 23:44:10 +00:00
|
|
|
def write_to_es_ads(self, msgtype, ads):
|
|
|
|
if settings.ES.Enabled == "1":
|
|
|
|
for ad in ads:
|
|
|
|
cast = {
|
|
|
|
"id": ad[0],
|
|
|
|
"username": ad[1],
|
|
|
|
"price": ad[2],
|
|
|
|
"provider": ad[3],
|
|
|
|
"asset": ad[4],
|
|
|
|
"currency": ad[5],
|
|
|
|
"margin": ad[6],
|
|
|
|
}
|
|
|
|
cast["type"] = msgtype
|
|
|
|
cast["ts"] = str(datetime.now().isoformat())
|
|
|
|
cast["xtype"] = "platorm"
|
|
|
|
cast["market"] = "agora"
|
|
|
|
self.es.index(index=settings.ES.MetaIndex, document=cast)
|
|
|
|
|
2022-01-27 13:45:08 +00:00
|
|
|
def slow_ad_update(self, ads):
|
|
|
|
"""
|
|
|
|
Slow ad equation update utilising exponential backoff in order to guarantee all ads are updated.
|
|
|
|
:param ads: our list of ads
|
|
|
|
"""
|
|
|
|
iterations = 0
|
|
|
|
throttled = 0
|
2022-01-27 16:52:32 +00:00
|
|
|
assets = set()
|
2022-01-27 19:12:15 +00:00
|
|
|
currencies = set()
|
|
|
|
while not all([x[4] for x in ads]) or iterations == 1000:
|
2022-01-27 13:45:08 +00:00
|
|
|
for ad_index in range(len(ads)):
|
2022-01-27 19:12:15 +00:00
|
|
|
ad_id, new_formula, asset, currency, actioned = ads[ad_index]
|
2022-01-27 16:52:32 +00:00
|
|
|
assets.add(asset)
|
2022-01-27 19:12:15 +00:00
|
|
|
currencies.add(currency)
|
2022-01-27 13:45:08 +00:00
|
|
|
if not actioned:
|
|
|
|
rtrn = self.agora.ad_equation(ad_id, new_formula)
|
|
|
|
if rtrn["success"]:
|
2022-01-27 19:12:15 +00:00
|
|
|
ads[ad_index][4] = True
|
2022-01-27 13:45:08 +00:00
|
|
|
throttled = 0
|
|
|
|
continue
|
|
|
|
else:
|
2022-02-15 13:56:42 +00:00
|
|
|
if "error_code" not in rtrn["response"]["error"]:
|
2022-03-05 21:52:31 +00:00
|
|
|
self.log.error(f"Error code not in return for ad {ad_id}: {rtrn['response']}")
|
2022-02-15 13:56:42 +00:00
|
|
|
return
|
2022-01-27 13:45:08 +00:00
|
|
|
if rtrn["response"]["error"]["error_code"] == 429:
|
|
|
|
throttled += 1
|
|
|
|
sleep_time = pow(throttled, float(settings.Agora.SleepExponent))
|
2022-04-12 21:06:28 +00:00
|
|
|
self.log.info(
|
|
|
|
f"Throttled {throttled} times while updating {ad_id}, sleeping for {sleep_time} seconds"
|
|
|
|
)
|
2022-01-27 13:45:08 +00:00
|
|
|
# We're running in a thread, so this is fine
|
|
|
|
sleep(sleep_time)
|
2022-03-05 21:52:31 +00:00
|
|
|
self.log.error(f"Error updating ad {ad_id}: {rtrn['response']}")
|
2022-01-27 13:45:08 +00:00
|
|
|
continue
|
|
|
|
iterations += 1
|
2022-01-09 14:34:37 +00:00
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2021-12-30 14:15:24 +00:00
|
|
|
def nuke_ads(self):
|
|
|
|
"""
|
|
|
|
Delete all of our adverts.
|
|
|
|
:return: True or False
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2021-12-31 00:39:52 +00:00
|
|
|
ads = self.enum_ad_ids()
|
|
|
|
return_ids = []
|
2022-01-27 12:08:26 +00:00
|
|
|
if ads is False:
|
2021-12-30 14:15:24 +00:00
|
|
|
return False
|
|
|
|
for ad_id in ads:
|
2021-12-31 00:39:52 +00:00
|
|
|
rtrn = self.agora.ad_delete(ad_id)
|
|
|
|
return_ids.append(rtrn["success"])
|
|
|
|
return all(return_ids)
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2022-03-08 20:42:47 +00:00
|
|
|
@util.handle_exceptions
|
2022-04-12 21:06:28 +00:00
|
|
|
def create_ad(
|
|
|
|
self,
|
|
|
|
asset,
|
|
|
|
countrycode,
|
|
|
|
currency,
|
|
|
|
provider,
|
|
|
|
payment_details,
|
|
|
|
visible=None,
|
|
|
|
edit=False,
|
|
|
|
ad_id=None,
|
|
|
|
):
|
2022-03-08 20:42:47 +00:00
|
|
|
"""
|
|
|
|
Post an ad with the given asset in a country with a given currency.
|
|
|
|
Convert the min and max amounts from settings to the given currency with CurrencyRates.
|
|
|
|
:param asset: the crypto asset to list (XMR or BTC)
|
|
|
|
:type asset: string
|
|
|
|
:param countrycode: country code
|
|
|
|
:param currency: currency code
|
|
|
|
:param payment_details: the payment details
|
|
|
|
:type countrycode: string
|
|
|
|
:type currency: string
|
|
|
|
:type payment_details: dict
|
|
|
|
:return: data about created object or error
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
|
|
|
|
|
|
|
if payment_details:
|
2022-03-19 15:43:51 +00:00
|
|
|
payment_details_text = self.markets.format_payment_details(currency, payment_details)
|
|
|
|
ad_text = self.markets.format_ad(asset, currency, payment_details_text)
|
2022-04-18 16:22:33 +00:00
|
|
|
min_amount, max_amount = self.money.get_minmax(self.platform, asset, currency)
|
2022-03-08 20:42:47 +00:00
|
|
|
|
2022-01-23 17:08:01 +00:00
|
|
|
price_formula = f"coingecko{asset.lower()}usd*usd{currency.lower()}*{settings.Agora.Margin}"
|
2022-03-08 20:42:47 +00:00
|
|
|
|
2022-01-25 08:43:53 +00:00
|
|
|
form = {
|
|
|
|
"country_code": countrycode,
|
|
|
|
"currency": currency,
|
|
|
|
"trade_type": "ONLINE_SELL",
|
|
|
|
"asset": asset,
|
|
|
|
"price_equation": price_formula,
|
|
|
|
"track_max_amount": False,
|
|
|
|
"require_trusted_by_advertiser": False,
|
2022-01-29 09:02:27 +00:00
|
|
|
"online_provider": provider,
|
2022-03-16 18:52:36 +00:00
|
|
|
"payment_method_details": settings.Platform.PaymentMethodDetails,
|
2022-04-20 20:15:50 +00:00
|
|
|
"require_feedback_score": int(settings.Agora.FeedbackScore),
|
2022-01-25 08:43:53 +00:00
|
|
|
}
|
2022-03-08 21:27:45 +00:00
|
|
|
if visible is False:
|
|
|
|
form["visible"] = False
|
|
|
|
elif visible is True:
|
|
|
|
form["visible"] = False
|
2022-03-08 20:42:47 +00:00
|
|
|
if payment_details:
|
|
|
|
form["account_info"] = payment_details_text
|
|
|
|
form["msg"] = ad_text
|
|
|
|
form["min_amount"] = min_amount
|
|
|
|
form["max_amount"] = max_amount
|
|
|
|
|
2022-02-01 20:49:54 +00:00
|
|
|
if edit:
|
|
|
|
ad = self.agora.ad(ad_id=ad_id, **form)
|
|
|
|
else:
|
|
|
|
ad = self.agora.ad_create(**form)
|
2021-12-26 21:36:25 +00:00
|
|
|
return ad
|
2021-12-24 02:23:38 +00:00
|
|
|
|
2022-02-15 21:30:16 +00:00
|
|
|
def dist_countries(self, filter_asset=None):
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2022-02-01 20:49:54 +00:00
|
|
|
Distribute our advert into all countries and providers listed in the config.
|
2021-12-30 22:41:28 +00:00
|
|
|
Exits on errors.
|
|
|
|
:return: False or dict with response
|
|
|
|
:rtype: bool or dict
|
2021-12-27 21:33:49 +00:00
|
|
|
"""
|
2022-04-20 19:46:46 +00:00
|
|
|
dist_list = list(self.markets.create_distribution_list(self.platform, filter_asset))
|
2022-02-01 20:49:54 +00:00
|
|
|
our_ads = self.enum_ads()
|
2022-04-12 21:06:28 +00:00
|
|
|
(
|
|
|
|
supported_currencies,
|
|
|
|
account_info,
|
2022-04-18 16:22:33 +00:00
|
|
|
) = self.markets.get_valid_account_details(self.platform)
|
2022-02-01 20:49:54 +00:00
|
|
|
# Let's get rid of the ad IDs and make it a tuple like dist_list
|
|
|
|
our_ads = [(x[0], x[2], x[3], x[4]) for x in our_ads]
|
|
|
|
for asset, countrycode, currency, provider in dist_list:
|
|
|
|
if (asset, countrycode, currency, provider) not in our_ads:
|
2022-03-08 21:27:45 +00:00
|
|
|
if currency in supported_currencies:
|
|
|
|
# Create the actual ad and pass in all the stuff
|
2022-04-12 21:06:28 +00:00
|
|
|
rtrn = self.create_ad(
|
|
|
|
asset,
|
|
|
|
countrycode,
|
|
|
|
currency,
|
|
|
|
provider,
|
|
|
|
payment_details=account_info[currency],
|
|
|
|
)
|
2022-03-08 21:27:45 +00:00
|
|
|
# Bail on first error, let's not continue
|
|
|
|
if rtrn is False:
|
|
|
|
return False
|
|
|
|
yield rtrn
|
2021-12-31 00:39:52 +00:00
|
|
|
|
2022-02-01 20:49:54 +00:00
|
|
|
def redist_countries(self):
|
|
|
|
"""
|
|
|
|
Redistribute our advert details into all our listed adverts.
|
|
|
|
This will edit all ads and update the details. Only works if we have already run dist.
|
|
|
|
This will not post any new ads.
|
|
|
|
Exits on errors.
|
|
|
|
:return: False or dict with response
|
|
|
|
:rtype: bool or dict
|
|
|
|
"""
|
|
|
|
our_ads = self.enum_ads()
|
2022-04-12 21:06:28 +00:00
|
|
|
(
|
|
|
|
supported_currencies,
|
|
|
|
account_info,
|
2022-04-18 16:22:33 +00:00
|
|
|
) = self.markets.get_valid_account_details(self.platform)
|
2022-04-20 20:15:50 +00:00
|
|
|
if not our_ads:
|
|
|
|
self.log.error("Could not get our ads.")
|
|
|
|
return False
|
2022-02-01 20:49:54 +00:00
|
|
|
for asset, ad_id, countrycode, currency, provider in our_ads:
|
2022-03-08 21:27:45 +00:00
|
|
|
if currency in supported_currencies:
|
2022-03-09 15:18:54 +00:00
|
|
|
rtrn = self.create_ad(
|
2022-04-12 21:06:28 +00:00
|
|
|
asset,
|
|
|
|
countrycode,
|
|
|
|
currency,
|
|
|
|
provider,
|
|
|
|
payment_details=account_info[currency],
|
|
|
|
edit=True,
|
|
|
|
ad_id=ad_id,
|
2022-03-09 15:18:54 +00:00
|
|
|
)
|
2022-03-08 21:27:45 +00:00
|
|
|
# Bail on first error, let's not continue
|
|
|
|
if rtrn is False:
|
|
|
|
return False
|
|
|
|
yield (rtrn, ad_id)
|
2022-02-01 20:49:54 +00:00
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2021-12-31 00:39:52 +00:00
|
|
|
def strip_duplicate_ads(self):
|
|
|
|
"""
|
|
|
|
Remove duplicate ads.
|
|
|
|
:return: list of duplicate ads
|
|
|
|
:rtype: list
|
|
|
|
"""
|
|
|
|
existing_ads = self.enum_ads()
|
|
|
|
_size = len(existing_ads)
|
|
|
|
repeated = []
|
|
|
|
for i in range(_size):
|
|
|
|
k = i + 1
|
|
|
|
for j in range(k, _size):
|
|
|
|
if existing_ads[i] == existing_ads[j] and existing_ads[i] not in repeated:
|
|
|
|
repeated.append(existing_ads[i])
|
|
|
|
|
|
|
|
actioned = []
|
|
|
|
for ad_id, country, currency in repeated:
|
|
|
|
rtrn = self.agora.ad_delete(ad_id)
|
|
|
|
actioned.append(rtrn["success"])
|
2022-03-19 15:38:24 +00:00
|
|
|
|
2021-12-31 00:39:52 +00:00
|
|
|
return all(actioned)
|
|
|
|
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2021-12-29 19:21:05 +00:00
|
|
|
def release_funds(self, contact_id):
|
|
|
|
"""
|
|
|
|
Release funds for a contact_id.
|
|
|
|
:param contact_id: trade/contact ID
|
|
|
|
:type contact_id: string
|
|
|
|
:return: response dict
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
2022-03-16 18:52:36 +00:00
|
|
|
if settings.Agora.Dummy == "1":
|
|
|
|
self.log.error(f"Running in dummy mode, not releasing funds for {contact_id}")
|
|
|
|
return
|
2021-12-29 19:21:05 +00:00
|
|
|
payload = {"tradeId": contact_id, "password": settings.Agora.Pass}
|
2022-04-12 21:06:28 +00:00
|
|
|
rtrn = self.agora._api_call(
|
|
|
|
api_method=f"contact_release/{contact_id}",
|
|
|
|
http_method="POST",
|
|
|
|
query_values=payload,
|
|
|
|
)
|
2022-01-25 18:39:35 +00:00
|
|
|
|
|
|
|
# Check if we can withdraw funds
|
2022-02-22 20:16:36 +00:00
|
|
|
self.withdraw_funds()
|
2022-01-25 18:39:35 +00:00
|
|
|
|
2021-12-29 19:21:05 +00:00
|
|
|
return rtrn
|
2022-01-25 17:54:42 +00:00
|
|
|
|
2022-02-15 22:28:14 +00:00
|
|
|
# TODO: write test before re-enabling adding total_trades
|
2022-02-22 19:53:22 +00:00
|
|
|
@util.handle_exceptions
|
2022-01-25 17:54:42 +00:00
|
|
|
def withdraw_funds(self):
|
|
|
|
"""
|
2022-02-13 09:56:22 +00:00
|
|
|
Withdraw excess funds to our XMR wallets.
|
2022-01-25 17:54:42 +00:00
|
|
|
"""
|
2022-01-25 18:39:35 +00:00
|
|
|
totals_all = self.tx.get_total()
|
2022-01-27 13:45:08 +00:00
|
|
|
if totals_all is False:
|
|
|
|
return False
|
2022-01-25 18:39:35 +00:00
|
|
|
|
|
|
|
wallet_xmr, _ = totals_all[2]
|
|
|
|
|
|
|
|
# Get the wallet balances in USD
|
|
|
|
total_usd = totals_all[0][1]
|
2022-02-15 22:28:14 +00:00
|
|
|
|
2022-03-16 18:52:36 +00:00
|
|
|
# total_trades_usd = self.tx.get_open_trades_usd()
|
2022-02-13 09:56:22 +00:00
|
|
|
if not total_usd:
|
|
|
|
return False
|
2022-02-15 22:28:14 +00:00
|
|
|
# total_usd += total_trades_usd
|
|
|
|
# print("total_usd after trades add", total_usd)
|
|
|
|
|
2022-01-25 18:39:35 +00:00
|
|
|
profit_usd = total_usd - float(settings.Money.BaseUSD)
|
|
|
|
# Get the XMR -> USD exchange rate
|
2022-03-19 15:38:24 +00:00
|
|
|
xmr_usd = self.money.cg.get_price(ids="monero", vs_currencies=["USD"])
|
2022-01-25 18:39:35 +00:00
|
|
|
|
|
|
|
# Convert the USD total to XMR
|
|
|
|
profit_usd_in_xmr = float(profit_usd) / xmr_usd["monero"]["usd"]
|
|
|
|
|
|
|
|
# Check profit is above zero
|
|
|
|
if not profit_usd >= 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not float(wallet_xmr) > profit_usd_in_xmr:
|
|
|
|
# Not enough funds to withdraw
|
2022-03-06 22:21:33 +00:00
|
|
|
# TODO: Send a message to notify
|
2022-03-05 21:52:31 +00:00
|
|
|
self.log.error(f"Not enough funds to withdraw {profit_usd_in_xmr}, as wallet only contains {wallet_xmr}")
|
2022-01-25 18:57:35 +00:00
|
|
|
self.irc.sendmsg(f"Not enough funds to withdraw {profit_usd_in_xmr}, as wallet only contains {wallet_xmr}")
|
2022-01-25 18:39:35 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if not profit_usd >= float(settings.Money.WithdrawLimit):
|
|
|
|
# Not enough profit to withdraw
|
|
|
|
return
|
|
|
|
|
|
|
|
half = profit_usd_in_xmr / 2
|
|
|
|
|
|
|
|
half_rounded = round(half, 8)
|
|
|
|
|
2022-02-02 13:36:18 +00:00
|
|
|
# Read OTP secret
|
|
|
|
with open("otp.key", "r") as f:
|
|
|
|
otp_key = f.read()
|
|
|
|
f.close()
|
|
|
|
otp_key = otp_key.replace("\n", "")
|
|
|
|
|
|
|
|
# Get OTP code
|
|
|
|
otp_code = TOTP(otp_key)
|
|
|
|
|
2022-01-25 18:39:35 +00:00
|
|
|
# Set up the format for calling wallet_send_xmr
|
|
|
|
send_cast = {
|
|
|
|
"address": None,
|
|
|
|
"amount": half_rounded,
|
|
|
|
"password": settings.Agora.Pass,
|
2022-02-02 13:36:18 +00:00
|
|
|
"otp": otp_code.now(),
|
2022-01-25 18:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
send_cast["address"] = settings.XMR.Wallet1
|
|
|
|
rtrn1 = self.agora.wallet_send_xmr(**send_cast)
|
|
|
|
|
|
|
|
send_cast["address"] = settings.XMR.Wallet2
|
|
|
|
rtrn2 = self.agora.wallet_send_xmr(**send_cast)
|
|
|
|
|
2022-01-25 18:57:35 +00:00
|
|
|
self.irc.sendmsg(f"Withdrawal: {rtrn1['success']} | {rtrn2['success']}")
|
2022-03-04 22:47:06 +00:00
|
|
|
self.ux.notify.notify_withdrawal(half_rounded)
|