fix expiry integer

This commit is contained in:
callebtc 2022-12-06 10:48:16 +01:00
parent befdeb040e
commit 441d5337a3
3 changed files with 25 additions and 26 deletions

View file

@ -377,6 +377,7 @@ async def create_payment(
invoice = bolt11.decode(payment_request) invoice = bolt11.decode(payment_request)
expiration_date = datetime.datetime.fromtimestamp(invoice.date + invoice.expiry) expiration_date = datetime.datetime.fromtimestamp(invoice.date + invoice.expiry)
except: except:
# assume maximum bolt11 expiry of 31 days to be on the safe side
expiration_date = datetime.datetime.now() + datetime.timedelta(days=31) expiration_date = datetime.datetime.now() + datetime.timedelta(days=31)
await (conn or db).execute( await (conn or db).execute(

View file

@ -1,5 +1,5 @@
import datetime import datetime
from loguru import logger
from sqlalchemy.exc import OperationalError # type: ignore from sqlalchemy.exc import OperationalError # type: ignore
from lnbits import bolt11 from lnbits import bolt11
@ -216,37 +216,35 @@ async def m006_add_invoice_expiry_to_apipayments(db):
""" """
) )
).fetchall() ).fetchall()
# then we delete all expired invoices, checking one by one logger.info(f"Checking expiry of {len(rows)} invoices")
print(f"Checking expiry of {len(rows)} invoices")
for i, ( for i, (
payment_request, payment_request,
payment_hash, checking_id,
) in enumerate(rows): ) in enumerate(rows):
print(f"Checking invoice {i}/{len(rows)}") logger.info(f"Checking invoice {i}/{len(rows)}")
try: try:
invoice = bolt11.decode(payment_request) invoice = bolt11.decode(payment_request)
if invoice.expiry is None:
continue
expiration_date = datetime.datetime.fromtimestamp(
invoice.date + invoice.expiry
)
logger.info(
f"Setting expiry of invoice {invoice.payment_hash} to {expiration_date}"
)
await db.execute(
"""
UPDATE apipayments SET expiry = ?
WHERE checking_id = ? AND amount > 0
""",
(
db.datetime_to_timestamp(expiration_date),
checking_id,
),
)
except: except:
continue continue
if payment_hash != invoice.payment_hash:
print("Error: {payment_hash} != {invoice.payment_hash}")
continue
expiration_date = datetime.datetime.fromtimestamp(
invoice.date + invoice.expiry
)
print(
f"Setting expiry of invoice {invoice.payment_hash} to {expiration_date}"
)
await db.execute(
"""
UPDATE apipayments SET expiry = ?
WHERE checking_id = ? AND amount > 0
""",
(
db.datetime_to_timestamp(expiration_date),
invoice.payment_hash,
),
)
except OperationalError: except OperationalError:
# this is necessary now because it may be the case that this migration will # this is necessary now because it may be the case that this migration will
# run twice in some environments. # run twice in some environments.

View file

@ -85,7 +85,7 @@ class Payment(BaseModel):
bolt11: str bolt11: str
preimage: str preimage: str
payment_hash: str payment_hash: str
expiry: int expiry: float
extra: Optional[Dict] = {} extra: Optional[Dict] = {}
wallet_id: str wallet_id: str
webhook: Optional[str] webhook: Optional[str]