From 939b43e4d33d8964daf5d74d845efca0fcaf3967 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 14 Feb 2022 20:37:17 +0000 Subject: [PATCH] Fix transaction handling for non-Revolut trades --- handler/agora.py | 12 +++++++++++- handler/transactions.py | 27 +++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index 560c59b..86de8da 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -738,23 +738,31 @@ class Agora(object): Withdraw excess funds to our XMR wallets. """ totals_all = self.tx.get_total() + print("totals_all", totals_all) if totals_all is False: return False wallet_xmr, _ = totals_all[2] + print("wallet_xmr", wallet_xmr) # Get the wallet balances in USD total_usd = totals_all[0][1] + print("total_usd", total_usd) total_trades_usd = self.tx.get_open_trades_usd() + print("total_trades_usd", total_trades_usd) if not total_usd: return False total_usd += total_trades_usd + print("total_usd after trades add", total_usd) profit_usd = total_usd - float(settings.Money.BaseUSD) + print("profit_usd", profit_usd) # Get the XMR -> USD exchange rate xmr_usd = self.cg.get_price(ids="monero", vs_currencies=["USD"]) + print("xmr_usd", xmr_usd) # Convert the USD total to XMR profit_usd_in_xmr = float(profit_usd) / xmr_usd["monero"]["usd"] + print("profit_usd_in_xmr", profit_usd_in_xmr) # Check profit is above zero if not profit_usd >= 0: @@ -773,8 +781,10 @@ class Agora(object): return half = profit_usd_in_xmr / 2 + print("half", half) half_rounded = round(half, 8) + print("half_rounded", half_rounded) # Read OTP secret with open("otp.key", "r") as f: @@ -800,7 +810,7 @@ class Agora(object): rtrn2 = self.agora.wallet_send_xmr(**send_cast) self.irc.sendmsg(f"Withdrawal: {rtrn1['success']} | {rtrn2['success']}") - self.notify.notify_withdrawal(profit_usd / 2) + self.notify.notify_withdrawal(half_rounded) def to_usd(self, amount, currency): if currency == "USD": diff --git a/handler/transactions.py b/handler/transactions.py index 461c3e5..11a8688 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -39,6 +39,7 @@ class Transactions(object): txid = inside["id"] if "type" not in inside: + # stored_trade here is actually TX stored_trade = r.hgetall(f"tx.{txid}") if not stored_trade: self.log.error("Could not find entry in DB for typeless transaction: {id}", id=txid) @@ -67,10 +68,21 @@ class Transactions(object): r.hmset(f"tx.{txid}", stored_trade) # Check it's all been previously validated if "valid" not in stored_trade: - print("Valid not in stored trade") print("valid not in stored_trade", stored_trade) if stored_trade["valid"] == "1": - print("WOULD RELEASE ESCROW FROM SECONDARY NOTINSIDE UPDATE", stored_trade["trade_id"], stored_trade["txid"]) + print("STORED TRADE IS VALID") + # Make it invalid immediately, as we're going to release now + stored_trade["valid"] = "0" + print("SETTING STORED TRADE INVALID") + r.hmset(f"tx.{txid}", stored_trade) + print("SAVING STORED TRADE") + print("STORED TRADE SAVED:", dumps(stored_trade, indent=2)) + reference = self.tx_to_ref(stored_trade["trade_id"]) + print("REFERENCE", reference) + self.release_funds(stored_trade["trade_id"], reference) + print("RELEASED FUNDS") + self.notify.notify_complete_trade(stored_trade["amount"], stored_trade["currency"]) + print("NOTIFICATION SENT") return # If type not in inside and we haven't hit any more returns return @@ -416,12 +428,15 @@ class Transactions(object): def get_total(self): total_usd_revolut = self.revolut.get_total_usd() if total_usd_revolut is False: + self.log.error("Could not get USD total.") return False agora_wallet_xmr = self.agora.agora.wallet_balance_xmr() if not agora_wallet_xmr["success"]: + self.log.error("Could not get Agora XMR wallet total.") return False agora_wallet_btc = self.agora.agora.wallet_balance() if not agora_wallet_btc["success"]: + self.log.error("Could not get Agora BTC wallet total.") return False total_xmr_agora = agora_wallet_xmr["response"]["data"]["total"]["balance"] total_btc_agora = agora_wallet_btc["response"]["data"]["total"]["balance"] @@ -485,13 +500,21 @@ class Transactions(object): return cumul_usd def get_total_remaining(self): + print("withdrawalLimit", settings.Money.WithdrawLimit) total_usd = self.get_total_usd() + print("total_usd", total_usd) total_trades_usd = self.get_open_trades_usd() + print("total_trades_usd", total_trades_usd) if not total_usd: return False total_usd += total_trades_usd + print("total_usd after add trades", total_usd) + print("baseUSD", settings.Money.BaseUSD) + print("withdrawalLimit", settings.Money.WithdrawLimit) withdraw_threshold = float(settings.Money.BaseUSD) + float(settings.Money.WithdrawLimit) + print("withdraw_threshold", withdraw_threshold) remaining = withdraw_threshold - total_usd + print("remaining", remaining) return remaining