Fix reward withdrawal failure handling

This commit is contained in:
Reckless_Satoshi 2022-03-09 03:35:50 -08:00
parent 4179a856bc
commit a616e4945e
No known key found for this signature in database
GPG Key ID: 9C4585B561315571
2 changed files with 42 additions and 19 deletions

View File

@ -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

View File

@ -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