mint-token.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. use std::sync::Arc;
  2. use cdk::amount::SplitTarget;
  3. use cdk::cdk_database::WalletMemoryDatabase;
  4. use cdk::error::Error;
  5. use cdk::nuts::{CurrencyUnit, MintQuoteState, NotificationPayload};
  6. use cdk::wallet::types::SendKind;
  7. use cdk::wallet::{Wallet, WalletSubscription};
  8. use cdk::Amount;
  9. use rand::Rng;
  10. #[tokio::main]
  11. async fn main() -> Result<(), Error> {
  12. let localstore = WalletMemoryDatabase::default();
  13. let seed = rand::thread_rng().gen::<[u8; 32]>();
  14. let mint_url = "https://testnut.cashu.space";
  15. let unit = CurrencyUnit::Sat;
  16. let amount = Amount::from(10);
  17. let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
  18. let quote = wallet.mint_quote(amount, None).await.unwrap();
  19. println!("Quote: {:#?}", quote);
  20. let mut subscription = wallet
  21. .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
  22. .id
  23. .clone()]))
  24. .await;
  25. while let Some(msg) = subscription.recv().await {
  26. if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
  27. if response.state == MintQuoteState::Paid {
  28. break;
  29. }
  30. }
  31. }
  32. let receive_amount = wallet
  33. .mint(&quote.id, SplitTarget::default(), None)
  34. .await
  35. .unwrap();
  36. println!("Received {receive_amount} from mint {mint_url}");
  37. let token = wallet
  38. .send(
  39. amount,
  40. None,
  41. None,
  42. &SplitTarget::default(),
  43. &SendKind::OnlineExact,
  44. false,
  45. )
  46. .await
  47. .unwrap();
  48. println!("Token:");
  49. println!("{}", token);
  50. Ok(())
  51. }