diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index b00c982..b7ec8bb 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -388,24 +388,31 @@ class Wallet(LedgerAPI): ).decode() return token - async def _get_spendable_proofs(self, proofs: List[Proof]): + async def _select_proofs_to_send(self, proofs: List[Proof], amount_to_send: int): """ Selects proofs that can be used with the current mint. Chooses: 1) Proofs that are not marked as reserved 2) Proofs that have a keyset id that is in self.keysets (active keysets of mint) - !!! optional for backwards compatibility with legacy clients """ + # select proofs that are in the active keysets of the mint proofs = [ p for p in proofs if p.id in self.keysets or not p.id ] # "or not p.id" is for backwards compatibility with proofs without a keyset id + # select proofs that are not reserved proofs = [p for p in proofs if not p.reserved] - return proofs + # select proofs based on amount to send + sorted_proofs = sorted(proofs, key=lambda p: p.amount) + send_proofs = [] + while sum_proofs(send_proofs) < amount_to_send: + send_proofs.append(sorted_proofs[len(send_proofs)]) + return send_proofs async def split_to_send(self, proofs: List[Proof], amount, scnd_secret: str = None): """Like self.split but only considers non-reserved tokens.""" if scnd_secret: logger.debug(f"Spending conditions: {scnd_secret}") - spendable_proofs = await self._get_spendable_proofs(proofs) + spendable_proofs = await self._select_proofs_to_send(proofs, amount) if sum_proofs(spendable_proofs) < amount: raise Exception("balance too low.") return await self.split(