|
|
@ -1,6 +1,6 @@
|
|
|
|
# Twisted/Klein imports
|
|
|
|
# Twisted/Klein imports
|
|
|
|
from twisted.internet.task import LoopingCall
|
|
|
|
from twisted.internet.task import LoopingCall
|
|
|
|
from twisted.internet.threads import deferToThread
|
|
|
|
from twisted.internet.defer import inlineCallbacks
|
|
|
|
|
|
|
|
|
|
|
|
# Other library imports
|
|
|
|
# Other library imports
|
|
|
|
from json import dumps
|
|
|
|
from json import dumps
|
|
|
@ -49,13 +49,14 @@ class Transactions(util.Base):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Run all the balance checks that output into ES in another thread.
|
|
|
|
Run all the balance checks that output into ES in another thread.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
deferToThread(self.get_total)
|
|
|
|
|
|
|
|
deferToThread(self.get_remaining)
|
|
|
|
self.get_total()
|
|
|
|
deferToThread(self.money.get_profit)
|
|
|
|
self.get_remaining()
|
|
|
|
deferToThread(self.money.get_profit, True)
|
|
|
|
self.money.get_profit()
|
|
|
|
deferToThread(self.get_open_trades_usd)
|
|
|
|
self.money.get_profit(True)
|
|
|
|
deferToThread(self.get_total_remaining)
|
|
|
|
self.get_open_trades_usd()
|
|
|
|
deferToThread(self.get_total_with_trades)
|
|
|
|
self.get_total_remaining()
|
|
|
|
|
|
|
|
self.get_total_with_trades()
|
|
|
|
|
|
|
|
|
|
|
|
def setup_loops(self):
|
|
|
|
def setup_loops(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -259,12 +260,9 @@ class Transactions(util.Base):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
senders = self.get_previous_senders(platform, platform_buyer)
|
|
|
|
senders = self.get_previous_senders(platform, platform_buyer)
|
|
|
|
if senders is None: # no senders yet, assume it's valid
|
|
|
|
if senders is None: # no senders yet, assume it's valid
|
|
|
|
print("Senders is none, assuming its a valid TX!")
|
|
|
|
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
if platform_buyer in senders:
|
|
|
|
if platform_buyer in senders:
|
|
|
|
print("Platform buyer is in senders!")
|
|
|
|
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
print("Platform buyer is not in senders")
|
|
|
|
|
|
|
|
self.ux.notify.notify_sender_name_mismatch(reference, platform_buyer, bank_sender)
|
|
|
|
self.ux.notify.notify_sender_name_mismatch(reference, platform_buyer, bank_sender)
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
@ -295,7 +293,6 @@ class Transactions(util.Base):
|
|
|
|
Return False if the trade already has a mapped transaction.
|
|
|
|
Return False if the trade already has a mapped transaction.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
existing_tx = r.hget(f"trade.{reference}", "tx")
|
|
|
|
existing_tx = r.hget(f"trade.{reference}", "tx")
|
|
|
|
print("existing_tx", existing_tx)
|
|
|
|
|
|
|
|
if existing_tx is None:
|
|
|
|
if existing_tx is None:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
elif existing_tx == b"":
|
|
|
|
elif existing_tx == b"":
|
|
|
@ -314,7 +311,6 @@ class Transactions(util.Base):
|
|
|
|
valid = self.valid_transaction(data)
|
|
|
|
valid = self.valid_transaction(data)
|
|
|
|
if not valid:
|
|
|
|
if not valid:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
print(f"Raw transaction data: {data}")
|
|
|
|
|
|
|
|
ts = data["timestamp"]
|
|
|
|
ts = data["timestamp"]
|
|
|
|
txid = data["transaction_id"]
|
|
|
|
txid = data["transaction_id"]
|
|
|
|
amount = float(data["amount"])
|
|
|
|
amount = float(data["amount"])
|
|
|
@ -383,7 +379,7 @@ class Transactions(util.Base):
|
|
|
|
|
|
|
|
|
|
|
|
# Check sender - we don't do anything with this yet
|
|
|
|
# Check sender - we don't do anything with this yet
|
|
|
|
sender_valid = self.check_valid_sender(reference, platform, sender, platform_buyer)
|
|
|
|
sender_valid = self.check_valid_sender(reference, platform, sender, platform_buyer)
|
|
|
|
print("Sender valid for trade: ", sender_valid)
|
|
|
|
self.log.info(f"Trade {reference} buyer {platform_buyer} valid: {sender_valid}")
|
|
|
|
# trade_released = self.release_map_trade(reference, txid)
|
|
|
|
# trade_released = self.release_map_trade(reference, txid)
|
|
|
|
# if trade_released:
|
|
|
|
# if trade_released:
|
|
|
|
# self.ux.notify.notify_complete_trade(amount, currency)
|
|
|
|
# self.ux.notify.notify_complete_trade(amount, currency)
|
|
|
@ -426,14 +422,11 @@ class Transactions(util.Base):
|
|
|
|
Map a trade to a transaction and release if no other TX is
|
|
|
|
Map a trade to a transaction and release if no other TX is
|
|
|
|
mapped to the same trade.
|
|
|
|
mapped to the same trade.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
print("Release map trade called", reference, tx)
|
|
|
|
|
|
|
|
stored_trade = self.get_ref(reference)
|
|
|
|
stored_trade = self.get_ref(reference)
|
|
|
|
print("Stored trade", stored_trade)
|
|
|
|
|
|
|
|
if not stored_trade:
|
|
|
|
if not stored_trade:
|
|
|
|
self.log.error(f"Could not get stored trade for {reference}.")
|
|
|
|
self.log.error(f"Could not get stored trade for {reference}.")
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
tx_obj = self.get_tx(tx)
|
|
|
|
tx_obj = self.get_tx(tx)
|
|
|
|
print("tx_obj", tx_obj)
|
|
|
|
|
|
|
|
if not tx_obj:
|
|
|
|
if not tx_obj:
|
|
|
|
self.log.error(f"Could not get TX for {tx}.")
|
|
|
|
self.log.error(f"Could not get TX for {tx}.")
|
|
|
|
return None
|
|
|
|
return None
|
|
|
@ -442,13 +435,11 @@ class Transactions(util.Base):
|
|
|
|
bank_sender = tx_obj["sender"]
|
|
|
|
bank_sender = tx_obj["sender"]
|
|
|
|
trade_id = stored_trade["id"]
|
|
|
|
trade_id = stored_trade["id"]
|
|
|
|
is_updated = self.update_trade_tx(reference, tx)
|
|
|
|
is_updated = self.update_trade_tx(reference, tx)
|
|
|
|
print("is_updated", is_updated)
|
|
|
|
|
|
|
|
if is_updated is None:
|
|
|
|
if is_updated is None:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
elif is_updated is True:
|
|
|
|
elif is_updated is True:
|
|
|
|
# We mapped the trade successfully
|
|
|
|
# We mapped the trade successfully
|
|
|
|
self.release_funds(trade_id, reference)
|
|
|
|
self.release_funds(trade_id, reference)
|
|
|
|
print("Adding mapped bank sender", platform_buyer, bank_sender)
|
|
|
|
|
|
|
|
self.add_bank_sender(platform, platform_buyer, bank_sender)
|
|
|
|
self.add_bank_sender(platform, platform_buyer, bank_sender)
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
elif is_updated is False:
|
|
|
|
elif is_updated is False:
|
|
|
@ -523,7 +514,6 @@ class Transactions(util.Base):
|
|
|
|
def send_verification_url(self, platform, uid, trade_id):
|
|
|
|
def send_verification_url(self, platform, uid, trade_id):
|
|
|
|
send_setting, post_message = self.get_send_settings(platform)
|
|
|
|
send_setting, post_message = self.get_send_settings(platform)
|
|
|
|
if send_setting == "1":
|
|
|
|
if send_setting == "1":
|
|
|
|
print("SEND SETTING IS 1", platform, uid, trade_id)
|
|
|
|
|
|
|
|
auth_url = self.ux.verify.create_applicant_and_get_link(uid)
|
|
|
|
auth_url = self.ux.verify.create_applicant_and_get_link(uid)
|
|
|
|
if platform == "lbtc":
|
|
|
|
if platform == "lbtc":
|
|
|
|
auth_url = auth_url.replace("https://", "") # hack
|
|
|
|
auth_url = auth_url.replace("https://", "") # hack
|
|
|
@ -583,7 +573,6 @@ class Transactions(util.Base):
|
|
|
|
self.irc.sendmsg(f"Generated reference for {trade_id}: {reference}")
|
|
|
|
self.irc.sendmsg(f"Generated reference for {trade_id}: {reference}")
|
|
|
|
self.ux.notify.notify_new_trade(amount, currency)
|
|
|
|
self.ux.notify.notify_new_trade(amount, currency)
|
|
|
|
uid = self.create_uid(subclass, buyer)
|
|
|
|
uid = self.create_uid(subclass, buyer)
|
|
|
|
print("UID of new trade", uid)
|
|
|
|
|
|
|
|
verified = self.ux.verify.get_external_user_id_status(uid)
|
|
|
|
verified = self.ux.verify.get_external_user_id_status(uid)
|
|
|
|
if verified != "GREEN":
|
|
|
|
if verified != "GREEN":
|
|
|
|
self.log.info(f"UID {uid} is not verified, sending link.")
|
|
|
|
self.log.info(f"UID {uid} is not verified, sending link.")
|
|
|
@ -718,7 +707,6 @@ class Transactions(util.Base):
|
|
|
|
for reference in refs:
|
|
|
|
for reference in refs:
|
|
|
|
ref_data = util.convert(r.hgetall(f"trade.{reference}"))
|
|
|
|
ref_data = util.convert(r.hgetall(f"trade.{reference}"))
|
|
|
|
if not ref_data:
|
|
|
|
if not ref_data:
|
|
|
|
print("NOT REF DATA")
|
|
|
|
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
if ref_data["id"] == tx:
|
|
|
|
if ref_data["id"] == tx:
|
|
|
|
return reference
|
|
|
|
return reference
|
|
|
@ -736,6 +724,7 @@ class Transactions(util.Base):
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
return ref_data["id"]
|
|
|
|
return ref_data["id"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def get_total_usd(self):
|
|
|
|
def get_total_usd(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Get total USD in all our accounts, bank and trading.
|
|
|
|
Get total USD in all our accounts, bank and trading.
|
|
|
@ -743,15 +732,21 @@ class Transactions(util.Base):
|
|
|
|
:rtype float:
|
|
|
|
:rtype float:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
total_sinks_usd = self.sinks.get_total_usd()
|
|
|
|
total_sinks_usd = self.sinks.get_total_usd()
|
|
|
|
agora_wallet_xmr = self.agora.agora.wallet_balance_xmr()
|
|
|
|
agora_wallet_xmr = yield self.agora.agora.wallet_balance_xmr()
|
|
|
|
|
|
|
|
agora_wallet_btc = yield self.agora.agora.wallet_balance()
|
|
|
|
|
|
|
|
lbtc_wallet_btc = yield self.lbtc.lbtc.wallet_balance()
|
|
|
|
if not agora_wallet_xmr["success"]:
|
|
|
|
if not agora_wallet_xmr["success"]:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
agora_wallet_btc = self.agora.agora.wallet_balance()
|
|
|
|
|
|
|
|
if not agora_wallet_btc["success"]:
|
|
|
|
if not agora_wallet_btc["success"]:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
lbtc_wallet_btc = self.lbtc.lbtc.wallet_balance()
|
|
|
|
|
|
|
|
if not lbtc_wallet_btc["success"]:
|
|
|
|
if not lbtc_wallet_btc["success"]:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not agora_wallet_xmr["response"]:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not agora_wallet_btc["response"]:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not lbtc_wallet_btc["response"]:
|
|
|
|
|
|
|
|
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"]
|
|
|
|
total_btc_lbtc = lbtc_wallet_btc["response"]["data"]["total"]["balance"]
|
|
|
|
total_btc_lbtc = lbtc_wallet_btc["response"]["data"]["total"]["balance"]
|
|
|
@ -792,6 +787,7 @@ class Transactions(util.Base):
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: possibly refactor this into smaller functions which don't return as much stuff
|
|
|
|
# TODO: possibly refactor this into smaller functions which don't return as much stuff
|
|
|
|
# check if this is all really needed in the corresponding withdraw function
|
|
|
|
# check if this is all really needed in the corresponding withdraw function
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def get_total(self):
|
|
|
|
def get_total(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Get all the values corresponding to the amount of money we hold.
|
|
|
|
Get all the values corresponding to the amount of money we hold.
|
|
|
@ -799,15 +795,15 @@ class Transactions(util.Base):
|
|
|
|
:rtype: tuple(tuple(float, float, float), tuple(float, float), tuple(float, float))
|
|
|
|
:rtype: tuple(tuple(float, float, float), tuple(float, float), tuple(float, float))
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
total_sinks_usd = self.sinks.get_total_usd()
|
|
|
|
total_sinks_usd = self.sinks.get_total_usd()
|
|
|
|
agora_wallet_xmr = self.agora.agora.wallet_balance_xmr()
|
|
|
|
agora_wallet_xmr = yield 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.")
|
|
|
|
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 = yield 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.")
|
|
|
|
self.log.error("Could not get Agora BTC wallet total.")
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
lbtc_wallet_btc = self.lbtc.lbtc.wallet_balance()
|
|
|
|
lbtc_wallet_btc = yield self.lbtc.lbtc.wallet_balance()
|
|
|
|
if not lbtc_wallet_btc["success"]:
|
|
|
|
if not lbtc_wallet_btc["success"]:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
total_xmr_agora = agora_wallet_xmr["response"]["data"]["total"]["balance"]
|
|
|
|
total_xmr_agora = agora_wallet_xmr["response"]["data"]["total"]["balance"]
|
|
|
@ -883,13 +879,14 @@ class Transactions(util.Base):
|
|
|
|
cast["xtype"] = "tx"
|
|
|
|
cast["xtype"] = "tx"
|
|
|
|
self.es.index(index=settings.ES.Index, document=cast)
|
|
|
|
self.es.index(index=settings.ES.Index, document=cast)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def get_remaining(self):
|
|
|
|
def get_remaining(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Check how much profit we need to make in order to withdraw.
|
|
|
|
Check how much profit we need to make in order to withdraw.
|
|
|
|
:return: profit remaining in USD
|
|
|
|
:return: profit remaining in USD
|
|
|
|
:rtype: float
|
|
|
|
:rtype: float
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
total_usd = self.get_total_usd()
|
|
|
|
total_usd = yield self.get_total_usd()
|
|
|
|
if not total_usd:
|
|
|
|
if not total_usd:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
@ -908,6 +905,7 @@ class Transactions(util.Base):
|
|
|
|
created_at = contact["data"]["created_at"]
|
|
|
|
created_at = contact["data"]["created_at"]
|
|
|
|
|
|
|
|
|
|
|
|
# Reformat the date how CoinGecko likes
|
|
|
|
# Reformat the date how CoinGecko likes
|
|
|
|
|
|
|
|
# 2022-05-02T11:17:14+00:00
|
|
|
|
date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ")
|
|
|
|
date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ")
|
|
|
|
date_formatted = date_parsed.strftime("%d-%m-%Y")
|
|
|
|
date_formatted = date_parsed.strftime("%d-%m-%Y")
|
|
|
|
|
|
|
|
|
|
|
@ -934,6 +932,7 @@ class Transactions(util.Base):
|
|
|
|
cumul_usd += amount_usd
|
|
|
|
cumul_usd += amount_usd
|
|
|
|
return cumul_usd
|
|
|
|
return cumul_usd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def get_open_trades_usd(self):
|
|
|
|
def get_open_trades_usd(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Get total value of open trades in USD.
|
|
|
|
Get total value of open trades in USD.
|
|
|
@ -941,9 +940,11 @@ class Transactions(util.Base):
|
|
|
|
:rtype: float
|
|
|
|
:rtype: float
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
dash_agora = self.agora.wrap_dashboard()
|
|
|
|
dash_agora = self.agora.wrap_dashboard()
|
|
|
|
|
|
|
|
dash_lbtc = self.lbtc.wrap_dashboard()
|
|
|
|
|
|
|
|
dash_agora = yield dash_agora
|
|
|
|
|
|
|
|
dash_lbtc = yield dash_lbtc
|
|
|
|
if dash_agora is False:
|
|
|
|
if dash_agora is False:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
dash_lbtc = self.lbtc.wrap_dashboard()
|
|
|
|
|
|
|
|
if dash_lbtc is False:
|
|
|
|
if dash_lbtc is False:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
@ -958,14 +959,15 @@ class Transactions(util.Base):
|
|
|
|
self.write_to_es("get_open_trades_usd", cast_es)
|
|
|
|
self.write_to_es("get_open_trades_usd", cast_es)
|
|
|
|
return cumul_usd
|
|
|
|
return cumul_usd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def get_total_remaining(self):
|
|
|
|
def get_total_remaining(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Check how much profit we need to make in order to withdraw, taking into account open trade value.
|
|
|
|
Check how much profit we need to make in order to withdraw, taking into account open trade value.
|
|
|
|
:return: profit remaining in USD
|
|
|
|
:return: profit remaining in USD
|
|
|
|
:rtype: float
|
|
|
|
:rtype: float
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
total_usd = self.get_total_usd()
|
|
|
|
total_usd = yield self.get_total_usd()
|
|
|
|
total_trades_usd = self.get_open_trades_usd()
|
|
|
|
total_trades_usd = yield self.get_open_trades_usd()
|
|
|
|
if not total_usd:
|
|
|
|
if not total_usd:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
total_usd += total_trades_usd
|
|
|
|
total_usd += total_trades_usd
|
|
|
@ -978,11 +980,12 @@ class Transactions(util.Base):
|
|
|
|
self.write_to_es("get_total_remaining", cast_es)
|
|
|
|
self.write_to_es("get_total_remaining", cast_es)
|
|
|
|
return remaining
|
|
|
|
return remaining
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@inlineCallbacks
|
|
|
|
def get_total_with_trades(self):
|
|
|
|
def get_total_with_trades(self):
|
|
|
|
total_usd = self.get_total_usd()
|
|
|
|
total_usd = yield self.get_total_usd()
|
|
|
|
if not total_usd:
|
|
|
|
if not total_usd:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
total_trades_usd = self.get_open_trades_usd()
|
|
|
|
total_trades_usd = yield self.get_open_trades_usd()
|
|
|
|
total_with_trades = total_usd + total_trades_usd
|
|
|
|
total_with_trades = total_usd + total_trades_usd
|
|
|
|
cast_es = {
|
|
|
|
cast_es = {
|
|
|
|
"total_with_trades": total_with_trades,
|
|
|
|
"total_with_trades": total_with_trades,
|
|
|
|