diff --git a/cashu/wallet/cli/cli.py b/cashu/wallet/cli/cli.py index 0c8725e3..591ddd44 100644 --- a/cashu/wallet/cli/cli.py +++ b/cashu/wallet/cli/cli.py @@ -203,10 +203,8 @@ async def pay( quote = await wallet.melt_quote(invoice, amount) logger.debug(f"Quote: {quote}") total_amount = quote.amount + quote.fee_reserve - # we need to include fees so we can use the proofs for melting the `total_amount` - send_proofs, ecash_fees = await wallet.select_to_send( - wallet.proofs, total_amount, include_fees=True, set_reserved=True - ) + # estimate ecash fee for the coinselected proofs + ecash_fees = wallet.coinselect_fee(wallet.proofs, total_amount) if not yes: potential = ( f" ({wallet.unit.str(total_amount + ecash_fees)} with potential fees)" @@ -219,7 +217,10 @@ async def pay( abort=True, default=True, ) - + # we need to include fees so we can use the proofs for melting the `total_amount` + send_proofs, _ = await wallet.select_to_send( + wallet.proofs, total_amount, include_fees=True, set_reserved=True + ) print("Paying Lightning invoice ...", end="", flush=True) assert total_amount > 0, "amount is not positive" if wallet.available_balance < total_amount: diff --git a/cashu/wallet/transactions.py b/cashu/wallet/transactions.py index 084d95c6..51b0343e 100644 --- a/cashu/wallet/transactions.py +++ b/cashu/wallet/transactions.py @@ -36,7 +36,7 @@ def get_fees_for_proofs(self, proofs: List[Proof]) -> int: def get_fees_for_proofs_ppk(self, proofs: List[Proof]) -> int: return sum([self.keysets[p.id].input_fee_ppk for p in proofs]) - async def coinselect( + def coinselect( self, proofs: List[Proof], amount_to_send: Union[int, float], @@ -91,7 +91,7 @@ async def coinselect( logger.trace( f"> selecting more proofs from {amount_summary(smaller_proofs[1:], self.unit)} sum: {sum_proofs(smaller_proofs[1:])} to reach {remainder}" ) - selected_proofs += await self.coinselect( + selected_proofs += self.coinselect( smaller_proofs[1:], remainder, include_fees=include_fees ) sum_selected_proofs = sum_proofs(selected_proofs) @@ -105,6 +105,10 @@ async def coinselect( ) return selected_proofs + def coinselect_fee(self, proofs: List[Proof], amount: int) -> int: + proofs_send = self.coinselect(proofs, amount, include_fees=True) + return self.get_fees_for_proofs(proofs_send) + async def set_reserved(self, proofs: List[Proof], reserved: bool) -> None: """Mark a proof as reserved or reset it in the wallet db to avoid reuse when it is sent. diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index 43f10b9c..3842aea6 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -1078,7 +1078,7 @@ async def select_to_send( raise Exception("balance too low.") # coin selection for potentially offline sending - send_proofs = await self.coinselect(proofs, amount, include_fees=include_fees) + send_proofs = self.coinselect(proofs, amount, include_fees=include_fees) fees = self.get_fees_for_proofs(send_proofs) logger.trace( f"select_to_send: selected: {self.unit.str(sum_proofs(send_proofs))} (+ {self.unit.str(fees)} fees) – wanted: {self.unit.str(amount)}" @@ -1136,7 +1136,7 @@ async def swap_to_send( raise Exception("balance too low.") # coin selection for swapping, needs to include fees - swap_proofs = await self.coinselect(proofs, amount, include_fees=True) + swap_proofs = self.coinselect(proofs, amount, include_fees=True) # Extra rule: add proofs from inactive keysets to swap_proofs to get rid of them swap_proofs += [