lnurlpayout fix
This commit is contained in:
parent
f7b37fb843
commit
f1fb569e2b
2 changed files with 42 additions and 20 deletions
|
|
@ -1,15 +1,17 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
from http import HTTPStatus
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
from starlette.exceptions import HTTPException
|
||||||
|
|
||||||
from lnbits.core import db as core_db
|
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.crud import get_wallet
|
||||||
from lnbits.core.views.api import api_payment, api_payments_decode, pay_invoice
|
from lnbits.core.models import Payment
|
||||||
|
from lnbits.core.services import pay_invoice
|
||||||
|
from lnbits.core.views.api import api_payments_decode
|
||||||
|
from lnbits.tasks import register_invoice_listener
|
||||||
|
|
||||||
from .crud import get_lnurlpayout, get_lnurlpayout_from_wallet
|
from .crud import get_lnurlpayout_from_wallet
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_paid_invoices():
|
async def wait_for_paid_invoices():
|
||||||
|
|
@ -25,16 +27,16 @@ 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", 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 = await get_wallet(lnurlpayout_link.wallet)
|
wallet = await get_wallet(lnurlpayout_link.wallet)
|
||||||
if wallet.balance < lnurlpayout_link.threshold + (
|
threshold = lnurlpayout_link.threshold + (lnurlpayout_link.threshold * 0.02)
|
||||||
lnurlpayout_link.threshold * 0.02
|
|
||||||
):
|
|
||||||
return
|
|
||||||
|
|
||||||
|
if wallet.balance < threshold:
|
||||||
|
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:
|
||||||
try:
|
try:
|
||||||
|
|
@ -43,6 +45,7 @@ async def on_invoice_paid(payment: Payment) -> None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken"
|
status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken"
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
r = await client.get(str(url["domain"]), timeout=40)
|
r = await client.get(str(url["domain"]), timeout=40)
|
||||||
res = r.json()
|
res = r.json()
|
||||||
|
|
@ -56,6 +59,12 @@ async def on_invoice_paid(payment: Payment) -> None:
|
||||||
timeout=40,
|
timeout=40,
|
||||||
)
|
)
|
||||||
res = r.json()
|
res = r.json()
|
||||||
|
|
||||||
|
if hasattr(res, "status") and res["status"] == "ERROR":
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.FORBIDDEN,
|
||||||
|
detail=res["reason"],
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
await pay_invoice(
|
await pay_invoice(
|
||||||
wallet_id=payment.wallet_id,
|
wallet_id=payment.wallet_id,
|
||||||
|
|
@ -65,7 +74,9 @@ async def on_invoice_paid(payment: Payment) -> None:
|
||||||
return
|
return
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
except:
|
|
||||||
|
except Exception as e:
|
||||||
|
print("ERROR", str(e))
|
||||||
return
|
return
|
||||||
except (httpx.ConnectError, httpx.RequestError):
|
except (httpx.ConnectError, httpx.RequestError):
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@ from fastapi import Query
|
||||||
from fastapi.params import Depends
|
from fastapi.params import Depends
|
||||||
from starlette.exceptions import HTTPException
|
from starlette.exceptions import HTTPException
|
||||||
|
|
||||||
from lnbits.core.crud import get_user, get_payments
|
from lnbits.core.crud import get_payments, get_user
|
||||||
|
from lnbits.core.models import Payment
|
||||||
from lnbits.core.services import create_invoice
|
from lnbits.core.services import create_invoice
|
||||||
from lnbits.core.views.api import api_payment, api_payments_decode
|
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
|
||||||
|
|
@ -14,10 +15,10 @@ from .crud import (
|
||||||
create_lnurlpayout,
|
create_lnurlpayout,
|
||||||
delete_lnurlpayout,
|
delete_lnurlpayout,
|
||||||
get_lnurlpayout,
|
get_lnurlpayout,
|
||||||
get_lnurlpayouts,
|
|
||||||
get_lnurlpayout_from_wallet,
|
get_lnurlpayout_from_wallet,
|
||||||
|
get_lnurlpayouts,
|
||||||
)
|
)
|
||||||
from .models import lnurlpayout, CreateLnurlPayoutData
|
from .models import CreateLnurlPayoutData, lnurlpayout
|
||||||
from .tasks import on_invoice_paid
|
from .tasks import on_invoice_paid
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -87,14 +88,24 @@ async def api_lnurlpayout_check(
|
||||||
lnurlpayout_id: str, wallet: WalletTypeInfo = Depends(get_key_type)
|
lnurlpayout_id: str, wallet: WalletTypeInfo = Depends(get_key_type)
|
||||||
):
|
):
|
||||||
lnurlpayout = await get_lnurlpayout(lnurlpayout_id)
|
lnurlpayout = await get_lnurlpayout(lnurlpayout_id)
|
||||||
payments = await get_payments(
|
## THIS
|
||||||
wallet_id=lnurlpayout.wallet,
|
mock_payment = Payment(
|
||||||
complete=True,
|
checking_id="mock",
|
||||||
pending=False,
|
pending=False,
|
||||||
outgoing=True,
|
amount=1,
|
||||||
incoming=True,
|
fee=1,
|
||||||
|
time=0000,
|
||||||
|
bolt11="mock",
|
||||||
|
preimage="mock",
|
||||||
|
payment_hash="mock",
|
||||||
|
wallet_id=lnurlpayout.wallet,
|
||||||
)
|
)
|
||||||
result = await on_invoice_paid(payments[0])
|
## INSTEAD OF THIS
|
||||||
|
# payments = await get_payments(
|
||||||
|
# wallet_id=lnurlpayout.wallet, complete=True, pending=False, outgoing=True, incoming=True
|
||||||
|
# )
|
||||||
|
|
||||||
|
result = await on_invoice_paid(mock_payment)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue