Skip to content

Commit

Permalink
chore: refactor mint-config
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 24, 2023
1 parent f42bfe6 commit 97b559f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 47 deletions.
23 changes: 22 additions & 1 deletion moksha-mint/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ use std::env;

use serde_derive::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct MintConfig {
pub info: MintInfoConfig,
pub build: BuildConfig,
pub lightning_fee: LightningFeeConfig,
}

impl MintConfig {
pub fn new(
info: MintInfoConfig,
build: BuildConfig,
lightning_fee: LightningFeeConfig,
) -> Self {
Self {
info,
build,
lightning_fee,
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct MintInfoConfig {
pub name: Option<String>,
Expand Down Expand Up @@ -45,7 +66,7 @@ impl BuildConfig {
}
}

#[derive(Clone, Debug)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct LightningFeeConfig {
pub fee_percent: f32,
pub fee_reserve_min: u64,
Expand Down
30 changes: 11 additions & 19 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use moksha_core::{
};

use crate::{
config::{BuildConfig, LightningFeeConfig, MintInfoConfig},
config::{BuildConfig, LightningFeeConfig, MintConfig, MintInfoConfig},
database::Database,
error::MokshaMintError,
lightning::{AlbyLightning, Lightning, LightningType, LnbitsLightning, StrikeLightning},
Expand All @@ -24,9 +24,7 @@ pub struct Mint {
pub keyset: MintKeyset,
pub db: Arc<dyn Database + Send + Sync>,
pub dhke: Dhke,
pub lightning_fee_config: LightningFeeConfig,
pub mint_info_config: MintInfoConfig,
pub build_config: BuildConfig,
pub config: MintConfig,
}

impl Mint {
Expand All @@ -37,20 +35,16 @@ impl Mint {
lightning: Arc<dyn Lightning + Send + Sync>,
lightning_type: LightningType,
db: Arc<dyn Database + Send + Sync>,
lightning_fee_config: LightningFeeConfig,
mint_info_config: MintInfoConfig,
build_config: BuildConfig,
config: MintConfig,
) -> Self {
Self {
lightning,
lightning_type,
lightning_fee_config,
keyset_legacy: MintKeyset::legacy_new(&secret, &derivation_path),
keyset: MintKeyset::new(&secret, &derivation_path),
db,
dhke: Dhke::new(),
mint_info_config,
build_config,
config,
}
}

Expand All @@ -59,9 +53,9 @@ impl Mint {
}

pub fn fee_reserve(&self, amount_msat: u64) -> u64 {
let fee_percent = self.lightning_fee_config.fee_percent as f64 / 100.0;
let fee_percent = self.config.lightning_fee.fee_percent as f64 / 100.0;
let fee_reserve = (amount_msat as f64 * fee_percent) as u64;
std::cmp::max(fee_reserve, self.lightning_fee_config.fee_reserve_min)
std::cmp::max(fee_reserve, self.config.lightning_fee.fee_reserve_min)
}

pub fn create_blinded_signatures(
Expand Down Expand Up @@ -281,9 +275,11 @@ impl MintBuilder {
ln,
self.lightning_type.expect("Lightning backend not set"),
db,
self.fee_config.expect("fee-config not set"),
self.mint_info_settings.unwrap_or_default(),
BuildConfig::from_env(),
MintConfig::new(
self.mint_info_settings.unwrap_or_default(),
BuildConfig::from_env(),
self.fee_config.expect("fee-config not set"),
),
))
}
}
Expand Down Expand Up @@ -436,8 +432,6 @@ mod tests {
LightningType::Lnbits(Default::default()),
Arc::new(create_mock_db_get_used_proofs()),
Default::default(),
Default::default(),
Default::default(),
);

let tokens = create_token_from_fixture("token_60.cashu".to_string())?;
Expand Down Expand Up @@ -496,8 +490,6 @@ mod tests {
LightningType::Lnbits(Default::default()),
db,
Default::default(),
Default::default(),
Default::default(),
)
}

Expand Down
53 changes: 26 additions & 27 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ pub async fn run_server(
.with(tracing_subscriber::fmt::layer())
.init();

if let Some(ref buildtime) = mint.build_config.build_time {
if let Some(ref buildtime) = mint.config.build.build_time {
info!("build time: {}", buildtime);
}
if let Some(ref commithash) = mint.build_config.commit_hash {
if let Some(ref commithash) = mint.config.build.commit_hash {
info!("git commit-hash: {}", commithash);
}
if let Some(ref serve_wallet_path) = serve_wallet_path {
info!("serving wallet from path: {:?}", serve_wallet_path);
}
info!("listening on: {}", addr);
info!("mint-info (legacy): {:?}", mint.mint_info_config);
info!("lightning fee-reserve: {:?}", mint.lightning_fee_config);
info!("mint-info: {:?}", mint.config.info);
info!("lightning fee-reserve: {:?}", mint.config.lightning_fee);
info!("lightning-backend: {}", mint.lightning_type);

let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
Expand Down Expand Up @@ -273,15 +273,15 @@ async fn get_legacy_info(
State(mint): State<Mint>,
) -> Result<Json<MintLegacyInfoResponse>, MokshaMintError> {
let mint_info = MintLegacyInfoResponse {
name: mint.mint_info_config.name,
name: mint.config.info.name,
pubkey: mint.keyset_legacy.mint_pubkey,
version: match mint.mint_info_config.version {
true => Some(mint.build_config.full_version()),
version: match mint.config.info.version {
true => Some(mint.config.build.full_version()),
_ => None,
},
description: mint.mint_info_config.description,
description_long: mint.mint_info_config.description_long,
contact: mint.mint_info_config.contact,
description: mint.config.info.description,
description_long: mint.config.info.description_long,
contact: mint.config.info.contact,
nuts: vec![
"NUT-00".to_string(),
"NUT-01".to_string(),
Expand All @@ -293,7 +293,7 @@ async fn get_legacy_info(
"NUT-08".to_string(),
"NUT-09".to_string(),
],
motd: mint.mint_info_config.motd,
motd: mint.config.info.motd,
parameter: Default::default(),
};
Ok(Json(mint_info))
Expand Down Expand Up @@ -634,18 +634,19 @@ async fn get_melt_quote_bolt11(
)
)]
async fn get_info(State(mint): State<Mint>) -> Result<Json<MintInfoResponse>, MokshaMintError> {
// TODO implement From-trait
let mint_info = MintInfoResponse {
name: mint.mint_info_config.name,
pubkey: mint.keyset_legacy.mint_pubkey,
version: match mint.mint_info_config.version {
true => Some(mint.build_config.full_version()),
name: mint.config.info.name,
pubkey: mint.keyset.mint_pubkey,
version: match mint.config.info.version {
true => Some(mint.config.build.full_version()),
_ => None,
},
description: mint.mint_info_config.description,
description_long: mint.mint_info_config.description_long,
contact: mint.mint_info_config.contact,
description: mint.config.info.description,
description_long: mint.config.info.description_long,
contact: mint.config.info.contact,
nuts: Nuts::default(),
motd: mint.mint_info_config.motd,
motd: mint.config.info.motd,
};
Ok(Json(mint_info))
}
Expand All @@ -654,10 +655,7 @@ async fn get_info(State(mint): State<Mint>) -> Result<Json<MintInfoResponse>, Mo
mod tests {
use std::{collections::HashMap, sync::Arc};

use crate::{
config::{BuildConfig, LightningFeeConfig},
server::app,
};
use crate::{config::MintConfig, server::app};
use axum::{
body::Body,
http::{Request, StatusCode},
Expand Down Expand Up @@ -733,7 +731,7 @@ mod tests {
Ok(())
}

fn create_mock_mint(mint_info: MintInfoConfig) -> Mint {
fn create_mock_mint(info: MintInfoConfig) -> Mint {
let db = Arc::new(MockDatabase::new());
let lightning = Arc::new(MockLightning::new());

Expand All @@ -743,9 +741,10 @@ mod tests {
lightning,
LightningType::Lnbits(Default::default()),
db,
LightningFeeConfig::default(),
mint_info,
BuildConfig::default(),
MintConfig {
info,
..Default::default()
},
)
}

Expand Down

0 comments on commit 97b559f

Please sign in to comment.