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
|
2021-12-28 23:58:00 +00:00
|
|
|
from settings import settings
|
2021-12-23 18:46:39 +00:00
|
|
|
from db import r
|
2021-12-28 23:58:00 +00:00
|
|
|
from util import convert
|
2021-12-27 21:04:57 +00:00
|
|
|
|
|
|
|
|
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):
|
|
|
|
"""
|
2021-12-28 23:58:00 +00:00
|
|
|
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.
|
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)
|
2022-01-01 16:39:36 +00:00
|
|
|
self.irc.sendmsg(f"AUTO Incoming transaction: {amount}{currency} ({reference}) - {state} - {description}")
|
2022-01-01 19:33:15 +00:00
|
|
|
|
|
|
|
# Partial reference implementation
|
2022-01-01 19:20:20 +00:00
|
|
|
# Account for silly people not removing the default string
|
|
|
|
# Split the reference into parts
|
|
|
|
ref_split = reference.split(" ")
|
|
|
|
# Get all existing references
|
|
|
|
existing_refs = self.get_refs()
|
|
|
|
# Get all parts of the given reference split that match the existing references
|
|
|
|
stored_trade_reference = set(existing_refs).intersection(set(ref_split))
|
|
|
|
if len(stored_trade_reference) > 1:
|
|
|
|
self.log.error("Multiple references valid for TXID {txid}: {reference}", txid=txid, reference=reference)
|
|
|
|
self.irc.sendmsg(f"Multiple references valid for TXID {txid}: {reference}")
|
|
|
|
return
|
|
|
|
|
|
|
|
stored_trade = False
|
2022-01-01 19:33:15 +00:00
|
|
|
looked_up_without_reference = False
|
|
|
|
|
|
|
|
# Amount/currency lookup implementation
|
2022-01-01 19:20:20 +00:00
|
|
|
if not stored_trade_reference:
|
|
|
|
self.log.info(f"No reference in DB refs for {reference}", reference=reference)
|
|
|
|
self.irc.sendmsg(f"No reference in DB refs for {reference}")
|
|
|
|
# Try checking just amount and currency, as some people (usually people buying small amounts)
|
|
|
|
# are unable to put in a reference properly.
|
|
|
|
|
|
|
|
self.log.info("Checking against amount and currency for TXID {txid}", txid=txid)
|
|
|
|
self.irc.sendmsg(f"Checking against amount and currency for TXID {txid}")
|
|
|
|
stored_trade = self.find_trade(txid, currency, amount)
|
|
|
|
if not stored_trade:
|
|
|
|
self.log.info(
|
|
|
|
"Failed to get reference by amount and currency: {txid} {currency} {amount}",
|
|
|
|
txid=txid,
|
|
|
|
currency=currency,
|
|
|
|
amount=amount,
|
|
|
|
)
|
|
|
|
self.irc.sendmsg(f"Failed to get reference by amount and currency: {txid} {currency} {amount}")
|
|
|
|
return
|
2022-01-01 20:01:01 +00:00
|
|
|
if currency == "USD":
|
|
|
|
amount_usd = amount
|
|
|
|
else:
|
2022-01-01 20:22:53 +00:00
|
|
|
rates = self.agora.get_rates_all()
|
2022-01-01 20:01:01 +00:00
|
|
|
amount_usd = amount / rates[currency]
|
|
|
|
# Amount is reliable here as it is checked by find_trade, so no need for stored_trade["amount"]
|
2022-01-01 20:22:53 +00:00
|
|
|
if float(amount_usd) > float(settings.Agora.AcceptableAltLookupUSD):
|
2022-01-01 19:20:20 +00:00
|
|
|
self.log.info("Not checking against amount and currency as amount exceeds MAX")
|
|
|
|
self.irc.sendmsg(f"Not checking against amount and currency as amount exceeds MAX")
|
|
|
|
# Close here if the amount exceeds the allowable limit for no reference
|
|
|
|
return
|
2022-01-01 19:33:15 +00:00
|
|
|
# Note that we have looked it up without reference so we don't use +- below
|
2022-01-01 20:01:01 +00:00
|
|
|
# This might be redundant given the amount checks in find_trade, but better safe than sorry!
|
2022-01-01 19:33:15 +00:00
|
|
|
looked_up_without_reference = True
|
2022-01-01 19:20:20 +00:00
|
|
|
if not stored_trade:
|
|
|
|
stored_trade = self.get_ref(stored_trade_reference.pop())
|
2021-12-28 17:47:07 +00:00
|
|
|
if not stored_trade:
|
2022-01-01 20:27:30 +00:00
|
|
|
self.log.info("No reference in DB for {reference}", reference=reference)
|
2022-01-01 16:39:36 +00:00
|
|
|
self.irc.sendmsg(f"No reference in DB for {reference}")
|
2021-12-28 17:47:07 +00:00
|
|
|
return
|
2021-12-29 15:26:23 +00:00
|
|
|
|
2021-12-28 17:47:07 +00:00
|
|
|
amount = float(amount)
|
|
|
|
stored_trade["amount"] = float(stored_trade["amount"])
|
2021-12-29 15:32:38 +00:00
|
|
|
|
|
|
|
# Make sure it was sent in the expected currency
|
2021-12-28 15:29:15 +00:00
|
|
|
if not stored_trade["currency"] == currency:
|
2022-01-01 20:27:30 +00:00
|
|
|
self.log.info(
|
|
|
|
"Currency mismatch, Agora: {currency_agora} / Revolut: {currency}",
|
|
|
|
currency_agora=stored_trade["currency"],
|
|
|
|
currency=currency,
|
|
|
|
)
|
2022-01-01 16:39:36 +00:00
|
|
|
self.irc.sendmsg(f"Currency mismatch, Agora: {stored_trade['currency']} / Revolut: {currency}")
|
2021-12-28 15:29:15 +00:00
|
|
|
return
|
2021-12-29 15:32:38 +00:00
|
|
|
|
|
|
|
# Make sure the expected amount was sent
|
2021-12-28 15:29:15 +00:00
|
|
|
if not stored_trade["amount"] == amount:
|
2022-01-01 19:33:15 +00:00
|
|
|
if looked_up_without_reference:
|
|
|
|
return
|
2021-12-28 23:58:00 +00:00
|
|
|
# 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)
|
2022-01-01 20:27:30 +00:00
|
|
|
self.log.info(
|
|
|
|
"Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}",
|
|
|
|
min_amount=min_amount,
|
|
|
|
max_amount=max_amount,
|
|
|
|
)
|
2022-01-01 16:39:36 +00:00
|
|
|
self.irc.sendmsg(f"Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}")
|
2021-12-28 23:58:00 +00:00
|
|
|
if not min_amount < stored_trade["amount"] < max_amount:
|
2022-01-01 20:27:30 +00:00
|
|
|
self.log.info(
|
|
|
|
"Amount mismatch - not in margins: {amount} (min: {min_amount} / max: {max_amount}",
|
|
|
|
amount=stored_trade["amount"],
|
|
|
|
min_amount=min_amount,
|
|
|
|
max_amount=max_amount,
|
|
|
|
)
|
2022-01-01 16:39:36 +00:00
|
|
|
self.irc.sendmsg(f"Amount mismatch - not in margins: {stored_trade['amount']} (min: {min_amount} / max: {max_amount}")
|
2021-12-28 23:58:00 +00:00
|
|
|
return
|
2021-12-29 15:32:38 +00:00
|
|
|
# Make sure the account type was Revolut, as these are completed instantly
|
2021-12-28 17:47:07 +00:00
|
|
|
if not account_type == "revolut":
|
2022-01-01 20:27:30 +00:00
|
|
|
self.log.info("Account type is not Revolut: {account_type}", account_type=account_type)
|
2022-01-01 16:39:36 +00:00
|
|
|
self.irc.sendmsg(f"Account type is not Revolut: {account_type}")
|
2021-12-28 15:29:15 +00:00
|
|
|
return
|
2022-01-01 20:27:30 +00:00
|
|
|
self.log.info("All checks passed, releasing funds for {trade_id} {reference}", trade_id=stored_trade["id"], reference=reference)
|
2022-01-01 20:22:53 +00:00
|
|
|
self.irc.sendmsg(f"All checks passed, releasing funds for {stored_trade['id']} / {reference}")
|
2022-01-01 19:20:20 +00:00
|
|
|
# rtrn = self.agora.release_funds(stored_trade["id"])
|
|
|
|
# self.agora.agora.contact_message_post(stored_trade["id"], "Thanks! Releasing now :)")
|
|
|
|
# self.irc.sendmsg(dumps(rtrn))
|
2021-12-28 15:29:15 +00:00
|
|
|
|
|
|
|
def new_trade(self, trade_id, buyer, currency, amount, amount_xmr):
|
2021-12-29 00:04:24 +00:00
|
|
|
"""
|
|
|
|
Called when we have a new trade in Agora.
|
|
|
|
Store details in Redis, generate a reference and optionally let the customer know the reference.
|
|
|
|
"""
|
2021-12-28 15:29:15 +00:00
|
|
|
reference = "".join(choices(ascii_uppercase, k=5))
|
|
|
|
reference = f"XMR-{reference}"
|
|
|
|
existing_ref = r.get(f"trade.{trade_id}.reference")
|
2021-12-29 14:32:06 +00:00
|
|
|
if not existing_ref:
|
2021-12-28 15:29:15 +00:00
|
|
|
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,
|
|
|
|
}
|
2021-12-29 15:05:21 +00:00
|
|
|
self.log.info("Storing trade information: {info}", info=str(to_store))
|
2021-12-28 15:29:15 +00:00
|
|
|
r.hmset(f"trade.{reference}", to_store)
|
2022-01-01 16:39:36 +00:00
|
|
|
self.irc.sendmsg(f"Generated reference for {trade_id}: {reference}")
|
2021-12-28 23:58:00 +00:00
|
|
|
if settings.Agora.Send == "1":
|
|
|
|
self.agora.agora.contact_message_post(trade_id, f"Hi! When sending the payment please use reference code: {reference}")
|
2021-12-29 18:40:44 +00:00
|
|
|
if existing_ref:
|
|
|
|
return convert(existing_ref)
|
|
|
|
else:
|
|
|
|
return reference
|
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
|
2021-12-28 23:58:00 +00:00
|
|
|
|
2022-01-01 19:20:20 +00:00
|
|
|
def find_trade(self, txid, currency, amount):
|
|
|
|
"""
|
|
|
|
Get a trade reference that matches the given currency and amount.
|
|
|
|
Only works if there is one result.
|
|
|
|
:param txid: Revolut transaction ID
|
|
|
|
:param currency: currency
|
|
|
|
:param amount: amount
|
|
|
|
:type txid: string
|
|
|
|
:type currency: string
|
|
|
|
:type amount: int
|
|
|
|
:return: matching trade object or False
|
|
|
|
:rtype: dict or bool
|
|
|
|
"""
|
|
|
|
refs = self.get_refs()
|
|
|
|
matching_refs = []
|
|
|
|
for ref in refs:
|
|
|
|
stored_trade = self.get_ref(ref)
|
2022-01-01 20:22:53 +00:00
|
|
|
if stored_trade["currency"] == currency and float(stored_trade["amount"]) == float(amount):
|
2022-01-01 19:20:20 +00:00
|
|
|
matching_refs.append(stored_trade)
|
|
|
|
if len(matching_refs) != 1:
|
|
|
|
self.log.error("Find trade returned multiple results for TXID {txid}: {matching_refs}", txid=txid, matching_refs=matching_refs)
|
|
|
|
return False
|
|
|
|
return matching_refs[0]
|
|
|
|
|
2021-12-28 23:58:00 +00:00
|
|
|
def get_refs(self):
|
2021-12-29 00:04:24 +00:00
|
|
|
"""
|
|
|
|
Get all reference IDs for trades.
|
|
|
|
:return: list of trade IDs
|
|
|
|
:rtype: list
|
|
|
|
"""
|
2021-12-28 23:58:00 +00:00
|
|
|
references = []
|
|
|
|
ref_keys = r.keys("trade.*.reference")
|
|
|
|
for key in ref_keys:
|
|
|
|
references.append(r.get(key))
|
|
|
|
return convert(references)
|
|
|
|
|
2021-12-29 15:03:47 +00:00
|
|
|
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)
|
|
|
|
|
2021-12-28 23:58:00 +00:00
|
|
|
def get_ref(self, reference):
|
2021-12-29 00:04:24 +00:00
|
|
|
"""
|
|
|
|
Get a reference ID for a single trade.
|
|
|
|
:return: trade ID
|
|
|
|
:rtype: string
|
|
|
|
"""
|
2021-12-30 14:15:07 +00:00
|
|
|
ref_data = r.hgetall(f"trade.{reference}")
|
|
|
|
ref_data = convert(ref_data)
|
2021-12-28 23:58:00 +00:00
|
|
|
if not ref_data:
|
|
|
|
return False
|
|
|
|
return ref_data
|
|
|
|
|
|
|
|
def del_ref(self, reference):
|
2021-12-29 00:04:24 +00:00
|
|
|
"""
|
|
|
|
Delete a given reference from the Redis database.
|
|
|
|
"""
|
2021-12-29 15:03:47 +00:00
|
|
|
tx = self.ref_to_tx(reference)
|
2021-12-28 23:58:00 +00:00
|
|
|
r.delete(f"trade.{reference}")
|
2021-12-29 15:03:47 +00:00
|
|
|
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}")
|
2021-12-28 23:58:00 +00:00
|
|
|
|
|
|
|
def del_tx(self, txid):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tx_to_ref(self, tx):
|
2021-12-29 14:32:06 +00:00
|
|
|
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
|
2021-12-28 23:58:00 +00:00
|
|
|
|
|
|
|
def ref_to_tx(self, reference):
|
2021-12-29 14:32:06 +00:00
|
|
|
ref_data = convert(r.hgetall(f"trade.{reference}"))
|
|
|
|
if not ref_data:
|
|
|
|
return False
|
|
|
|
return ref_data["id"]
|