Jelajahi Sumber

feat(cli): auto-select mint when only one exists (#1402)

* feat(cli): auto-select mint when only one exists

Problem:
When users have only one mint in their wallet and don't specify
a target mint, the CLI still prompts them to select from the list,
which is unnecessary and adds friction to the UX.

Solution:
Modified the mint selection logic to automatically select the mint
when only one exists in the wallet. When multiple mints are available,
it continues to display the selection prompt as before.

Behavior:
- No --mint-url + 1 mint: Auto-selects without prompt
- No --mint-url + multiple mints: Shows selection prompt (existing)
- With --mint-url: Uses specified mint (existing)

* feat(cli): auto-select mint in melt command when only one exists

Applies the same auto-selection logic from the send command to the melt
command. When the user has only one mint in their wallet and doesn't
specify --mint-url, the CLI now automatically selects it without
prompting, improving UX for single-mint users.

Behavior:
- No --mint-url + 1 mint: Auto-selects without prompt (new)
- No --mint-url + multiple mints: Shows selection prompt (existing)
- With --mint-url: Uses specified mint (existing)
shroominic 1 bulan lalu
induk
melakukan
78675dabb7

+ 29 - 23
crates/cdk-cli/src/sub_commands/melt.rs

@@ -85,34 +85,40 @@ pub async fn pay(
 
         let balances_vec: Vec<(MintUrl, Amount)> = balances_map.into_iter().collect();
 
-        println!("\nAvailable mints and balances:");
-        for (index, (mint_url, balance)) in balances_vec.iter().enumerate() {
-            println!(
-                "  {}: {} - {} {}",
-                index,
-                mint_url,
-                balance,
-                multi_mint_wallet.unit()
-            );
-        }
-        println!("  {}: Any mint (auto-select best)", balances_vec.len());
+        // If only one mint exists, automatically select it
+        if balances_vec.len() == 1 {
+            Some(balances_vec[0].0.clone())
+        } else {
+            // Display all mints with their balances and let user select
+            println!("\nAvailable mints and balances:");
+            for (index, (mint_url, balance)) in balances_vec.iter().enumerate() {
+                println!(
+                    "  {}: {} - {} {}",
+                    index,
+                    mint_url,
+                    balance,
+                    multi_mint_wallet.unit()
+                );
+            }
+            println!("  {}: Any mint (auto-select best)", balances_vec.len());
 
-        let selection = loop {
-            let selection: usize =
-                get_number_input("Enter mint number to melt from (or select Any)")?;
+            let selection = loop {
+                let selection: usize =
+                    get_number_input("Enter mint number to melt from (or select Any)")?;
 
-            if selection == balances_vec.len() {
-                break None; // "Any" option selected
-            }
+                if selection == balances_vec.len() {
+                    break None; // "Any" option selected
+                }
 
-            if let Some((mint_url, _)) = balances_vec.get(selection) {
-                break Some(mint_url.clone());
-            }
+                if let Some((mint_url, _)) = balances_vec.get(selection) {
+                    break Some(mint_url.clone());
+                }
 
-            println!("Invalid selection, please try again.");
-        };
+                println!("Invalid selection, please try again.");
+            };
 
-        selection
+            selection
+        }
     };
 
     if sub_command_args.mpp {

+ 30 - 24
crates/cdk-cli/src/sub_commands/send.rs

@@ -68,7 +68,7 @@ pub async fn send(
     let selected_mint = if let Some(mint_url) = &sub_command_args.mint_url {
         Some(MintUrl::from_str(mint_url)?)
     } else {
-        // Display all mints with their balances and let user select
+        // Get all mints with their balances
         let balances_map = multi_mint_wallet.get_balances().await?;
         if balances_map.is_empty() {
             return Err(anyhow!("No mints available in the wallet"));
@@ -76,34 +76,40 @@ pub async fn send(
 
         let balances_vec: Vec<(MintUrl, Amount)> = balances_map.into_iter().collect();
 
-        println!("\nAvailable mints and balances:");
-        for (index, (mint_url, balance)) in balances_vec.iter().enumerate() {
-            println!(
-                "  {}: {} - {} {}",
-                index,
-                mint_url,
-                balance,
-                multi_mint_wallet.unit()
-            );
-        }
-        println!("  {}: Any mint (auto-select best)", balances_vec.len());
+        // If only one mint exists, automatically select it
+        if balances_vec.len() == 1 {
+            Some(balances_vec[0].0.clone())
+        } else {
+            // Display all mints with their balances and let user select
+            println!("\nAvailable mints and balances:");
+            for (index, (mint_url, balance)) in balances_vec.iter().enumerate() {
+                println!(
+                    "  {}: {} - {} {}",
+                    index,
+                    mint_url,
+                    balance,
+                    multi_mint_wallet.unit()
+                );
+            }
+            println!("  {}: Any mint (auto-select best)", balances_vec.len());
 
-        let selection = loop {
-            let selection: usize =
-                get_number_input("Enter mint number to send from (or select Any)")?;
+            let selection = loop {
+                let selection: usize =
+                    get_number_input("Enter mint number to send from (or select Any)")?;
 
-            if selection == balances_vec.len() {
-                break None; // "Any" option selected
-            }
+                if selection == balances_vec.len() {
+                    break None; // "Any" option selected
+                }
 
-            if let Some((mint_url, _)) = balances_vec.get(selection) {
-                break Some(mint_url.clone());
-            }
+                if let Some((mint_url, _)) = balances_vec.get(selection) {
+                    break Some(mint_url.clone());
+                }
 
-            println!("Invalid selection, please try again.");
-        };
+                println!("Invalid selection, please try again.");
+            };
 
-        selection
+            selection
+        }
     };
 
     let token_amount = Amount::from(get_number_input::<u64>(&format!(