From 0477e55361a5aaef24973b5548ba536f03c72df0 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 11 Mar 2023 11:51:19 +0000 Subject: [PATCH] Fix formatting issues --- core/clients/aggregator.py | 35 +++++++------ core/clients/platforms/agora.py | 1 + core/clients/platforms/api/agoradesk.py | 2 - core/lib/antifraud.py | 21 ++++---- core/lib/money.py | 68 +++++++++++-------------- core/management/commands/polling.py | 2 +- core/views/aggregators.py | 1 - core/views/banks.py | 4 +- core/views/profit.py | 33 +++++------- handler/lib/money.py | 2 +- 10 files changed, 79 insertions(+), 90 deletions(-) diff --git a/core/clients/aggregator.py b/core/clients/aggregator.py index 32abef4..c851c80 100644 --- a/core/clients/aggregator.py +++ b/core/clients/aggregator.py @@ -1,5 +1,7 @@ from abc import ABC +import orjson + from core.lib import db, notify # from core.lib.money import money @@ -159,19 +161,20 @@ class AggregatorClient(ABC): return None return stored_trade_reference.pop() - async def can_alt_lookup(self, amount, currency, reference): - amount_usd = self.money.to_usd(amount, currency) - # Amount is reliable here as it is checked by find_trade, - # so no need for stored_trade["amount"] - if float(amount_usd) > float(settings.Agora.AcceptableAltLookupUSD): - message = ( - f"Amount exceeds max for {reference}" - f"Currency: {currency} | Amount: {amount}" - ) - title = "Amount exceeds max for {reference}" - await notify.sendmsg(self.instance.user, message, title=title) - return False - return True + # TODO: pass platform here + # async def can_alt_lookup(self, amount, currency, reference): + # amount_usd = self.money.to_usd(amount, currency) + # # Amount is reliable here as it is checked by find_trade, + # # so no need for stored_trade["amount"] + # if float(amount_usd) > float(settings.Agora.AcceptableAltLookupUSD): + # message = ( + # f"Amount exceeds max for {reference}" + # f"Currency: {currency} | Amount: {amount}" + # ) + # title = "Amount exceeds max for {reference}" + # await notify.sendmsg(self.instance.user, message, title=title) + # return False + # return True async def amount_currency_lookup(self, amount, currency, txid, reference): log.info(f"No reference in DB refs for {reference}") @@ -288,7 +291,7 @@ class AggregatorClient(ABC): } db.r.hmset(f"tx.{txid}", to_store) - log.info(f"Transaction processed: {dumps(to_store, indent=2)}") + log.info(f"Transaction processed: {orjson.dumps(to_store, indent=2)}") self.irc.sendmsg( ( f"AUTO Incoming transaction on {subclass}: {txid} {amount}{currency} " @@ -347,10 +350,10 @@ class AggregatorClient(ABC): ): return platform = stored_trade["subclass"] - platform_buyer = stored_trade["buyer"] + # platform_buyer = stored_trade["buyer"] # Check sender - we don't do anything with this yet - # sender_valid = self.antifraud.check_valid_sender( + # sender_valid = antifraud.check_valid_sender( # reference, platform, sender, platform_buyer # ) # log.info(f"Trade {reference} buyer {platform_buyer} valid: {sender_valid}") diff --git a/core/clients/platforms/agora.py b/core/clients/platforms/agora.py index c778d04..012ad48 100644 --- a/core/clients/platforms/agora.py +++ b/core/clients/platforms/agora.py @@ -108,6 +108,7 @@ class AgoraClient(LocalPlatformClient, BaseClient): "password": self.instance.password, "otp": otp_code.now(), } + print("SENDING", send_cast) return # TODO # send_cast["address"] = settings.XMR.Wallet1 diff --git a/core/clients/platforms/api/agoradesk.py b/core/clients/platforms/api/agoradesk.py index e992787..3fe913e 100644 --- a/core/clients/platforms/api/agoradesk.py +++ b/core/clients/platforms/api/agoradesk.py @@ -90,8 +90,6 @@ class AgoraDesk: status_code = response_raw.status else: - # response = httpx.get(url=api_call_url, headers=headers, params=query_values) - # response = treq.get(api_call_url, headers=headers, params=query_values) cast["params"] = query_values async with aiohttp.ClientSession() as session: async with session.get(api_call_url, **cast) as response_raw: diff --git a/core/lib/antifraud.py b/core/lib/antifraud.py index aaaaa5e..791b1c7 100644 --- a/core/lib/antifraud.py +++ b/core/lib/antifraud.py @@ -1,5 +1,5 @@ # Project imports -from core.lib import db, notify +from core.lib import db # , notify from core.util import logs log = logs.get_logger("antifraud") @@ -53,19 +53,19 @@ class AntiFraud(object): self.ux.notify.notify_sender_name_mismatch( reference, platform_buyer, bank_sender ) - title = "Sender name mismatch" - message = ( - f"Sender name mismatch for {reference}:\n" - f"Platform buyer: {platform_buyer}" - f"Bank sender: {bank_sender}" - ) + # title = "Sender name mismatch" + # message = ( + # f"Sender name mismatch for {reference}:\n" + # f"Platform buyer: {platform_buyer}" + # f"Bank sender: {bank_sender}" + # ) # await notify.sendmsg(self.instance.) # TODO return False async def check_tx_sender(self, tx, reference): """ - Check whether the sender of a given transaction is authorised based on the previous - transactions of the username that originated the trade reference. + Check whether the sender of a given transaction is authorised based on the + previous transactions of the username that originated the trade reference. :param tx: the transaction ID :param reference: the trade reference """ @@ -103,7 +103,8 @@ class AntiFraud(object): # auth_url = auth_url.replace("https://", "") # hack # post_message( # trade_id, - # f"Hi! To continue the trade, please complete the verification form: {auth_url}", + # f"Hi! To continue the trade, please complete the verification form: + # {auth_url}", # ) diff --git a/core/lib/money.py b/core/lib/money.py index 0cf35b9..b4d7e38 100644 --- a/core/lib/money.py +++ b/core/lib/money.py @@ -1,18 +1,17 @@ # Twisted imports +import asyncio import logging from datetime import datetime -import asyncio import urllib3 - -# Other library imports -from core.models import Aggregator, Platform - from aiocoingecko import AsyncCoinGeckoAPISession from django.conf import settings from elasticsearch import AsyncElasticsearch from forex_python.converter import CurrencyRates +# Other library imports +from core.models import Aggregator, Platform + # TODO: secure ES traffic properly urllib3.disable_warnings() @@ -47,19 +46,17 @@ class Money(object): """ if not all([user, nordigen, agora]): raise Exception - + # I hate circular dependencies self.nordigen = nordigen self.agora = agora - + aggregators = Aggregator.objects.filter(user=user, enabled=True) platforms = Platform.objects.filter(user=user, enabled=True) total = await self.get_total(aggregators, platforms, trades=True) - - return total - + return total # def setup_loops(self): # """ @@ -113,26 +110,27 @@ class Money(object): rates = self.cr.get_rates("USD") return rates - async def get_acceptable_margins(self, platform, currency, amount): - """ - Get the minimum and maximum amounts we would accept a trade for. - :param currency: currency code - :param amount: amount - :return: (min, max) - :rtype: tuple - """ - sets = util.get_settings(platform) - rates = await self.get_rates_all() - if currency == "USD": - min_amount = amount - float(sets.AcceptableUSDMargin) - max_amount = amount + float(sets.AcceptableUSDMargin) - return (min_amount, max_amount) - amount_usd = amount / rates[currency] - min_usd = amount_usd - float(sets.AcceptableUSDMargin) - max_usd = amount_usd + float(sets.AcceptableUSDMargin) - min_local = min_usd * rates[currency] - max_local = max_usd * rates[currency] - return (min_local, max_local) + # TODO: pass platform + # async def get_acceptable_margins(self, platform, currency, amount): + # """ + # Get the minimum and maximum amounts we would accept a trade for. + # :param currency: currency code + # :param amount: amount + # :return: (min, max) + # :rtype: tuple + # """ + # sets = util.get_settings(platform) + # rates = await self.get_rates_all() + # if currency == "USD": + # min_amount = amount - float(sets.AcceptableUSDMargin) + # max_amount = amount + float(sets.AcceptableUSDMargin) + # return (min_amount, max_amount) + # amount_usd = amount / rates[currency] + # min_usd = amount_usd - float(sets.AcceptableUSDMargin) + # max_usd = amount_usd + float(sets.AcceptableUSDMargin) + # min_local = min_usd * rates[currency] + # max_local = max_usd * rates[currency] + # return (min_local, max_local) async def get_minmax(self, min_usd, max_usd, asset, currency): rates = await self.get_rates_all() @@ -246,7 +244,7 @@ class Money(object): total = sum(total) return total - + async def gather_wallet_balance_xmr(self, platforms): """ Gather the total XMR of the specified platforms. @@ -274,19 +272,18 @@ class Money(object): btc = [float(x["response"]["data"]["total"]["balance"]) for x in btc_pre] btc = sum(btc) return btc - + def gather_base_usd(self, platforms): total = 0 for platform in platforms: total += platform.base_usd return total - + def gather_withdrawal_limit(self, platforms): total = 0 for platform in platforms: total += platform.withdrawal_trigger return total - # TODO: possibly refactor this into smaller functions which don't return as much # check if this is all really needed in the corresponding withdraw function @@ -334,7 +331,6 @@ class Money(object): profit = total_usd - total_base_usd - # Convert the total USD price to GBP and SEK price_sek = rates["SEK"] * total_usd price_usd = total_usd @@ -353,8 +349,6 @@ class Money(object): total_profit = total_with_trades - total_base_usd - - # cast = ( # ( # price_sek, diff --git a/core/management/commands/polling.py b/core/management/commands/polling.py index 107a111..0785c57 100644 --- a/core/management/commands/polling.py +++ b/core/management/commands/polling.py @@ -3,7 +3,7 @@ import asyncio from apscheduler.schedulers.asyncio import AsyncIOScheduler from django.core.management.base import BaseCommand -from core.clients.aggregators.nordigen import NordigenClient +# from core.clients.aggregators.nordigen import NordigenClient from core.clients.platforms.agora import AgoraClient from core.models import Aggregator, Platform from core.util import logs diff --git a/core/views/aggregators.py b/core/views/aggregators.py index 6965453..66bfc21 100644 --- a/core/views/aggregators.py +++ b/core/views/aggregators.py @@ -13,7 +13,6 @@ from mixins.views import ( from two_factor.views.mixins import OTPRequiredMixin from core.clients.aggregators.nordigen import NordigenClient - from core.forms import AggregatorForm from core.models import Aggregator from core.util import logs diff --git a/core/views/banks.py b/core/views/banks.py index e2deae5..658a359 100644 --- a/core/views/banks.py +++ b/core/views/banks.py @@ -121,5 +121,7 @@ class BanksTransactions(LoginRequiredMixin, OTPRequiredMixin, ObjectList): return self.render_to_response(context) run = synchronize_async_helper(NordigenClient(aggregator)) - transactions = synchronize_async_helper(run.get_transactions(account_id, pending=True)) + transactions = synchronize_async_helper( + run.get_transactions(account_id, pending=True) + ) return transactions diff --git a/core/views/profit.py b/core/views/profit.py index a5875d2..ecaa11f 100644 --- a/core/views/profit.py +++ b/core/views/profit.py @@ -1,26 +1,16 @@ 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 ( - ObjectCreate, - ObjectDelete, - ObjectList, - ObjectRead, - ObjectUpdate, -) +from mixins.views import ObjectRead from two_factor.views.mixins import OTPRequiredMixin from core.clients.aggregators.nordigen import NordigenClient from core.clients.platforms.agora import AgoraClient -from core.util import logs -from core.models import Platform from core.lib.money import money +from core.util import logs from core.views.helpers import synchronize_async_helper log = logs.get_logger(__name__) + class Profit(LoginRequiredMixin, OTPRequiredMixin, ObjectRead): context_object_name_singular = "profit" context_object_name = "profit" @@ -28,25 +18,26 @@ class Profit(LoginRequiredMixin, OTPRequiredMixin, ObjectRead): # detail_template = "partials/profit-info.html" def get_object(self, **kwargs): - res = synchronize_async_helper(money.check_all(user=self.request.user, nordigen=NordigenClient, agora=AgoraClient)) + res = synchronize_async_helper( + money.check_all( + user=self.request.user, nordigen=NordigenClient, agora=AgoraClient + ) + ) print("RES", res) results = { "Bank balance total": res["total_sinks_usd"], "Platform balance total": res["total_usd_agora"], "Open trade value": res["open_trade_value"], - "Total": res["total_with_trades"], "Profit": res["total_profit"], "Remaining before withdrawal": res["total_remaining"], - "Total (without open trades)": res["total_usd"], "Profit (without open trades)": res["profit"], "Remaining before withdrawal (without open trades)": res["remaining"], - "Base balance required": res["total_base_usd"], - "Amount above base balance to trigger withdrawal": - res["total_withdrawal_limit"], + "Amount above base balance to trigger withdrawal": res[ + "total_withdrawal_limit" + ], "Withdrawal trigger": res["withdraw_threshold"], - } - return results \ No newline at end of file + return results diff --git a/handler/lib/money.py b/handler/lib/money.py index 289e750..abfb8c0 100644 --- a/handler/lib/money.py +++ b/handler/lib/money.py @@ -239,7 +239,7 @@ class Money(util.Base): agora_wallet_xmr = yield self.agora.api.wallet_balance_xmr() agora_wallet_btc = yield self.agora.api.wallet_balance() lbtc_wallet_btc = yield self.lbtc.api.wallet_balance() - + total_xmr_agora = agora_wallet_xmr["response"]["data"]["total"]["balance"] total_btc_agora = agora_wallet_btc["response"]["data"]["total"]["balance"] total_btc_lbtc = lbtc_wallet_btc["response"]["data"]["total"]["balance"]