Libraries refactor and add some sinks #4

Closed
m wants to merge 136 commits from library-refactor into master
2 changed files with 78 additions and 26 deletions
Showing only changes of commit d11745319d - Show all commits

View File

@ -56,6 +56,9 @@ class Agora(object):
# Cache for detecting new messages
self.last_messages = {}
# Assets that cheat has been run on
self.cheat_run_on = []
def set_irc(self, irc):
self.irc = irc
@ -69,15 +72,19 @@ class Agora(object):
self.lc_dash = LoopingCall(self.loop_check)
self.lc_dash.start(int(settings.Agora.RefreshSec))
if settings.Agora.Cheat == "1":
self.lc_cheat = LoopingCall(self._update_prices)
self.lc_cheat = LoopingCall(self._update_prices, None, None)
self.lc_cheat.start(int(settings.Agora.CheatSec))
@handle_exceptions
def wrap_dashboard(self):
dash = self.agora.dashboard_seller()
if dash is None:
return False
if dash is False:
return False
dash_tmp = {}
if not dash.items():
return False
if "data" not in dash["response"].keys():
self.log.error("Data not in dashboard response: {content}", content=dash)
return dash_tmp
@ -271,10 +278,12 @@ class Agora(object):
return ads_total
@handle_exceptions
def enum_ads(self, page=0):
ads = self.agora._api_call(api_method="ads", query_values={"page": page})
def enum_ads(self, asset=None, page=0):
query_values = {"page": page}
if asset:
query_values["asset"] = asset
ads = self.agora._api_call(api_method="ads", query_values=query_values)
if ads is False:
print("enum ads is false for", page)
return False
ads_total = []
if not ads["success"]:
@ -284,13 +293,10 @@ class Agora(object):
if "pagination" in ads["response"]:
if "next" in ads["response"]["pagination"]:
page += 1
ads_iter = self.enum_ads(page)
print("ADS ITER", ads_iter)
ads_iter = self.enum_ads(asset, page)
if ads_iter is None:
print("enum ads iter is none for", page)
return False
if ads_iter is False:
print("enum ads iter is false for", page)
return False
for ad in ads_iter:
ads_total.append([ad[0], ad[1], ad[2], ad[3]])
@ -318,10 +324,10 @@ class Agora(object):
coin = "bitcoins"
ads = self.agora._api_call(api_method=f"buy-{coin}-online/{currency}/REVOLUT", query_values={"page": page})
if ads is None:
print("public ads is none for", coin, currency)
return False
if ads is False:
print("public ads is false for", coin, currency)
return False
if "data" not in ads["response"]:
return False
for ad in ads["response"]["data"]["ad_list"]:
if ad["data"]["online_provider"] == "REVOLUT":
@ -335,10 +341,8 @@ class Agora(object):
page += 1
ads_iter = self.enum_public_ads(coin, currency, page)
if ads_iter is None:
print("public ads iter is false for", coin, currency, page)
return False
if ads_iter is False:
print("public ads iter is none for", coin, currency, page)
return False
for ad in ads_iter:
yield [ad[0], ad[1], ad[2]]
@ -353,11 +357,9 @@ class Agora(object):
coin = "bitcoin"
ads_obj = self.enum_public_ads(coin, currency.upper())
if ads_obj is False:
print("wrap public ads is false for", asset, currency)
return False
ads = list(ads_obj)
if ads_obj is False:
print("wrap public ads is false2 for", asset, currency)
return False
if not rates:
# Set the price based on the asset
@ -370,21 +372,48 @@ class Agora(object):
ad.append(rate)
return ads
def _update_prices(self, xmr=True, btc=True):
def _update_prices(self, xmr=None, btc=None):
"""
Update prices in another thread.
"""
deferToThread(self.update_prices, xmr, btc)
if xmr is None and btc is None:
all_assets = loads(settings.Agora.AssetList)
assets_not_run = set(all_assets) ^ set(self.cheat_run_on)
if not assets_not_run:
print("TOP")
self.cheat_run_on = []
asset = list(all_assets).pop()
self.cheat_run_on.append(asset)
else:
print("BOTTOM")
asset = assets_not_run.pop()
self.cheat_run_on.append(asset)
print("end cheat", self.cheat_run_on)
if asset == "XMR":
deferToThread(self.update_prices, True, False) # XMR, BTC
print("Running cheat on XMR")
elif asset == "BTC":
deferToThread(self.update_prices, False, True) # XMR, BTC
print("Running cheat on BTC")
return asset
else:
print("Running cheat normally")
deferToThread(self.update_prices, xmr, btc)
@handle_exceptions
def update_prices(self, xmr=True, btc=True):
our_ads = self.enum_ads()
if xmr and btc:
our_ads = self.enum_ads()
elif xmr and not btc:
our_ads = self.enum_ads("XMR")
elif not xmr and btc:
our_ads = self.enum_ads("BTC")
self.log.debug("Our ads: {ads}", ads=our_ads)
to_update = []
if our_ads is None:
print("our ads is none for", xmr, btc)
return False
if our_ads is False:
print("our ads is false for", xmr, btc)
return False
currencies = [x[3].lower() for x in our_ads]
public_ad_dict_xmr = {}
@ -397,12 +426,10 @@ class Agora(object):
if xmr:
public_ad_dict_xmr[currency] = self.wrap_public_ads("XMR", currency, rates=rates_xmr)
if public_ad_dict_xmr[currency] is False:
print("public ad dict xmr is false for", currency)
return False
if btc:
public_ad_dict_btc[currency] = self.wrap_public_ads("BTC", currency, rates=rates_btc)
if public_ad_dict_btc[currency] is False:
print("public ad dict btc is false for", currency)
return False
except KeyError:
self.log.error("Error getting public ads for currency {currency}", currency=currency)
@ -443,7 +470,7 @@ class Agora(object):
# Don't waste API rate limits on setting the same margin as before
if new_margin != our_margin:
# rtrn = self.agora.ad_equation(ad_id, new_formula)
to_update.append([ad_id, new_formula, False])
to_update.append([ad_id, new_formula, asset, False])
# if not rtrn["success"]:
# self.log.error("Error updating ad {ad_id}: {response}", ad_id=ad_id, response=rtrn["response"])
self.log.info("Rate for {currency}: {margin}", currency=currency, margin=new_margin)
@ -459,9 +486,12 @@ class Agora(object):
self.log.info("Beginning slow ad update for {num} ads", num=len(ads))
iterations = 0
throttled = 0
assets = set()
while not all([x[2] for x in ads]) or iterations == 1000:
for ad_index in range(len(ads)):
ad_id, new_formula, actioned = ads[ad_index]
ad_id, new_formula, asset, actioned = ads[ad_index]
assets.add(asset)
self.log.error("ASSET {a}", a=asset)
if not actioned:
rtrn = self.agora.ad_equation(ad_id, new_formula)
if rtrn["success"]:
@ -484,8 +514,12 @@ class Agora(object):
self.log.error("Error updating ad {ad_id}: {response}", ad_id=ad_id, response=rtrn["response"])
continue
iterations += 1
self.log.info("Slow ad update completed with {iterations} iterations", iterations=iterations)
self.irc.sendmsg(f"Slow ad update completed with {iterations} iterations")
if iterations == 0:
self.log.info("Slow ad update finished, no ads to update")
self.irc.sendmsg("Slow ad update finished, no ads to update")
else:
self.log.info("Slow ad update completed with {iterations} iterations: [{assets}]", iterations=iterations, assets=assets)
self.irc.sendmsg(f"Slow ad update completed with {iterations} iterations: [{assets}]")
def autoprice(self, ads, currency):
"""
@ -538,6 +572,9 @@ class Agora(object):
if lowball_lowest_not_ours < float(settings.Agora.MinMargin):
self.log.debug("Lowball lowest not ours less than MinMargin")
return float(settings.Agora.MinMargin)
elif lowball_lowest_not_ours > float(settings.Agora.MaxMargin):
self.log.debug("Lowball lowest not ours more than MaxMargin")
return float(settings.Agora.MaxMargin)
else:
self.log.debug("Returning lowballed figure: {x}", x=lowball_lowest_not_ours)
return lowball_lowest_not_ours
@ -550,8 +587,12 @@ class Agora(object):
return float(settings.Agora.MaxMargin)
# Find cheapest ad above our min that is not us
cheapest_ad = min(ads_above_our_min_not_us, key=lambda x: x[3])
cheapest_ad_margin = cheapest_ad[3] - 0.001
if cheapest_ad_margin > float(settings.Agora.MaxMargin):
self.log.debug("Cheapest ad not ours more than MaxMargin")
return float(settings.Agora.MaxMargin)
self.log.debug("Cheapest ad above our min that is not us: {x}", x=cheapest_ad)
return cheapest_ad[3] - 0.001
return cheapest_ad_margin
@handle_exceptions
def nuke_ads(self):

View File

@ -363,6 +363,17 @@ class IRCCommands(object):
agora._update_prices(xmr=xmr, btc=btc)
msg(f"Running cheat in thread for {asset}.")
class cheatnext(object):
name = "cheatnext"
authed = True
helptext = "Run the next currency for cheat."
@staticmethod
def run(cmd, spl, length, authed, msg, agora, revolut, tx):
if length == 1:
asset = agora._update_prices(None, None)
msg(f"Running next asset for cheat in thread: {asset}")
class ads(object):
name = "ads"
authed = True