diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index 3bd4bd1..b500c99 100755 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -134,17 +134,10 @@ async def pay(ctx, invoice: str, yes: bool): wallet: Wallet = ctx.obj["WALLET"] await wallet.load_mint() wallet.status() - decoded_invoice: Invoice = bolt11.decode(invoice) - - # check if it's an internal payment - fees = (await wallet.check_fees(invoice))["fee"] - amount = math.ceil( - (decoded_invoice.amount_msat + fees * 1000) / 1000 - ) # 1% fee for Lightning - + amount, fees = await wallet.get_pay_amount_with_fees(invoice) if not yes: click.confirm( - f"Pay {decoded_invoice.amount_msat//1000} sat ({amount} sat incl. fees)?", + f"Pay {amount - fees} sat ({amount} sat incl. fees)?", abort=True, default=True, ) diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index 8c55e68..b1c3338 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -3,11 +3,14 @@ import json import secrets as scrts import time import uuid +import math from itertools import groupby from typing import Dict, List import requests from loguru import logger +import cashu.core.bolt11 as bolt11 +from cashu.core.bolt11 import Invoice as InvoiceBolt11 import cashu.core.b_dhke as b_dhke from cashu.core.base import ( @@ -436,6 +439,25 @@ class Wallet(LedgerAPI): proofs = [p for p in proofs if not p.reserved] return proofs + async def get_pay_amount_with_fees(self, invoice: str): + """ + Decodes the amount from a Lightning invoice and returns the + total amount (amount+fees) to be paid. + """ + decoded_invoice: InvoiceBolt11 = bolt11.decode(invoice) + # check if it's an internal payment + fees = int((await self.check_fees(invoice))["fee"]) + amount = math.ceil((decoded_invoice.amount_msat + fees * 1000) / 1000) # 1% fee + return amount, fees + + async def split_to_pay(self, invoice: str): + """ + Splits proofs such that a Lightning invoice can be paid. + """ + amount, _ = await self.get_pay_amount_with_fees(invoice) + _, send_proofs = await self.split_to_send(self.proofs, amount) + return send_proofs + async def split_to_send( self, proofs: List[Proof],