mint-token.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. use std::sync::Arc;
  2. use cdk::amount::SplitTarget;
  3. use cdk::error::Error;
  4. use cdk::nuts::nut00::ProofsMethods;
  5. use cdk::nuts::{CurrencyUnit, MintQuoteState, NotificationPayload};
  6. use cdk::wallet::{SendOptions, Wallet, WalletSubscription};
  7. use cdk::Amount;
  8. use cdk_sqlite::wallet::memory;
  9. use rand::random;
  10. use tracing_subscriber::EnvFilter;
  11. #[tokio::main]
  12. async fn main() -> Result<(), Error> {
  13. let default_filter = "debug";
  14. let sqlx_filter = "sqlx=warn,hyper_util=warn,reqwest=warn,rustls=warn";
  15. let env_filter = EnvFilter::new(format!("{},{}", default_filter, sqlx_filter));
  16. // Parse input
  17. tracing_subscriber::fmt().with_env_filter(env_filter).init();
  18. // Initialize the memory store for the wallet
  19. let localstore = Arc::new(memory::empty().await?);
  20. // Generate a random seed for the wallet
  21. let seed = random::<[u8; 32]>();
  22. // Define the mint URL and currency unit
  23. let mint_url = "https://fake.thesimplekid.dev";
  24. let unit = CurrencyUnit::Sat;
  25. let amount = Amount::from(10);
  26. // Create a new wallet
  27. let wallet = Wallet::new(mint_url, unit, localstore, &seed, None)?;
  28. // Request a mint quote from the wallet
  29. let quote = wallet.mint_quote(amount, None).await?;
  30. println!("Quote: {:#?}", quote);
  31. // Subscribe to updates on the mint quote state
  32. let mut subscription = wallet
  33. .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
  34. .id
  35. .clone()]))
  36. .await;
  37. // Wait for the mint quote to be paid
  38. while let Some(msg) = subscription.recv().await {
  39. if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
  40. if response.state == MintQuoteState::Paid {
  41. break;
  42. }
  43. }
  44. }
  45. // Mint the received amount
  46. let proofs = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
  47. let receive_amount = proofs.total_amount()?;
  48. println!("Received {} from mint {}", receive_amount, mint_url);
  49. // Send a token with the specified amount
  50. let prepared_send = wallet.prepare_send(amount, SendOptions::default()).await?;
  51. let token = wallet.send(prepared_send, None).await?;
  52. println!("Token:");
  53. println!("{}", token);
  54. Ok(())
  55. }