Fix transaction handling for non-Revolut trades

This commit is contained in:
Mark Veidemanis 2022-02-14 20:37:17 +00:00
parent 372e246659
commit 5dcd33433d
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 36 additions and 3 deletions

View File

@ -738,23 +738,31 @@ class Agora(object):
Withdraw excess funds to our XMR wallets. Withdraw excess funds to our XMR wallets.
""" """
totals_all = self.tx.get_total() totals_all = self.tx.get_total()
print("totals_all", totals_all)
if totals_all is False: if totals_all is False:
return False return False
wallet_xmr, _ = totals_all[2] wallet_xmr, _ = totals_all[2]
print("wallet_xmr", wallet_xmr)
# Get the wallet balances in USD # Get the wallet balances in USD
total_usd = totals_all[0][1] total_usd = totals_all[0][1]
print("total_usd", total_usd)
total_trades_usd = self.tx.get_open_trades_usd() total_trades_usd = self.tx.get_open_trades_usd()
print("total_trades_usd", total_trades_usd)
if not total_usd: if not total_usd:
return False return False
total_usd += total_trades_usd total_usd += total_trades_usd
print("total_usd after trades add", total_usd)
profit_usd = total_usd - float(settings.Money.BaseUSD) profit_usd = total_usd - float(settings.Money.BaseUSD)
print("profit_usd", profit_usd)
# Get the XMR -> USD exchange rate # Get the XMR -> USD exchange rate
xmr_usd = self.cg.get_price(ids="monero", vs_currencies=["USD"]) xmr_usd = self.cg.get_price(ids="monero", vs_currencies=["USD"])
print("xmr_usd", xmr_usd)
# Convert the USD total to XMR # Convert the USD total to XMR
profit_usd_in_xmr = float(profit_usd) / xmr_usd["monero"]["usd"] 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 # Check profit is above zero
if not profit_usd >= 0: if not profit_usd >= 0:
@ -773,8 +781,10 @@ class Agora(object):
return return
half = profit_usd_in_xmr / 2 half = profit_usd_in_xmr / 2
print("half", half)
half_rounded = round(half, 8) half_rounded = round(half, 8)
print("half_rounded", half_rounded)
# Read OTP secret # Read OTP secret
with open("otp.key", "r") as f: with open("otp.key", "r") as f:
@ -800,7 +810,7 @@ class Agora(object):
rtrn2 = self.agora.wallet_send_xmr(**send_cast) rtrn2 = self.agora.wallet_send_xmr(**send_cast)
self.irc.sendmsg(f"Withdrawal: {rtrn1['success']} | {rtrn2['success']}") 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): def to_usd(self, amount, currency):
if currency == "USD": if currency == "USD":

View File

@ -39,6 +39,7 @@ class Transactions(object):
txid = inside["id"] txid = inside["id"]
if "type" not in inside: if "type" not in inside:
# stored_trade here is actually TX
stored_trade = r.hgetall(f"tx.{txid}") stored_trade = r.hgetall(f"tx.{txid}")
if not stored_trade: if not stored_trade:
self.log.error("Could not find entry in DB for typeless transaction: {id}", id=txid) 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) r.hmset(f"tx.{txid}", stored_trade)
# Check it's all been previously validated # Check it's all been previously validated
if "valid" not in stored_trade: if "valid" not in stored_trade:
print("Valid not in stored trade")
print("valid not in stored_trade", stored_trade) print("valid not in stored_trade", stored_trade)
if stored_trade["valid"] == "1": 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 return
# If type not in inside and we haven't hit any more returns # If type not in inside and we haven't hit any more returns
return return
@ -416,12 +428,15 @@ class Transactions(object):
def get_total(self): def get_total(self):
total_usd_revolut = self.revolut.get_total_usd() total_usd_revolut = self.revolut.get_total_usd()
if total_usd_revolut is False: if total_usd_revolut is False:
self.log.error("Could not get USD total.")
return False return False
agora_wallet_xmr = self.agora.agora.wallet_balance_xmr() agora_wallet_xmr = self.agora.agora.wallet_balance_xmr()
if not agora_wallet_xmr["success"]: if not agora_wallet_xmr["success"]:
self.log.error("Could not get Agora XMR wallet total.")
return False return False
agora_wallet_btc = self.agora.agora.wallet_balance() agora_wallet_btc = self.agora.agora.wallet_balance()
if not agora_wallet_btc["success"]: if not agora_wallet_btc["success"]:
self.log.error("Could not get Agora BTC wallet total.")
return False return False
total_xmr_agora = agora_wallet_xmr["response"]["data"]["total"]["balance"] total_xmr_agora = agora_wallet_xmr["response"]["data"]["total"]["balance"]
total_btc_agora = agora_wallet_btc["response"]["data"]["total"]["balance"] total_btc_agora = agora_wallet_btc["response"]["data"]["total"]["balance"]
@ -485,13 +500,21 @@ class Transactions(object):
return cumul_usd return cumul_usd
def get_total_remaining(self): def get_total_remaining(self):
print("withdrawalLimit", settings.Money.WithdrawLimit)
total_usd = self.get_total_usd() total_usd = self.get_total_usd()
print("total_usd", total_usd)
total_trades_usd = self.get_open_trades_usd() total_trades_usd = self.get_open_trades_usd()
print("total_trades_usd", total_trades_usd)
if not total_usd: if not total_usd:
return False return False
total_usd += total_trades_usd 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) withdraw_threshold = float(settings.Money.BaseUSD) + float(settings.Money.WithdrawLimit)
print("withdraw_threshold", withdraw_threshold)
remaining = withdraw_threshold - total_usd remaining = withdraw_threshold - total_usd
print("remaining", remaining)
return remaining return remaining