From a616e4945e016484e7742746a6e1df4c80d0537c Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Wed, 9 Mar 2022 03:35:50 -0800 Subject: [PATCH] Fix reward withdrawal failure handling --- api/lightning/node.py | 12 +++++++---- api/logics.py | 49 ++++++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/api/lightning/node.py b/api/lightning/node.py index b0da223c..28ad6847 100644 --- a/api/lightning/node.py +++ b/api/lightning/node.py @@ -232,7 +232,7 @@ class LNNode: )) # 200 ppm or 10 sats request = routerrpc.SendPaymentRequest(payment_request=lnpayment.invoice, fee_limit_sat=fee_limit_sat, - timeout_seconds=60) + timeout_seconds=30) for response in cls.routerstub.SendPaymentV2(request, metadata=[("macaroon", @@ -244,7 +244,7 @@ class LNNode: pass if response.status == 1: # Status 1 'IN_FLIGHT' - return True, "In flight" + pass if response.status == 3: # Status 3 'FAILED' """0 Payment isn't failed (yet). @@ -254,10 +254,14 @@ class LNNode: 4 Payment details incorrect (unknown hash, invalid amt or invalid final cltv delta) 5 Insufficient local balance. """ - context = cls.payment_failure_context[response.failure_reason] - return False, context + failure_reason = cls.payment_failure_context[response.failure_reason] + lnpayment.status = LNPayment.Status.FAILRO + lnpayment.save() + return False, failure_reason if response.status == 2: # STATUS 'SUCCEEDED' + lnpayment.status = LNPayment.Status.SUCCED + lnpayment.save() return True, None return False diff --git a/api/logics.py b/api/logics.py index 3cef3505..803ce98a 100644 --- a/api/logics.py +++ b/api/logics.py @@ -1145,29 +1145,48 @@ class Logics: return False, {"bad_invoice": "You have not earned rewards"} num_satoshis = user.profile.earned_rewards + reward_payout = LNNode.validate_ln_invoice(invoice, num_satoshis) if not reward_payout["valid"]: return False, reward_payout["context"] - lnpayment = LNPayment.objects.create( - concept= LNPayment.Concepts.WITHREWA, - type= LNPayment.Types.NORM, - sender= User.objects.get(username=ESCROW_USERNAME), - status= LNPayment.Status.VALIDI, - receiver=user, - invoice= invoice, - num_satoshis= num_satoshis, - description= reward_payout["description"], - payment_hash= reward_payout["payment_hash"], - created_at= reward_payout["created_at"], - expires_at= reward_payout["expires_at"], - ) + try: + lnpayment = LNPayment.objects.create( + concept= LNPayment.Concepts.WITHREWA, + type= LNPayment.Types.NORM, + sender= User.objects.get(username=ESCROW_USERNAME), + status= LNPayment.Status.VALIDI, + receiver=user, + invoice= invoice, + num_satoshis= num_satoshis, + description= reward_payout["description"], + payment_hash= reward_payout["payment_hash"], + created_at= reward_payout["created_at"], + expires_at= reward_payout["expires_at"], + ) + # Might fail if payment_hash already exists in DB + except: + return False, {"bad_invoice": "Give me a new invoice"} - if LNNode.pay_invoice(lnpayment): + user.profile.earned_rewards = 0 + user.profile.save() + + # Pays the invoice. + paid, failure_reason = LNNode.pay_invoice(lnpayment) + if paid: user.profile.earned_rewards = 0 user.profile.claimed_rewards += num_satoshis user.profile.save() + return True, None - return True, None + # If fails, adds the rewards again. + else: + user.profile.earned_rewards = num_satoshis + user.profile.save() + context = {} + context['bad_invoice'] = failure_reason + return False, context + +