From 143ab9ec5d00315976ff42fe32d6263e780d65a5 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi <90936742+Reckless-Satoshi@users.noreply.github.com> Date: Mon, 19 Jun 2023 20:06:11 +0000 Subject: [PATCH] Add admin action to solve disputes as successful trades (#672) --- api/admin.py | 41 ++++++++ .../0039_alter_currency_currency.py | 98 +++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 api/migrations/0039_alter_currency_currency.py diff --git a/api/admin.py b/api/admin.py index 9cf90596..b771c041 100644 --- a/api/admin.py +++ b/api/admin.py @@ -130,6 +130,7 @@ class OrderAdmin(AdminChangeLinksMixin, admin.ModelAdmin): "maker_wins", "taker_wins", "return_everything", + "successful_trade", "compute_median_trade_time", ] @@ -251,6 +252,46 @@ class OrderAdmin(AdminChangeLinksMixin, admin.ModelAdmin): messages.ERROR, ) + @admin.action(description="Solve dispute: successful trade") + def successful_trade(self, request, queryset): + """ + Solves a dispute as if the trade had been successful, i.e., + returns both bonds (added as compensations) and triggers the payout. + """ + for order in queryset: + if ( + order.status in [Order.Status.DIS, Order.Status.WFR] + and order.is_disputed + ): + + order.maker.robot.earned_rewards = order.maker_bond.num_satoshis + order.maker.robot.save(update_fields=["earned_rewards"]) + order.taker.robot.earned_rewards = order.taker_bond.num_satoshis + order.taker.robot.save(update_fields=["earned_rewards"]) + + if order.is_swap: + order.payout_tx.status = OnchainPayment.Status.VALID + order.payout_tx.save(update_fields=["status"]) + order.status = Order.Status.SUC + else: + order.status = Order.Status.PAY + order.save(update_fields=["status"]) + + Logics.pay_buyer(order) + + self.message_user( + request, + f"Dispute of order {order.id} solved as successful trade", + messages.SUCCESS, + ) + + else: + self.message_user( + request, + f"Order {order.id} is not in a disputed state", + messages.ERROR, + ) + @admin.action(description="Compute median trade completion time") def compute_median_trade_time(self, request, queryset): """ diff --git a/api/migrations/0039_alter_currency_currency.py b/api/migrations/0039_alter_currency_currency.py new file mode 100644 index 00000000..0c12925e --- /dev/null +++ b/api/migrations/0039_alter_currency_currency.py @@ -0,0 +1,98 @@ +# Generated by Django 4.2.2 on 2023-06-19 19:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("api", "0038_alter_lnpayment_order_donated"), + ] + + operations = [ + migrations.AlterField( + model_name="currency", + name="currency", + field=models.PositiveSmallIntegerField( + choices=[ + (1, "USD"), + (2, "EUR"), + (3, "JPY"), + (4, "GBP"), + (5, "AUD"), + (6, "CAD"), + (7, "CHF"), + (8, "CNY"), + (9, "HKD"), + (10, "NZD"), + (11, "SEK"), + (12, "KRW"), + (13, "SGD"), + (14, "NOK"), + (15, "MXN"), + (16, "BYN"), + (17, "RUB"), + (18, "ZAR"), + (19, "TRY"), + (20, "BRL"), + (21, "CLP"), + (22, "CZK"), + (23, "DKK"), + (24, "HRK"), + (25, "HUF"), + (26, "INR"), + (27, "ISK"), + (28, "PLN"), + (29, "RON"), + (30, "ARS"), + (31, "VES"), + (32, "COP"), + (33, "PEN"), + (34, "UYU"), + (35, "PYG"), + (36, "BOB"), + (37, "IDR"), + (38, "ANG"), + (39, "CRC"), + (40, "CUP"), + (41, "DOP"), + (42, "GHS"), + (43, "GTQ"), + (44, "ILS"), + (45, "JMD"), + (46, "KES"), + (47, "KZT"), + (48, "MYR"), + (49, "NAD"), + (50, "NGN"), + (51, "AZN"), + (52, "PAB"), + (53, "PHP"), + (54, "PKR"), + (55, "QAR"), + (56, "SAR"), + (57, "THB"), + (58, "TTD"), + (59, "VND"), + (60, "XOF"), + (61, "TWD"), + (62, "TZS"), + (63, "XAF"), + (64, "UAH"), + (65, "EGP"), + (66, "LKR"), + (67, "MAD"), + (68, "AED"), + (69, "TND"), + (70, "ETB"), + (71, "GEL"), + (72, "UGX"), + (73, "RSD"), + (74, "IRT"), + (75, "BDT"), + (300, "XAU"), + (1000, "BTC"), + ], + unique=True, + ), + ), + ]