Bump platform and requisition throughput on successful trades

This commit is contained in:
2023-03-20 13:54:56 +00:00
parent 9627fb7d41
commit 04f5595a86
10 changed files with 139 additions and 28 deletions

View File

@@ -50,10 +50,13 @@ class AggregatorClient(ABC):
self.instance.currencies = currencies
self.instance.save()
async def process_transactions(self, account_id, transactions):
async def process_transactions(self, account_id, transactions, req):
if not transactions:
return False
if not req:
return False
platforms = self.instance.platforms
for transaction in transactions:
transaction_id = transaction["transaction_id"]
@@ -71,6 +74,7 @@ class AggregatorClient(ABC):
"note": transaction["reference"],
}
tx_obj = self.instance.add_transaction(
req,
account_id,
tx_cast,
)

View File

@@ -294,7 +294,9 @@ class NordigenClient(BaseClient, AggregatorClient):
else:
raise Exception(f"No way to get reference: {transaction}")
async def get_transactions(self, account_id, process=False, pending=False):
async def get_transactions(
self, account_id, req=None, process=False, pending=False
):
"""
Get all transactions for an account.
:param account_id: account to fetch transactions for
@@ -307,7 +309,7 @@ class NordigenClient(BaseClient, AggregatorClient):
self.normalise_transactions(parsed, state="booked")
if process:
await self.process_transactions(account_id, parsed)
await self.process_transactions(account_id, parsed, req=req)
if pending:
parsed_pending = response["pending"]
self.normalise_transactions(parsed_pending, state="pending")

View File

@@ -750,6 +750,53 @@ class LocalPlatformClient(ABC):
return True
async def reset_trade_tx(self, stored_trade, tx_obj):
"""
Remove a trade to point to a given transaction ID.
"""
if not tx_obj.reconciled:
return False
if tx_obj not in stored_trade.linked.all():
return False
stored_trade.linked.remove(tx_obj)
stored_trade.save()
tx_obj.reconciled = False
tx_obj.save()
return True
async def successful_release(self, trade, transaction):
"""
Called when a trade has been successfully released.
Increment the platform and requisition throughput by the trade value.
Currently only XMR is supported.
"""
if trade.asset != "XMR":
raise NotImplementedError("Only XMR is supported at the moment.")
# Increment the platform throughput
trade.platform.throughput += trade.amount_crypto
# Increment the requisition throughput
if transaction.requisition is not None:
transaction.requisition.throughput += trade.amount_crypto
async def successful_withdrawal(self):
platforms = self.instance.platforms.all()
aggregators = self.instance.aggregators.all()
for platform in platforms:
platform.throughput = 0
platform.save()
for aggregator in aggregators:
for requisition in aggregator.requisitions.all():
requisition.throughput = 0
requisition.save()
async def release_map_trade(self, stored_trade, tx_obj):
"""
Map a trade to a transaction and release if no other TX is
@@ -761,9 +808,15 @@ class LocalPlatformClient(ABC):
is_updated = await self.update_trade_tx(stored_trade, tx_obj)
if is_updated is True:
# We mapped the trade successfully
await self.release_trade_escrow(trade_id, stored_trade.reference)
await antifraud.add_bank_sender(platform_buyer, bank_sender)
return True
released = await self.release_trade_escrow(trade_id, stored_trade.reference)
if not released:
# We failed to release the funds
# Set the TX back to not reconciled, so we can try this again
await self.reset_trade_tx(stored_trade, tx_obj)
return False
await self.successful_release(stored_trade, tx_obj)
return released
else:
# Already mapped
log.error(

View File

@@ -119,3 +119,5 @@ class AgoraClient(LocalPlatformClient, BaseClient):
# self.irc.sendmsg(f"Withdrawal: {rtrn1['success']} | {rtrn2['success']}")
# self.ux.notify.notify_withdrawal(half_rounded)
# await self.successful_withdrawal()