From f1fb569e2b03515dd3881d6d48af3ce7403d52f5 Mon Sep 17 00:00:00 2001 From: benarc Date: Fri, 11 Feb 2022 10:03:26 +0000 Subject: [PATCH] lnurlpayout fix --- lnbits/extensions/lnurlpayout/tasks.py | 33 ++++++++++++++-------- lnbits/extensions/lnurlpayout/views_api.py | 29 +++++++++++++------ 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/lnbits/extensions/lnurlpayout/tasks.py b/lnbits/extensions/lnurlpayout/tasks.py index e0757129b..7f2a83248 100644 --- a/lnbits/extensions/lnurlpayout/tasks.py +++ b/lnbits/extensions/lnurlpayout/tasks.py @@ -1,15 +1,17 @@ import asyncio -import json +from http import HTTPStatus + import httpx +from starlette.exceptions import HTTPException from lnbits.core import db as core_db -from lnbits.core.models import Payment -from lnbits.tasks import register_invoice_listener -from lnbits.core.views.api import api_wallet from lnbits.core.crud import get_wallet -from lnbits.core.views.api import api_payment, api_payments_decode, pay_invoice +from lnbits.core.models import Payment +from lnbits.core.services import pay_invoice +from lnbits.core.views.api import api_payments_decode +from lnbits.tasks import register_invoice_listener -from .crud import get_lnurlpayout, get_lnurlpayout_from_wallet +from .crud import get_lnurlpayout_from_wallet async def wait_for_paid_invoices(): @@ -25,16 +27,16 @@ async def on_invoice_paid(payment: Payment) -> None: try: # Check its got a payout associated with it lnurlpayout_link = await get_lnurlpayout_from_wallet(payment.wallet_id) + print("LNURLpayout", lnurlpayout_link) if lnurlpayout_link: # Check the wallet balance is more than the threshold wallet = await get_wallet(lnurlpayout_link.wallet) - if wallet.balance < lnurlpayout_link.threshold + ( - lnurlpayout_link.threshold * 0.02 - ): - return + threshold = lnurlpayout_link.threshold + (lnurlpayout_link.threshold * 0.02) + if wallet.balance < threshold: + return # Get the invoice from the LNURL to pay async with httpx.AsyncClient() as client: try: @@ -43,6 +45,7 @@ async def on_invoice_paid(payment: Payment) -> None: raise HTTPException( status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken" ) + try: r = await client.get(str(url["domain"]), timeout=40) res = r.json() @@ -56,6 +59,12 @@ async def on_invoice_paid(payment: Payment) -> None: timeout=40, ) res = r.json() + + if hasattr(res, "status") and res["status"] == "ERROR": + raise HTTPException( + status_code=HTTPStatus.FORBIDDEN, + detail=res["reason"], + ) try: await pay_invoice( wallet_id=payment.wallet_id, @@ -65,7 +74,9 @@ async def on_invoice_paid(payment: Payment) -> None: return except: pass - except: + + except Exception as e: + print("ERROR", str(e)) return except (httpx.ConnectError, httpx.RequestError): return diff --git a/lnbits/extensions/lnurlpayout/views_api.py b/lnbits/extensions/lnurlpayout/views_api.py index d45de6fe6..67562909c 100644 --- a/lnbits/extensions/lnurlpayout/views_api.py +++ b/lnbits/extensions/lnurlpayout/views_api.py @@ -4,7 +4,8 @@ from fastapi import Query from fastapi.params import Depends from starlette.exceptions import HTTPException -from lnbits.core.crud import get_user, get_payments +from lnbits.core.crud import get_payments, get_user +from lnbits.core.models import Payment from lnbits.core.services import create_invoice from lnbits.core.views.api import api_payment, api_payments_decode from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key @@ -14,10 +15,10 @@ from .crud import ( create_lnurlpayout, delete_lnurlpayout, get_lnurlpayout, - get_lnurlpayouts, get_lnurlpayout_from_wallet, + get_lnurlpayouts, ) -from .models import lnurlpayout, CreateLnurlPayoutData +from .models import CreateLnurlPayoutData, lnurlpayout from .tasks import on_invoice_paid @@ -87,14 +88,24 @@ async def api_lnurlpayout_check( lnurlpayout_id: str, wallet: WalletTypeInfo = Depends(get_key_type) ): lnurlpayout = await get_lnurlpayout(lnurlpayout_id) - payments = await get_payments( - wallet_id=lnurlpayout.wallet, - complete=True, + ## THIS + mock_payment = Payment( + checking_id="mock", pending=False, - outgoing=True, - incoming=True, + amount=1, + fee=1, + time=0000, + bolt11="mock", + preimage="mock", + payment_hash="mock", + wallet_id=lnurlpayout.wallet, ) - result = await on_invoice_paid(payments[0]) + ## INSTEAD OF THIS + # payments = await get_payments( + # wallet_id=lnurlpayout.wallet, complete=True, pending=False, outgoing=True, incoming=True + # ) + + result = await on_invoice_paid(mock_payment) return