memory.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //! In-memory database that is provided by the `cdk-sqlite` crate, mainly for testing purposes.
  2. use std::collections::HashMap;
  3. use cdk_common::common::LnKey;
  4. use cdk_common::database::{self, MintDatabase};
  5. use cdk_common::mint::{self, MintKeySetInfo, MintQuote};
  6. use cdk_common::nuts::{CurrencyUnit, Id, MeltBolt11Request, Proofs};
  7. use cdk_common::MintInfo;
  8. use uuid::Uuid;
  9. use super::MintSqliteDatabase;
  10. /// Creates a new in-memory [`MintSqliteDatabase`] instance
  11. pub async fn empty() -> Result<MintSqliteDatabase, database::Error> {
  12. let db = MintSqliteDatabase::new(":memory:").await?;
  13. db.migrate().await;
  14. Ok(db)
  15. }
  16. /// Creates a new in-memory [`MintSqliteDatabase`] instance with the given state
  17. #[allow(clippy::too_many_arguments)]
  18. pub async fn new_with_state(
  19. active_keysets: HashMap<CurrencyUnit, Id>,
  20. keysets: Vec<MintKeySetInfo>,
  21. mint_quotes: Vec<MintQuote>,
  22. melt_quotes: Vec<mint::MeltQuote>,
  23. pending_proofs: Proofs,
  24. spent_proofs: Proofs,
  25. melt_request: Vec<(MeltBolt11Request<Uuid>, LnKey)>,
  26. mint_info: MintInfo,
  27. ) -> Result<MintSqliteDatabase, database::Error> {
  28. let db = empty().await?;
  29. db.migrate().await;
  30. for active_keyset in active_keysets {
  31. db.set_active_keyset(active_keyset.0, active_keyset.1)
  32. .await?;
  33. }
  34. for keyset in keysets {
  35. db.add_keyset_info(keyset).await?;
  36. }
  37. for quote in mint_quotes {
  38. db.add_mint_quote(quote).await?;
  39. }
  40. for quote in melt_quotes {
  41. db.add_melt_quote(quote).await?;
  42. }
  43. db.add_proofs(pending_proofs, None).await?;
  44. db.add_proofs(spent_proofs, None).await?;
  45. for (melt_request, ln_key) in melt_request {
  46. db.add_melt_request(melt_request, ln_key).await?;
  47. }
  48. db.set_mint_info(mint_info).await?;
  49. Ok(db)
  50. }