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) 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): class Payment(NamedTuple):
checking_id: str 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 lnbits.utils.exchange_rates import currencies, fiat_amount_as_satoshis
from .. import core_app, db from .. import core_app, db
from ..crud import save_balance_check from ..crud import get_payments, save_balance_check
from ..services import ( from ..services import (
PaymentFailure, PaymentFailure,
InvoiceFailure, InvoiceFailure,
@ -42,7 +42,7 @@ async def api_wallet():
@core_app.route("/api/v1/payments", methods=["GET"]) @core_app.route("/api/v1/payments", methods=["GET"])
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_payments(): 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") @api_check_wallet_key("invoice")

View File

@ -1,6 +1,7 @@
import os import os
import trio import trio
import time import time
import datetime
from typing import Optional from typing import Optional
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from sqlalchemy import create_engine # type: ignore from sqlalchemy import create_engine # type: ignore
@ -101,13 +102,25 @@ class Database(Compat):
) )
psycopg2.extensions.register_type( psycopg2.extensions.register_type(
psycopg2.extensions.new_type( psycopg2.extensions.new_type(
psycopg2.extensions.TIME.values + psycopg2.extensions.DATE.values, psycopg2.extensions.DATE.values + psycopg2.extensions.TIME.values,
"DATE2INT", "DATE2INT",
lambda value, curs: time.mktime(value.timetuple()) lambda value, curs: time.mktime(value.timetuple())
if value is not None if value is not None
else 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: else:
self.path = os.path.join(LNBITS_DATA_FOLDER, f"{self.name}.sqlite3") self.path = os.path.join(LNBITS_DATA_FOLDER, f"{self.name}.sqlite3")
database_uri = f"sqlite:///{self.path}" database_uri = f"sqlite:///{self.path}"

View File

@ -3,7 +3,7 @@ from base64 import urlsafe_b64encode
from quart import jsonify, g, request from quart 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.core.crud import get_payments, 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.settings import WALLET
from lnbits import bolt11 from lnbits import bolt11
@ -125,7 +125,8 @@ async def lndhub_balance():
@lndhub_ext.route("/ext/gettxs", methods=["GET"]) @lndhub_ext.route("/ext/gettxs", methods=["GET"])
@check_wallet() @check_wallet()
async def lndhub_gettxs(): 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, complete=False,
pending=True, pending=True,
outgoing=True, outgoing=True,
@ -153,8 +154,12 @@ async def lndhub_gettxs():
} }
for payment in reversed( for payment in reversed(
( (
await g.wallet.get_payments( await get_payments(
pending=True, complete=True, outgoing=True, incoming=False wallet_id=g.wallet.id,
pending=True,
complete=True,
outgoing=True,
incoming=False,
) )
)[:limit] )[:limit]
) )
@ -166,7 +171,8 @@ async def lndhub_gettxs():
@check_wallet() @check_wallet()
async def lndhub_getuserinvoices(): async def lndhub_getuserinvoices():
await delete_expired_invoices() 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, complete=False,
pending=True, pending=True,
outgoing=False, outgoing=False,
@ -194,8 +200,12 @@ async def lndhub_getuserinvoices():
} }
for invoice in reversed( for invoice in reversed(
( (
await g.wallet.get_payments( await get_payments(
pending=True, complete=True, incoming=True, outgoing=False wallet_id=g.wallet.id,
pending=True,
complete=True,
incoming=True,
outgoing=False,
) )
)[:limit] )[:limit]
) )

View File

@ -75,6 +75,7 @@ async def internal_invoice_listener():
async def invoice_listener(): async def invoice_listener():
async for checking_id in WALLET.paid_invoices_stream(): 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) 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: async with httpx.AsyncClient() as client:
try: try:
r = await client.get( 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: except Exception as exc:
return StatusResponse( return StatusResponse(