From 96858da88ab021c175f6bcbfc52761b55637cce8 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 10 Mar 2023 15:34:46 +0000 Subject: [PATCH] Refactor and add base USD and withdrawal triggers --- app/urls.py | 2 +- core/clients/platform.py | 19 ++++++++------- core/clients/platforms/agora.py | 20 ++++++++-------- core/forms.py | 4 ++++ ...rm_base_usd_platform_withdrawal_trigger.py | 23 +++++++++++++++++++ core/models.py | 3 +++ core/views/ads.py | 4 ++-- core/views/platforms.py | 4 ---- 8 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 core/migrations/0015_platform_base_usd_platform_withdrawal_trigger.py diff --git a/app/urls.py b/app/urls.py index 5b01f4b..93dca0b 100644 --- a/app/urls.py +++ b/app/urls.py @@ -20,7 +20,7 @@ from django.contrib.auth.views import LogoutView from django.urls import include, path from two_factor.urls import urlpatterns as tf_urls -from core.views import aggregators, banks, base, notifications, platforms, ads +from core.views import ads, aggregators, banks, base, notifications, platforms # from core.views.stripe_callbacks import Callback diff --git a/core/clients/platform.py b/core/clients/platform.py index 1b674c9..e92c3d8 100644 --- a/core/clients/platform.py +++ b/core/clients/platform.py @@ -763,7 +763,7 @@ class LocalPlatformClient(ABC): return all(actioned) async def release_funds(self, trade_id, reference): - stored_trade = await db.get_ref(reference) + # stored_trade = await db.get_ref(reference) logmessage = f"All checks passed, releasing funds for {trade_id} {reference}" log.info(logmessage) title = "Releasing escrow" @@ -782,10 +782,10 @@ class LocalPlatformClient(ABC): await notify.sendmsg(self.instance.user, logmessage, title=title) return - # Parse the escrow release response - message = rtrn["message"] - # message_long = rtrn["response"]["data"]["message"] - self.irc.sendmsg(f"{dumps(message)}") + # # Parse the escrow release response + # message = rtrn["message"] + # # message_long = rtrn["response"]["data"]["message"] + # self.irc.sendmsg(f"{dumps(message)}") async def update_trade_tx(self, reference, txid): """ @@ -1180,7 +1180,8 @@ class LocalPlatformClient(ABC): return False return currency_account_info_map[currency] - # def _distribute_account_details(self, platform, currencies=None, account_info=None): + # def _distribute_account_details(self, platform, currencies= + # None, account_info=None): # """ # Distribute account details for ads. # We will disable ads we can't support. @@ -1200,9 +1201,11 @@ class LocalPlatformClient(ABC): # our_ads = self.enum_ads() - # supported_ads = [ad for ad in our_ads if ad[3] in supported_currencies] + # supported_ads = [ad for ad in our_ads if ad[3] in supported_curr + # encies] - # not_supported_ads = [ad for ad in our_ads if ad[3] not in supported_currencies] + # not_supported_ads = [ad for ad in our_ads if ad[3] not in supporte + # d_currencies] # for ad in supported_ads: # asset = ad[0] diff --git a/core/clients/platforms/agora.py b/core/clients/platforms/agora.py index d690f6e..c778d04 100644 --- a/core/clients/platforms/agora.py +++ b/core/clients/platforms/agora.py @@ -3,7 +3,6 @@ from pyotp import TOTP from core.clients.base import BaseClient from core.clients.platform import LocalPlatformClient -from core.lib.money import Money class AgoraClient(LocalPlatformClient, BaseClient): @@ -57,7 +56,7 @@ class AgoraClient(LocalPlatformClient, BaseClient): return False # total_usd += total_trades_usd - profit_usd = total_usd - float(settings.Money.BaseUSD) + profit_usd = total_usd - self.instance.base_usd # Get the XMR -> USD exchange rate xmr_usd = self.money.cg.get_price(ids="monero", vs_currencies=["USD"]) @@ -85,7 +84,7 @@ class AgoraClient(LocalPlatformClient, BaseClient): self.ux.notify.notify_need_topup(profit_usd_in_xmr) return - if not profit_usd >= float(settings.Money.WithdrawLimit): + if not profit_usd >= self.instance.withdrawal_trigger: # Not enough profit to withdraw return @@ -106,15 +105,16 @@ class AgoraClient(LocalPlatformClient, BaseClient): send_cast = { "address": None, "amount": half_rounded, - "password": settings.Agora.Pass, + "password": self.instance.password, "otp": otp_code.now(), } - send_cast["address"] = settings.XMR.Wallet1 - rtrn1 = await self.api.wallet_send_xmr(**send_cast) + return # TODO + # send_cast["address"] = settings.XMR.Wallet1 + # rtrn1 = await self.api.wallet_send_xmr(**send_cast) - send_cast["address"] = settings.XMR.Wallet2 - rtrn2 = await self.api.wallet_send_xmr(**send_cast) + # send_cast["address"] = settings.XMR.Wallet2 + # rtrn2 = await self.api.wallet_send_xmr(**send_cast) - self.irc.sendmsg(f"Withdrawal: {rtrn1['success']} | {rtrn2['success']}") - self.ux.notify.notify_withdrawal(half_rounded) + # self.irc.sendmsg(f"Withdrawal: {rtrn1['success']} | {rtrn2['success']}") + # self.ux.notify.notify_withdrawal(half_rounded) diff --git a/core/forms.py b/core/forms.py index 8ef4892..ea49d49 100644 --- a/core/forms.py +++ b/core/forms.py @@ -114,6 +114,8 @@ class PlatformForm(RestrictedFormMixin, ModelForm): "max_trade_size_usd", "accept_within_usd", "no_reference_amount_check_max_usd", + "base_usd", + "withdrawal_trigger", "enabled", ) help_texts = { @@ -134,6 +136,8 @@ class PlatformForm(RestrictedFormMixin, ModelForm): "max_trade_size_usd": "The maximum trade size in USD.", "accept_within_usd": "When a trade is wrong by less than this amount, it will be accepted.", "no_reference_amount_check_max_usd": "When ticked, when no reference was found and a trade is higher than this amount, we will not accept payment even if it is the only one with this amount.", + "base_usd": "The amount in USD to keep in the platform.", + "withdrawal_trigger": "The amount above the base USD to trigger a withdrawal.", "enabled": "Whether or not the platform connection is enabled.", } diff --git a/core/migrations/0015_platform_base_usd_platform_withdrawal_trigger.py b/core/migrations/0015_platform_base_usd_platform_withdrawal_trigger.py new file mode 100644 index 0000000..50ab7de --- /dev/null +++ b/core/migrations/0015_platform_base_usd_platform_withdrawal_trigger.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1.7 on 2023-03-10 15:33 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0014_ad_account_whitelist'), + ] + + operations = [ + migrations.AddField( + model_name='platform', + name='base_usd', + field=models.FloatField(default=2800), + ), + migrations.AddField( + model_name='platform', + name='withdrawal_trigger', + field=models.FloatField(default=200), + ), + ] diff --git a/core/models.py b/core/models.py index 06ac9db..d59cb1d 100644 --- a/core/models.py +++ b/core/models.py @@ -123,6 +123,9 @@ class Platform(models.Model): last_messages = models.JSONField(default=dict) platform_ad_ids = models.JSONField(default=dict) + base_usd = models.FloatField(default=2800) + withdrawal_trigger = models.FloatField(default=200) + enabled = models.BooleanField(default=True) def __str__(self): diff --git a/core/views/ads.py b/core/views/ads.py index 2b05ad8..6d27e35 100644 --- a/core/views/ads.py +++ b/core/views/ads.py @@ -6,7 +6,6 @@ from mixins.views import ( ObjectCreate, ObjectDelete, ObjectList, - ObjectRead, ObjectUpdate, ) from two_factor.views.mixins import OTPRequiredMixin @@ -14,9 +13,9 @@ from two_factor.views.mixins import OTPRequiredMixin from core.clients.platforms.agora import AgoraClient from core.forms import AdForm from core.models import Ad -from core.util import logs from core.views.helpers import synchronize_async_helper + class AdNuke(LoginRequiredMixin, OTPRequiredMixin, View): template_name = "mixins/partials/notify.html" @@ -30,6 +29,7 @@ class AdNuke(LoginRequiredMixin, OTPRequiredMixin, View): context = {"class": "success", "message": "Nuking ads"} return render(request, self.template_name, context) + class AdDist(LoginRequiredMixin, OTPRequiredMixin, View): template_name = "mixins/partials/notify.html" diff --git a/core/views/platforms.py b/core/views/platforms.py index 5b49fab..ced181c 100644 --- a/core/views/platforms.py +++ b/core/views/platforms.py @@ -1,8 +1,4 @@ from django.contrib.auth.mixins import LoginRequiredMixin -from django.http import HttpResponse -from django.shortcuts import render -from django.urls import reverse -from django.views import View from mixins.views import ( # ObjectRead, ObjectCreate, ObjectDelete,