2022-01-17 16:41:55 +00:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2022-01-17 18:11:44 +00:00
|
|
|
|
2022-01-17 16:41:55 +00:00
|
|
|
from api.lightning.node import LNNode
|
2022-01-17 23:11:41 +00:00
|
|
|
from api.models import LNPayment, Order
|
|
|
|
from api.logics import Logics
|
|
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
from datetime import timedelta
|
2022-01-17 16:41:55 +00:00
|
|
|
from decouple import config
|
|
|
|
from base64 import b64decode
|
|
|
|
import time
|
|
|
|
|
|
|
|
MACAROON = b64decode(config('LND_MACAROON_BASE64'))
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
'''
|
2022-01-17 23:11:41 +00:00
|
|
|
Background: SubscribeInvoices stub iterator would be great to use here.
|
|
|
|
However, it only sends updates when the invoice is OPEN (new) or SETTLED.
|
2022-01-17 16:41:55 +00:00
|
|
|
We are very interested on the other two states (CANCELLED and ACCEPTED).
|
|
|
|
Therefore, this thread (follow_invoices) will iterate over all LNpayment
|
2022-01-17 23:11:41 +00:00
|
|
|
objects and do InvoiceLookupV2 every X seconds to update their state 'live'
|
|
|
|
'''
|
2022-01-17 16:41:55 +00:00
|
|
|
|
|
|
|
help = 'Follows all active hold invoices'
|
2022-01-18 13:20:19 +00:00
|
|
|
rest = 5 # seconds between consecutive checks for invoice updates
|
2022-01-17 16:41:55 +00:00
|
|
|
|
|
|
|
# def add_arguments(self, parser):
|
|
|
|
# parser.add_argument('debug', nargs='+', type=boolean)
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
''' Follows and updates LNpayment objects
|
|
|
|
until settled or canceled'''
|
|
|
|
|
2022-01-19 19:37:10 +00:00
|
|
|
# TODO handle 'database is locked'
|
|
|
|
|
2022-01-17 16:41:55 +00:00
|
|
|
lnd_state_to_lnpayment_status = {
|
2022-01-17 23:11:41 +00:00
|
|
|
0: LNPayment.Status.INVGEN, # OPEN
|
|
|
|
1: LNPayment.Status.SETLED, # SETTLED
|
|
|
|
2: LNPayment.Status.CANCEL, # CANCELLED
|
|
|
|
3: LNPayment.Status.LOCKED # ACCEPTED
|
2022-01-17 16:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stub = LNNode.invoicesstub
|
|
|
|
|
|
|
|
while True:
|
2022-01-18 13:20:19 +00:00
|
|
|
time.sleep(self.rest)
|
2022-01-17 16:41:55 +00:00
|
|
|
|
|
|
|
# time it for debugging
|
|
|
|
t0 = time.time()
|
|
|
|
queryset = LNPayment.objects.filter(type=LNPayment.Types.HOLD, status__in=[LNPayment.Status.INVGEN, LNPayment.Status.LOCKED])
|
|
|
|
|
|
|
|
debug = {}
|
|
|
|
debug['num_active_invoices'] = len(queryset)
|
|
|
|
debug['invoices'] = []
|
2022-01-17 23:11:41 +00:00
|
|
|
at_least_one_changed = False
|
2022-01-17 16:41:55 +00:00
|
|
|
|
|
|
|
for idx, hold_lnpayment in enumerate(queryset):
|
|
|
|
old_status = LNPayment.Status(hold_lnpayment.status).label
|
|
|
|
|
|
|
|
try:
|
|
|
|
request = LNNode.invoicesrpc.LookupInvoiceMsg(payment_hash=bytes.fromhex(hold_lnpayment.payment_hash))
|
|
|
|
response = stub.LookupInvoiceV2(request, metadata=[('macaroon', MACAROON.hex())])
|
|
|
|
hold_lnpayment.status = lnd_state_to_lnpayment_status[response.state]
|
2022-01-18 22:17:41 +00:00
|
|
|
|
2022-01-17 23:11:41 +00:00
|
|
|
except Exception as e:
|
2022-01-18 20:52:10 +00:00
|
|
|
# If it fails at finding the invoice it has been canceled.
|
|
|
|
# On RoboSats DB we make a distinction between cancelled and returned (LND does not)
|
2022-01-19 13:32:54 +00:00
|
|
|
if 'unable to locate invoice' in str(e):
|
2022-01-19 19:37:10 +00:00
|
|
|
self.stdout.write('unable to locate invoice')
|
2022-01-17 23:11:41 +00:00
|
|
|
hold_lnpayment.status = LNPayment.Status.CANCEL
|
2022-01-18 20:52:10 +00:00
|
|
|
# LND restarted.
|
2022-01-19 13:32:54 +00:00
|
|
|
if 'wallet locked, unlock it' in str(e):
|
2022-01-18 20:52:10 +00:00
|
|
|
self.stdout.write(str(timezone.now())+':: Wallet Locked')
|
|
|
|
# Other write to logs
|
2022-01-17 23:11:41 +00:00
|
|
|
else:
|
|
|
|
self.stdout.write(str(e))
|
2022-01-17 16:41:55 +00:00
|
|
|
|
|
|
|
new_status = LNPayment.Status(hold_lnpayment.status).label
|
|
|
|
|
|
|
|
# Only save the hold_payments that change (otherwise this function does not scale)
|
|
|
|
changed = not old_status==new_status
|
|
|
|
if changed:
|
2022-01-17 23:11:41 +00:00
|
|
|
# self.handle_status_change(hold_lnpayment, old_status)
|
2022-01-17 16:41:55 +00:00
|
|
|
hold_lnpayment.save()
|
2022-01-17 23:11:41 +00:00
|
|
|
self.update_order_status(hold_lnpayment)
|
|
|
|
|
|
|
|
# Report for debugging
|
|
|
|
new_status = LNPayment.Status(hold_lnpayment.status).label
|
|
|
|
debug['invoices'].append({idx:{
|
|
|
|
'payment_hash': str(hold_lnpayment.payment_hash),
|
|
|
|
'old_status': old_status,
|
|
|
|
'new_status': new_status,
|
|
|
|
}})
|
|
|
|
|
|
|
|
at_least_one_changed = at_least_one_changed or changed
|
|
|
|
|
|
|
|
debug['time']=time.time()-t0
|
|
|
|
|
|
|
|
if at_least_one_changed:
|
|
|
|
self.stdout.write(str(timezone.now()))
|
|
|
|
self.stdout.write(str(debug))
|
|
|
|
|
|
|
|
|
|
|
|
def update_order_status(self, lnpayment):
|
|
|
|
''' Background process following LND hold invoices
|
2022-01-18 13:20:19 +00:00
|
|
|
can catch LNpayments changing status. If they do,
|
2022-01-18 16:57:55 +00:00
|
|
|
the order status might have to change too.'''
|
2022-01-17 23:11:41 +00:00
|
|
|
|
|
|
|
# If the LNPayment goes to LOCKED (ACCEPTED)
|
|
|
|
if lnpayment.status == LNPayment.Status.LOCKED:
|
2022-01-18 16:57:55 +00:00
|
|
|
try:
|
|
|
|
# It is a maker bond => Publish order.
|
2022-01-18 22:17:41 +00:00
|
|
|
if hasattr(lnpayment, 'order_made' ):
|
2022-01-18 16:57:55 +00:00
|
|
|
Logics.publish_order(lnpayment.order_made)
|
|
|
|
return
|
|
|
|
|
|
|
|
# It is a taker bond => close contract.
|
2022-01-18 22:17:41 +00:00
|
|
|
elif hasattr(lnpayment, 'order_taken' ):
|
2022-01-18 16:57:55 +00:00
|
|
|
if lnpayment.order_taken.status == Order.Status.TAK:
|
|
|
|
Logics.finalize_contract(lnpayment.order_taken)
|
|
|
|
return
|
2022-01-17 23:11:41 +00:00
|
|
|
|
2022-01-18 16:57:55 +00:00
|
|
|
# It is a trade escrow => move foward order status.
|
2022-01-18 22:17:41 +00:00
|
|
|
elif hasattr(lnpayment, 'order_escrow' ):
|
2022-01-18 16:57:55 +00:00
|
|
|
Logics.trade_escrow_received(lnpayment.order_escrow)
|
2022-01-18 13:20:19 +00:00
|
|
|
return
|
2022-01-18 16:57:55 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.stdout.write(str(e))
|
2022-01-18 13:20:19 +00:00
|
|
|
|
2022-01-18 20:52:10 +00:00
|
|
|
# TODO If a lnpayment goes from LOCKED to INVGED. Totally weird
|
2022-01-18 16:57:55 +00:00
|
|
|
# halt the order
|
|
|
|
if lnpayment.status == LNPayment.Status.LOCKED:
|
|
|
|
pass
|