websocket internal payment notifications (#1831)

* add send_payment_notification service
payment notifications are sent from multiple places with inconsistent and incomplete data
* adopt new send_payment_notification service
* add tests
This commit is contained in:
jackstar12
2023-07-26 12:08:22 +02:00
committed by GitHub
parent cf0a87582c
commit dda6c1b3c1
4 changed files with 65 additions and 28 deletions
+21 -13
View File
@@ -44,7 +44,7 @@ from .crud import (
update_super_user,
)
from .helpers import to_valid_user_id
from .models import Payment
from .models import Payment, Wallet
class PaymentFailure(Exception):
@@ -172,7 +172,7 @@ async def pay_invoice(
logger.debug(f"creating temporary internal payment with id {internal_id}")
# create a new payment from this wallet
await create_payment(
new_payment = await create_payment(
checking_id=internal_id,
fee=0,
pending=False,
@@ -184,7 +184,7 @@ async def pay_invoice(
# create a temporary payment here so we can check if
# the balance is enough in the next step
try:
await create_payment(
new_payment = await create_payment(
checking_id=temp_id,
fee=-fee_reserve_msat,
conn=conn,
@@ -215,6 +215,7 @@ async def pay_invoice(
await update_payment_status(
checking_id=internal_checking_id, pending=False, conn=conn
)
await send_payment_notification(wallet, new_payment)
# notify receiver asynchronously
from lnbits.tasks import internal_invoice_queue
@@ -248,16 +249,11 @@ async def pay_invoice(
conn=conn,
)
wallet = await get_wallet(wallet_id, conn=conn)
if wallet:
await websocketUpdater(
wallet_id,
json.dumps(
{
"wallet_balance": wallet.balance or None,
"payment": payment._asdict(),
}
),
)
updated = await get_wallet_payment(
wallet_id, payment.checking_id, conn=conn
)
if wallet and updated:
await send_payment_notification(wallet, updated)
logger.debug(f"payment successful {payment.checking_id}")
elif payment.checking_id is None and payment.ok is False:
# payment failed
@@ -431,6 +427,18 @@ def fee_reserve(amount_msat: int) -> int:
return max(int(reserve_min), int(amount_msat * reserve_percent / 100.0))
async def send_payment_notification(wallet: Wallet, payment: Payment):
await websocketUpdater(
wallet.id,
json.dumps(
{
"wallet_balance": wallet.balance,
"payment": payment.dict(),
}
),
)
async def update_wallet_balance(wallet_id: str, amount: int):
payment_hash, _ = await create_invoice(
wallet_id=wallet_id,
+2 -11
View File
@@ -1,5 +1,4 @@
import asyncio
import json
from typing import Dict, Optional
import httpx
@@ -11,7 +10,7 @@ from lnbits.tasks import SseListenersDict, register_invoice_listener
from . import db
from .crud import get_balance_notify, get_wallet
from .models import Payment
from .services import get_balance_delta, switch_to_voidwallet, websocketUpdater
from .services import get_balance_delta, send_payment_notification, switch_to_voidwallet
api_invoice_listeners: Dict[str, asyncio.Queue] = SseListenersDict(
"api_invoice_listeners"
@@ -123,15 +122,7 @@ async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue):
await dispatch_api_invoice_listeners(payment)
wallet = await get_wallet(payment.wallet_id)
if wallet:
await websocketUpdater(
payment.wallet_id,
json.dumps(
{
"wallet_balance": wallet.balance or None,
"payment": payment.dict(),
}
),
)
await send_payment_notification(wallet, payment)
# dispatch webhook
if payment.webhook and not payment.webhook_status:
await dispatch_webhook(payment)