From bb34baee31059bc7cb6c920d1eaed50c46426225 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 25 Jan 2022 18:39:35 +0000 Subject: [PATCH] Implement withdrawing to wallets --- handler/agora.py | 54 +++++++++++++++++++++++++++++++++++++++-- handler/agoradesk_py.py | 4 +-- handler/transactions.py | 17 +++++++++---- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index 9dd61d2..124d5cb 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -620,11 +620,61 @@ class Agora(object): """ payload = {"tradeId": contact_id, "password": settings.Agora.Pass} rtrn = self.agora._api_call(api_method=f"contact_release/{contact_id}", http_method="POST", query_values=payload) + + # Check if we can withdraw funds + self.withdraw_funds() + return rtrn def withdraw_funds(self): """ Withdraw excess funds to our XMR/BTC wallets. """ - # wallet_xmr = - pass + totals_all = self.tx.get_total() + + wallet_xmr, _ = totals_all[2] + + # Get the wallet balances in USD + total_usd = totals_all[0][1] + + profit_usd = total_usd - float(settings.Money.BaseUSD) + + # Get the XMR -> USD exchange rate + xmr_usd = self.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( + "Not enough funds to withdraw {profit}, as wallet only contains {wallet}", profit=profit_usd_in_xmr, wallet=wallet_xmr + ) + return + + if not profit_usd >= float(settings.Money.WithdrawLimit): + # Not enough profit to withdraw + return + + half = profit_usd_in_xmr / 2 + + half_rounded = round(half, 8) + + # Set up the format for calling wallet_send_xmr + send_cast = { + "address": None, + "amount": half_rounded, + "password": settings.Agora.Pass, + } + + send_cast["address"] = settings.XMR.Wallet1 + rtrn1 = self.agora.wallet_send_xmr(**send_cast) + + send_cast["address"] = settings.XMR.Wallet2 + rtrn2 = self.agora.wallet_send_xmr(**send_cast) + + return (rtrn1["success"], rtrn2["success"]) diff --git a/handler/agoradesk_py.py b/handler/agoradesk_py.py index c6b659f..fdf1988 100644 --- a/handler/agoradesk_py.py +++ b/handler/agoradesk_py.py @@ -992,7 +992,7 @@ class AgoraDesk: address: str, amount: float, password: str, - fee_level: str, + # fee_level: str, otp: Optional[int] = None, ) -> Dict[str, Any]: """See Agoradesk API. @@ -1005,7 +1005,7 @@ class AgoraDesk: "address": address, "amount": amount, "password": password, - "fee_level": fee_level, + # "fee_level": fee_level, } if otp: params["otp"] = otp diff --git a/handler/transactions.py b/handler/transactions.py index 1a7f97c..421a1c5 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -44,7 +44,13 @@ class Transactions(object): ts = data["timestamp"] inside = data["data"] + txid = inside["id"] + txtype = inside["type"] + if txtype == "card_payment": + self.log.info("Ignoring card payment: {id}", id=txid) + return + if "type" not in inside: stored_trade = r.hgetall(f"tx.{txid}") print("stored trade", stored_trade) @@ -74,10 +80,7 @@ class Transactions(object): return # If type not in inside and we haven't hit any more returns return - txtype = inside["type"] - if txtype == "card_payment": - self.log.info("Ignoring card payment: {id}", id=txid) - return + state = inside["state"] if "reference" in inside: reference = inside["reference"] @@ -414,4 +417,8 @@ class Transactions(object): price_usd = total_usd price_gbp = rates["GBP"] * total_usd - return ((price_sek, price_usd, price_gbp), (total_usd_agora_xmr, total_usd_agora_btc)) + return ( + (price_sek, price_usd, price_gbp), # Total prices in our 3 favourite currencies + (total_usd_agora_xmr, total_usd_agora_btc), # Total USD balance in only Agora + (total_xmr_agora, total_btc_agora), + ) # Total XMR and BTC balance in Agora