2021-12-24 02:23:38 +00:00
|
|
|
# Other library imports
|
2022-02-02 13:36:18 +00:00
|
|
|
from pyotp import TOTP
|
2021-12-24 02:23:38 +00:00
|
|
|
|
|
|
|
# Project imports
|
|
|
|
from settings import settings
|
2022-02-22 19:53:22 +00:00
|
|
|
import util
|
2022-05-05 11:50:41 +00:00
|
|
|
import sources.local
|
2022-05-02 17:20:50 +00:00
|
|
|
|
2022-01-27 12:08:26 +00:00
|
|
|
|
2022-05-05 11:50:41 +00:00
|
|
|
class Agora(sources.local.Local):
|
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-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-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-05-05 11:50:41 +00:00
|
|
|
if self.sets.Dummy == "1":
|
2022-03-16 18:52:36 +00:00
|
|
|
self.log.error(f"Running in dummy mode, not releasing funds for {contact_id}")
|
|
|
|
return
|
2022-05-05 11:50:41 +00:00
|
|
|
payload = {"tradeId": contact_id, "password": self.sets.Pass}
|
|
|
|
rtrn = self.api._api_call(
|
2022-04-12 21:06:28 +00:00
|
|
|
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
|
|
|
|
|
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-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-05-09 07:24:27 +00:00
|
|
|
self.ux.notify.notify_need_topup(profit_usd_in_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
|
2022-05-05 11:50:41 +00:00
|
|
|
rtrn1 = self.api.wallet_send_xmr(**send_cast)
|
2022-01-25 18:39:35 +00:00
|
|
|
|
|
|
|
send_cast["address"] = settings.XMR.Wallet2
|
2022-05-05 11:50:41 +00:00
|
|
|
rtrn2 = self.api.wallet_send_xmr(**send_cast)
|
2022-01-25 18:39:35 +00:00
|
|
|
|
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)
|