diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index 0963416..35f1019 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -145,7 +145,13 @@ def get_wallet_payment(wallet_id: str, payment_hash: str) -> Optional[Payment]: def get_wallet_payments( - wallet_id: str, *, complete: bool = False, pending: bool = False, outgoing: bool = False, incoming: bool = False + wallet_id: str, + *, + complete: bool = False, + pending: bool = False, + outgoing: bool = False, + incoming: bool = False, + exclude_uncheckable: bool = False, ) -> List[Payment]: """ Filters payments to be returned by complete | pending | outgoing | incoming. @@ -161,6 +167,8 @@ def get_wallet_payments( else: raise TypeError("at least one of [complete, pending] must be True.") + clause += " " + if outgoing and incoming: clause += "" elif outgoing: @@ -170,6 +178,12 @@ def get_wallet_payments( else: raise TypeError("at least one of [outgoing, incoming] must be True.") + clause += " " + + if exclude_uncheckable: # checkable means it has a checking_id that isn't internal + clause += "AND checking_id NOT LIKE 'temp_%' " + clause += "AND checking_id NOT LIKE 'internal_%' " + rows = g.db.fetchall( f""" SELECT * diff --git a/lnbits/core/models.py b/lnbits/core/models.py index ed4ed43..24d7649 100644 --- a/lnbits/core/models.py +++ b/lnbits/core/models.py @@ -37,11 +37,24 @@ class Wallet(NamedTuple): return get_wallet_payment(self.id, payment_hash) def get_payments( - self, *, complete: bool = True, pending: bool = False, outgoing: bool = True, incoming: bool = True + self, + *, + complete: bool = True, + pending: bool = False, + outgoing: bool = True, + incoming: bool = True, + exclude_uncheckable: bool = False ) -> List["Payment"]: from .crud import get_wallet_payments - return get_wallet_payments(self.id, complete=complete, pending=pending, outgoing=outgoing, incoming=incoming) + return get_wallet_payments( + self.id, + complete=complete, + pending=pending, + outgoing=outgoing, + incoming=incoming, + exclude_uncheckable=exclude_uncheckable, + ) class Payment(NamedTuple): @@ -60,9 +73,9 @@ class Payment(NamedTuple): def from_row(cls, row: Row): return cls( checking_id=row["checking_id"], - payment_hash=row["hash"], - bolt11=row["bolt11"], - preimage=row["preimage"], + payment_hash=row["hash"] or "0" * 64, + bolt11=row["bolt11"] or "", + preimage=row["preimage"] or "0" * 64, extra=json.loads(row["extra"] or "{}"), pending=row["pending"], amount=row["amount"], diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index b9da9ff..e4cb4fa 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -16,10 +16,8 @@ def api_payments(): if "check_pending" in request.args: delete_expired_invoices() - for payment in g.wallet.get_payments(complete=False, pending=True): - if payment.is_uncheckable: - pass - elif payment.is_out: + for payment in g.wallet.get_payments(complete=False, pending=True, exclude_uncheckable=True): + if payment.is_out: payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending) else: payment.set_pending(WALLET.get_invoice_status(payment.checking_id).pending) diff --git a/lnbits/extensions/lndhub/views_api.py b/lnbits/extensions/lndhub/views_api.py index 1e9604b..92fc6e5 100644 --- a/lnbits/extensions/lndhub/views_api.py +++ b/lnbits/extensions/lndhub/views_api.py @@ -3,7 +3,9 @@ from base64 import urlsafe_b64encode from flask import jsonify, g, request from lnbits.core.services import pay_invoice, create_invoice +from lnbits.core.crud import delete_expired_invoices from lnbits.decorators import api_validate_post_request +from lnbits.settings import WALLET from lnbits import bolt11 from lnbits.extensions.lndhub import lndhub_ext @@ -48,6 +50,7 @@ def lndhub_addinvoice(): wallet_id=g.wallet.id, amount=int(g.data["amt"]), memo=g.data["memo"], + extra={"tag": "lndhub"}, ) except Exception as e: return jsonify( @@ -75,7 +78,11 @@ def lndhub_addinvoice(): @api_validate_post_request(schema={"invoice": {"type": "string", "required": True}}) def lndhub_payinvoice(): try: - pay_invoice(wallet_id=g.wallet.id, payment_request=g.data["invoice"]) + pay_invoice( + wallet_id=g.wallet.id, + payment_request=g.data["invoice"], + extra={"tag": "lndhub"}, + ) except Exception as e: return jsonify( { @@ -112,8 +119,12 @@ def lndhub_balance(): @lndhub_ext.route("/ext/gettxs", methods=["GET"]) @check_wallet() def lndhub_gettxs(): - limit = int(request.args.get("limit", 200)) + for payment in g.wallet.get_payments( + complete=False, pending=True, outgoing=True, incoming=False, exclude_uncheckable=True + ): + payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending) + limit = int(request.args.get("limit", 200)) return jsonify( [ { @@ -126,9 +137,9 @@ def lndhub_gettxs(): "timestamp": payment.time, "memo": payment.memo if not payment.pending else "Payment in transition", } - for payment in g.wallet.get_payments( - pending=True, complete=True, outgoing=True, incoming=False, order="ASC" - )[0:limit] + for payment in reversed( + g.wallet.get_payments(pending=True, complete=True, outgoing=True, incoming=False)[:limit] + ) ] ) @@ -136,8 +147,13 @@ def lndhub_gettxs(): @lndhub_ext.route("/ext/getuserinvoices", methods=["GET"]) @check_wallet() def lndhub_getuserinvoices(): - limit = int(request.args.get("limit", 200)) + delete_expired_invoices() + for invoice in g.wallet.get_payments( + complete=False, pending=True, outgoing=False, incoming=True, exclude_uncheckable=True + ): + invoice.set_pending(WALLET.get_invoice_status(invoice.checking_id).pending) + limit = int(request.args.get("limit", 200)) return jsonify( [ { @@ -152,9 +168,9 @@ def lndhub_getuserinvoices(): "timestamp": invoice.time, "type": "user_invoice", } - for invoice in g.wallet.get_payments( - pending=True, complete=True, incoming=True, outgoing=False, order="ASC" - )[:limit] + for invoice in reversed( + g.wallet.get_payments(pending=True, complete=True, incoming=True, outgoing=False)[:limit] + ) ] )