Skip to content

Commit

Permalink
Handle hodl invoices asynchronously tasks.py
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
santyr authored Jul 16, 2024
1 parent eb18fda commit e549097
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ async def on_invoice_paid(payment: Payment) -> None:
logger.trace(f"splitpayments: performing split payments to {len(targets)} targets")

for target in targets:

if target.percent > 0:

amount_msat = int(payment.amount * target.percent / 100)
memo = (
f"Split payment: {target.percent}% for {target.alias or target.wallet}"
)

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

if payment_request:
await pay_invoice(
asyncio.create_task(pay_invoice_in_background(
payment_request=payment_request,
wallet_id=payment.wallet_id,
description=memo,
extra=extra,
)
extra=extra
))

async def pay_invoice_in_background(payment_request, wallet_id, description, extra):
try:
await pay_invoice(
payment_request=payment_request,
wallet_id=wallet_id,
description=description,
extra=extra,
)
except Exception as e:
logger.error(f"Failed to pay invoice: {e}")


async def get_lnurl_invoice(
Expand Down

0 comments on commit e549097

Please sign in to comment.