From 8a4813aee674423e1adee555079bd5b5253601fd Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Wed, 18 Oct 2023 16:18:57 +0200 Subject: [PATCH] Zero amount invoices: throw error before attempting a payment (#349) * Zero amount invoices: throw error before attempting a payment * better naming of fee variables during melt --- cashu/mint/ledger.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index 179baad..8b1b804 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -445,16 +445,19 @@ class Ledger(LedgerVerification, LedgerSpendingConditions): # verify amounts total_provided = sum_proofs(proofs) invoice_obj = bolt11.decode(invoice) + assert invoice_obj.amount_msat, "invoice has no amount." invoice_amount = math.ceil(invoice_obj.amount_msat / 1000) if settings.mint_max_peg_out and invoice_amount > settings.mint_max_peg_out: raise NotAllowedError( f"Maximum melt amount is {settings.mint_max_peg_out} sat." ) - fees_sat = await self.get_melt_fees(invoice) + reserve_fees_sat = await self.get_melt_fees(invoice) # verify overspending attempt - assert total_provided >= invoice_amount + fees_sat, TransactionError( + assert ( + total_provided >= invoice_amount + reserve_fees_sat + ), TransactionError( "provided proofs not enough for Lightning payment. Provided:" - f" {total_provided}, needed: {invoice_amount + fees_sat}" + f" {total_provided}, needed: {invoice_amount + reserve_fees_sat}" ) # verify spending inputs and their spending conditions @@ -462,16 +465,17 @@ class Ledger(LedgerVerification, LedgerSpendingConditions): if settings.lightning: logger.trace(f"paying lightning invoice {invoice}") - status, preimage, fee_msat = await self._pay_lightning_invoice( - invoice, fees_sat * 1000 + status, preimage, paid_fee_msat = await self._pay_lightning_invoice( + invoice, reserve_fees_sat * 1000 ) preimage = preimage or "" logger.trace("paid lightning invoice") else: - status, preimage, fee_msat = True, "preimage", 0 + status, preimage, paid_fee_msat = True, "preimage", 0 logger.debug( - f"Melt status: {status}: preimage: {preimage}, fee_msat: {fee_msat}" + f"Melt status: {status}: preimage: {preimage}, fee_msat:" + f" {paid_fee_msat}" ) if not status: @@ -482,11 +486,11 @@ class Ledger(LedgerVerification, LedgerSpendingConditions): # prepare change to compensate wallet for overpaid fees return_promises: List[BlindedSignature] = [] - if outputs and fee_msat is not None: + if outputs and paid_fee_msat is not None: return_promises = await self._generate_change_promises( total_provided=total_provided, invoice_amount=invoice_amount, - ln_fee_msat=fee_msat, + ln_fee_msat=paid_fee_msat, outputs=outputs, )