Browse Source

checking pending invoices and payments from lndhub interface.

aiosqlite
fiatjaf 4 years ago
committed by fiatjaf
parent
commit
6210305791
  1. 16
      lnbits/core/crud.py
  2. 23
      lnbits/core/models.py
  3. 6
      lnbits/core/views/api.py
  4. 34
      lnbits/extensions/lndhub/views_api.py

16
lnbits/core/crud.py

@ -145,7 +145,13 @@ def get_wallet_payment(wallet_id: str, payment_hash: str) -> Optional[Payment]:
def get_wallet_payments( 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]: ) -> List[Payment]:
""" """
Filters payments to be returned by complete | pending | outgoing | incoming. Filters payments to be returned by complete | pending | outgoing | incoming.
@ -161,6 +167,8 @@ def get_wallet_payments(
else: else:
raise TypeError("at least one of [complete, pending] must be True.") raise TypeError("at least one of [complete, pending] must be True.")
clause += " "
if outgoing and incoming: if outgoing and incoming:
clause += "" clause += ""
elif outgoing: elif outgoing:
@ -170,6 +178,12 @@ def get_wallet_payments(
else: else:
raise TypeError("at least one of [outgoing, incoming] must be True.") 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( rows = g.db.fetchall(
f""" f"""
SELECT * SELECT *

23
lnbits/core/models.py

@ -37,11 +37,24 @@ class Wallet(NamedTuple):
return get_wallet_payment(self.id, payment_hash) return get_wallet_payment(self.id, payment_hash)
def get_payments( 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"]: ) -> List["Payment"]:
from .crud import get_wallet_payments 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): class Payment(NamedTuple):
@ -60,9 +73,9 @@ class Payment(NamedTuple):
def from_row(cls, row: Row): def from_row(cls, row: Row):
return cls( return cls(
checking_id=row["checking_id"], checking_id=row["checking_id"],
payment_hash=row["hash"], payment_hash=row["hash"] or "0" * 64,
bolt11=row["bolt11"], bolt11=row["bolt11"] or "",
preimage=row["preimage"], preimage=row["preimage"] or "0" * 64,
extra=json.loads(row["extra"] or "{}"), extra=json.loads(row["extra"] or "{}"),
pending=row["pending"], pending=row["pending"],
amount=row["amount"], amount=row["amount"],

6
lnbits/core/views/api.py

@ -16,10 +16,8 @@ def api_payments():
if "check_pending" in request.args: if "check_pending" in request.args:
delete_expired_invoices() delete_expired_invoices()
for payment in g.wallet.get_payments(complete=False, pending=True): for payment in g.wallet.get_payments(complete=False, pending=True, exclude_uncheckable=True):
if payment.is_uncheckable: if payment.is_out:
pass
elif payment.is_out:
payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending) payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending)
else: else:
payment.set_pending(WALLET.get_invoice_status(payment.checking_id).pending) payment.set_pending(WALLET.get_invoice_status(payment.checking_id).pending)

34
lnbits/extensions/lndhub/views_api.py

@ -3,7 +3,9 @@ from base64 import urlsafe_b64encode
from flask import jsonify, g, request from flask import jsonify, g, request
from lnbits.core.services import pay_invoice, create_invoice 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.decorators import api_validate_post_request
from lnbits.settings import WALLET
from lnbits import bolt11 from lnbits import bolt11
from lnbits.extensions.lndhub import lndhub_ext from lnbits.extensions.lndhub import lndhub_ext
@ -48,6 +50,7 @@ def lndhub_addinvoice():
wallet_id=g.wallet.id, wallet_id=g.wallet.id,
amount=int(g.data["amt"]), amount=int(g.data["amt"]),
memo=g.data["memo"], memo=g.data["memo"],
extra={"tag": "lndhub"},
) )
except Exception as e: except Exception as e:
return jsonify( return jsonify(
@ -75,7 +78,11 @@ def lndhub_addinvoice():
@api_validate_post_request(schema={"invoice": {"type": "string", "required": True}}) @api_validate_post_request(schema={"invoice": {"type": "string", "required": True}})
def lndhub_payinvoice(): def lndhub_payinvoice():
try: 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: except Exception as e:
return jsonify( return jsonify(
{ {
@ -112,8 +119,12 @@ def lndhub_balance():
@lndhub_ext.route("/ext/gettxs", methods=["GET"]) @lndhub_ext.route("/ext/gettxs", methods=["GET"])
@check_wallet() @check_wallet()
def lndhub_gettxs(): 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( return jsonify(
[ [
{ {
@ -126,9 +137,9 @@ def lndhub_gettxs():
"timestamp": payment.time, "timestamp": payment.time,
"memo": payment.memo if not payment.pending else "Payment in transition", "memo": payment.memo if not payment.pending else "Payment in transition",
} }
for payment in g.wallet.get_payments( for payment in reversed(
pending=True, complete=True, outgoing=True, incoming=False, order="ASC" g.wallet.get_payments(pending=True, complete=True, outgoing=True, incoming=False)[:limit]
)[0:limit] )
] ]
) )
@ -136,8 +147,13 @@ def lndhub_gettxs():
@lndhub_ext.route("/ext/getuserinvoices", methods=["GET"]) @lndhub_ext.route("/ext/getuserinvoices", methods=["GET"])
@check_wallet() @check_wallet()
def lndhub_getuserinvoices(): 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( return jsonify(
[ [
{ {
@ -152,9 +168,9 @@ def lndhub_getuserinvoices():
"timestamp": invoice.time, "timestamp": invoice.time,
"type": "user_invoice", "type": "user_invoice",
} }
for invoice in g.wallet.get_payments( for invoice in reversed(
pending=True, complete=True, incoming=True, outgoing=False, order="ASC" g.wallet.get_payments(pending=True, complete=True, incoming=True, outgoing=False)[:limit]
)[:limit] )
] ]
) )

Loading…
Cancel
Save