feat: emulate behaviour of paid_invoices_stream for wallets that dont support it (#2071)
This commit is contained in:
parent
5f8ccee5b8
commit
25d59e4142
3 changed files with 24 additions and 4 deletions
|
|
@ -1,8 +1,11 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import TYPE_CHECKING, AsyncGenerator, Coroutine, NamedTuple, Optional
|
from typing import TYPE_CHECKING, AsyncGenerator, Coroutine, NamedTuple, Optional
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from lnbits.nodes.base import Node
|
from lnbits.nodes.base import Node
|
||||||
|
|
||||||
|
|
@ -93,6 +96,9 @@ class Wallet(ABC):
|
||||||
|
|
||||||
__node_cls__: Optional[type[Node]] = None
|
__node_cls__: Optional[type[Node]] = None
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.pending_invoices: list[str] = []
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def cleanup(self):
|
async def cleanup(self):
|
||||||
pass
|
pass
|
||||||
|
|
@ -130,9 +136,19 @@ class Wallet(ABC):
|
||||||
) -> Coroutine[None, None, PaymentStatus]:
|
) -> Coroutine[None, None, PaymentStatus]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
||||||
def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
while True:
|
||||||
pass
|
for invoice in self.pending_invoices:
|
||||||
|
try:
|
||||||
|
status = await self.get_invoice_status(invoice)
|
||||||
|
if status.paid:
|
||||||
|
yield invoice
|
||||||
|
self.pending_invoices.remove(invoice)
|
||||||
|
elif status.failed:
|
||||||
|
self.pending_invoices.remove(invoice)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error(f"could not get status of invoice {invoice}: '{exc}' ")
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
def normalize_endpoint(self, endpoint: str, add_proto=True) -> str:
|
def normalize_endpoint(self, endpoint: str, add_proto=True) -> str:
|
||||||
endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
|
endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ class LNPayWallet(Wallet):
|
||||||
if not settings.lnpay_api_key:
|
if not settings.lnpay_api_key:
|
||||||
raise ValueError("cannot initialize LNPayWallet: missing lnpay_api_key")
|
raise ValueError("cannot initialize LNPayWallet: missing lnpay_api_key")
|
||||||
|
|
||||||
|
super().__init__()
|
||||||
wallet_key = settings.lnpay_wallet_key or settings.lnpay_admin_key
|
wallet_key = settings.lnpay_wallet_key or settings.lnpay_admin_key
|
||||||
if not wallet_key:
|
if not wallet_key:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
|
@ -35,7 +36,6 @@ class LNPayWallet(Wallet):
|
||||||
"missing lnpay_wallet_key or lnpay_admin_key"
|
"missing lnpay_wallet_key or lnpay_admin_key"
|
||||||
)
|
)
|
||||||
self.wallet_key = wallet_key
|
self.wallet_key = wallet_key
|
||||||
|
|
||||||
self.endpoint = self.normalize_endpoint(settings.lnpay_api_endpoint)
|
self.endpoint = self.normalize_endpoint(settings.lnpay_api_endpoint)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
|
|
@ -102,6 +102,8 @@ class LNPayWallet(Wallet):
|
||||||
data = r.json()
|
data = r.json()
|
||||||
checking_id, payment_request = data["id"], data["payment_request"]
|
checking_id, payment_request = data["id"], data["payment_request"]
|
||||||
|
|
||||||
|
self.pending_invoices.append(checking_id)
|
||||||
|
|
||||||
return InvoiceResponse(ok, checking_id, payment_request, error_message)
|
return InvoiceResponse(ok, checking_id, payment_request, error_message)
|
||||||
|
|
||||||
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ class OpenNodeWallet(Wallet):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"cannot initialize OpenNodeWallet: missing opennode_api_endpoint"
|
"cannot initialize OpenNodeWallet: missing opennode_api_endpoint"
|
||||||
)
|
)
|
||||||
|
super().__init__()
|
||||||
key = (
|
key = (
|
||||||
settings.opennode_key
|
settings.opennode_key
|
||||||
or settings.opennode_admin_key
|
or settings.opennode_admin_key
|
||||||
|
|
@ -92,6 +93,7 @@ class OpenNodeWallet(Wallet):
|
||||||
data = r.json()["data"]
|
data = r.json()["data"]
|
||||||
checking_id = data["id"]
|
checking_id = data["id"]
|
||||||
payment_request = data["lightning_invoice"]["payreq"]
|
payment_request = data["lightning_invoice"]["payreq"]
|
||||||
|
self.pending_invoices.append(checking_id)
|
||||||
return InvoiceResponse(True, checking_id, payment_request, None)
|
return InvoiceResponse(True, checking_id, payment_request, None)
|
||||||
|
|
||||||
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue