init_fake_wallet.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. use std::collections::{HashMap, HashSet};
  2. use std::sync::Arc;
  3. use anyhow::Result;
  4. use axum::Router;
  5. use cdk::cdk_database::{self, MintDatabase};
  6. use cdk::cdk_lightning::MintLightning;
  7. use cdk::mint::FeeReserve;
  8. use cdk::nuts::CurrencyUnit;
  9. use cdk::types::LnKey;
  10. use cdk_fake_wallet::FakeWallet;
  11. use tokio::sync::Notify;
  12. use tower_http::cors::CorsLayer;
  13. use tracing_subscriber::EnvFilter;
  14. use crate::init_regtest::create_mint;
  15. pub async fn start_fake_mint<D>(addr: &str, port: u16, database: D) -> Result<()>
  16. where
  17. D: MintDatabase<Err = cdk_database::Error> + Send + Sync + 'static,
  18. {
  19. let default_filter = "debug";
  20. let sqlx_filter = "sqlx=warn";
  21. let hyper_filter = "hyper=warn";
  22. let env_filter = EnvFilter::new(format!(
  23. "{},{},{}",
  24. default_filter, sqlx_filter, hyper_filter
  25. ));
  26. // Parse input
  27. tracing_subscriber::fmt().with_env_filter(env_filter).init();
  28. let mut ln_backends: HashMap<
  29. LnKey,
  30. Arc<dyn MintLightning<Err = cdk::cdk_lightning::Error> + Sync + Send>,
  31. > = HashMap::new();
  32. let fee_reserve = FeeReserve {
  33. min_fee_reserve: 1.into(),
  34. percent_fee_reserve: 1.0,
  35. };
  36. let fake_wallet = FakeWallet::new(fee_reserve, HashMap::default(), HashSet::default(), 0);
  37. ln_backends.insert(
  38. LnKey::new(CurrencyUnit::Sat, cdk::nuts::PaymentMethod::Bolt11),
  39. Arc::new(fake_wallet),
  40. );
  41. let mint = create_mint(database, ln_backends.clone()).await?;
  42. let cache_ttl = 3600;
  43. let cache_tti = 3600;
  44. let mint_arc = Arc::new(mint);
  45. let v1_service = cdk_axum::create_mint_router(Arc::clone(&mint_arc), cache_ttl, cache_tti)
  46. .await
  47. .unwrap();
  48. let mint_service = Router::new()
  49. .merge(v1_service)
  50. .layer(CorsLayer::permissive());
  51. let mint = Arc::clone(&mint_arc);
  52. let shutdown = Arc::new(Notify::new());
  53. tokio::spawn({
  54. let shutdown = Arc::clone(&shutdown);
  55. async move { mint.wait_for_paid_invoices(shutdown).await }
  56. });
  57. println!("Staring Axum server");
  58. axum::Server::bind(&format!("{}:{}", addr, port).as_str().parse().unwrap())
  59. .serve(mint_service.into_make_service())
  60. .await?;
  61. Ok(())
  62. }