From be9f9e7363403a3e9010593e7cc868ee1b5a3734 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 11 Mar 2023 11:45:24 +0000 Subject: [PATCH] Fix getting all profit variables --- core/lib/money.py | 299 +++++++++++++++++------------------- core/lib/schemas/agora_s.py | 6 +- core/views/profit.py | 22 ++- 3 files changed, 167 insertions(+), 160 deletions(-) diff --git a/core/lib/money.py b/core/lib/money.py index 128e2d8..0cf35b9 100644 --- a/core/lib/money.py +++ b/core/lib/money.py @@ -55,30 +55,9 @@ class Money(object): aggregators = Aggregator.objects.filter(user=user, enabled=True) platforms = Platform.objects.filter(user=user, enabled=True) - order = [ - "total", - # "remaining", - # "profit", - # "profit_with_trades", - # "open_trades", - # "total_remaining", - # "total_with_trades", - ] - tasks = [ - self.get_total(aggregators, platforms), - # self.get_remaining(), - # self.get_profit(), - # self.get_profit(True), - # self.get_open_trades_usd(), - # self.get_total_remaining(), - # self.get_total_with_trades(), - ] - task_output = await asyncio.gather(*tasks) - print("TASK OUTPUT", task_output) - results = dict(zip(order, task_output)) - print("RESULTS", results) + total = await self.get_total(aggregators, platforms, trades=True) - return results + return total @@ -176,11 +155,12 @@ class Money(object): rates = await self.get_rates_all() return float(amount) / rates[currency] - async def multiple_to_usd(self, currency_map): + async def multiple_to_usd(self, currency_map, rates=None): """ Convert multiple curencies to USD while saving API calls. """ - rates = await self.get_rates_all() + if not rates: + rates = await self.get_rates_all() cumul = 0 for currency, amount in currency_map.items(): if currency == "USD": @@ -189,32 +169,6 @@ class Money(object): cumul += float(amount) / rates[currency] return cumul - async def get_profit(self, trades=False): - """ - Check how much total profit we have made. - :return: profit in USD - :rtype: float - """ - total_usd = await self.get_total_usd() - if not total_usd: - return False - if trades: - trades_usd = await self.get_open_trades_usd() - total_usd += trades_usd - - profit = total_usd - float(settings.Money.BaseUSD) - if trades: - cast_es = { - "profit_trades_usd": profit, - } - else: - cast_es = { - "profit_usd": profit, - } - - await self.write_to_es("get_profit", cast_es) - return profit - async def get_total_usd(self): """ Get total USD in all our accounts, bank and trading. @@ -276,43 +230,67 @@ class Money(object): await self.write_to_es("get_total_usd", cast_es) return total_usd - async def gather_total_map(self, aggregators): - total = 0 - for aggregator in aggregators: - run = await self.nordigen(aggregator) - total_map = await run.get_total_map() - total_usd = await self.multiple_to_usd(total_map) - print("gather_total_map total_usd", total_usd) - total += total_usd + async def gather_total_map(self, aggregators, rates): + """ + Gather the total USD of specified aggregators. + """ + total_run_tasks = [self.nordigen(x) for x in aggregators] + total_run = await asyncio.gather(*total_run_tasks) + + total_map_tasks = [x.get_total_map() for x in total_run] + total_map = await asyncio.gather(*total_map_tasks) + + to_usd_tasks = [self.multiple_to_usd(x, rates=rates) for x in total_map] + total = await asyncio.gather(*to_usd_tasks) + + total = sum(total) return total async def gather_wallet_balance_xmr(self, platforms): - # TODO: check success - total = 0 - for platform in platforms: - run = await self.agora(platform) - xmr = await run.api.wallet_balance_xmr() - xmr = float(xmr["response"]["data"]["total"]["balance"]) - print("gather waller xmr", xmr) - total += xmr - return total + """ + Gather the total XMR of the specified platforms. + """ + run_tasks = [self.agora(platform) for platform in platforms] + run = await asyncio.gather(*run_tasks) + + xmr_tasks = [x.api.wallet_balance_xmr() for x in run] + xmr_pre = await asyncio.gather(*xmr_tasks) + + xmr = [float(x["response"]["data"]["total"]["balance"]) for x in xmr_pre] + xmr = sum(xmr) + return xmr async def gather_wallet_balance(self, platforms): - # TODO: check success + """ + Gather the total BTC of the specified platforms. + """ + run_tasks = [self.agora(platform) for platform in platforms] + run = await asyncio.gather(*run_tasks) + + btc_tasks = [x.api.wallet_balance() for x in run] + btc_pre = await asyncio.gather(*btc_tasks) + + 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: - run = await self.agora(platform) - btc = await run.api.wallet_balance() - btc = float(btc["response"]["data"]["total"]["balance"]) - total += btc + 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 - async def get_total(self, aggregators, platforms): + async def get_total(self, aggregators, platforms, trades=False): """ Get all the values corresponding to the amount of money we hold. :return: ((total SEK, total USD, total GBP), @@ -322,14 +300,13 @@ class Money(object): tuple(float, float), tuple(float, float)) """ - total_sinks_usd = await self.gather_total_map(aggregators) + rates = await self.get_rates_all() + total_sinks_usd = await self.gather_total_map(aggregators, rates=rates) agora_wallet_xmr = await self.gather_wallet_balance_xmr(platforms) agora_wallet_btc = await self.gather_wallet_balance(platforms) - # lbtc_wallet_btc = await self.lbtc.api.wallet_balance() total_xmr_agora = agora_wallet_xmr total_btc_agora = agora_wallet_btc - # total_btc_lbtc = lbtc_wallet_btc["response"]["data"]["total"]["balance"] # Get the XMR -> USD exchange rate async with AsyncCoinGeckoAPISession() as cg: xmr_usd = await cg.get_price(ids="monero", vs_currencies="USD") @@ -343,28 +320,41 @@ class Money(object): # Convert the Agora BTC total to USD total_usd_agora_btc = float(total_btc_agora) * btc_usd["bitcoin"]["usd"] - # Convert the LBTC BTC total to USD - # total_usd_lbtc_btc = float(total_btc_lbtc) * btc_usd["bitcoin"]["usd"] - # Add it all up total_usd_agora = total_usd_agora_xmr + total_usd_agora_btc - # total_usd_lbtc = total_usd_lbtc_btc total_usd = total_usd_agora + total_sinks_usd - # total_usd_lbtc - total_btc_usd = total_usd_agora_btc # + total_usd_lbtc_btc - total_xmr_usd = total_usd_agora_xmr + # Get aggregate totals and withdrawal limits + total_base_usd = self.gather_base_usd(platforms) + total_withdrawal_limit = self.gather_withdrawal_limit(platforms) + + # Use those to calculate amount remaining + withdraw_threshold = total_base_usd + total_withdrawal_limit + remaining = withdraw_threshold - total_usd + + profit = total_usd - total_base_usd - total_xmr = total_xmr_agora - total_btc = total_btc_agora - # total_btc_lbtc # Convert the total USD price to GBP and SEK - rates = await self.get_rates_all() price_sek = rates["SEK"] * total_usd price_usd = total_usd price_gbp = rates["GBP"] * total_usd + # Get open trades value + if trades: + dashboards = await self.gather_dashboards(platforms) + cumul_trades = 0 + for dash in dashboards: + cumul_add = await self.open_trades_usd_parse_dash(dash, rates) + cumul_trades += cumul_add + + total_with_trades = total_usd + cumul_trades + total_remaining = withdraw_threshold - total_with_trades + + total_profit = total_with_trades - total_base_usd + + + # cast = ( # ( # price_sek, @@ -393,33 +383,25 @@ class Money(object): "btc_usd": btc_usd["bitcoin"]["usd"], "total_sinks_usd": total_sinks_usd, "total_usd_agora": total_usd_agora, + "total_usd": total_usd, + "total_base_usd": total_base_usd, + "total_withdrawal_limit": total_withdrawal_limit, + "remaining": remaining, + "profit": profit, + "withdraw_threshold": withdraw_threshold, } + if trades: + cast_es["open_trade_value"] = cumul_trades + cast_es["total_with_trades"] = total_with_trades + cast_es["total_remaining"] = total_remaining + cast_es["total_profit"] = total_profit # await self.write_to_es("get_total", cast_es) + print("CAST ES", cast_es) return cast_es - async def get_remaining(self): - """ - Check how much profit we need to make in order to withdraw. - :return: profit remaining in USD - :rtype: float - """ - total_usd = await self.get_total_usd() - if not total_usd: - return False - - withdraw_threshold = float(settings.Money.BaseUSD) + float( - settings.Money.WithdrawLimit - ) - remaining = withdraw_threshold - total_usd - cast_es = { - "remaining_usd": remaining, - } - await self.write_to_es("get_remaining", cast_es) - return remaining - - async def open_trades_usd_parse_dash(self, platform, dash, rates): + async def open_trades_usd_parse_dash(self, dash, rates): cumul_usd = 0 - for contact_id, contact in dash.items(): + for _, contact in dash.items(): # We need created at in order to look up the historical prices created_at = contact["data"]["created_at"] @@ -437,24 +419,23 @@ class Money(object): date_formatted = date_parsed.strftime("%d-%m-%Y") # Get the historical rates for the right asset, extract the price - if platform == "agora": - asset = contact["data"]["advertisement"]["asset"] - elif platform == "lbtc": - asset = "BTC" - if asset == "XMR": - amount_crypto = contact["data"]["amount_xmr"] - history = await self.cg.get_coin_history_by_id( - id="monero", date=date_formatted - ) - if "market_data" not in history: - return False - crypto_usd = float(history["market_data"]["current_price"]["usd"]) - elif asset == "BTC": - amount_crypto = contact["data"]["amount_btc"] - history = await self.cg.get_coin_history_by_id( - id="bitcoin", date=date_formatted - ) - crypto_usd = float(history["market_data"]["current_price"]["usd"]) + asset = contact["data"]["advertisement"]["asset"] + + async with AsyncCoinGeckoAPISession() as cg: + if asset == "XMR": + amount_crypto = contact["data"]["amount_xmr"] + history = await cg.get_coin_history_by_id( + coin_id="monero", date=date_formatted + ) + if "market_data" not in history: + return False + crypto_usd = float(history["market_data"]["current_price"]["usd"]) + elif asset == "BTC": + amount_crypto = contact["data"]["amount_btc"] + history = await cg.get_coin_history_by_id( + coin_id="bitcoin", date=date_formatted + ) + crypto_usd = float(history["market_data"]["current_price"]["usd"]) # Convert crypto to fiat amount = float(amount_crypto) * crypto_usd currency = contact["data"]["currency"] @@ -468,33 +449,41 @@ class Money(object): cumul_usd += amount_usd return cumul_usd - async def get_open_trades_usd(self): - """ - Get total value of open trades in USD. - :return: total trade value - :rtype: float - """ - dash_agora = await self.agora.wrap_dashboard() - # dash_lbtc = self.lbtc.wrap_dashboard() - # dash_lbtc = yield dash_lbtc - if dash_agora is False: - return False - # if dash_lbtc is False: - # return False + async def gather_dashboards(self, platforms): + dashboards = [] + for platform in platforms: + run = await self.agora(platform) + dash = await run.wrap_dashboard() + dashboards.append(dash) + return dashboards - rates = await self.get_rates_all() - cumul_usd_agora = await self.open_trades_usd_parse_dash( - "agora", dash_agora, rates - ) - # cumul_usd_lbtc = await self.open_trades_usd_parse_dash("lbtc", dash_lbtc, - # rates) - cumul_usd = cumul_usd_agora # + cumul_usd_lbtc + # async def get_open_trades_usd(self, rates): + # """ + # Get total value of open trades in USD. + # :return: total trade value + # :rtype: float + # """ + # dash_agora = await self.agora.wrap_dashboard() + # # dash_lbtc = self.lbtc.wrap_dashboard() + # # dash_lbtc = yield dash_lbtc + # if dash_agora is False: + # return False + # # if dash_lbtc is False: + # # return False - cast_es = { - "trades_usd": cumul_usd, - } - await self.write_to_es("get_open_trades_usd", cast_es) - return cumul_usd + # # rates = await self.get_rates_all() + # cumul_usd_agora = await self.open_trades_usd_parse_dash( + # "agora", dash_agora, rates + # ) + # # cumul_usd_lbtc = await self.open_trades_usd_parse_dash("lbtc", dash_lbtc, + # # rates) + # cumul_usd = cumul_usd_agora # + cumul_usd_lbtc + + # cast_es = { + # "trades_usd": cumul_usd, + # } + # await self.write_to_es("get_open_trades_usd", cast_es) + # return cumul_usd async def get_total_remaining(self): """ diff --git a/core/lib/schemas/agora_s.py b/core/lib/schemas/agora_s.py index 40fcbf3..cd98af3 100644 --- a/core/lib/schemas/agora_s.py +++ b/core/lib/schemas/agora_s.py @@ -22,8 +22,10 @@ class ContactData(BaseModel): buyer: ContactDataBuyerSeller seller: ContactDataBuyerSeller amount: str - amount_xmr: str - fee_xmr: str + amount_xmr: str | None + fee_xmr: str | None + amount_btc: str | None + fee_btc: str | None advertisement: ContactDataAd contact_id: str currency: str diff --git a/core/views/profit.py b/core/views/profit.py index ae60899..a5875d2 100644 --- a/core/views/profit.py +++ b/core/views/profit.py @@ -24,13 +24,29 @@ log = logs.get_logger(__name__) class Profit(LoginRequiredMixin, OTPRequiredMixin, ObjectRead): context_object_name_singular = "profit" context_object_name = "profit" + page_title = "Profit info (USD)" # 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)) + print("RES", res) results = { - "Bank balance total (USD)": res["total"]["total_sinks_usd"], - "Platform balance total (USD)": res["total"]["total_usd_agora"], - "Sum of above": res["total"]["total_sinks_usd"] + res["total"]["total_usd_agora"], + "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"], + "Withdrawal trigger": res["withdraw_threshold"], + } return results \ No newline at end of file