diff --git a/api/logics.py b/api/logics.py index 5bbccb72c..c61302e58 100644 --- a/api/logics.py +++ b/api/logics.py @@ -996,7 +996,17 @@ def is_penalized(user): return False, None @classmethod - def cancel_order(cls, order, user, state=None): + def cancel_order(cls, order, user, cancel_status=None): + # If cancel status is specified, do no cancel the order + # if it is not the correct one. + # This prevents the client from cancelling an order that + # recently changed status. + if cancel_status is not None: + if order.status != cancel_status: + return False, { + "bad_request": f"Current order status is {order.status}, not {cancel_status}." + } + # Do not change order status if an is in order # any of these status do_not_cancel = [ diff --git a/api/oas_schemas.py b/api/oas_schemas.py index 8d214f327..f2a7de0c5 100644 --- a/api/oas_schemas.py +++ b/api/oas_schemas.py @@ -245,6 +245,10 @@ class OrderViewSchema: - `17` - Maker lost dispute - `18` - Taker lost dispute + The client can use `cancel_status` to cancel the order only + if it is in the specified status. The server will + return an error without cancelling the trade otherwise. + Note that there are penalties involved for cancelling a order mid-trade so use this action carefully: diff --git a/api/serializers.py b/api/serializers.py index 46fb1c582..a2a083ab4 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -641,6 +641,13 @@ class UpdateOrderSerializer(serializers.Serializer): mining_fee_rate = serializers.DecimalField( max_digits=6, decimal_places=3, allow_null=True, required=False, default=None ) + cancel_status = serializers.ChoiceField( + choices=Order.Status.choices, + allow_null=True, + allow_blank=True, + default=None, + help_text="Status the order should have for it to be cancelled.", + ) class ClaimRewardSerializer(serializers.Serializer): diff --git a/api/views.py b/api/views.py index a03dbd33d..f6939eeee 100644 --- a/api/views.py +++ b/api/views.py @@ -518,6 +518,7 @@ def take_update_confirm_dispute_cancel(self, request, format=None): mining_fee_rate = serializer.data.get("mining_fee_rate") statement = serializer.data.get("statement") rating = serializer.data.get("rating") + cancel_status = serializer.data.get("cancel_status") # 1) If action is take, it is a taker request! if action == "take": @@ -593,7 +594,7 @@ def take_update_confirm_dispute_cancel(self, request, format=None): # 3) If action is cancel elif action == "cancel": - valid, context = Logics.cancel_order(order, request.user) + valid, context = Logics.cancel_order(order, request.user, cancel_status) if not valid: return Response(context, status.HTTP_400_BAD_REQUEST) diff --git a/docs/assets/schemas/api-latest.yaml b/docs/assets/schemas/api-latest.yaml index 04a0577fa..d70ac5009 100644 --- a/docs/assets/schemas/api-latest.yaml +++ b/docs/assets/schemas/api-latest.yaml @@ -1996,6 +1996,9 @@ components: format: decimal pattern: ^-?\d{0,3}(?:\.\d{0,3})?$ nullable: true + cancel_status: + allOf: + - $ref: '#/components/schemas/StatusEnum' required: - action Version: diff --git a/frontend/src/components/TradeBox/index.tsx b/frontend/src/components/TradeBox/index.tsx index 223e8db3b..1b4d66309 100644 --- a/frontend/src/components/TradeBox/index.tsx +++ b/frontend/src/components/TradeBox/index.tsx @@ -150,6 +150,7 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element => mining_fee_rate?: number; statement?: string; rating?: number; + cancel_status?: number; } const renewOrder = function (): void { @@ -188,6 +189,7 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element => mining_fee_rate, statement, rating, + cancel_status }: SubmitActionProps): void { const slot = garage.getSlot(); @@ -201,6 +203,7 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element => mining_fee_rate, statement, rating, + cancel_status }) .then((data: Order) => { setOpen(closeAll); @@ -222,8 +225,14 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element => }; const cancel = function (): void { + const order = garage.getSlot()?.order; + const noConfirmation = Boolean(order?.is_maker && [0, 1, 2].includes(order?.status)); + setLoadingButtons({ ...noLoadingButtons, cancel: true }); - submitAction({ action: 'cancel' }); + submitAction({ + action: 'cancel', + cancel_status: noConfirmation ? order?.status : undefined + }); }; const openDispute = function (): void { diff --git a/frontend/src/models/Order.model.ts b/frontend/src/models/Order.model.ts index ede9e4bdf..acae1da35 100644 --- a/frontend/src/models/Order.model.ts +++ b/frontend/src/models/Order.model.ts @@ -21,6 +21,7 @@ export interface SubmitActionProps { statement?: string; rating?: number; amount?: number; + cancel_status?: number; } export interface TradeRobotSummary { diff --git a/robosats/settings.py b/robosats/settings.py index 555061f06..75547f91c 100644 --- a/robosats/settings.py +++ b/robosats/settings.py @@ -143,6 +143,9 @@ } }, "REDOC_DIST": "SIDECAR", + "ENUM_NAME_OVERRIDES": { + "StatusEnum": "api.models.order.Order.Status", + } } diff --git a/tests/test_trade_pipeline.py b/tests/test_trade_pipeline.py index c226b5c01..6bf8d3b14 100644 --- a/tests/test_trade_pipeline.py +++ b/tests/test_trade_pipeline.py @@ -760,6 +760,58 @@ def test_cancel_public_order(self): f"❌ Hey {maker_nick}, you have cancelled your public order with ID {trade.order_id}.", ) + def test_cancel_order_cancel_status(self): + """ + Tests the cancellation of a public order using cancel_status. + """ + trade = Trade(self.client) + trade.publish_order() + data = trade.response.json() + + self.assertEqual(trade.response.status_code, 200) + self.assertResponse(trade.response) + + self.assertEqual(data["status_message"], Order.Status(Order.Status.WFB).label) + + # Cancel order if the order status is waiting for maker bond + trade.cancel_order(cancel_status=Order.Status.WFB) + data = trade.response.json() + + self.assertEqual(trade.response.status_code, 400) + self.assertResponse(trade.response) + + self.assertEqual( + data["bad_request"], "This order has been cancelled by the maker" + ) + + def test_cancel_order_different_cancel_status(self): + """ + Tests the cancellation of a paused order with a different cancel_status. + """ + trade = Trade(self.client) + trade.get_order() + data = trade.response.json() + + self.assertEqual(trade.response.status_code, 200) + self.assertResponse(trade.response) + + self.assertEqual(data["status_message"], Order.Status(Order.Status.WFB).label) + + # Try to cancel order if it is public + trade.cancel_order(cancel_status=Order.Status.PUB) + data = trade.response.json() + + self.assertEqual(trade.response.status_code, 400) + self.assertResponse(trade.response) + + self.assertEqual( + data["bad_request"], + f"Current order status is {Order.Status.WFB}, not {Order.Status.PUB}." + ) + + # Cancel order to avoid leaving pending HTLCs after a successful test + trade.cancel_order() + def test_collaborative_cancel_order_in_chat(self): """ Tests the collaborative cancellation of an order in the chat state diff --git a/tests/utils/trade.py b/tests/utils/trade.py index 39bcda0a1..0ed012fd0 100644 --- a/tests/utils/trade.py +++ b/tests/utils/trade.py @@ -112,11 +112,13 @@ def get_order(self, robot_index=1, first_encounter=False): self.response = self.client.get(path + params, **headers) @patch("api.tasks.send_notification.delay", send_notification) - def cancel_order(self, robot_index=1): + def cancel_order(self, robot_index=1, cancel_status=None): path = reverse("order") params = f"?order_id={self.order_id}" headers = self.get_robot_auth(robot_index) body = {"action": "cancel"} + if cancel_status is not None: + body.update({"cancel_status": cancel_status}) self.response = self.client.post(path + params, body, **headers) @patch("api.tasks.send_notification.delay", send_notification)