Skip to content

Commit

Permalink
Merge pull request #531 from thesimplekid/itest_mint_builder
Browse files Browse the repository at this point in the history
refactor: mint builder in itest
  • Loading branch information
thesimplekid authored Jan 13, 2025
2 parents 8fe0982 + 1f45be2 commit 0840dfe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 71 deletions.
1 change: 0 additions & 1 deletion crates/cashu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! CDK common types and traits
//!
pub mod amount;
pub mod dhke;
#[cfg(feature = "mint")]
Expand Down
36 changes: 22 additions & 14 deletions crates/cdk-integration-tests/src/init_fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ use std::sync::Arc;

use anyhow::Result;
use axum::Router;
use bip39::Mnemonic;
use cdk::cdk_database::{self, MintDatabase};
use cdk::cdk_lightning::MintLightning;
use cdk::mint::FeeReserve;
use cdk::nuts::CurrencyUnit;
use cdk::types::LnKey;
use cdk::mint::{FeeReserve, MintBuilder, MintMeltLimits};
use cdk::nuts::{CurrencyUnit, PaymentMethod};
use cdk_fake_wallet::FakeWallet;
use tokio::sync::Notify;
use tower_http::cors::CorsLayer;
use tracing_subscriber::EnvFilter;

use crate::init_regtest::create_mint;

pub async fn start_fake_mint<D>(addr: &str, port: u16, database: D) -> Result<()>
where
D: MintDatabase<Err = cdk_database::Error> + Send + Sync + 'static,
Expand All @@ -32,24 +29,35 @@ where
// Parse input
tracing_subscriber::fmt().with_env_filter(env_filter).init();

let mut ln_backends: HashMap<
LnKey,
Arc<dyn MintLightning<Err = cdk::cdk_lightning::Error> + Sync + Send>,
> = HashMap::new();

let fee_reserve = FeeReserve {
min_fee_reserve: 1.into(),
percent_fee_reserve: 1.0,
};

let fake_wallet = FakeWallet::new(fee_reserve, HashMap::default(), HashSet::default(), 0);

ln_backends.insert(
LnKey::new(CurrencyUnit::Sat, cdk::nuts::PaymentMethod::Bolt11),
let mut mint_builder = MintBuilder::new();

mint_builder = mint_builder.with_localstore(Arc::new(database));

mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::default(),
Arc::new(fake_wallet),
);

let mint = create_mint(database, ln_backends.clone()).await?;
let mnemonic = Mnemonic::generate(12)?;

mint_builder = mint_builder
.with_name("fake test mint".to_string())
.with_mint_url(format!("http://{addr}:{port}"))
.with_description("fake test mint".to_string())
.with_quote_ttl(10000, 10000)
.with_seed(mnemonic.to_seed_normalized("").to_vec());

let mint = mint_builder.build().await?;

let mint_arc = Arc::new(mint);

let v1_service = cdk_axum::create_mint_router(Arc::clone(&mint_arc))
Expand Down
75 changes: 20 additions & 55 deletions crates/cdk-integration-tests/src/init_regtest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::env;
use std::path::PathBuf;
use std::sync::Arc;
Expand All @@ -7,10 +6,8 @@ use anyhow::Result;
use axum::Router;
use bip39::Mnemonic;
use cdk::cdk_database::{self, MintDatabase};
use cdk::cdk_lightning::MintLightning;
use cdk::mint::{FeeReserve, Mint};
use cdk::nuts::{CurrencyUnit, MintInfo};
use cdk::types::{LnKey, QuoteTTL};
use cdk::mint::{FeeReserve, MintBuilder, MintMeltLimits};
use cdk::nuts::{CurrencyUnit, PaymentMethod};
use cdk_cln::Cln as CdkCln;
use ln_regtest_rs::bitcoin_client::BitcoinClient;
use ln_regtest_rs::bitcoind::Bitcoind;
Expand Down Expand Up @@ -145,49 +142,6 @@ pub async fn create_cln_backend(cln_client: &ClnClient) -> Result<CdkCln> {
Ok(CdkCln::new(rpc_path, fee_reserve).await?)
}

pub async fn create_mint<D>(
database: D,
ln_backends: HashMap<
LnKey,
Arc<dyn MintLightning<Err = cdk::cdk_lightning::Error> + Sync + Send>,
>,
) -> Result<Mint>
where
D: MintDatabase<Err = cdk_database::Error> + Send + Sync + 'static,
{
let nuts = cdk::nuts::Nuts::new()
.nut07(true)
.nut08(true)
.nut09(true)
.nut10(true)
.nut11(true)
.nut12(true)
.nut14(true);

let mint_info = MintInfo::new().nuts(nuts);

let mnemonic = Mnemonic::generate(12)?;

let mut supported_units: HashMap<CurrencyUnit, (u64, u8)> = HashMap::new();
supported_units.insert(CurrencyUnit::Sat, (0, 32));

let quote_ttl = QuoteTTL::new(10000, 10000);

let mint = Mint::new(
&get_mint_url(),
&mnemonic.to_seed_normalized(""),
mint_info,
quote_ttl,
Arc::new(database),
ln_backends,
supported_units,
HashMap::new(),
)
.await?;

Ok(mint)
}

pub async fn start_cln_mint<D>(addr: &str, port: u16, database: D) -> Result<()>
where
D: MintDatabase<Err = cdk_database::Error> + Send + Sync + 'static,
Expand All @@ -196,17 +150,28 @@ where

let cln_backend = create_cln_backend(&cln_client).await?;

let mut ln_backends: HashMap<
LnKey,
Arc<dyn MintLightning<Err = cdk::cdk_lightning::Error> + Sync + Send>,
> = HashMap::new();
let mut mint_builder = MintBuilder::new();

mint_builder = mint_builder.with_localstore(Arc::new(database));

ln_backends.insert(
LnKey::new(CurrencyUnit::Sat, cdk::nuts::PaymentMethod::Bolt11),
mint_builder = mint_builder.add_ln_backend(
CurrencyUnit::Sat,
PaymentMethod::Bolt11,
MintMeltLimits::default(),
Arc::new(cln_backend),
);

let mint = create_mint(database, ln_backends.clone()).await?;
let mnemonic = Mnemonic::generate(12)?;

mint_builder = mint_builder
.with_name("regtest mint".to_string())
.with_mint_url(format!("http://{addr}:{port}"))
.with_description("regtest mint".to_string())
.with_quote_ttl(10000, 10000)
.with_seed(mnemonic.to_seed_normalized("").to_vec());

let mint = mint_builder.build().await?;

let mint_arc = Arc::new(mint);

let v1_service = cdk_axum::create_mint_router(Arc::clone(&mint_arc))
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk/src/wallet/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ impl Wallet {
/// use anyhow::Result;
/// use cdk::amount::{Amount, SplitTarget};
/// use cdk::cdk_database::WalletMemoryDatabase;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::nuts::nut00::ProofsMethods;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
/// use rand::Rng;
///
Expand Down

0 comments on commit 0840dfe

Please sign in to comment.