From 6a4290c0e9b4984416448e4023aee60039e43bf1 Mon Sep 17 00:00:00 2001 From: benarc Date: Sun, 19 Dec 2021 22:28:19 +0000 Subject: [PATCH] paying function works --- lnbits/extensions/lnurlpayout/crud.py | 2 +- lnbits/extensions/lnurlpayout/models.py | 2 +- lnbits/extensions/lnurlpayout/tasks.py | 27 ++++++++++++---------- lnbits/extensions/lnurlpayout/views_api.py | 5 ++-- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lnbits/extensions/lnurlpayout/crud.py b/lnbits/extensions/lnurlpayout/crud.py index 873099ab..4a09a223 100644 --- a/lnbits/extensions/lnurlpayout/crud.py +++ b/lnbits/extensions/lnurlpayout/crud.py @@ -11,7 +11,7 @@ async def create_lnurlpayout(wallet_id: str, admin_key: str, data: CreateLnurlPa await db.execute( """ INSERT INTO lnurlpayout.lnurlpayouts (id, title, wallet, admin_key, lnurlpay, threshold) - VALUES (?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?) """, (lnurlpayout_id, data.title, wallet_id, admin_key, data.lnurlpay, data.threshold), ) diff --git a/lnbits/extensions/lnurlpayout/models.py b/lnbits/extensions/lnurlpayout/models.py index e4bc7bce..3d6a1401 100644 --- a/lnbits/extensions/lnurlpayout/models.py +++ b/lnbits/extensions/lnurlpayout/models.py @@ -13,4 +13,4 @@ class lnurlpayout(BaseModel): wallet: str admin_key: str lnurlpay: str - threshold: str + threshold: int diff --git a/lnbits/extensions/lnurlpayout/tasks.py b/lnbits/extensions/lnurlpayout/tasks.py index cf4e3b24..5f02bade 100644 --- a/lnbits/extensions/lnurlpayout/tasks.py +++ b/lnbits/extensions/lnurlpayout/tasks.py @@ -6,6 +6,8 @@ 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 .crud import get_lnurlpayout, get_lnurlpayout_from_wallet @@ -23,19 +25,18 @@ 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_link) if lnurlpayout_link: - print("poo11") + # Check the wallet balance is more than the threshold - wallet = await api_wallet(lnurlpayout_link.admin_key) - print("poo1") - if wallet.balance + (wallet.balance/100*2) < lnurlpayout_link.threshold: + + wallet = await get_wallet(lnurlpayout_link.wallet) + if wallet.balance < lnurlpayout_link.threshold + (lnurlpayout_link.threshold*0.02): return - print("poo2") + # Get the invoice from the LNURL to pay async with httpx.AsyncClient() as client: try: - url = await api_payments_decode({"data": data.lnurlpay}) + url = await api_payments_decode({"data": lnurlpayout_link.lnurlpay}) if str(url["domain"])[0:4] != "http": raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken") try: @@ -44,16 +45,18 @@ async def on_invoice_paid(payment: Payment) -> None: timeout=40, ) res = r.json() - print(res) - print(res["callback"]) try: r = await client.get( - res["callback"] + "?amount=" + (lnurlpayout_link.threshold * 1000), + res["callback"] + "?amount=" + str(int((wallet.balance - wallet.balance*0.02) * 1000)), timeout=40, ) res = r.json() - print(res["pr"]) - await api_payments_pay_invoice(res["pr"], payment.wallet_id) + await pay_invoice( + wallet_id=payment.wallet_id, + payment_request=res["pr"], + extra={"tag": "lnurlpayout"}, + ) + return except: return except (httpx.ConnectError, httpx.RequestError): diff --git a/lnbits/extensions/lnurlpayout/views_api.py b/lnbits/extensions/lnurlpayout/views_api.py index dda7142d..7be97d97 100644 --- a/lnbits/extensions/lnurlpayout/views_api.py +++ b/lnbits/extensions/lnurlpayout/views_api.py @@ -18,7 +18,7 @@ from .tasks import on_invoice_paid async def api_lnurlpayouts( all_wallets: bool = Query(None), wallet: WalletTypeInfo = Depends(get_key_type) ): - wallet_ids = [wallet.wallet.id] + wallet_ids = wallet.wallet.id if all_wallets: wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids @@ -39,7 +39,8 @@ async def api_lnurlpayout_create( if str(url["domain"])[0:4] != "http": raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not valid LNURL") return - lnurlpayout = await create_lnurlpayout(wallet_id=wallet.wallet.id, admin_key=wallet.admin_key, data=data) + print(wallet) + lnurlpayout = await create_lnurlpayout(wallet_id=wallet.wallet.id, admin_key=wallet.wallet.adminkey, data=data) if not lnurlpayout: raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout") return