Skip to content

Commit

Permalink
refactor: get onchain settings from config
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Jul 31, 2024
1 parent 836fe25 commit d2dfb88
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
14 changes: 14 additions & 0 deletions crates/cdk-mintd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ pub struct Ln {
pub reserve_fee_min: Amount,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Onchain {
pub send_enabled: bool,
pub receive_enabled: bool,
pub mnemonic: String,
pub min_melt: u64,
pub max_melt: u64,
pub min_mint: u64,
pub max_mint: u64,
pub ohttp_relay: Option<String>,
pub payjoin_directory: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Strike {
pub api_key: String,
Expand Down Expand Up @@ -79,6 +92,7 @@ pub struct Settings {
pub cln: Option<Cln>,
pub strike: Option<Strike>,
pub fake_wallet: Option<FakeWallet>,
pub onchain: Option<Onchain>,
pub database: Database,
}

Expand Down
71 changes: 46 additions & 25 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use cdk_sqlite::MintSqliteDatabase;
use cdk_strike::Strike;
use clap::Parser;
use cli::CLIArgs;
use config::{DatabaseEngine, LnBackend};
use config::{DatabaseEngine, LnBackend, Onchain};
use futures::StreamExt;
use tokio::sync::Mutex;
use tower_http::cors::CorsLayer;
Expand Down Expand Up @@ -321,35 +321,56 @@ async fn main() -> anyhow::Result<()> {
.seconds_quote_is_valid_for
.unwrap_or(DEFAULT_QUOTE_TTL_SECS);

let payjoing_settings = PayjoinSettings {
receive_enabled: true,
send_enabled: false,
ohttp_relay: Some("https://pj.bobspacebkk.com".to_string()),
payjoin_directory: Some("https://payjo.in".to_string()),
};

let onchain = BdkWallet::new(0, 0, 0, 0, Mnemonic::parse("promote actress hand galaxy metal buzz square general outside business hard mother keen sound various").unwrap(), &work_dir, payjoing_settings)
.await
.unwrap();

let onchain_clone = onchain.clone();
tokio::spawn(async move {
loop {
if let Err(err) = onchain_clone.wait_handle_proposal().await {
tracing::debug!("Handle proposal stopped: {}", err);
}
}
});

let mut onchain_backends: HashMap<
LnKey,
Arc<dyn MintOnChain<Err = cdk_onchain::Error> + Sync + Send>,
> = HashMap::new();

onchain_backends.insert(
LnKey::new(CurrencyUnit::Sat, PaymentMethod::BtcOnChain),
Arc::new(onchain),
);
if let Some(onchain) = settings.onchain {
let Onchain {
mnemonic,
min_melt,
max_melt,
min_mint,
max_mint,
ohttp_relay,
payjoin_directory,
send_enabled,
receive_enabled,
} = onchain;

let payjoing_settings = PayjoinSettings {
receive_enabled,
send_enabled,
ohttp_relay,
payjoin_directory,
};

let onchain = BdkWallet::new(
min_melt,
max_melt,
min_mint,
max_mint,
Mnemonic::parse(mnemonic)?,
&work_dir,
payjoing_settings,
)
.await?;

let onchain_clone = onchain.clone();
tokio::spawn(async move {
loop {
if let Err(err) = onchain_clone.wait_handle_proposal().await {
tracing::debug!("Handle proposal stopped: {}", err);
}
}
});

onchain_backends.insert(
LnKey::new(CurrencyUnit::Sat, PaymentMethod::BtcOnChain),
Arc::new(onchain),
);
}

let v1_service = cdk_axum::create_mint_router(
&mint_url,
Expand Down

0 comments on commit d2dfb88

Please sign in to comment.