mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-30 14:06:28 +02:00
improve checking routine.
check pending invoices only once on startup, then check outgoing payments every 30 minutes, and delete outgoing payments that return False (meaning they have failed). also fix a bug on sparko.
This commit is contained in:
@@ -58,7 +58,7 @@ class Wallet(NamedTuple):
|
|||||||
pending: bool = False,
|
pending: bool = False,
|
||||||
outgoing: bool = True,
|
outgoing: bool = True,
|
||||||
incoming: bool = True,
|
incoming: bool = True,
|
||||||
exclude_uncheckable: bool = False
|
exclude_uncheckable: bool = False,
|
||||||
) -> List["Payment"]:
|
) -> List["Payment"]:
|
||||||
from .crud import get_payments
|
from .crud import get_payments
|
||||||
|
|
||||||
@@ -141,11 +141,18 @@ class Payment(NamedTuple):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if self.is_out:
|
if self.is_out:
|
||||||
pending = await WALLET.get_payment_status(self.checking_id)
|
status = await WALLET.get_payment_status(self.checking_id)
|
||||||
else:
|
else:
|
||||||
pending = await WALLET.get_invoice_status(self.checking_id)
|
status = await WALLET.get_invoice_status(self.checking_id)
|
||||||
|
|
||||||
await self.set_pending(pending.pending)
|
print(
|
||||||
|
f" - checking '{'in' if self.is_in else 'out'}' {self.checking_id}: {status.paid}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.is_out and status.failed:
|
||||||
|
await self.delete()
|
||||||
|
elif not status.pending:
|
||||||
|
await self.set_pending(status.pending)
|
||||||
|
|
||||||
async def delete(self) -> None:
|
async def delete(self) -> None:
|
||||||
from .crud import delete_payment
|
from .crud import delete_payment
|
||||||
|
@@ -71,19 +71,26 @@ async def invoice_listener(nursery):
|
|||||||
|
|
||||||
async def check_pending_payments():
|
async def check_pending_payments():
|
||||||
await delete_expired_invoices()
|
await delete_expired_invoices()
|
||||||
|
|
||||||
|
outgoing = True
|
||||||
|
incoming = True
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
for payment in await get_payments(
|
for payment in await get_payments(
|
||||||
since=(int(time.time()) - 60 * 60 * 24 * 15), # 15 days ago
|
since=(int(time.time()) - 60 * 60 * 24 * 15), # 15 days ago
|
||||||
complete=False,
|
complete=False,
|
||||||
pending=True,
|
pending=True,
|
||||||
|
outgoing=outgoing,
|
||||||
|
incoming=incoming,
|
||||||
exclude_uncheckable=True,
|
exclude_uncheckable=True,
|
||||||
):
|
):
|
||||||
print(
|
|
||||||
f" - checking {'in' if payment.is_in else 'out'} pending: {payment.checking_id}"
|
|
||||||
)
|
|
||||||
await payment.check_pending()
|
await payment.check_pending()
|
||||||
|
|
||||||
await trio.sleep(60 * 120)
|
# after the first check we will only check outgoing, not incoming
|
||||||
|
# that will be handled by the global invoice listeners, hopefully
|
||||||
|
incoming = False
|
||||||
|
|
||||||
|
await trio.sleep(60 * 30) # every 30 minutes
|
||||||
|
|
||||||
|
|
||||||
async def invoice_callback_dispatcher(checking_id: str):
|
async def invoice_callback_dispatcher(checking_id: str):
|
||||||
|
@@ -30,6 +30,20 @@ class PaymentStatus(NamedTuple):
|
|||||||
def pending(self) -> bool:
|
def pending(self) -> bool:
|
||||||
return self.paid is not True
|
return self.paid is not True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def failed(self) -> bool:
|
||||||
|
return self.paid == False
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
if self.paid == True:
|
||||||
|
return "settled"
|
||||||
|
elif self.paid == False:
|
||||||
|
return "failed"
|
||||||
|
elif self.paid == None:
|
||||||
|
return "still pending"
|
||||||
|
else:
|
||||||
|
return "unknown (should never happen)"
|
||||||
|
|
||||||
|
|
||||||
class Wallet(ABC):
|
class Wallet(ABC):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@@ -130,7 +130,7 @@ class SparkWallet(Wallet):
|
|||||||
if pay["status"] == "failed":
|
if pay["status"] == "failed":
|
||||||
return PaymentResponse(False, None, 0, None, str(exc))
|
return PaymentResponse(False, None, 0, None, str(exc))
|
||||||
elif pay["status"] == "pending":
|
elif pay["status"] == "pending":
|
||||||
return PaymentResponse(None, listpays["pays"], 0, None, None)
|
return PaymentResponse(None, payment_hash, 0, None, None)
|
||||||
elif pay["status"] == "complete":
|
elif pay["status"] == "complete":
|
||||||
r = pay
|
r = pay
|
||||||
r["payment_preimage"] = pay["preimage"]
|
r["payment_preimage"] = pay["preimage"]
|
||||||
|
Reference in New Issue
Block a user