From 258de87a9a53f68cfef14ffc4db4c2972c8a94eb Mon Sep 17 00:00:00 2001 From: sihamon <126967444+sihamon@users.noreply.github.com> Date: Sun, 19 Mar 2023 15:09:03 +0100 Subject: [PATCH] Fix: Mints are sorted by balance which can suddenly change (#145) * Sort mints by URL when displaying balances * Use mint with largest balance as default when spending * Make format * Display mint with largest balance in prompt --- cashu/wallet/cli/cli_helpers.py | 26 ++++++++++++++++---------- cashu/wallet/wallet.py | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cashu/wallet/cli/cli_helpers.py b/cashu/wallet/cli/cli_helpers.py index 2d884b3..5633056 100644 --- a/cashu/wallet/cli/cli_helpers.py +++ b/cashu/wallet/cli/cli_helpers.py @@ -134,17 +134,23 @@ async def get_mint_wallet(ctx: Context): if len(mint_balances) > 1: await print_mint_balances(ctx, wallet, show_mints=True) - mint_nr_str = ( - input(f"Select mint [1-{len(mint_balances)}, press enter for default 1]: ") - or "1" - ) - if not mint_nr_str.isdigit(): - raise Exception("invalid input.") - mint_nr = int(mint_nr_str) - else: - mint_nr = 1 + url_max = max(mint_balances, key=lambda v: mint_balances[v]["available"]) + nr_max = list(mint_balances).index(url_max) + 1 - mint_url = list(mint_balances.keys())[mint_nr - 1] + mint_nr_str = input( + f"Select mint [1-{len(mint_balances)}] or " + f"press enter for mint with largest balance (Mint {nr_max}): " + ) + if not mint_nr_str: # largest balance + mint_url = url_max + elif mint_nr_str.isdigit() and int(mint_nr_str) <= len( + mint_balances + ): # specific mint + mint_url = list(mint_balances.keys())[int(mint_nr_str) - 1] + else: + raise Exception("invalid input.") + else: + mint_url = list(mint_balances.keys())[0] # load this mint_url into a wallet mint_wallet = Wallet( diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index b0b0fc4..f6e27d2 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -843,4 +843,4 @@ class Wallet(LedgerAPI): } for key, proofs in balances.items() } - return dict(sorted(balances_return.items(), key=lambda item: item[1]["available"], reverse=True)) # type: ignore + return dict(sorted(balances_return.items(), key=lambda item: item[0])) # type: ignore