Files
xmrcheckout/api/app/payment_confirmations.py
T
such-gitea fbd0f9e1c1 fix(payments): confirm an invoice only when confirmed payments cover it
The reconciler took the deepest single payment's confirmations as the
invoice's. A tiny early payment could therefore make an unconfirmed top-up
count as confirmed, and InvoiceSettled fired before the money was final.

The invoice depth is now the depth at which payments, taken deepest first,
cover the whole amount. It is 0 while the invoice is underpaid.
2026-11-18 15:38:47 -05:00

31 lines
1.0 KiB
Python

from __future__ import annotations
from typing import Iterable, Protocol
class _Payment(Protocol):
amount_atomic: int
confirmations: int
def effective_confirmations(transfers: Iterable[_Payment], required_atomic: int) -> int:
"""Return the confirmation depth at which the invoice amount is fully covered.
Payments are taken from the deepest down, and the depth of the last payment
needed to reach required_atomic is the invoice's depth. An invoice is
therefore confirmed to N only when payments with at least N confirmations
cover the whole amount. A small early payment cannot lend its depth to an
unconfirmed top-up. Returns 0 while the payments do not cover the amount.
"""
payments = sorted(
(t for t in transfers if t.amount_atomic > 0),
key=lambda t: t.confirmations,
reverse=True,
)
covered = 0
for payment in payments:
covered += payment.amount_atomic
if covered >= required_atomic:
return max(0, int(payment.confirmations))
return 0