fix postgres type translation bug and add some logs.

This commit is contained in:
fiatjaf 2021-08-01 10:30:27 -03:00
parent 83137ba0a0
commit e0496fb244
6 changed files with 37 additions and 31 deletions

View File

@ -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

View File

@ -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")

View File

@ -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}"

View File

@ -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]
)

View File

@ -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)

View File

@ -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(