stop doing the check_invoices thing on each call. do it once on lnbits starup and then rely on the invoices listener.

This commit is contained in:
fiatjaf
2021-03-21 17:57:33 -03:00
parent f27c2ebc21
commit 7e4a42e7ff
8 changed files with 61 additions and 70 deletions
+30 -23
View File
@@ -1,7 +1,7 @@
import json
import datetime
from uuid import uuid4
from typing import List, Optional, Dict
from typing import List, Optional, Dict, Any
from lnbits import bolt11
from lnbits.settings import DEFAULT_WALLET_NAME
@@ -158,9 +158,9 @@ async def get_wallet_payment(wallet_id: str, payment_hash: str) -> Optional[Paym
return Payment.from_row(row) if row else None
async def get_wallet_payments(
wallet_id: str,
async def get_payments(
*,
wallet_id: Optional[str] = None,
complete: bool = False,
pending: bool = False,
outgoing: bool = False,
@@ -171,41 +171,48 @@ async def get_wallet_payments(
Filters payments to be returned by complete | pending | outgoing | incoming.
"""
clause = ""
if complete and pending:
clause += ""
elif complete:
clause += "AND ((amount > 0 AND pending = 0) OR amount < 0)"
elif pending:
clause += "AND pending = 1"
else:
raise TypeError("at least one of [complete, pending] must be True.")
args: Any = ()
clause += " "
clause = []
if wallet_id:
clause.append("wallet = ?")
args = (wallet_id,)
if complete and pending:
pass
elif complete:
clause.append("((amount > 0 AND pending = 0) OR amount < 0)")
elif pending:
clause.append("pending = 1")
else:
pass
if outgoing and incoming:
clause += ""
pass
elif outgoing:
clause += "AND amount < 0"
clause.append("amount < 0")
elif incoming:
clause += "AND amount > 0"
clause.append("amount > 0")
else:
raise TypeError("at least one of [outgoing, incoming] must be True.")
clause += " "
pass
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_%' "
clause.append("checking_id NOT LIKE 'temp_%'")
clause.append("checking_id NOT LIKE 'internal_%'")
where = ""
if clause:
where = f"WHERE {' AND '.join(clause)}"
rows = await db.fetchall(
f"""
SELECT *
FROM apipayments
WHERE wallet = ? {clause}
{where}
ORDER BY time DESC
""",
(wallet_id,),
args,
)
return [Payment.from_row(row) for row in rows]
+3 -3
View File
@@ -60,10 +60,10 @@ class Wallet(NamedTuple):
incoming: bool = True,
exclude_uncheckable: bool = False
) -> List["Payment"]:
from .crud import get_wallet_payments
from .crud import get_payments
return await get_wallet_payments(
self.id,
return await get_payments(
wallet_id=self.id,
complete=complete,
pending=pending,
outgoing=outgoing,
-14
View File
@@ -604,19 +604,6 @@ new Vue({
])
})
},
checkPendingPayments: function () {
var dismissMsg = this.$q.notify({
timeout: 0,
message: 'Checking pending transactions...'
})
LNbits.api
.checkPending(this.g.wallet)
.then(() => LNbits.api.fetchPayments)
.then(() => {
dismissMsg()
})
},
exportCSV: function () {
LNbits.utils.exportCSV(this.paymentsTable.columns, this.payments)
}
@@ -629,7 +616,6 @@ new Vue({
created: function () {
this.fetchBalance()
this.fetchPayments()
this.checkPendingPayments()
},
mounted: function () {
// show disclaimer
-12
View File
@@ -13,7 +13,6 @@ from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from .. import core_app, db
from ..services import create_invoice, pay_invoice, perform_lnurlauth
from ..crud import delete_expired_invoices
from ..tasks import sse_listeners
@@ -32,17 +31,6 @@ async def api_wallet():
)
@core_app.route("/api/v1/checkpending", methods=["POST"])
@api_check_wallet_key("invoice")
async def api_checkpending():
g.nursery.start_soon(delete_expired_invoices)
for payment in await g.wallet.get_payments(complete=False, pending=True, exclude_uncheckable=True):
await payment.check_pending()
return "", HTTPStatus.NO_CONTENT
@core_app.route("/api/v1/payments", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_payments():