configure_wallet.rs 3.0 KB

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