From 0ab158992ee8ae95423799a328c242f8d0332974 Mon Sep 17 00:00:00 2001 From: benarc Date: Wed, 8 Dec 2021 10:28:43 +0000 Subject: [PATCH] Checks wallet balance is at threshold --- lnbits/extensions/lnurlpayout/tasks.py | 61 ++++++++++++++------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/lnbits/extensions/lnurlpayout/tasks.py b/lnbits/extensions/lnurlpayout/tasks.py index a97bbbb4..169b05b2 100644 --- a/lnbits/extensions/lnurlpayout/tasks.py +++ b/lnbits/extensions/lnurlpayout/tasks.py @@ -2,10 +2,10 @@ import asyncio import json import httpx -from lnbits.core.crud import get_wallet from lnbits.core import db as core_db from lnbits.core.models import Payment from lnbits.tasks import register_invoice_listener +from lnbits.core.api import api_wallet from .crud import get_lnurlpayout @@ -20,33 +20,38 @@ async def wait_for_paid_invoices(): async def on_invoice_paid(payment: Payment) -> None: - # Check its got a payout associated with it - lnurlpayout_link = await get_lnurlpayout(payment.extra.get("link", -1)) + try: + # Check its got a payout associated with it + lnurlpayout_link = await get_lnurlpayout(payment.extra.get("link", -1)) - if lnurlpayout_link: - # Check the wallet balance is more than the threshold - - # Getthe invoice from the LNURL to pay - async with httpx.AsyncClient() as client: - try: - url = await api_payments_decode({"data": data.lnurlpay}) - if str(url["domain"])[0:4] != "http": - raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken") + if lnurlpayout_link: + # Check the wallet balance is more than the threshold + wallet = api_wallet(payment.wallet_id) + if wallet.balance < lnurlpayout_link.threshold: + return + # Get the invoice from the LNURL to pay + async with httpx.AsyncClient() as client: try: - r = await client.post( - str(url["domain"]), - json={ - "payment_hash": payment.payment_hash, - "payment_request": payment.bolt11, - "amount": payment.amount, - "comment": payment.extra.get("comment"), - "lnurlp": pay_link.id, - }, - timeout=40, - ) - await mark_webhook_sent(payment, r.status_code) - except (httpx.ConnectError, httpx.RequestError): - await mark_webhook_sent(payment, -1) + url = await api_payments_decode({"data": data.lnurlpay}) + if str(url["domain"])[0:4] != "http": + raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken") + try: + r = await client.post( + str(url["domain"]), + json={ + "payment_hash": payment.payment_hash, + "payment_request": payment.bolt11, + "amount": payment.amount, + "comment": payment.extra.get("comment"), + "lnurlp": pay_link.id, + }, + timeout=40, + ) + await mark_webhook_sent(payment, r.status_code) + except (httpx.ConnectError, httpx.RequestError): + await mark_webhook_sent(payment, -1) - except Exception: - raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout") \ No newline at end of file + except Exception: + raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout") + except: + return \ No newline at end of file