pluto/core/clients/platforms/agora.py

122 lines
3.6 KiB
Python
Raw Normal View History

2023-03-09 23:27:16 +00:00
# Other library imports
from pyotp import TOTP
from core.clients.base import BaseClient
from core.clients.platform import LocalPlatformClient
2023-03-13 18:49:47 +00:00
from core.util import logs
log = logs.get_logger("agora")
2023-03-09 23:27:16 +00:00
2023-03-10 00:23:05 +00:00
class AgoraClient(LocalPlatformClient, BaseClient):
2023-03-09 23:27:16 +00:00
"""
AgoraDesk API handler.
"""
async 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
"""
print("CALLING RELEASE FUNDS", contact_id)
if self.instance.dummy:
2023-03-13 18:49:47 +00:00
log.error(f"Running in dummy mode, not releasing funds for {contact_id}")
return {"message": "OK"} # Pretend to succeed
2023-03-13 18:49:47 +00:00
rtrn = await self.api.contact_release(
contact_id,
self.instance.password,
2023-03-09 23:27:16 +00:00
)
return rtrn
# TODO: write test before re-enabling adding total_trades
async def withdraw_funds(self, checks):
2023-03-09 23:27:16 +00:00
"""
Withdraw excess funds to our XMR wallets.
"""
print("CALLING WITHDRAW FUNDS")
# checks = self.check_all()
2023-03-09 23:27:16 +00:00
totals_all = await self.money.get_total()
if totals_all is False:
return False
wallet_xmr, _ = totals_all[2]
# Get the wallet balances in USD
total_usd = totals_all[0][1]
# total_trades_usd = self.tx.get_open_trades_usd()
if not total_usd:
return False
# total_usd += total_trades_usd
profit_usd = total_usd - self.instance.base_usd
2023-03-09 23:27:16 +00:00
# Get the XMR -> USD exchange rate
xmr_usd = self.money.cg.get_price(ids="monero", vs_currencies=["USD"])
# 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
self.log.error(
(
f"Not enough funds to withdraw {profit_usd_in_xmr}, "
f"as wallet only contains {wallet_xmr}"
)
)
self.irc.sendmsg(
(
f"Not enough funds to withdraw {profit_usd_in_xmr}, "
f"as wallet only contains {wallet_xmr}"
)
)
self.ux.notify.notify_need_topup(profit_usd_in_xmr)
return
if not profit_usd >= self.instance.withdrawal_trigger:
2023-03-09 23:27:16 +00:00
# Not enough profit to withdraw
return
half = profit_usd_in_xmr / 2
half_rounded = round(half, 8)
# 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)
# Set up the format for calling wallet_send_xmr
send_cast = {
"address": None,
"amount": half_rounded,
"password": self.instance.password,
2023-03-09 23:27:16 +00:00
"otp": otp_code.now(),
}
2023-03-11 11:51:19 +00:00
print("SENDING", send_cast)
2023-03-09 23:27:16 +00:00
return # TODO
# send_cast["address"] = settings.XMR.Wallet1
# rtrn1 = await self.api.wallet_send_xmr(**send_cast)
2023-03-09 23:27:16 +00:00
# send_cast["address"] = settings.XMR.Wallet2
# rtrn2 = await self.api.wallet_send_xmr(**send_cast)
2023-03-09 23:27:16 +00:00
# self.irc.sendmsg(f"Withdrawal: {rtrn1['success']} | {rtrn2['success']}")
# self.ux.notify.notify_withdrawal(half_rounded)
# await self.successful_withdrawal()