Make cheat system async
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
# Twisted/Klein imports
|
||||
from twisted.internet.task import LoopingCall
|
||||
from twisted.internet.threads import deferToThread
|
||||
from twisted.internet.defer import inlineCallbacks
|
||||
|
||||
# Other library imports
|
||||
@@ -61,16 +60,14 @@ class Agora(util.Base):
|
||||
def wrap_dashboard(self, dash=None): # backwards compatibility with TX
|
||||
if not dash:
|
||||
dash = yield self.agora.dashboard_seller()
|
||||
if dash is None:
|
||||
return False
|
||||
if dash is False:
|
||||
return False
|
||||
# if dash["response"] is None:
|
||||
# return False
|
||||
dash_tmp = {}
|
||||
if not dash.items():
|
||||
if not dash:
|
||||
return False
|
||||
if "data" not in dash["response"].keys():
|
||||
if not dash["response"]:
|
||||
return False
|
||||
if "data" not in dash["response"]:
|
||||
# self.log.error(f"Data not in dashboard response: {dash}")
|
||||
return dash_tmp
|
||||
if dash["response"]["data"]["contact_count"] > 0:
|
||||
@@ -182,7 +179,7 @@ class Agora(util.Base):
|
||||
Get recent messages.
|
||||
"""
|
||||
messages_tmp = {}
|
||||
if messages is False:
|
||||
if not messages:
|
||||
return False
|
||||
if not messages["success"]:
|
||||
return False
|
||||
@@ -222,9 +219,9 @@ class Agora(util.Base):
|
||||
|
||||
return messages_tmp
|
||||
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def enum_ad_ids(self, page=0):
|
||||
ads = self.agora._api_call(api_method="ads", query_values={"page": page})
|
||||
ads = yield self.agora._api_call(api_method="ads", query_values={"page": page})
|
||||
if ads is False:
|
||||
return False
|
||||
ads_total = []
|
||||
@@ -235,7 +232,7 @@ class Agora(util.Base):
|
||||
if "pagination" in ads["response"]:
|
||||
if "next" in ads["response"]["pagination"]:
|
||||
page += 1
|
||||
ads_iter = self.enum_ad_ids(page)
|
||||
ads_iter = yield self.enum_ad_ids(page)
|
||||
if ads_iter is None:
|
||||
return False
|
||||
if ads_iter is False:
|
||||
@@ -244,12 +241,12 @@ class Agora(util.Base):
|
||||
ads_total.append(ad)
|
||||
return ads_total
|
||||
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def enum_ads(self, requested_asset=None, page=0):
|
||||
query_values = {"page": page}
|
||||
if requested_asset:
|
||||
query_values["asset"] = requested_asset
|
||||
ads = self.agora._api_call(api_method="ads", query_values=query_values)
|
||||
ads = yield self.agora._api_call(api_method="ads", query_values=query_values)
|
||||
if ads is False:
|
||||
return False
|
||||
ads_total = []
|
||||
@@ -265,7 +262,7 @@ class Agora(util.Base):
|
||||
if "pagination" in ads["response"]:
|
||||
if "next" in ads["response"]["pagination"]:
|
||||
page += 1
|
||||
ads_iter = self.enum_ads(requested_asset, page)
|
||||
ads_iter = yield self.enum_ads(requested_asset, page)
|
||||
if ads_iter is None:
|
||||
return False
|
||||
if ads_iter is False:
|
||||
@@ -274,7 +271,7 @@ class Agora(util.Base):
|
||||
ads_total.append([ad[0], ad[1], ad[2], ad[3], ad[4]])
|
||||
return ads_total
|
||||
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def enum_public_ads(self, asset, currency, providers=None, page=0):
|
||||
to_return = []
|
||||
if asset == "XMR":
|
||||
@@ -285,7 +282,7 @@ class Agora(util.Base):
|
||||
providers = ["REVOLUT"]
|
||||
# buy-monero-online, buy-bitcoin-online
|
||||
# Work around Agora weirdness calling it bitcoins
|
||||
ads = self.agora._api_call(
|
||||
ads = yield self.agora._api_call(
|
||||
api_method=f"buy-{coin}-online/{currency}",
|
||||
query_values={"page": page},
|
||||
)
|
||||
@@ -321,7 +318,7 @@ class Agora(util.Base):
|
||||
if "pagination" in ads["response"]:
|
||||
if "next" in ads["response"]["pagination"]:
|
||||
page += 1
|
||||
ads_iter = self.enum_public_ads(asset, currency, providers, page)
|
||||
ads_iter = yield self.enum_public_ads(asset, currency, providers, page)
|
||||
if ads_iter is None:
|
||||
return False
|
||||
if ads_iter is False:
|
||||
@@ -346,15 +343,16 @@ class Agora(util.Base):
|
||||
else:
|
||||
asset = assets_not_run.pop()
|
||||
self.cheat_run_on.append(asset)
|
||||
deferToThread(self.update_prices, [asset])
|
||||
self.update_prices([asset])
|
||||
return asset
|
||||
else:
|
||||
deferToThread(self.update_prices, assets)
|
||||
# deferToThread(self.update_prices, assets)
|
||||
self.update_prices(assets)
|
||||
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def update_prices(self, assets=None):
|
||||
# Get all public ads for the given assets
|
||||
public_ads = self.get_all_public_ads(assets)
|
||||
public_ads = yield self.get_all_public_ads(assets)
|
||||
if not public_ads:
|
||||
return False
|
||||
|
||||
@@ -363,7 +361,7 @@ class Agora(util.Base):
|
||||
self.slow_ad_update(to_update)
|
||||
|
||||
# TODO: make generic and move to markets
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def get_all_public_ads(self, assets=None, currencies=None, providers=None):
|
||||
"""
|
||||
Get all public ads for our listed currencies.
|
||||
@@ -396,7 +394,7 @@ class Agora(util.Base):
|
||||
except KeyError:
|
||||
# self.log.error("Error getting public ads for currency {currency}", currency=currency)
|
||||
continue
|
||||
ads_list = self.enum_public_ads(asset, currency, providers)
|
||||
ads_list = yield self.enum_public_ads(asset, currency, providers)
|
||||
if not ads_list:
|
||||
continue
|
||||
ads = self.money.lookup_rates(self.platform, ads_list, rates=rates)
|
||||
@@ -430,6 +428,7 @@ class Agora(util.Base):
|
||||
cast["market"] = self.platform
|
||||
self.es.index(index=settings.ES.MetaIndex, document=cast)
|
||||
|
||||
@inlineCallbacks
|
||||
def slow_ad_update(self, ads):
|
||||
"""
|
||||
Slow ad equation update utilising exponential backoff in order to guarantee all ads are updated.
|
||||
@@ -445,7 +444,7 @@ class Agora(util.Base):
|
||||
assets.add(asset)
|
||||
currencies.add(currency)
|
||||
if not actioned:
|
||||
rtrn = self.agora.ad_equation(ad_id, new_formula)
|
||||
rtrn = yield self.agora.ad_equation(ad_id, new_formula)
|
||||
if rtrn["success"]:
|
||||
ads[ad_index][4] = True
|
||||
throttled = 0
|
||||
@@ -674,7 +673,6 @@ class Agora(util.Base):
|
||||
if not total_usd:
|
||||
return False
|
||||
# total_usd += total_trades_usd
|
||||
# print("total_usd after trades add", total_usd)
|
||||
|
||||
profit_usd = total_usd - float(settings.Money.BaseUSD)
|
||||
# Get the XMR -> USD exchange rate
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
# Twisted/Klein imports
|
||||
from twisted.internet.task import LoopingCall
|
||||
from twisted.internet.threads import deferToThread
|
||||
from twisted.internet.defer import inlineCallbacks
|
||||
|
||||
# Other library imports
|
||||
@@ -61,16 +60,14 @@ class LBTC(util.Base):
|
||||
def wrap_dashboard(self, dash=None): # backwards compatibility with TX
|
||||
if not dash:
|
||||
dash = yield self.lbtc.dashboard() # no dashboard_seller for lbtc
|
||||
if dash is None:
|
||||
return False
|
||||
if dash is False:
|
||||
return False
|
||||
# if dash["response"] is None:
|
||||
# return False
|
||||
dash_tmp = {}
|
||||
if not dash.items():
|
||||
if not dash:
|
||||
return False
|
||||
if "data" not in dash["response"].keys():
|
||||
if not dash["response"]:
|
||||
return False
|
||||
if "data" not in dash["response"]:
|
||||
self.log.error(f"Data not in dashboard response: {dash}")
|
||||
return dash_tmp
|
||||
if dash["response"]["data"]["contact_count"] > 0:
|
||||
@@ -178,10 +175,12 @@ class LBTC(util.Base):
|
||||
Get recent messages.
|
||||
"""
|
||||
messages_tmp = {}
|
||||
if messages is False:
|
||||
if not messages:
|
||||
return False
|
||||
if not messages["success"]:
|
||||
return False
|
||||
if not messages["response"]:
|
||||
return False
|
||||
if "data" not in messages["response"]:
|
||||
self.log.error(f"Data not in messages response: {messages['response']}")
|
||||
return False
|
||||
@@ -218,9 +217,9 @@ class LBTC(util.Base):
|
||||
|
||||
return messages_tmp
|
||||
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def enum_ad_ids(self, page=1):
|
||||
ads = self.lbtc._api_call(api_method="api/ads/", query_values={"page": page})
|
||||
ads = yield self.lbtc._api_call(api_method="api/ads/", query_values={"page": page})
|
||||
if ads is False:
|
||||
return False
|
||||
ads_total = []
|
||||
@@ -231,7 +230,7 @@ class LBTC(util.Base):
|
||||
if "pagination" in ads["response"]:
|
||||
if "next" in ads["response"]["pagination"]:
|
||||
page += 1
|
||||
ads_iter = self.enum_ad_ids(page)
|
||||
ads_iter = yield self.enum_ad_ids(page)
|
||||
if ads_iter is None:
|
||||
return False
|
||||
if ads_iter is False:
|
||||
@@ -240,12 +239,12 @@ class LBTC(util.Base):
|
||||
ads_total.append(ad)
|
||||
return ads_total
|
||||
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def enum_ads(self, requested_asset=None, page=1):
|
||||
query_values = {"page": page}
|
||||
if requested_asset:
|
||||
query_values["asset"] = requested_asset
|
||||
ads = self.lbtc._api_call(api_method="api/ads/", query_values=query_values)
|
||||
ads = yield self.lbtc._api_call(api_method="api/ads/", query_values=query_values)
|
||||
if ads is False:
|
||||
return False
|
||||
ads_total = []
|
||||
@@ -261,7 +260,7 @@ class LBTC(util.Base):
|
||||
if "pagination" in ads["response"]:
|
||||
if "next" in ads["response"]["pagination"]:
|
||||
page += 1
|
||||
ads_iter = self.enum_ads(requested_asset, page)
|
||||
ads_iter = yield self.enum_ads(requested_asset, page)
|
||||
if ads_iter is None:
|
||||
return False
|
||||
if ads_iter is False:
|
||||
@@ -283,19 +282,19 @@ class LBTC(util.Base):
|
||||
except KeyError:
|
||||
return False
|
||||
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def enum_public_ads(self, asset, currency, providers=None, page=1):
|
||||
to_return = []
|
||||
if not providers:
|
||||
providers = ["NATIONAL_BANK"]
|
||||
if len(providers) == 1:
|
||||
provider = providers[0]
|
||||
ads = self.lbtc._api_call(
|
||||
ads = yield self.lbtc._api_call(
|
||||
api_method=f"buy-bitcoins-online/{currency}/{provider}/.json",
|
||||
query_values={"page": page},
|
||||
)
|
||||
else:
|
||||
ads = self.lbtc._api_call(
|
||||
ads = yield self.lbtc._api_call(
|
||||
api_method=f"buy-bitcoins-online/{currency}/.json",
|
||||
query_values={"page": page},
|
||||
)
|
||||
@@ -335,7 +334,7 @@ class LBTC(util.Base):
|
||||
if "pagination" in ads["response"]:
|
||||
if "next" in ads["response"]["pagination"]:
|
||||
page += 1
|
||||
ads_iter = self.enum_public_ads(asset, currency, providers, page)
|
||||
ads_iter = yield self.enum_public_ads(asset, currency, providers, page)
|
||||
if ads_iter is None:
|
||||
return False
|
||||
if ads_iter is False:
|
||||
@@ -360,15 +359,15 @@ class LBTC(util.Base):
|
||||
else:
|
||||
asset = assets_not_run.pop()
|
||||
self.cheat_run_on.append(asset)
|
||||
deferToThread(self.update_prices, [asset])
|
||||
self.update_prices([asset])
|
||||
return asset
|
||||
else:
|
||||
deferToThread(self.update_prices, assets)
|
||||
self.update_prices(assets)
|
||||
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def update_prices(self, assets=None):
|
||||
# Get all public ads for the given assets
|
||||
public_ads = self.get_all_public_ads(assets)
|
||||
public_ads = yield self.get_all_public_ads(assets)
|
||||
if not public_ads:
|
||||
return False
|
||||
|
||||
@@ -377,7 +376,7 @@ class LBTC(util.Base):
|
||||
self.slow_ad_update(to_update)
|
||||
|
||||
# TODO: make generic and move to markets
|
||||
@util.handle_exceptions
|
||||
@inlineCallbacks
|
||||
def get_all_public_ads(self, assets=None, currencies=None, providers=None):
|
||||
"""
|
||||
Get all public ads for our listed currencies.
|
||||
@@ -409,7 +408,7 @@ class LBTC(util.Base):
|
||||
except KeyError:
|
||||
# self.log.error("Error getting public ads for currency {currency}", currency=currency)
|
||||
continue
|
||||
ads_list = self.enum_public_ads(asset, currency, providers)
|
||||
ads_list = yield self.enum_public_ads(asset, currency, providers)
|
||||
if not ads_list:
|
||||
continue
|
||||
ads = self.money.lookup_rates(self.platform, ads_list, rates=rates)
|
||||
@@ -442,6 +441,7 @@ class LBTC(util.Base):
|
||||
cast["market"] = "localbitcoins"
|
||||
self.es.index(index=settings.ES.MetaIndex, document=cast)
|
||||
|
||||
@inlineCallbacks
|
||||
def slow_ad_update(self, ads):
|
||||
"""
|
||||
Slow ad equation update utilising exponential backoff in order to guarantee all ads are updated.
|
||||
@@ -457,7 +457,7 @@ class LBTC(util.Base):
|
||||
assets.add(asset)
|
||||
currencies.add(currency)
|
||||
if not actioned:
|
||||
rtrn = self.lbtc.ad_equation(ad_id, new_formula)
|
||||
rtrn = yield self.lbtc.ad_equation(ad_id, new_formula)
|
||||
if rtrn["success"]:
|
||||
ads[ad_index][4] = True
|
||||
throttled = 0
|
||||
@@ -685,7 +685,6 @@ class LBTC(util.Base):
|
||||
if not total_usd:
|
||||
return False
|
||||
# total_usd += total_trades_usd
|
||||
# print("total_usd after trades add", total_usd)
|
||||
|
||||
profit_usd = total_usd - float(settings.Money.BaseUSD)
|
||||
# Get the XMR -> USD exchange rate
|
||||
|
||||
Reference in New Issue
Block a user