From 734aa51ac9de20e427e9b20cc5cdd36f71d97df4 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:26:53 +0200 Subject: [PATCH] add fee response to payments --- cashu/core/helpers.py | 6 +++++- cashu/wallet/lightning/lightning.py | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cashu/core/helpers.py b/cashu/core/helpers.py index 15b77cd3..6b73d507 100644 --- a/cashu/core/helpers.py +++ b/cashu/core/helpers.py @@ -3,7 +3,7 @@ from functools import partial, wraps from typing import List -from ..core.base import Proof +from ..core.base import BlindedSignature, Proof from ..core.settings import settings @@ -11,6 +11,10 @@ def sum_proofs(proofs: List[Proof]): return sum([p.amount for p in proofs]) +def sum_promises(promises: List[BlindedSignature]): + return sum([p.amount for p in promises]) + + def async_wrap(func): @wraps(func) async def run(*args, loop=None, executor=None, **kwargs): diff --git a/cashu/wallet/lightning/lightning.py b/cashu/wallet/lightning/lightning.py index 4c3b85cf..c22bf3cd 100644 --- a/cashu/wallet/lightning/lightning.py +++ b/cashu/wallet/lightning/lightning.py @@ -1,3 +1,6 @@ +import bolt11 + +from ...core.helpers import sum_promises from ...core.settings import settings from ...lightning.base import ( InvoiceResponse, @@ -56,8 +59,19 @@ async def pay_invoice(self, pr: str) -> PaymentResponse: return PaymentResponse(ok=False) _, send_proofs = await self.split_to_send(self.proofs, total_amount) try: - await self.pay_lightning(send_proofs, pr, fee_reserve_sat) - return PaymentResponse(ok=True) + resp = await self.pay_lightning(send_proofs, pr, fee_reserve_sat) + if resp.change: + fees_paid_sat = fee_reserve_sat - sum_promises(resp.change) + else: + fees_paid_sat = fee_reserve_sat + + invoice_obj = bolt11.decode(pr) + return PaymentResponse( + ok=True, + checking_id=invoice_obj.payment_hash, + preimage=resp.preimage, + fee_msat=fees_paid_sat * 1000, + ) except Exception as e: print("Exception:", e) return PaymentResponse(ok=False, error_message=str(e))