From e0496fb244538568456606ae300a56efeb01140a Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 1 Aug 2021 10:30:27 -0300 Subject: [PATCH] fix postgres type translation bug and add some logs. --- lnbits/core/models.py | 20 -------------------- lnbits/core/views/api.py | 4 ++-- lnbits/db.py | 15 ++++++++++++++- lnbits/extensions/lndhub/views_api.py | 24 +++++++++++++++++------- lnbits/tasks.py | 1 + lnbits/wallets/lnbits.py | 4 +++- 6 files changed, 37 insertions(+), 31 deletions(-) diff --git a/lnbits/core/models.py b/lnbits/core/models.py index bef29135..29d6f7d1 100644 --- a/lnbits/core/models.py +++ b/lnbits/core/models.py @@ -72,26 +72,6 @@ class Wallet(NamedTuple): return await get_wallet_payment(self.id, payment_hash) - async def get_payments( - self, - *, - complete: bool = True, - pending: bool = False, - outgoing: bool = True, - incoming: bool = True, - exclude_uncheckable: bool = False, - ) -> List["Payment"]: - from .crud import get_payments - - return await get_payments( - wallet_id=self.id, - complete=complete, - pending=pending, - outgoing=outgoing, - incoming=incoming, - exclude_uncheckable=exclude_uncheckable, - ) - class Payment(NamedTuple): checking_id: str diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index f3a3c5f6..b7c44e12 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -13,7 +13,7 @@ from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.utils.exchange_rates import currencies, fiat_amount_as_satoshis from .. import core_app, db -from ..crud import save_balance_check +from ..crud import get_payments, save_balance_check from ..services import ( PaymentFailure, InvoiceFailure, @@ -42,7 +42,7 @@ async def api_wallet(): @core_app.route("/api/v1/payments", methods=["GET"]) @api_check_wallet_key("invoice") async def api_payments(): - return jsonify(await g.wallet.get_payments(pending=True)), HTTPStatus.OK + return jsonify(await get_payments(wallet_id=g.wallet.id, pending=True)) @api_check_wallet_key("invoice") diff --git a/lnbits/db.py b/lnbits/db.py index 3eb43338..3f5b9484 100644 --- a/lnbits/db.py +++ b/lnbits/db.py @@ -1,6 +1,7 @@ import os import trio import time +import datetime from typing import Optional from contextlib import asynccontextmanager from sqlalchemy import create_engine # type: ignore @@ -101,13 +102,25 @@ class Database(Compat): ) psycopg2.extensions.register_type( psycopg2.extensions.new_type( - psycopg2.extensions.TIME.values + psycopg2.extensions.DATE.values, + psycopg2.extensions.DATE.values + psycopg2.extensions.TIME.values, "DATE2INT", lambda value, curs: time.mktime(value.timetuple()) if value is not None else None, ) ) + + psycopg2.extensions.register_type( + psycopg2.extensions.new_type( + (1184, 1114), + "TIMESTAMP2INT", + lambda value, curs: time.mktime( + datetime.datetime.strptime( + value, "%Y-%m-%d %H:%M:%S.%f" + ).timetuple() + ), + ) + ) else: self.path = os.path.join(LNBITS_DATA_FOLDER, f"{self.name}.sqlite3") database_uri = f"sqlite:///{self.path}" diff --git a/lnbits/extensions/lndhub/views_api.py b/lnbits/extensions/lndhub/views_api.py index 5f3b2a8b..de61820a 100644 --- a/lnbits/extensions/lndhub/views_api.py +++ b/lnbits/extensions/lndhub/views_api.py @@ -3,7 +3,7 @@ from base64 import urlsafe_b64encode from quart import jsonify, g, request from lnbits.core.services import pay_invoice, create_invoice -from lnbits.core.crud import delete_expired_invoices +from lnbits.core.crud import get_payments, delete_expired_invoices from lnbits.decorators import api_validate_post_request from lnbits.settings import WALLET from lnbits import bolt11 @@ -125,7 +125,8 @@ async def lndhub_balance(): @lndhub_ext.route("/ext/gettxs", methods=["GET"]) @check_wallet() async def lndhub_gettxs(): - for payment in await g.wallet.get_payments( + for payment in await get_payments( + wallet_id=g.wallet.id, complete=False, pending=True, outgoing=True, @@ -153,8 +154,12 @@ async def lndhub_gettxs(): } for payment in reversed( ( - await g.wallet.get_payments( - pending=True, complete=True, outgoing=True, incoming=False + await get_payments( + wallet_id=g.wallet.id, + pending=True, + complete=True, + outgoing=True, + incoming=False, ) )[:limit] ) @@ -166,7 +171,8 @@ async def lndhub_gettxs(): @check_wallet() async def lndhub_getuserinvoices(): await delete_expired_invoices() - for invoice in await g.wallet.get_payments( + for invoice in await get_payments( + wallet_id=g.wallet.id, complete=False, pending=True, outgoing=False, @@ -194,8 +200,12 @@ async def lndhub_getuserinvoices(): } for invoice in reversed( ( - await g.wallet.get_payments( - pending=True, complete=True, incoming=True, outgoing=False + await get_payments( + wallet_id=g.wallet.id, + pending=True, + complete=True, + incoming=True, + outgoing=False, ) )[:limit] ) diff --git a/lnbits/tasks.py b/lnbits/tasks.py index 7da90ba4..d7b297a0 100644 --- a/lnbits/tasks.py +++ b/lnbits/tasks.py @@ -75,6 +75,7 @@ async def internal_invoice_listener(): async def invoice_listener(): async for checking_id in WALLET.paid_invoices_stream(): + print("> got a payment notification", checking_id) current_app.nursery.start_soon(invoice_callback_dispatcher, checking_id) diff --git a/lnbits/wallets/lnbits.py b/lnbits/wallets/lnbits.py index 31bc32d3..e6ded79d 100644 --- a/lnbits/wallets/lnbits.py +++ b/lnbits/wallets/lnbits.py @@ -30,7 +30,9 @@ class LNbitsWallet(Wallet): async with httpx.AsyncClient() as client: try: r = await client.get( - url=f"{self.endpoint}/api/v1/wallet", headers=self.key + url=f"{self.endpoint}/api/v1/wallet", + headers=self.key, + timeout=15, ) except Exception as exc: return StatusResponse(