Skip to content

Commit e549097

Browse files
authored
Handle hodl invoices asynchronously tasks.py
- Modified the on_invoice_paid function to call pay_invoice asynchronously using asyncio.create_task. - Added a new function pay_invoice_in_background to handle the asynchronous payment processing and exception handling. - Ensured that the main event loop remains responsive even when encountering hodl invoices by running pay_invoice in the background.
1 parent eb18fda commit e549097

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

tasks.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@ async def on_invoice_paid(payment: Payment) -> None:
4545
logger.trace(f"splitpayments: performing split payments to {len(targets)} targets")
4646

4747
for target in targets:
48-
4948
if target.percent > 0:
50-
5149
amount_msat = int(payment.amount * target.percent / 100)
5250
memo = (
5351
f"Split payment: {target.percent}% for {target.alias or target.wallet}"
5452
)
5553

56-
if target.wallet.find("@") >= 0 or target.wallet.find("LNURL") >= 0:
54+
if "@" in target.wallet or "LNURL" in target.wallet:
5755
safe_amount_msat = amount_msat - fee_reserve(amount_msat)
5856
payment_request = await get_lnurl_invoice(
5957
target.wallet, payment.wallet_id, safe_amount_msat, memo
@@ -69,12 +67,23 @@ async def on_invoice_paid(payment: Payment) -> None:
6967
extra = {**payment.extra, "tag": "splitpayments", "splitted": True}
7068

7169
if payment_request:
72-
await pay_invoice(
70+
asyncio.create_task(pay_invoice_in_background(
7371
payment_request=payment_request,
7472
wallet_id=payment.wallet_id,
7573
description=memo,
76-
extra=extra,
77-
)
74+
extra=extra
75+
))
76+
77+
async def pay_invoice_in_background(payment_request, wallet_id, description, extra):
78+
try:
79+
await pay_invoice(
80+
payment_request=payment_request,
81+
wallet_id=wallet_id,
82+
description=description,
83+
extra=extra,
84+
)
85+
except Exception as e:
86+
logger.error(f"Failed to pay invoice: {e}")
7887

7988

8089
async def get_lnurl_invoice(

0 commit comments

Comments
 (0)