diff --git a/api/lightning/node.py b/api/lightning/node.py index 4f552b60..05d4c544 100644 --- a/api/lightning/node.py +++ b/api/lightning/node.py @@ -265,6 +265,7 @@ class LNNode: from api.models import LNPayment status = lnpayment.status + expiry_height = 0 lnd_response_state_to_lnpayment_status = { 0: LNPayment.Status.INVGEN, # OPEN @@ -280,15 +281,16 @@ class LNNode: ) response = cls.invoicesstub.LookupInvoiceV2(request) - # try saving expiry height + status = lnd_response_state_to_lnpayment_status[response.state] + + # get expiry height if hasattr(response, "htlcs"): try: - lnpayment.expiry_height = response.htlcs[0].expiry_height + for htlc in response.htlcs: + expiry_height = max(expiry_height, htlc.expiry_height) except Exception: pass - status = lnd_response_state_to_lnpayment_status[response.state] - except Exception as e: # If it fails at finding the invoice: it has been canceled. # In RoboSats DB we make a distinction between cancelled and returned (LND does not) @@ -304,7 +306,7 @@ class LNNode: else: print(str(e)) - return status + return status, expiry_height @classmethod def resetmc(cls): diff --git a/api/management/commands/follow_invoices.py b/api/management/commands/follow_invoices.py index 1b19cce1..33ce82d9 100644 --- a/api/management/commands/follow_invoices.py +++ b/api/management/commands/follow_invoices.py @@ -77,7 +77,7 @@ class Command(BaseCommand): for idx, hold_lnpayment in enumerate(queryset): old_status = hold_lnpayment.status - new_status = LNNode.lookup_invoice_status(hold_lnpayment) + new_status, expiry_height = LNNode.lookup_invoice_status(hold_lnpayment) # Only save the hold_payments that change (otherwise this function does not scale) changed = not old_status == new_status @@ -94,8 +94,9 @@ class Command(BaseCommand): # if these are still different, we update the lnpayment with its new status. lnpayment.status = new_status + lnpayment.expiry_height = expiry_height self.update_order_status(lnpayment) - lnpayment.save(update_fields=["status"]) + lnpayment.save(update_fields=["status", "expiry_height"]) # Report for debugging old = LNPayment.Status(old_status).label