wallet.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //! Wallet example with memory store
  2. use std::sync::Arc;
  3. use std::time::Duration;
  4. use cdk::amount::SplitTarget;
  5. use cdk::cdk_database::WalletMemoryDatabase;
  6. use cdk::nuts::{CurrencyUnit, MintQuoteState};
  7. use cdk::wallet::types::SendKind;
  8. use cdk::wallet::Wallet;
  9. use cdk::Amount;
  10. use rand::Rng;
  11. use tokio::time::sleep;
  12. #[tokio::main]
  13. async fn main() {
  14. let seed = rand::thread_rng().gen::<[u8; 32]>();
  15. let mint_url = "https://testnut.cashu.space";
  16. let unit = CurrencyUnit::Sat;
  17. let amount = Amount::from(10);
  18. let localstore = WalletMemoryDatabase::default();
  19. let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
  20. let quote = wallet.mint_quote(amount, None).await.unwrap();
  21. println!("Pay request: {}", quote.request);
  22. loop {
  23. let status = wallet.mint_quote_state(&quote.id).await.unwrap();
  24. if status.state == MintQuoteState::Paid {
  25. break;
  26. }
  27. println!("Quote state: {}", status.state);
  28. sleep(Duration::from_secs(5)).await;
  29. }
  30. let receive_amount = wallet
  31. .mint(&quote.id, SplitTarget::default(), None)
  32. .await
  33. .unwrap();
  34. println!("Minted {}", receive_amount);
  35. let token = wallet
  36. .send(
  37. amount,
  38. None,
  39. None,
  40. &SplitTarget::None,
  41. &SendKind::default(),
  42. false,
  43. )
  44. .await
  45. .unwrap();
  46. println!("{}", token);
  47. }