utils.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. use std::io::{self, Write};
  2. use std::str::FromStr;
  3. use anyhow::{bail, Result};
  4. use cdk::mint_url::MintUrl;
  5. use cdk::nuts::CurrencyUnit;
  6. use cdk::wallet::multi_mint_wallet::MultiMintWallet;
  7. use cdk::wallet::types::WalletKey;
  8. use cdk::Amount;
  9. /// Helper function to get user input with a prompt
  10. pub fn get_user_input(prompt: &str) -> Result<String> {
  11. println!("{prompt}");
  12. let mut user_input = String::new();
  13. io::stdout().flush()?;
  14. io::stdin().read_line(&mut user_input)?;
  15. Ok(user_input.trim().to_string())
  16. }
  17. /// Helper function to get a number from user input with a prompt
  18. pub fn get_number_input<T>(prompt: &str) -> Result<T>
  19. where
  20. T: FromStr,
  21. T::Err: std::error::Error + Send + Sync + 'static,
  22. {
  23. let input = get_user_input(prompt)?;
  24. let number = input.parse::<T>()?;
  25. Ok(number)
  26. }
  27. /// Helper function to validate a mint number against available mints
  28. pub fn validate_mint_number(mint_number: usize, mint_count: usize) -> Result<()> {
  29. if mint_number >= mint_count {
  30. bail!("Invalid mint number");
  31. }
  32. Ok(())
  33. }
  34. /// Helper function to check if there are enough funds for an operation
  35. pub fn check_sufficient_funds(available: Amount, required: Amount) -> Result<()> {
  36. if required.gt(&available) {
  37. bail!("Not enough funds");
  38. }
  39. Ok(())
  40. }
  41. /// Helper function to get a wallet from the multi-mint wallet by mint URL
  42. pub async fn get_wallet_by_mint_url(
  43. multi_mint_wallet: &MultiMintWallet,
  44. mint_url_str: &str,
  45. unit: CurrencyUnit,
  46. ) -> Result<cdk::wallet::Wallet> {
  47. let mint_url = MintUrl::from_str(mint_url_str)?;
  48. let wallet_key = WalletKey::new(mint_url.clone(), unit);
  49. let wallet = multi_mint_wallet
  50. .get_wallet(&wallet_key)
  51. .await
  52. .ok_or_else(|| anyhow::anyhow!("Wallet not found for mint URL: {}", mint_url_str))?;
  53. Ok(wallet.clone())
  54. }
  55. /// Helper function to get a wallet from the multi-mint wallet
  56. pub async fn get_wallet_by_index(
  57. multi_mint_wallet: &MultiMintWallet,
  58. mint_amounts: &[(MintUrl, Amount)],
  59. mint_number: usize,
  60. unit: CurrencyUnit,
  61. ) -> Result<cdk::wallet::Wallet> {
  62. validate_mint_number(mint_number, mint_amounts.len())?;
  63. let wallet_key = WalletKey::new(mint_amounts[mint_number].0.clone(), unit);
  64. let wallet = multi_mint_wallet
  65. .get_wallet(&wallet_key)
  66. .await
  67. .ok_or_else(|| anyhow::anyhow!("Wallet not found"))?;
  68. Ok(wallet.clone())
  69. }
  70. /// Helper function to create or get a wallet
  71. pub async fn get_or_create_wallet(
  72. multi_mint_wallet: &MultiMintWallet,
  73. mint_url: &MintUrl,
  74. unit: CurrencyUnit,
  75. ) -> Result<cdk::wallet::Wallet> {
  76. match multi_mint_wallet
  77. .get_wallet(&WalletKey::new(mint_url.clone(), unit.clone()))
  78. .await
  79. {
  80. Some(wallet) => Ok(wallet.clone()),
  81. None => {
  82. tracing::debug!("Wallet does not exist creating..");
  83. multi_mint_wallet
  84. .create_and_add_wallet(&mint_url.to_string(), unit, None)
  85. .await
  86. }
  87. }
  88. }