Checks wallet balance is at threshold

This commit is contained in:
benarc 2021-12-08 10:28:43 +00:00
parent 410689e04b
commit 0ab158992e

View file

@ -2,10 +2,10 @@ import asyncio
import json import json
import httpx import httpx
from lnbits.core.crud import get_wallet
from lnbits.core import db as core_db from lnbits.core import db as core_db
from lnbits.core.models import Payment from lnbits.core.models import Payment
from lnbits.tasks import register_invoice_listener from lnbits.tasks import register_invoice_listener
from lnbits.core.api import api_wallet
from .crud import get_lnurlpayout from .crud import get_lnurlpayout
@ -20,33 +20,38 @@ async def wait_for_paid_invoices():
async def on_invoice_paid(payment: Payment) -> None: async def on_invoice_paid(payment: Payment) -> None:
# Check its got a payout associated with it try:
lnurlpayout_link = await get_lnurlpayout(payment.extra.get("link", -1)) # Check its got a payout associated with it
lnurlpayout_link = await get_lnurlpayout(payment.extra.get("link", -1))
if lnurlpayout_link: if lnurlpayout_link:
# Check the wallet balance is more than the threshold # Check the wallet balance is more than the threshold
wallet = api_wallet(payment.wallet_id)
# Getthe invoice from the LNURL to pay if wallet.balance < lnurlpayout_link.threshold:
async with httpx.AsyncClient() as client: return
try: # Get the invoice from the LNURL to pay
url = await api_payments_decode({"data": data.lnurlpay}) async with httpx.AsyncClient() as client:
if str(url["domain"])[0:4] != "http":
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken")
try: try:
r = await client.post( url = await api_payments_decode({"data": data.lnurlpay})
str(url["domain"]), if str(url["domain"])[0:4] != "http":
json={ raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken")
"payment_hash": payment.payment_hash, try:
"payment_request": payment.bolt11, r = await client.post(
"amount": payment.amount, str(url["domain"]),
"comment": payment.extra.get("comment"), json={
"lnurlp": pay_link.id, "payment_hash": payment.payment_hash,
}, "payment_request": payment.bolt11,
timeout=40, "amount": payment.amount,
) "comment": payment.extra.get("comment"),
await mark_webhook_sent(payment, r.status_code) "lnurlp": pay_link.id,
except (httpx.ConnectError, httpx.RequestError): },
await mark_webhook_sent(payment, -1) timeout=40,
)
await mark_webhook_sent(payment, r.status_code)
except (httpx.ConnectError, httpx.RequestError):
await mark_webhook_sent(payment, -1)
except Exception: except Exception:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout") raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout")
except:
return