2021-12-23 18:46:39 +00:00
|
|
|
# Twisted/Klein imports
|
|
|
|
from twisted.logger import Logger
|
|
|
|
|
|
|
|
# Other library imports
|
|
|
|
from json import dumps
|
2021-12-28 15:29:15 +00:00
|
|
|
from random import choices
|
|
|
|
from string import ascii_uppercase
|
2021-12-23 18:46:39 +00:00
|
|
|
|
|
|
|
# Project imports
|
|
|
|
from db import r
|
|
|
|
|
|
|
|
|
2021-12-27 21:04:57 +00:00
|
|
|
def convert(data):
|
|
|
|
"""
|
|
|
|
Recursively convert a dictionary.
|
|
|
|
"""
|
|
|
|
if isinstance(data, bytes):
|
|
|
|
return data.decode("ascii")
|
|
|
|
if isinstance(data, dict):
|
|
|
|
return dict(map(convert, data.items()))
|
|
|
|
if isinstance(data, tuple):
|
|
|
|
return map(convert, data)
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2021-12-23 18:46:39 +00:00
|
|
|
class Transactions(object):
|
|
|
|
"""
|
|
|
|
Handler class for incoming Revolut transactions.
|
|
|
|
"""
|
|
|
|
|
2021-12-27 20:59:24 +00:00
|
|
|
def __init__(self):
|
2021-12-27 21:28:49 +00:00
|
|
|
"""
|
|
|
|
Initialise the Transaction object.
|
|
|
|
Set the logger.
|
|
|
|
"""
|
2021-12-23 18:46:39 +00:00
|
|
|
self.log = Logger("transactions")
|
2021-12-27 20:59:24 +00:00
|
|
|
|
|
|
|
def set_agora(self, agora):
|
2021-12-24 17:27:36 +00:00
|
|
|
self.agora = agora
|
2021-12-27 20:59:24 +00:00
|
|
|
|
|
|
|
def set_irc(self, irc):
|
2021-12-24 17:27:36 +00:00
|
|
|
self.irc = irc
|
2021-12-23 18:46:39 +00:00
|
|
|
|
|
|
|
def transaction(self, data):
|
|
|
|
"""
|
|
|
|
Store details of transaction.
|
2021-12-27 21:20:15 +00:00
|
|
|
:param data: details of transaction
|
|
|
|
:type data: dict
|
2021-12-23 18:46:39 +00:00
|
|
|
"""
|
|
|
|
event = data["event"]
|
|
|
|
ts = data["timestamp"]
|
|
|
|
|
|
|
|
inside = data["data"]
|
|
|
|
txid = inside["id"]
|
|
|
|
txtype = inside["type"]
|
|
|
|
state = inside["state"]
|
2021-12-28 14:09:33 +00:00
|
|
|
if "reference" in inside:
|
|
|
|
reference = inside["reference"]
|
|
|
|
else:
|
|
|
|
reference = "not_given"
|
2021-12-23 18:46:39 +00:00
|
|
|
|
|
|
|
leg = inside["legs"][0]
|
|
|
|
|
2021-12-28 14:09:33 +00:00
|
|
|
if "counterparty" in leg:
|
|
|
|
account_type = leg["counterparty"]["account_type"]
|
|
|
|
else:
|
|
|
|
account_type = "not_given"
|
2021-12-23 18:46:39 +00:00
|
|
|
|
|
|
|
amount = leg["amount"]
|
|
|
|
currency = leg["currency"]
|
|
|
|
description = leg["description"]
|
|
|
|
|
|
|
|
to_store = {
|
|
|
|
"event": event,
|
|
|
|
"ts": ts,
|
|
|
|
"txid": txid,
|
|
|
|
"txtype": txtype,
|
|
|
|
"state": state,
|
|
|
|
"reference": reference,
|
|
|
|
"account_type": account_type,
|
|
|
|
"amount": amount,
|
|
|
|
"currency": currency,
|
|
|
|
"description": description,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.log.info("Transaction processed: {formatted}", formatted=dumps(to_store, indent=2))
|
|
|
|
r.hmset(f"tx.{txid}", to_store)
|
2021-12-28 13:41:36 +00:00
|
|
|
self.irc.client.msg(
|
|
|
|
self.irc.client.channel, f"AUTO Incoming transaction: {amount}{currency} ({reference}) - {state} - {description}"
|
|
|
|
)
|
2021-12-28 15:29:15 +00:00
|
|
|
stored_trade = r.hmget(f"trade.{reference}")
|
|
|
|
if not stored_trade["currency"] == currency:
|
|
|
|
self.irc.client.msg(self.irc.client.channel, f"Currency mismatch, Agora: {stored_trade['currency']} / Revolut: {currency}")
|
|
|
|
return
|
|
|
|
if not stored_trade["amount"] == amount:
|
|
|
|
self.irc.client.msg(self.irc.client.channel, f"Amount mismatch, Agora: {stored_trade['amount']} / Revolut: {amount}")
|
|
|
|
return
|
|
|
|
if not account_type == "Revolut":
|
|
|
|
self.irc.client.msg(self.irc.client.channel, f"Account type is not Revolut: {account_type}")
|
|
|
|
return
|
|
|
|
self.irc.client.msg(self.irc.client.channel, f"All checks passed, would release funds for {stored_trade['id']}")
|
|
|
|
|
|
|
|
def new_trade(self, trade_id, buyer, currency, amount, amount_xmr):
|
|
|
|
reference = "".join(choices(ascii_uppercase, k=5))
|
|
|
|
reference = f"XMR-{reference}"
|
|
|
|
print("NEW TRADE")
|
|
|
|
existing_ref = r.get(f"trade.{trade_id}.reference")
|
|
|
|
if not existing_ref:
|
|
|
|
r.set(f"trade.{trade_id}.reference", reference)
|
|
|
|
to_store = {
|
|
|
|
"id": trade_id,
|
|
|
|
"buyer": buyer,
|
|
|
|
"currency": currency,
|
|
|
|
"amount": amount,
|
|
|
|
"amount_xmr": amount_xmr,
|
|
|
|
"reference": reference,
|
|
|
|
}
|
|
|
|
self.log.info(f"Storing trade information: {to_store}")
|
|
|
|
r.hmset(f"trade.{reference}", to_store)
|
|
|
|
self.irc.client.msg(self.irc.client.channel, f"Would send reference to {trade_id}: {reference}")
|
|
|
|
else:
|
|
|
|
self.irc.client.msg(self.irc.client.channel, f"Using existing reference for stored trade {trade_id}: {existing_ref}")
|
2021-12-23 18:46:39 +00:00
|
|
|
|
2021-12-23 19:09:16 +00:00
|
|
|
def find_tx(self, reference, amount):
|
2021-12-27 21:02:38 +00:00
|
|
|
"""
|
|
|
|
Find transactions that match the given reference and amount.
|
|
|
|
:param reference: transaction reference in Revolut
|
|
|
|
:param amount: transaction amount
|
2021-12-27 21:20:15 +00:00
|
|
|
:type reference: string
|
|
|
|
:type amount: int
|
2021-12-27 21:02:38 +00:00
|
|
|
:return: transaction details or AMOUNT_INVALID, or False
|
2021-12-27 21:28:49 +00:00
|
|
|
:rtype: dict or string or bool
|
2021-12-27 21:02:38 +00:00
|
|
|
"""
|
2021-12-23 19:09:16 +00:00
|
|
|
all_transactions = r.scan(0, match="tx.*")
|
|
|
|
for tx_iter in all_transactions[1]:
|
|
|
|
tx_obj = r.hgetall(tx_iter)
|
|
|
|
if tx_obj[b"reference"] == str.encode(reference):
|
|
|
|
if tx_obj[b"amount"] == str.encode(amount):
|
2021-12-27 21:04:57 +00:00
|
|
|
return convert(tx_obj)
|
2021-12-23 19:09:16 +00:00
|
|
|
else:
|
|
|
|
return "AMOUNT_INVALID"
|
|
|
|
return False
|