Skip to content

Commit

Permalink
Filter expired orders on queries
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat committed Feb 24, 2025
1 parent 8ba9f09 commit 9b1e00e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
18 changes: 12 additions & 6 deletions api/logics.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def validate_already_maker_or_taker(cls, user):
queryset_taker[0],
)

queryset_take_order = TakeOrder.objects.filter(taker=user)
queryset_take_order = TakeOrder.objects.filter(
taker=user, expires_at__gt=timezone.now()
)
if not queryset_take_order.exists():
return (
False,
Expand Down Expand Up @@ -271,6 +273,7 @@ def order_expires(cls, order):
# Do not change order status if an order in any with
# any of these status is sent to expire here
does_not_expire = [
Order.Status.UCA,
Order.Status.EXP,
Order.Status.TLD,
Order.Status.DIS,
Expand Down Expand Up @@ -1002,7 +1005,9 @@ def cancel_order(cls, order, user, state=None):
Order.Status.MLD,
]

take_order_query_set = TakeOrder.objects.filter(taker=user, order=order)
take_order_query_set = TakeOrder.objects.filter(
taker=user, order=order, expires_at__gt=timezone.now()
)

if order.status in do_not_cancel:
return False, {"bad_request": "You cannot cancel this order"}
Expand Down Expand Up @@ -1031,7 +1036,9 @@ def cancel_order(cls, order, user, state=None):
):
# Return the maker bond (Maker gets returned the bond for cancelling public order)
if cls.return_bond(order.maker_bond):
queryset = TakeOrder.objects.filter(order=order)
queryset = TakeOrder.objects.filter(
order=order, expires_at__gt=timezone.now()
)
for idx, take_order in enumerate(queryset):
cls.cancel_bond(take_order.taker_bond)
order.log(
Expand Down Expand Up @@ -1155,8 +1162,7 @@ def cancel_order(cls, order, user, state=None):
elif take_order_query_set.exists():
take_order = take_order_query_set.first()
cls.cancel_bond(take_order.taker_bond)
take_order.delete()

take_order.update(expires_at=timezone.now())
order.log("Taker cancelled before locking the bond")

return True, None
Expand Down Expand Up @@ -1366,7 +1372,7 @@ def finalize_contract(cls, take_order):
f"<b>Contract formalized.</b> Maker: Robot({order.maker.robot.id},{order.maker}). Taker: Robot({order.taker.robot.id},{order.taker}). API median price {order.currency.exchange_rate} {dict(Currency.currency_choices)[order.currency.currency]}/BTC. Premium is {order.premium}%. Contract size {order.last_satoshis} Sats"
)

queryset = TakeOrder.objects.filter(order=order)
queryset = TakeOrder.objects.filter(order=order, expires_at__gt=timezone.now())
for idx, take_order in enumerate(queryset):
if take_order.id is not take_order.id:
cls.cancel_bond(take_order.taker_bond)
Expand Down
33 changes: 17 additions & 16 deletions api/management/commands/clean_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,23 @@ def clean_orders(self):

for idx, take_order in enumerate(take_order_queryset):
context = str(take_order.id)
try:
if Logics.take_order_expires(
take_order
): # Take order send to expire here
debug["expired_take_orders"].append({idx: context})

# It should not happen, but if it cannot locate the hold invoice
# it probably was cancelled by another thread, try to remove it anyways
except Exception as e:
debug["failed_take_order_expiry"].append({idx: context})
debug["reason_take_failure"].append({idx: str(e)})

if "unable to locate invoice" in str(e):
self.stdout.write(str(e))
take_order.delete()
debug["expired_take_orders"].append({idx: context})
# Dont't cancel bonds that made through the order and became taker bonds
if take_order.order.taker_bond is not take_order.taker_bond:
try:
if Logics.take_order_expires(
take_order
): # Take order send to expire here
debug["expired_take_orders"].append({idx: context})

# It should not happen, but if it cannot locate the hold invoice
# it probably was cancelled by another thread, try to remove it anyways
except Exception as e:
debug["failed_take_order_expiry"].append({idx: context})
debug["reason_take_failure"].append({idx: str(e)})

if "unable to locate invoice" in str(e):
self.stdout.write(str(e))
debug["expired_take_orders"].append({idx: context})

if debug["num_expired_take_orders"] > 0:
self.stdout.write(str(timezone.now()))
Expand Down
2 changes: 1 addition & 1 deletion api/management/commands/follow_invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def update_order_status(self, lnpayment):

# It is a taker bond => close contract.
elif take_order_query.exists():
take_order = take_order_query.last()
take_order = take_order_query.first()
take_order.order.log("Taker bond <b>locked</b>")
Logics.finalize_contract(take_order)
return
Expand Down
2 changes: 1 addition & 1 deletion api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def get(self, request, format=None):
data["penalty"] = request.user.robot.penalty_expiration

take_order_queryset = TakeOrder.objects.filter(
order_id=order_id, taker=request.user
order_id=order_id, taker=request.user, expires_at__gt=timezone.now()
)

# Add booleans if user is maker, taker, partipant, buyer or seller
Expand Down

0 comments on commit 9b1e00e

Please sign in to comment.