Should work, listener logic in

This commit is contained in:
benarc 2021-12-08 11:43:13 +00:00
parent 0ab158992e
commit 7e6b2ba029
3 changed files with 40 additions and 26 deletions

View File

@ -25,6 +25,10 @@ async def get_lnurlpayout(lnurlpayout_id: str) -> Optional[lnurlpayout]:
row = await db.fetchone("SELECT * FROM lnurlpayout.lnurlpayouts WHERE id = ?", (lnurlpayout_id,)) row = await db.fetchone("SELECT * FROM lnurlpayout.lnurlpayouts WHERE id = ?", (lnurlpayout_id,))
return lnurlpayout(**row) if row else None return lnurlpayout(**row) if row else None
async def get_lnurlpayout_from_wallet(wallet_id: str) -> Optional[lnurlpayout]:
row = await db.fetchone("SELECT * FROM lnurlpayout.lnurlpayouts WHERE wallet = ?", (wallet_id,))
return lnurlpayout(**row) if row else None
async def get_lnurlpayouts(wallet_ids: Union[str, List[str]]) -> List[lnurlpayout]: async def get_lnurlpayouts(wallet_ids: Union[str, List[str]]) -> List[lnurlpayout]:
if isinstance(wallet_ids, str): if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids] wallet_ids = [wallet_ids]

View File

@ -5,9 +5,9 @@ import httpx
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 lnbits.core.views.api import api_wallet
from .crud import get_lnurlpayout from .crud import get_lnurlpayout, get_lnurlpayout_from_wallet
async def wait_for_paid_invoices(): async def wait_for_paid_invoices():
@ -22,12 +22,13 @@ async def wait_for_paid_invoices():
async def on_invoice_paid(payment: Payment) -> None: async def on_invoice_paid(payment: Payment) -> None:
try: try:
# Check its got a payout associated with it # Check its got a payout associated with it
lnurlpayout_link = await get_lnurlpayout(payment.extra.get("link", -1)) lnurlpayout_link = await get_lnurlpayout_from_wallet(payment.wallet_id)
print(lnurlpayout_link)
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) wallet = await api_wallet(payment.wallet_id)
if wallet.balance < lnurlpayout_link.threshold: if wallet.balance + (wallet.balance/100*2) < lnurlpayout_link.threshold:
return return
# Get the invoice from the LNURL to pay # Get the invoice from the LNURL to pay
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
@ -36,20 +37,24 @@ async def on_invoice_paid(payment: Payment) -> None:
if str(url["domain"])[0:4] != "http": if str(url["domain"])[0:4] != "http":
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken") raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken")
try: try:
r = await client.post( r = await client.get(
str(url["domain"]), 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, timeout=40,
) )
await mark_webhook_sent(payment, r.status_code) res = r.json()
print(res)
try:
r = await client.get(
res.callback + "?amount=" + (lnurlpayout_link.threshold * 1000),
timeout=40,
)
res = r.json()
print(res)
await api_payments_pay_invoice(res.pr, payment.wallet_id)
except:
return
except (httpx.ConnectError, httpx.RequestError): except (httpx.ConnectError, httpx.RequestError):
await mark_webhook_sent(payment, -1) return
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")

View File

@ -10,7 +10,7 @@ from lnbits.core.views.api import api_payment, api_payments_decode
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
from . import lnurlpayout_ext from . import lnurlpayout_ext
from .crud import create_lnurlpayout, delete_lnurlpayout, get_lnurlpayout, get_lnurlpayouts from .crud import create_lnurlpayout, delete_lnurlpayout, get_lnurlpayout, get_lnurlpayouts, get_lnurlpayout_from_wallet
from .models import lnurlpayout, CreateLnurlPayoutData from .models import lnurlpayout, CreateLnurlPayoutData
@ -29,17 +29,22 @@ async def api_lnurlpayouts(
async def api_lnurlpayout_create( async def api_lnurlpayout_create(
data: CreateLnurlPayoutData, wallet: WalletTypeInfo = Depends(get_key_type) data: CreateLnurlPayoutData, wallet: WalletTypeInfo = Depends(get_key_type)
): ):
try: print(await get_lnurlpayout_from_wallet(wallet.wallet.id))
if await get_lnurlpayout_from_wallet(wallet.wallet.id):
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Wallet already has lnurlpayout set")
return
url = await api_payments_decode({"data": data.lnurlpay}) url = await api_payments_decode({"data": data.lnurlpay})
if "domain" not in url:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL could not be decoded")
return
if str(url["domain"])[0:4] != "http": if str(url["domain"])[0:4] != "http":
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout") raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not valid LNURL")
return
lnurlpayout = await create_lnurlpayout(wallet_id=wallet.wallet.id, data=data) lnurlpayout = await create_lnurlpayout(wallet_id=wallet.wallet.id, data=data)
return lnurlpayout.dict() if not lnurlpayout:
except Exception:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout") raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout")
return
return lnurlpayout.dict()
@lnurlpayout_ext.delete("/api/v1/lnurlpayouts/{lnurlpayout_id}") @lnurlpayout_ext.delete("/api/v1/lnurlpayouts/{lnurlpayout_id}")
async def api_lnurlpayout_delete( async def api_lnurlpayout_delete(