mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-22 19:34:18 +01:00
refactor invoice pending
This commit is contained in:
@@ -229,24 +229,41 @@ class Ledger:
|
|||||||
) = await self.lightning.create_invoice(amount, "cashu deposit")
|
) = await self.lightning.create_invoice(amount, "cashu deposit")
|
||||||
return payment_request, checking_id
|
return payment_request, checking_id
|
||||||
|
|
||||||
async def _check_lightning_invoice(self, amounts, payment_hash: str):
|
async def _check_lightning_invoice(self, amount: int, payment_hash: str):
|
||||||
"""Checks with the Lightning backend whether an invoice with this payment_hash was paid."""
|
"""
|
||||||
|
Checks with the Lightning backend whether an invoice with this payment_hash was paid.
|
||||||
|
Raises exception if invoice is unpaid.
|
||||||
|
"""
|
||||||
invoice: Invoice = await self.crud.get_lightning_invoice(
|
invoice: Invoice = await self.crud.get_lightning_invoice(
|
||||||
hash=payment_hash, db=self.db
|
hash=payment_hash, db=self.db
|
||||||
)
|
)
|
||||||
|
if invoice is None:
|
||||||
|
raise Exception("invoice not found.")
|
||||||
if invoice.issued:
|
if invoice.issued:
|
||||||
raise Exception("tokens already issued for this invoice.")
|
raise Exception("tokens already issued for this invoice.")
|
||||||
total_requested = sum(amounts)
|
|
||||||
if total_requested > invoice.amount:
|
# set this invoice as issued
|
||||||
raise Exception(
|
|
||||||
f"Requested amount too high: {total_requested}. Invoice amount: {invoice.amount}"
|
|
||||||
)
|
|
||||||
status = await self.lightning.get_invoice_status(payment_hash)
|
|
||||||
if status.paid:
|
|
||||||
await self.crud.update_lightning_invoice(
|
await self.crud.update_lightning_invoice(
|
||||||
hash=payment_hash, issued=True, db=self.db
|
hash=payment_hash, issued=True, db=self.db
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if amount > invoice.amount:
|
||||||
|
raise Exception(
|
||||||
|
f"requested amount too high: {amount}. Invoice amount: {invoice.amount}"
|
||||||
|
)
|
||||||
|
|
||||||
|
status = await self.lightning.get_invoice_status(payment_hash)
|
||||||
|
if status.paid:
|
||||||
return status.paid
|
return status.paid
|
||||||
|
else:
|
||||||
|
raise Exception("Lightning invoice not paid yet.")
|
||||||
|
except Exception as e:
|
||||||
|
# unset issued
|
||||||
|
await self.crud.update_lightning_invoice(
|
||||||
|
hash=payment_hash, issued=False, db=self.db
|
||||||
|
)
|
||||||
|
raise e
|
||||||
|
|
||||||
async def _pay_lightning_invoice(self, invoice: str, fees_msat: int):
|
async def _pay_lightning_invoice(self, invoice: str, fees_msat: int):
|
||||||
"""Returns an invoice from the Lightning backend."""
|
"""Returns an invoice from the Lightning backend."""
|
||||||
@@ -345,14 +362,15 @@ class Ledger:
|
|||||||
):
|
):
|
||||||
"""Mints a promise for coins for B_."""
|
"""Mints a promise for coins for B_."""
|
||||||
amounts = [b.amount for b in B_s]
|
amounts = [b.amount for b in B_s]
|
||||||
|
amount = sum(amounts)
|
||||||
# check if lightning invoice was paid
|
# check if lightning invoice was paid
|
||||||
if LIGHTNING:
|
if LIGHTNING:
|
||||||
|
if not payment_hash:
|
||||||
|
raise Exception("no payment_hash provided.")
|
||||||
try:
|
try:
|
||||||
paid = await self._check_lightning_invoice(amounts, payment_hash)
|
paid = await self._check_lightning_invoice(amount, payment_hash)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception("could not check invoice: " + str(e))
|
raise e
|
||||||
if not paid:
|
|
||||||
raise Exception("Lightning invoice not paid yet.")
|
|
||||||
|
|
||||||
for amount in amounts:
|
for amount in amounts:
|
||||||
if amount not in [2**i for i in range(MAX_ORDER)]:
|
if amount not in [2**i for i in range(MAX_ORDER)]:
|
||||||
|
|||||||
Reference in New Issue
Block a user