Buckets:
| from __future__ import annotations | |
| import asyncio | |
| from abc import ABC, abstractmethod | |
| from collections.abc import AsyncGenerator, Coroutine | |
| from enum import Enum | |
| from typing import TYPE_CHECKING, NamedTuple | |
| from loguru import logger | |
| from lnbits.exceptions import InvoiceError | |
| from lnbits.settings import settings | |
| if TYPE_CHECKING: | |
| from lnbits.nodes.base import Node | |
| def payment_request_was_rejected(status_code: int) -> bool: | |
| """Return whether HTTP rejected the request before payment dispatch.""" | |
| # Generic 400 and 422 responses are provider-specific. They can report an | |
| # existing payment, so adapters must not treat them as terminal based only | |
| # on the status code. Timeouts, conflicts and rate limits are also ambiguous. | |
| return status_code in {401, 403, 404, 405} | |
| class Feature(Enum): | |
| nodemanager = "nodemanager" | |
| holdinvoice = "holdinvoice" | |
| descriptionhash = "descriptionhash" | |
| # bolt12 = "bolt12" | |
| class StatusResponse(NamedTuple): | |
| error_message: str | None | |
| balance_msat: int | |
| class InvoiceResponse(NamedTuple): | |
| ok: bool | |
| checking_id: str | None = None # payment_hash, rpc_id | |
| payment_request: str | None = None | |
| error_message: str | None = None | |
| preimage: str | None = None | |
| fee_msat: int | None = None | |
| def success(self) -> bool: | |
| return self.ok is True | |
| def pending(self) -> bool: | |
| return self.ok is None | |
| def failed(self) -> bool: | |
| return self.ok is False | |
| class PaymentResponse(NamedTuple): | |
| # when ok is None it means we don't know if this succeeded | |
| ok: bool | None = None | |
| checking_id: str | None = None # payment_hash, rcp_id | |
| fee_msat: int | None = None | |
| preimage: str | None = None | |
| error_message: str | None = None | |
| def success(self) -> bool: | |
| return self.ok is True | |
| def pending(self) -> bool: | |
| return self.ok is None | |
| def failed(self) -> bool: | |
| return self.ok is False | |
| class PaymentStatus(NamedTuple): | |
| paid: bool | None = None | |
| fee_msat: int | None = None | |
| preimage: str | None = None | |
| def success(self) -> bool: | |
| return self.paid is True | |
| def pending(self) -> bool: | |
| return self.paid is not True | |
| def failed(self) -> bool: | |
| return self.paid is False | |
| def __str__(self) -> str: | |
| if self.success: | |
| return "success" | |
| if self.failed: | |
| return "failed" | |
| return "pending" | |
| class PaymentSuccessStatus(PaymentStatus): | |
| paid = True # type: ignore[reportIncompatibleVariableOverride] | |
| class PaymentFailedStatus(PaymentStatus): | |
| paid = False # type: ignore[reportIncompatibleVariableOverride] | |
| class PaymentPendingStatus(PaymentStatus): | |
| paid = None # type: ignore[reportIncompatibleVariableOverride] | |
| class Wallet(ABC): | |
| __node_cls__: type[Node] | None = None | |
| features: list[Feature] | None = None | |
| def has_feature(self, feature: Feature) -> bool: | |
| return self.features is not None and feature in self.features | |
| def __init__(self) -> None: | |
| self.pending_invoices: list[str] = [] | |
| async def cleanup(self): | |
| pass | |
| def status(self) -> Coroutine[None, None, StatusResponse]: | |
| pass | |
| def create_invoice( | |
| self, | |
| amount: int, | |
| memo: str | None = None, | |
| description_hash: bytes | None = None, | |
| unhashed_description: bytes | None = None, | |
| **kwargs, | |
| ) -> Coroutine[None, None, InvoiceResponse]: | |
| pass | |
| def pay_invoice( | |
| self, bolt11: str, fee_limit_msat: int | |
| ) -> Coroutine[None, None, PaymentResponse]: | |
| pass | |
| def get_invoice_status( | |
| self, checking_id: str | |
| ) -> Coroutine[None, None, PaymentStatus]: | |
| pass | |
| def get_payment_status( | |
| self, checking_id: str | |
| ) -> Coroutine[None, None, PaymentStatus]: | |
| pass | |
| async def create_hold_invoice( | |
| self, | |
| amount: int, | |
| payment_hash: str, | |
| memo: str | None = None, | |
| description_hash: bytes | None = None, | |
| unhashed_description: bytes | None = None, | |
| **kwargs, | |
| ) -> InvoiceResponse: | |
| raise InvoiceError( | |
| message="Hold invoices are not supported by this wallet.", status="failed" | |
| ) | |
| async def settle_hold_invoice(self, preimage: str) -> InvoiceResponse: | |
| raise InvoiceError( | |
| message="Hold invoices are not supported by this wallet.", status="failed" | |
| ) | |
| async def cancel_hold_invoice(self, payment_hash: str) -> InvoiceResponse: | |
| raise InvoiceError( | |
| message="Hold invoices are not supported by this wallet.", status="failed" | |
| ) | |
| async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: | |
| while settings.lnbits_running: | |
| 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) | |
Xet Storage Details
- Size:
- 5.55 kB
- Xet hash:
- 88efe1fe368e0027718c4c260c2d5d76594b675785f44a83d4b1236e5546fe6c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.