From 82b70991a430f07c6359ad7733210b68cb50c26a Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 28 Dec 2021 23:58:00 +0000 Subject: [PATCH] Implement the min/max acceptable margin checks --- handler/transactions.py | 60 +++++++++++++++++++++++++++++------------ handler/util.py | 13 +++++++++ 2 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 handler/util.py diff --git a/handler/transactions.py b/handler/transactions.py index 1756488..87c78fe 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -7,20 +7,9 @@ from random import choices from string import ascii_uppercase # Project imports +from settings import settings from db import r - - -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 +from util import convert class Transactions(object): @@ -43,7 +32,8 @@ class Transactions(object): def transaction(self, data): """ - Store details of transaction. + 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 """ @@ -99,8 +89,17 @@ class Transactions(object): 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 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 if not account_type == "revolut": self.irc.client.msg(self.irc.client.channel, f"Account type is not Revolut: {account_type}") return @@ -125,7 +124,8 @@ class Transactions(object): self.log.info(f"Storing trade information: {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}") - self.agora.agora.contact_message_post(trade_id, f"Hi! When sending the payment please user reference code: {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}") def find_tx(self, reference, amount): """ @@ -146,3 +146,29 @@ class Transactions(object): else: return "AMOUNT_INVALID" return False + + def get_refs(self): + references = [] + ref_keys = r.keys("trade.*.reference") + for key in ref_keys: + references.append(r.get(key)) + return convert(references) + + def get_ref(self, reference): + ref_data = convert(r.hgetall(f"trade.{reference}")) + if not ref_data: + return False + return ref_data + + def del_ref(self, reference): + r.delete(f"trade.{reference}") + r.delete(f"trade.{reference}.reference") + + def del_tx(self, txid): + pass + + def tx_to_ref(self, tx): + pass + + def ref_to_tx(self, reference): + pass diff --git a/handler/util.py b/handler/util.py new file mode 100644 index 0000000..9133ae8 --- /dev/null +++ b/handler/util.py @@ -0,0 +1,13 @@ +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) + if isinstance(data, list): + return list(map(convert, data)) + return data