From e6a8a840508f98462ce81a7a9bf5db7d6b40ed55 Mon Sep 17 00:00:00 2001 From: ok300 <106775972+ok300@users.noreply.github.com> Date: Tue, 26 Mar 2024 05:58:30 +0100 Subject: [PATCH] Consolidate constants and default init values (#52) --- cli/src/main.rs | 17 ++++++----------- lib/src/lib.rs | 8 ++++++-- lib/src/model.rs | 6 ++++-- lib/src/wallet.rs | 15 +++++---------- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 8a846d81d..757ba954b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -24,20 +24,15 @@ fn show_results(res: Result) { } } -fn init_persistence(args: &Args) -> Result { - let data_dir = args.data_dir.clone().unwrap_or(".data".to_string()); - let data_dir = PathBuf::from(&data_dir); - - fs::create_dir_all(&data_dir)?; - - Ok(CliPersistence { data_dir }) -} - fn main() -> Result<()> { env_logger::init(); let args = Args::parse(); - let persistence = init_persistence(&args)?; + let data_dir_str = args.data_dir.clone().unwrap_or(".data".to_string()); + let data_dir = PathBuf::from(&data_dir_str); + fs::create_dir_all(&data_dir)?; + + let persistence = CliPersistence { data_dir }; let history_file = &persistence.history_file(); let rl = &mut Editor::new()?; @@ -50,7 +45,7 @@ fn main() -> Result<()> { } let mnemonic = persistence.get_or_create_mnemonic()?; - let wallet = Wallet::init(&mnemonic.to_string())?; + let wallet = Wallet::init(&mnemonic.to_string(), Some(data_dir_str))?; loop { let readline = rl.readline("breez-liquid> "); diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 066ee17d1..86a0db489 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -5,6 +5,11 @@ mod wallet; pub use model::*; pub use wallet::*; +// To avoid sendrawtransaction error "min relay fee not met" +const CLAIM_ABSOLUTE_FEES: u64 = 134; +const DEFAULT_DATA_DIR: &str = ".data"; +const DEFAULT_ELECTRUM_URL: &str = "blockstream.info:465"; + #[cfg(test)] mod tests { use std::{env, fs, io, path::PathBuf, str::FromStr}; @@ -12,9 +17,8 @@ mod tests { use anyhow::Result; use bip39::{Language, Mnemonic}; - use crate::{ReceivePaymentRequest, Wallet}; + use crate::{ReceivePaymentRequest, Wallet, DEFAULT_DATA_DIR}; - const DEFAULT_DATA_DIR: &str = ".data"; const PHRASE_FILE_NAME: &str = "phrase"; fn get_mnemonic() -> Result { diff --git a/lib/src/model.rs b/lib/src/model.rs index 7a6119d6a..cf49c5e49 100644 --- a/lib/src/model.rs +++ b/lib/src/model.rs @@ -23,8 +23,10 @@ pub struct WalletOptions { /// /// See pub descriptor: String, - pub db_root_path: Option, - pub chain_cache_path: Option, + /// Absolute or relative path to the data dir, including the dir name. + /// + /// If not set, it defaults to [crate::DEFAULT_DATA_DIR]. + pub data_dir_path: Option, pub electrum_url: Option, } diff --git a/lib/src/wallet.rs b/lib/src/wallet.rs index 95f5211d5..f0b69eaf8 100644 --- a/lib/src/wallet.rs +++ b/lib/src/wallet.rs @@ -26,13 +26,9 @@ use lwk_wollet::{ use crate::{ persist::Persister, Network, OngoingReceiveSwap, Payment, PaymentType, ReceivePaymentRequest, SendPaymentResponse, SwapError, SwapLbtcResponse, WalletInfo, WalletOptions, + CLAIM_ABSOLUTE_FEES, DEFAULT_DATA_DIR, DEFAULT_ELECTRUM_URL, }; -// To avoid sendrawtransaction error "min relay fee not met" -const CLAIM_ABSOLUTE_FEES: u64 = 134; -const DEFAULT_SWAPS_DIR: &str = ".data"; -const BLOCKSTREAM_ELECTRUM_URL: &str = "blockstream.info:465"; - pub struct Wallet { signer: SwSigner, electrum_url: ElectrumUrl, @@ -43,7 +39,7 @@ pub struct Wallet { } impl Wallet { - pub fn init(mnemonic: &str) -> Result> { + pub fn init(mnemonic: &str, data_dir: Option) -> Result> { let signer = SwSigner::new(mnemonic, false)?; let descriptor = singlesig_desc( &signer, @@ -57,8 +53,7 @@ impl Wallet { signer, descriptor, electrum_url: None, - db_root_path: None, - chain_cache_path: None, + data_dir_path: data_dir, network: Network::LiquidTestnet, }) } @@ -75,12 +70,12 @@ impl Wallet { let electrum_url = opts.electrum_url.unwrap_or(match network { ElementsNetwork::Liquid | ElementsNetwork::LiquidTestnet => { - ElectrumUrl::new(BLOCKSTREAM_ELECTRUM_URL, true, false) + ElectrumUrl::new(DEFAULT_ELECTRUM_URL, true, false) } ElementsNetwork::ElementsRegtest { .. } => todo!(), }); - let persister_path = opts.db_root_path.unwrap_or(DEFAULT_SWAPS_DIR.to_string()); + let persister_path = opts.data_dir_path.unwrap_or(DEFAULT_DATA_DIR.to_string()); fs::create_dir_all(&persister_path)?; let swap_persister = Persister::new(persister_path);