mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-04 02:14:23 +02:00
internal payments get reported on async listeners.
This commit is contained in:
@@ -12,7 +12,7 @@ from .core import core_app
|
|||||||
from .db import open_db, open_ext_db
|
from .db import open_db, open_ext_db
|
||||||
from .helpers import get_valid_extensions, get_js_vendored, get_css_vendored, url_for_vendored
|
from .helpers import get_valid_extensions, get_js_vendored, get_css_vendored, url_for_vendored
|
||||||
from .proxy_fix import ASGIProxyFix
|
from .proxy_fix import ASGIProxyFix
|
||||||
from .tasks import run_deferred_async, invoice_listener, webhook_handler, grab_app_for_later
|
from .tasks import run_deferred_async, invoice_listener, internal_invoice_listener, webhook_handler, grab_app_for_later
|
||||||
from .settings import WALLET
|
from .settings import WALLET
|
||||||
|
|
||||||
secure_headers = SecureHeaders(hsts=False)
|
secure_headers = SecureHeaders(hsts=False)
|
||||||
@@ -128,6 +128,7 @@ def register_async_tasks(app):
|
|||||||
async def listeners():
|
async def listeners():
|
||||||
run_deferred_async(app.nursery)
|
run_deferred_async(app.nursery)
|
||||||
app.nursery.start_soon(invoice_listener)
|
app.nursery.start_soon(invoice_listener)
|
||||||
|
app.nursery.start_soon(internal_invoice_listener)
|
||||||
|
|
||||||
@app.after_serving
|
@app.after_serving
|
||||||
async def stop_listeners():
|
async def stop_listeners():
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
import trio # type: ignore
|
||||||
import httpx
|
import httpx
|
||||||
from typing import Optional, Tuple, Dict
|
from typing import Optional, Tuple, Dict
|
||||||
from quart import g
|
from quart import g
|
||||||
@@ -89,8 +90,8 @@ def pay_invoice(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# check_internal() returns the checking_id of the invoice we're waiting for
|
# check_internal() returns the checking_id of the invoice we're waiting for
|
||||||
internal = check_internal(invoice.payment_hash)
|
internal_checking_id = check_internal(invoice.payment_hash)
|
||||||
if internal:
|
if internal_checking_id:
|
||||||
# create a new payment from this wallet
|
# create a new payment from this wallet
|
||||||
create_payment(checking_id=internal_id, fee=0, pending=False, **payment_kwargs)
|
create_payment(checking_id=internal_id, fee=0, pending=False, **payment_kwargs)
|
||||||
else:
|
else:
|
||||||
@@ -108,11 +109,19 @@ def pay_invoice(
|
|||||||
else:
|
else:
|
||||||
g.db.commit()
|
g.db.commit()
|
||||||
|
|
||||||
if internal:
|
if internal_checking_id:
|
||||||
# mark the invoice from the other side as not pending anymore
|
# mark the invoice from the other side as not pending anymore
|
||||||
# so the other side only has access to his new money when we are sure
|
# so the other side only has access to his new money when we are sure
|
||||||
# the payer has enough to deduct from
|
# the payer has enough to deduct from
|
||||||
update_payment_status(checking_id=internal, pending=False)
|
update_payment_status(checking_id=internal_checking_id, pending=False)
|
||||||
|
|
||||||
|
# notify receiver asynchronously
|
||||||
|
from lnbits.tasks import internal_invoice_paid
|
||||||
|
|
||||||
|
try:
|
||||||
|
internal_invoice_paid.send_nowait(internal_checking_id)
|
||||||
|
except trio.WouldBlock:
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
# actually pay the external invoice
|
# actually pay the external invoice
|
||||||
payment: PaymentResponse = WALLET.pay_invoice(payment_request)
|
payment: PaymentResponse = WALLET.pay_invoice(payment_request)
|
||||||
|
@@ -77,6 +77,14 @@ async def webhook_handler():
|
|||||||
return "", HTTPStatus.NO_CONTENT
|
return "", HTTPStatus.NO_CONTENT
|
||||||
|
|
||||||
|
|
||||||
|
internal_invoice_paid, internal_invoice_received = trio.open_memory_channel(0)
|
||||||
|
|
||||||
|
|
||||||
|
async def internal_invoice_listener():
|
||||||
|
async for checking_id in internal_invoice_received:
|
||||||
|
await run_on_pseudo_request(invoice_callback_dispatcher, checking_id)
|
||||||
|
|
||||||
|
|
||||||
async def invoice_listener():
|
async def invoice_listener():
|
||||||
async for checking_id in WALLET.paid_invoices_stream():
|
async for checking_id in WALLET.paid_invoices_stream():
|
||||||
await run_on_pseudo_request(invoice_callback_dispatcher, checking_id)
|
await run_on_pseudo_request(invoice_callback_dispatcher, checking_id)
|
||||||
|
Reference in New Issue
Block a user