mirror of
https://github.com/RoboSats/robosats.git
synced 2025-01-31 02:21:35 +00:00
Fix reward withdrawal failure handling
This commit is contained in:
parent
4179a856bc
commit
a616e4945e
@ -232,7 +232,7 @@ class LNNode:
|
|||||||
)) # 200 ppm or 10 sats
|
)) # 200 ppm or 10 sats
|
||||||
request = routerrpc.SendPaymentRequest(payment_request=lnpayment.invoice,
|
request = routerrpc.SendPaymentRequest(payment_request=lnpayment.invoice,
|
||||||
fee_limit_sat=fee_limit_sat,
|
fee_limit_sat=fee_limit_sat,
|
||||||
timeout_seconds=60)
|
timeout_seconds=30)
|
||||||
|
|
||||||
for response in cls.routerstub.SendPaymentV2(request,
|
for response in cls.routerstub.SendPaymentV2(request,
|
||||||
metadata=[("macaroon",
|
metadata=[("macaroon",
|
||||||
@ -244,7 +244,7 @@ class LNNode:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
if response.status == 1: # Status 1 'IN_FLIGHT'
|
if response.status == 1: # Status 1 'IN_FLIGHT'
|
||||||
return True, "In flight"
|
pass
|
||||||
|
|
||||||
if response.status == 3: # Status 3 'FAILED'
|
if response.status == 3: # Status 3 'FAILED'
|
||||||
"""0 Payment isn't failed (yet).
|
"""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)
|
4 Payment details incorrect (unknown hash, invalid amt or invalid final cltv delta)
|
||||||
5 Insufficient local balance.
|
5 Insufficient local balance.
|
||||||
"""
|
"""
|
||||||
context = cls.payment_failure_context[response.failure_reason]
|
failure_reason = cls.payment_failure_context[response.failure_reason]
|
||||||
return False, context
|
lnpayment.status = LNPayment.Status.FAILRO
|
||||||
|
lnpayment.save()
|
||||||
|
return False, failure_reason
|
||||||
|
|
||||||
if response.status == 2: # STATUS 'SUCCEEDED'
|
if response.status == 2: # STATUS 'SUCCEEDED'
|
||||||
|
lnpayment.status = LNPayment.Status.SUCCED
|
||||||
|
lnpayment.save()
|
||||||
return True, None
|
return True, None
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
@ -1145,29 +1145,48 @@ class Logics:
|
|||||||
return False, {"bad_invoice": "You have not earned rewards"}
|
return False, {"bad_invoice": "You have not earned rewards"}
|
||||||
|
|
||||||
num_satoshis = user.profile.earned_rewards
|
num_satoshis = user.profile.earned_rewards
|
||||||
|
|
||||||
reward_payout = LNNode.validate_ln_invoice(invoice, num_satoshis)
|
reward_payout = LNNode.validate_ln_invoice(invoice, num_satoshis)
|
||||||
|
|
||||||
if not reward_payout["valid"]:
|
if not reward_payout["valid"]:
|
||||||
return False, reward_payout["context"]
|
return False, reward_payout["context"]
|
||||||
|
|
||||||
lnpayment = LNPayment.objects.create(
|
try:
|
||||||
concept= LNPayment.Concepts.WITHREWA,
|
lnpayment = LNPayment.objects.create(
|
||||||
type= LNPayment.Types.NORM,
|
concept= LNPayment.Concepts.WITHREWA,
|
||||||
sender= User.objects.get(username=ESCROW_USERNAME),
|
type= LNPayment.Types.NORM,
|
||||||
status= LNPayment.Status.VALIDI,
|
sender= User.objects.get(username=ESCROW_USERNAME),
|
||||||
receiver=user,
|
status= LNPayment.Status.VALIDI,
|
||||||
invoice= invoice,
|
receiver=user,
|
||||||
num_satoshis= num_satoshis,
|
invoice= invoice,
|
||||||
description= reward_payout["description"],
|
num_satoshis= num_satoshis,
|
||||||
payment_hash= reward_payout["payment_hash"],
|
description= reward_payout["description"],
|
||||||
created_at= reward_payout["created_at"],
|
payment_hash= reward_payout["payment_hash"],
|
||||||
expires_at= reward_payout["expires_at"],
|
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.earned_rewards = 0
|
||||||
user.profile.claimed_rewards += num_satoshis
|
user.profile.claimed_rewards += num_satoshis
|
||||||
user.profile.save()
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user