configure_wallet.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //! Example of configuring the wallet with custom settings, including metadata cache TTL.
  2. //!
  3. //! This example demonstrates:
  4. //! 1. Creating a Wallet with a custom metadata cache TTL
  5. //! 2. Creating a MultiMintWallet and adding a mint with a custom configuration
  6. //! 3. Updating the configuration of an active wallet
  7. use std::str::FromStr;
  8. use std::sync::Arc;
  9. use std::time::Duration;
  10. use cdk::mint_url::MintUrl;
  11. use cdk::nuts::CurrencyUnit;
  12. use cdk::wallet::multi_mint_wallet::WalletConfig;
  13. use cdk::wallet::{MultiMintWallet, WalletBuilder};
  14. use cdk_sqlite::wallet::memory;
  15. use rand::random;
  16. #[tokio::main]
  17. async fn main() -> Result<(), Box<dyn std::error::Error>> {
  18. // Generate a random seed
  19. let seed = random::<[u8; 64]>();
  20. let unit = CurrencyUnit::Sat;
  21. let localstore = Arc::new(memory::empty().await?);
  22. // ==========================================
  23. // 1. Configure a single Wallet
  24. // ==========================================
  25. println!("\n=== Single Wallet Configuration ===");
  26. let mint_url = MintUrl::from_str("https://fake.thesimplekid.dev")?;
  27. // Create a wallet with a custom 10-minute TTL (default is 1 hour)
  28. let wallet = WalletBuilder::new()
  29. .mint_url(mint_url.clone())
  30. .unit(unit.clone())
  31. .localstore(localstore.clone())
  32. .seed(seed)
  33. .set_metadata_cache_ttl(Some(Duration::from_secs(600))) // 10 minutes
  34. .build()?;
  35. println!("Created wallet with 10 minute metadata cache TTL");
  36. // You can also update the TTL on an existing wallet
  37. wallet.set_metadata_cache_ttl(Some(Duration::from_secs(300))); // Change to 5 minutes
  38. println!("Updated wallet TTL to 5 minutes");
  39. // ==========================================
  40. // 2. Configure MultiMintWallet
  41. // ==========================================
  42. println!("\n=== MultiMintWallet Configuration ===");
  43. // Create the MultiMintWallet
  44. let multi_wallet = MultiMintWallet::new(localstore.clone(), seed, unit.clone()).await?;
  45. // Define configuration for a new mint
  46. // This config uses a very short 1-minute TTL
  47. let config = WalletConfig::new().with_metadata_cache_ttl(Some(Duration::from_secs(60)));
  48. let mint_url_2 = MintUrl::from_str("https://testnut.cashu.space")?;
  49. // Add the mint with the custom configuration
  50. multi_wallet
  51. .add_mint_with_config(mint_url_2.clone(), config.clone())
  52. .await?;
  53. println!("Added mint {} with 1 minute TTL", mint_url_2);
  54. // Update configuration for an existing mint
  55. // Let's disable auto-refresh (set to None) for the first mint
  56. let no_refresh_config = WalletConfig::new().with_metadata_cache_ttl(None); // Never expire
  57. multi_wallet.add_mint(mint_url.clone()).await?; // Add first mint with default settings
  58. multi_wallet
  59. .set_mint_config(mint_url.clone(), no_refresh_config)
  60. .await?;
  61. println!("Updated mint {} to never expire metadata cache", mint_url);
  62. Ok(())
  63. }