2023-03-05 20:09:31 +00:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
|
|
from django.core.management.base import BaseCommand
|
2023-05-05 14:11:15 +00:00
|
|
|
from pyotp import TOTP
|
2023-03-05 20:09:31 +00:00
|
|
|
|
2023-03-09 16:44:16 +00:00
|
|
|
from core.clients.aggregators.nordigen import NordigenClient
|
2023-03-11 17:43:10 +00:00
|
|
|
from core.clients.platforms.agora import AgoraClient
|
2023-05-05 13:39:02 +00:00
|
|
|
from core.lib.money import Money
|
|
|
|
from core.lib.notify import sendmsg
|
2023-05-06 10:52:42 +00:00
|
|
|
from core.models import (
|
|
|
|
INTERVAL_CHOICES,
|
|
|
|
Aggregator,
|
|
|
|
LinkGroup,
|
|
|
|
Payout,
|
|
|
|
Platform,
|
|
|
|
Requisition,
|
|
|
|
)
|
2023-03-05 20:09:31 +00:00
|
|
|
from core.util import logs
|
|
|
|
|
|
|
|
log = logs.get_logger("scheduling")
|
|
|
|
|
2023-03-20 14:10:31 +00:00
|
|
|
INTERVAL_AGGREGATOR = 10
|
2023-05-05 13:39:02 +00:00
|
|
|
INTERVAL_WITHDRAWAL = 7200
|
2023-03-05 20:09:31 +00:00
|
|
|
|
2023-03-11 17:43:10 +00:00
|
|
|
INTERVALS_PLATFORM = [x[0] for x in INTERVAL_CHOICES]
|
2023-03-05 20:09:31 +00:00
|
|
|
|
2023-03-11 17:43:10 +00:00
|
|
|
|
2023-05-05 13:39:02 +00:00
|
|
|
async def withdrawal_job(group=None):
|
|
|
|
money = Money()
|
|
|
|
if group is not None:
|
|
|
|
groups = [group]
|
|
|
|
else:
|
|
|
|
groups = LinkGroup.objects.filter(enabled=True)
|
|
|
|
for group in groups:
|
|
|
|
checks = await money.check_all(
|
2023-05-06 10:12:54 +00:00
|
|
|
link_group=group, nordigen=NordigenClient, agora=AgoraClient
|
2023-05-05 13:39:02 +00:00
|
|
|
)
|
2023-05-06 10:52:42 +00:00
|
|
|
|
|
|
|
if checks["total_remaining"] > 0:
|
|
|
|
# More than 0 remaining, so we can't withdraw
|
|
|
|
await sendmsg(
|
|
|
|
group.user,
|
|
|
|
f"{checks['total_remaining']} left until you can withdraw.",
|
|
|
|
title="Balance update",
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2023-05-05 13:58:58 +00:00
|
|
|
print("CHECKS", checks)
|
2023-05-05 13:39:02 +00:00
|
|
|
aggregators = Aggregator.objects.filter(
|
|
|
|
user=group.user,
|
|
|
|
link_group=group,
|
|
|
|
)
|
|
|
|
platforms = Platform.objects.filter(
|
|
|
|
user=group.user,
|
|
|
|
link_group=group,
|
|
|
|
)
|
|
|
|
requisitions = Requisition.objects.filter(
|
|
|
|
user=group.user,
|
|
|
|
aggregator__in=aggregators,
|
|
|
|
)
|
|
|
|
pay_list = money.get_pay_list(
|
|
|
|
group,
|
|
|
|
requisitions,
|
|
|
|
platforms,
|
|
|
|
group.user,
|
2023-05-05 13:58:58 +00:00
|
|
|
checks["total_profit_in_xmr"],
|
2023-05-05 13:39:02 +00:00
|
|
|
)
|
|
|
|
collapsed = money.collapse_pay_list(pay_list)
|
|
|
|
if any(collapsed.values()):
|
|
|
|
message = ""
|
|
|
|
print("COLLAPSED", collapsed)
|
|
|
|
for wallet, amount in collapsed.items():
|
|
|
|
print("ITER", wallet, amount)
|
|
|
|
message += f"{wallet}: {amount}\n"
|
|
|
|
print("MESSAGE", message)
|
|
|
|
await sendmsg(
|
|
|
|
group.user,
|
|
|
|
message,
|
|
|
|
title="Your withdrawal is ready!",
|
|
|
|
)
|
|
|
|
|
2023-05-06 10:52:42 +00:00
|
|
|
# TODO: UNCOMMENT
|
|
|
|
# COMMENTED FOR TESTING
|
|
|
|
# if not checks["total_profit_in_xmr"] >= 0:
|
|
|
|
# return
|
2023-05-05 14:11:15 +00:00
|
|
|
|
|
|
|
total_withdrawal = sum(collapsed.values())
|
|
|
|
if checks["total_xmr_agora"] < total_withdrawal:
|
|
|
|
await sendmsg(
|
|
|
|
group.user,
|
|
|
|
(
|
|
|
|
f"Attempting to withdraw {total_withdrawal}, but you only have"
|
|
|
|
f" {checks['total_xmr_agora']} in your Agora wallet."
|
|
|
|
),
|
|
|
|
title="Withdrawal failed",
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
if group.platforms.count() != 1:
|
|
|
|
raise Exception("You can only have one platform per group")
|
|
|
|
|
|
|
|
platform = group.platforms.first()
|
|
|
|
# run = await AgoraClient(platform)
|
|
|
|
otp_code = TOTP(platform.otp_token).now()
|
|
|
|
|
2023-05-06 10:52:42 +00:00
|
|
|
for wallet, pay_list_iter in pay_list.items():
|
|
|
|
for amount, reason in pay_list_iter:
|
|
|
|
print("ITER", wallet, pay_list_iter)
|
|
|
|
print("ITER SENT", wallet, amount, reason)
|
|
|
|
|
|
|
|
# for wallet, amount in collapsed.items():
|
|
|
|
print("ITER SEND", wallet, amount)
|
|
|
|
cast = {
|
|
|
|
"address": wallet.address,
|
|
|
|
"amount": amount,
|
|
|
|
"password": platform.password,
|
|
|
|
"otp": otp_code,
|
|
|
|
}
|
|
|
|
|
|
|
|
print("CAST ADDRESS", cast["address"])
|
|
|
|
print("CAST AMOUNT", cast["amount"])
|
|
|
|
print("CAST OTP TRUNCATED BY 2", cast["otp"][-2])
|
|
|
|
|
|
|
|
# TODO: UNCOMMENT
|
|
|
|
# sent = await run.call("wallet_send_xmr", **cast)
|
|
|
|
# print("SENT", sent)
|
|
|
|
|
|
|
|
payout = Payout.objects.create( # noqa
|
|
|
|
user=group.user,
|
|
|
|
wallet=wallet,
|
|
|
|
amount=amount,
|
|
|
|
description=reason,
|
|
|
|
)
|
|
|
|
|
|
|
|
# TODO: UNCOMMENT
|
|
|
|
# payout.response = sent
|
|
|
|
# payout.save()
|
2023-05-05 14:11:15 +00:00
|
|
|
|
2023-05-05 13:39:02 +00:00
|
|
|
|
2023-03-11 17:43:10 +00:00
|
|
|
async def aggregator_job():
|
2023-03-13 18:49:47 +00:00
|
|
|
aggregators = Aggregator.objects.filter(enabled=True)
|
2023-03-09 16:44:16 +00:00
|
|
|
for aggregator in aggregators:
|
2023-04-20 07:25:50 +00:00
|
|
|
open_trade_currencies = aggregator.trades_currencies
|
2023-03-09 16:44:16 +00:00
|
|
|
if aggregator.service == "nordigen":
|
2023-04-20 07:25:50 +00:00
|
|
|
instance = None
|
2023-03-13 18:49:47 +00:00
|
|
|
if aggregator.fetch_accounts is True:
|
2023-04-18 08:32:57 +00:00
|
|
|
aggregator.account_info = {}
|
|
|
|
aggregator.save()
|
2023-04-20 07:25:50 +00:00
|
|
|
instance = await NordigenClient(aggregator)
|
2023-03-13 18:49:47 +00:00
|
|
|
await instance.get_all_account_info(store=True)
|
|
|
|
|
2023-04-20 07:25:50 +00:00
|
|
|
# fetch_tasks = []
|
2023-03-13 18:49:47 +00:00
|
|
|
for bank, accounts in aggregator.account_info.items():
|
|
|
|
for account in accounts:
|
|
|
|
account_id = account["account_id"]
|
2023-03-20 13:54:56 +00:00
|
|
|
requisition_id = account["requisition_id"]
|
2023-04-20 07:25:50 +00:00
|
|
|
if account["currency"] not in open_trade_currencies:
|
|
|
|
continue # Next account
|
|
|
|
# Avoid hammering the API with new access token requests
|
|
|
|
if instance is None:
|
|
|
|
instance = await NordigenClient(aggregator)
|
|
|
|
# task = instance.get_transactions(
|
|
|
|
# account_id, req=requisition_id, process=True
|
|
|
|
# )
|
|
|
|
await instance.get_transactions(
|
2023-03-20 13:54:56 +00:00
|
|
|
account_id, req=requisition_id, process=True
|
|
|
|
)
|
2023-04-20 07:25:50 +00:00
|
|
|
# fetch_tasks.append(task)
|
|
|
|
# await asyncio.gather(*fetch_tasks)
|
2023-03-09 16:44:16 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError(f"No such client library: {aggregator.service}")
|
|
|
|
aggregator.fetch_accounts = False
|
|
|
|
aggregator.save()
|
2023-03-05 20:09:31 +00:00
|
|
|
|
|
|
|
|
2023-03-11 17:43:10 +00:00
|
|
|
async def platform_job(interval):
|
|
|
|
if interval == 0:
|
|
|
|
return
|
|
|
|
platforms = Platform.objects.filter(enabled=True, cheat_interval_seconds=interval)
|
|
|
|
for platform in platforms:
|
|
|
|
if platform.service == "agora":
|
|
|
|
if platform.cheat is True:
|
|
|
|
instance = await AgoraClient(platform)
|
|
|
|
await instance.cheat()
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(f"No such client library: {platform.service}")
|
|
|
|
|
|
|
|
|
2023-03-05 20:09:31 +00:00
|
|
|
class Command(BaseCommand):
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
"""
|
|
|
|
Start the scheduling process.
|
|
|
|
"""
|
|
|
|
scheduler = AsyncIOScheduler()
|
|
|
|
|
2023-03-11 17:43:10 +00:00
|
|
|
log.debug(f"Scheduling {INTERVAL_AGGREGATOR} second aggregator job")
|
|
|
|
scheduler.add_job(aggregator_job, "interval", seconds=INTERVAL_AGGREGATOR)
|
2023-05-05 13:39:02 +00:00
|
|
|
|
|
|
|
log.debug(f"Scheduling {INTERVAL_WITHDRAWAL} second withdrawal job")
|
|
|
|
scheduler.add_job(withdrawal_job, "interval", seconds=INTERVAL_WITHDRAWAL)
|
|
|
|
|
2023-03-11 17:43:10 +00:00
|
|
|
for interval in INTERVALS_PLATFORM:
|
|
|
|
if interval == 0:
|
|
|
|
continue
|
|
|
|
log.debug(f"Scheduling {interval} second platform job")
|
|
|
|
scheduler.add_job(
|
|
|
|
platform_job, "interval", seconds=interval, args=[interval]
|
|
|
|
)
|
2023-03-05 20:09:31 +00:00
|
|
|
scheduler.start()
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
try:
|
|
|
|
loop.run_forever()
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
log.info("Process terminating")
|
|
|
|
finally:
|
|
|
|
loop.close()
|