utils.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use std::io::{self, Write};
  2. use std::str::FromStr;
  3. use anyhow::Result;
  4. use cdk::mint_url::MintUrl;
  5. use cdk::wallet::multi_mint_wallet::MultiMintWallet;
  6. /// Helper function to get user input with a prompt
  7. pub fn get_user_input(prompt: &str) -> Result<String> {
  8. println!("{prompt}");
  9. let mut user_input = String::new();
  10. io::stdout().flush()?;
  11. io::stdin().read_line(&mut user_input)?;
  12. Ok(user_input.trim().to_string())
  13. }
  14. /// Helper function to get a number from user input with a prompt
  15. pub fn get_number_input<T>(prompt: &str) -> Result<T>
  16. where
  17. T: FromStr,
  18. T::Err: std::error::Error + Send + Sync + 'static,
  19. {
  20. let input = get_user_input(prompt)?;
  21. let number = input.parse::<T>()?;
  22. Ok(number)
  23. }
  24. /// Helper function to create or get a wallet
  25. pub async fn get_or_create_wallet(
  26. multi_mint_wallet: &MultiMintWallet,
  27. mint_url: &MintUrl,
  28. ) -> Result<cdk::wallet::Wallet> {
  29. match multi_mint_wallet.get_wallet(mint_url).await {
  30. Some(wallet) => Ok(wallet.clone()),
  31. None => {
  32. tracing::debug!("Wallet does not exist creating..");
  33. multi_mint_wallet.add_mint(mint_url.clone()).await?;
  34. Ok(multi_mint_wallet
  35. .get_wallet(mint_url)
  36. .await
  37. .expect("Wallet should exist after adding mint")
  38. .clone())
  39. }
  40. }
  41. }