# Twisted/Klein imports from twisted.logger import Logger # Other library imports from json import dumps from random import choices from string import ascii_uppercase # Project imports from settings import settings from db import r from util import convert class Transactions(object): """ Handler class for incoming Revolut transactions. """ def __init__(self): """ Initialise the Transaction object. Set the logger. """ self.log = Logger("transactions") def set_agora(self, agora): self.agora = agora def set_irc(self, irc): self.irc = irc def transaction(self, data): """ Store details of transaction and post notifications to IRC. Matches it up with data stored in Redis to attempt to reconcile with an Agora trade. :param data: details of transaction :type data: dict """ event = data["event"] ts = data["timestamp"] inside = data["data"] txid = inside["id"] txtype = inside["type"] state = inside["state"] if "reference" in inside: reference = inside["reference"] else: reference = "not_given" leg = inside["legs"][0] if "counterparty" in leg: account_type = leg["counterparty"]["account_type"] else: account_type = "not_given" 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) self.irc.client.msg( self.irc.client.channel, f"AUTO Incoming transaction: {amount}{currency} ({reference}) - {state} - {description}" ) # Try getting the trade by the reference ID given stored_trade = self.get_ref(reference) if not stored_trade: self.log.info(f"No reference in DB for {reference}", reference=reference) self.irc.client.msg(self.irc.client.channel, f"No reference in DB for {reference}") # Try getting the trade by the reference, using it as a TXID ref2 = self.tx_to_ref(reference) if not ref2: self.log.info("No TXID in DB for {reference}", reference=reference) self.irc.client.msg(self.irc.client.channel, f"No TXID in DB for {reference}") return else: reference = ref2 amount = float(amount) stored_trade["amount"] = float(stored_trade["amount"]) # Make sure it was sent in the expected currency if not stored_trade["currency"] == currency: self.irc.client.msg(self.irc.client.channel, f"Currency mismatch, Agora: {stored_trade['currency']} / Revolut: {currency}") return # Make sure the expected amount was sent if not stored_trade["amount"] == amount: # If the amount does not match exactly, get the min and max values for our given acceptable margins for trades min_amount, max_amount = self.agora.get_acceptable_margins(currency, amount) self.irc.client.msg( self.irc.client.channel, f"Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}" ) if not min_amount < stored_trade["amount"] < max_amount: self.irc.client.msg( self.irc.client.channel, f"Amount mismatch - not in margins: {stored_trade['amount']} (min: {min_amount} / max: {max_amount}", ) return # Make sure the account type was Revolut, as these are completed instantly 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']} / {reference}") def new_trade(self, trade_id, buyer, currency, amount, amount_xmr): """ Called when we have a new trade in Agora. Store details in Redis, generate a reference and optionally let the customer know the reference. """ reference = "".join(choices(ascii_uppercase, k=5)) reference = f"XMR-{reference}" existing_ref = r.get(f"trade.{trade_id}.reference") # if existing_ref: # self.irc.client.msg(self.irc.client.channel, f"Existing reference for {trade_id}: {existing_ref.decode('utf-8')}") 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("Storing trade information: {info}", info=str(to_store)) r.hmset(f"trade.{reference}", to_store) self.irc.client.msg(self.irc.client.channel, f"Generated reference for {trade_id}: {reference}") if settings.Agora.Send == "1": self.agora.agora.contact_message_post(trade_id, f"Hi! When sending the payment please use reference code: {reference}") if existing_ref: return convert(existing_ref) else: return reference def find_tx(self, reference, amount): """ Find transactions that match the given reference and amount. :param reference: transaction reference in Revolut :param amount: transaction amount :type reference: string :type amount: int :return: transaction details or AMOUNT_INVALID, or False :rtype: dict or string or bool """ 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): return convert(tx_obj) else: return "AMOUNT_INVALID" return False def get_refs(self): """ Get all reference IDs for trades. :return: list of trade IDs :rtype: list """ references = [] ref_keys = r.keys("trade.*.reference") for key in ref_keys: references.append(r.get(key)) return convert(references) def get_ref_map(self): """ Get all reference IDs for trades. :return: dict of references keyed by TXID :rtype: dict """ references = {} ref_keys = r.keys("trade.*.reference") for key in ref_keys: tx = convert(key).split(".")[1] references[tx] = r.get(key) return convert(references) def get_ref(self, reference): """ Get a reference ID for a single trade. :return: trade ID :rtype: string """ ref_data = convert(r.hgetall(f"trade.{reference}")) if not ref_data: return False return ref_data def del_ref(self, reference): """ Delete a given reference from the Redis database. """ tx = self.ref_to_tx(reference) r.delete(f"trade.{reference}") r.delete(f"trade.{tx}.reference") def cleanup(self, references): for tx, reference in self.get_ref_map().items(): if reference not in references: self.log.info("Archiving trade reference: {reference} / TX: {tx}", reference=reference, tx=tx) r.rename(f"trade.{tx}.reference", f"archive.trade.{tx}.reference") r.rename(f"trade.{reference}", f"archive.trade.{reference}") def del_tx(self, txid): pass def tx_to_ref(self, tx): refs = self.get_refs() for reference in refs: ref_data = convert(r.hgetall(f"trade.{reference}")) if not ref_data: continue if ref_data["id"] == tx: return reference def ref_to_tx(self, reference): ref_data = convert(r.hgetall(f"trade.{reference}")) if not ref_data: return False return ref_data["id"]