paying function works

This commit is contained in:
benarc 2021-12-19 22:28:19 +00:00
parent 331a383223
commit 6a4290c0e9
4 changed files with 20 additions and 16 deletions

View file

@ -11,7 +11,7 @@ async def create_lnurlpayout(wallet_id: str, admin_key: str, data: CreateLnurlPa
await db.execute( await db.execute(
""" """
INSERT INTO lnurlpayout.lnurlpayouts (id, title, wallet, admin_key, lnurlpay, threshold) 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), (lnurlpayout_id, data.title, wallet_id, admin_key, data.lnurlpay, data.threshold),
) )

View file

@ -13,4 +13,4 @@ class lnurlpayout(BaseModel):
wallet: str wallet: str
admin_key: str admin_key: str
lnurlpay: str lnurlpay: str
threshold: str threshold: int

View file

@ -6,6 +6,8 @@ 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.views.api import api_wallet 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 from .crud import get_lnurlpayout, get_lnurlpayout_from_wallet
@ -23,19 +25,18 @@ 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_from_wallet(payment.wallet_id) lnurlpayout_link = await get_lnurlpayout_from_wallet(payment.wallet_id)
print(lnurlpayout_link)
if lnurlpayout_link: if lnurlpayout_link:
print("poo11")
# Check the wallet balance is more than the threshold # Check the wallet balance is more than the threshold
wallet = await api_wallet(lnurlpayout_link.admin_key)
print("poo1") wallet = await get_wallet(lnurlpayout_link.wallet)
if wallet.balance + (wallet.balance/100*2) < lnurlpayout_link.threshold: if wallet.balance < lnurlpayout_link.threshold + (lnurlpayout_link.threshold*0.02):
return return
print("poo2")
# 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:
try: 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": 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:
@ -44,16 +45,18 @@ async def on_invoice_paid(payment: Payment) -> None:
timeout=40, timeout=40,
) )
res = r.json() res = r.json()
print(res)
print(res["callback"])
try: try:
r = await client.get( 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, timeout=40,
) )
res = r.json() res = r.json()
print(res["pr"]) await pay_invoice(
await api_payments_pay_invoice(res["pr"], payment.wallet_id) wallet_id=payment.wallet_id,
payment_request=res["pr"],
extra={"tag": "lnurlpayout"},
)
return
except: except:
return return
except (httpx.ConnectError, httpx.RequestError): except (httpx.ConnectError, httpx.RequestError):

View file

@ -18,7 +18,7 @@ from .tasks import on_invoice_paid
async def api_lnurlpayouts( async def api_lnurlpayouts(
all_wallets: bool = Query(None), wallet: WalletTypeInfo = Depends(get_key_type) all_wallets: bool = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)
): ):
wallet_ids = [wallet.wallet.id] wallet_ids = wallet.wallet.id
if all_wallets: if all_wallets:
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids 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": if str(url["domain"])[0:4] != "http":
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not valid LNURL") raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not valid LNURL")
return 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: if not lnurlpayout:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout") raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout")
return return