2021-12-03 10:50:17 +00:00
|
|
|
import asyncio
|
2022-02-11 10:03:26 +00:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
2021-12-03 10:50:17 +00:00
|
|
|
import httpx
|
2022-07-07 14:30:16 +02:00
|
|
|
from loguru import logger
|
2022-02-11 10:03:26 +00:00
|
|
|
from starlette.exceptions import HTTPException
|
2021-12-03 10:50:17 +00:00
|
|
|
|
|
|
|
from lnbits.core import db as core_db
|
2022-02-11 10:03:26 +00:00
|
|
|
from lnbits.core.crud import get_wallet
|
2021-12-03 10:50:17 +00:00
|
|
|
from lnbits.core.models import Payment
|
2022-02-11 10:03:26 +00:00
|
|
|
from lnbits.core.services import pay_invoice
|
|
|
|
from lnbits.core.views.api import api_payments_decode
|
2022-10-04 09:51:47 +02:00
|
|
|
from lnbits.helpers import get_current_extension_name
|
2021-12-03 10:50:17 +00:00
|
|
|
from lnbits.tasks import register_invoice_listener
|
|
|
|
|
2022-02-11 10:03:26 +00:00
|
|
|
from .crud import get_lnurlpayout_from_wallet
|
2021-12-03 10:50:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def wait_for_paid_invoices():
|
|
|
|
invoice_queue = asyncio.Queue()
|
2022-10-04 09:51:47 +02:00
|
|
|
register_invoice_listener(invoice_queue, get_current_extension_name())
|
2021-12-03 10:50:17 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
payment = await invoice_queue.get()
|
|
|
|
await on_invoice_paid(payment)
|
|
|
|
|
|
|
|
|
|
|
|
async def on_invoice_paid(payment: Payment) -> None:
|
2021-12-08 10:28:43 +00:00
|
|
|
try:
|
|
|
|
# Check its got a payout associated with it
|
2021-12-08 11:43:13 +00:00
|
|
|
lnurlpayout_link = await get_lnurlpayout_from_wallet(payment.wallet_id)
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.debug("LNURLpayout", lnurlpayout_link)
|
2021-12-08 10:28:43 +00:00
|
|
|
if lnurlpayout_link:
|
2022-01-30 19:43:30 +00:00
|
|
|
|
2021-12-08 10:28:43 +00:00
|
|
|
# Check the wallet balance is more than the threshold
|
2022-01-30 19:43:30 +00:00
|
|
|
|
2021-12-19 22:28:19 +00:00
|
|
|
wallet = await get_wallet(lnurlpayout_link.wallet)
|
2022-02-11 10:03:26 +00:00
|
|
|
threshold = lnurlpayout_link.threshold + (lnurlpayout_link.threshold * 0.02)
|
2022-01-30 19:43:30 +00:00
|
|
|
|
2022-02-11 10:03:26 +00:00
|
|
|
if wallet.balance < threshold:
|
|
|
|
return
|
2021-12-08 10:28:43 +00:00
|
|
|
# Get the invoice from the LNURL to pay
|
|
|
|
async with httpx.AsyncClient() as client:
|
2021-12-06 13:07:47 +00:00
|
|
|
try:
|
2021-12-19 22:28:19 +00:00
|
|
|
url = await api_payments_decode({"data": lnurlpayout_link.lnurlpay})
|
2021-12-08 10:28:43 +00:00
|
|
|
if str(url["domain"])[0:4] != "http":
|
2022-01-30 19:43:30 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken"
|
2021-12-08 10:28:43 +00:00
|
|
|
)
|
2022-02-11 10:03:26 +00:00
|
|
|
|
2022-01-30 19:43:30 +00:00
|
|
|
try:
|
|
|
|
r = await client.get(str(url["domain"]), timeout=40)
|
2021-12-08 11:43:13 +00:00
|
|
|
res = r.json()
|
|
|
|
try:
|
|
|
|
r = await client.get(
|
2022-01-30 19:43:30 +00:00
|
|
|
res["callback"]
|
|
|
|
+ "?amount="
|
|
|
|
+ str(
|
|
|
|
int((wallet.balance - wallet.balance * 0.02) * 1000)
|
|
|
|
),
|
2021-12-08 11:43:13 +00:00
|
|
|
timeout=40,
|
|
|
|
)
|
|
|
|
res = r.json()
|
2022-02-11 10:03:26 +00:00
|
|
|
|
|
|
|
if hasattr(res, "status") and res["status"] == "ERROR":
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.FORBIDDEN,
|
|
|
|
detail=res["reason"],
|
|
|
|
)
|
2021-12-19 22:58:23 +00:00
|
|
|
try:
|
|
|
|
await pay_invoice(
|
|
|
|
wallet_id=payment.wallet_id,
|
|
|
|
payment_request=res["pr"],
|
|
|
|
extra={"tag": "lnurlpayout"},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
pass
|
2022-02-11 10:03:26 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print("ERROR", str(e))
|
2021-12-08 11:43:13 +00:00
|
|
|
return
|
2021-12-08 10:28:43 +00:00
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
2021-12-08 11:43:13 +00:00
|
|
|
return
|
2021-12-08 10:28:43 +00:00
|
|
|
except Exception:
|
2022-01-30 19:43:30 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.FORBIDDEN,
|
|
|
|
detail="Failed to save LNURLPayout",
|
|
|
|
)
|
2021-12-08 10:28:43 +00:00
|
|
|
except:
|
2022-01-30 19:43:30 +00:00
|
|
|
return
|