Skip to content

Commit

Permalink
add fee response to payments
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 16, 2023
1 parent ff0f12d commit 734aa51
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion cashu/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
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


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):
Expand Down
18 changes: 16 additions & 2 deletions cashu/wallet/lightning/lightning.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import bolt11

from ...core.helpers import sum_promises
from ...core.settings import settings
from ...lightning.base import (
InvoiceResponse,
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 734aa51

Please sign in to comment.