Skip to content

Commit

Permalink
cleanup coinselect function, estimate fees
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 2, 2024
1 parent 673284e commit f2cc272
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
11 changes: 6 additions & 5 deletions cashu/wallet/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions cashu/wallet/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
Expand Down Expand Up @@ -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 += [
Expand Down

0 comments on commit f2cc272

Please sign in to comment.