Skip to content

Commit

Permalink
Consolidate constants and default init values (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 authored Mar 26, 2024
1 parent baabf39 commit e6a8a84
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
17 changes: 6 additions & 11 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ fn show_results(res: Result<String>) {
}
}

fn init_persistence(args: &Args) -> Result<CliPersistence> {
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()?;
Expand All @@ -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> ");
Expand Down
8 changes: 6 additions & 2 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ 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};

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<Mnemonic> {
Expand Down
6 changes: 4 additions & 2 deletions lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ pub struct WalletOptions {
///
/// See <https://github.com/bitcoin/bips/pull/1143>
pub descriptor: String,
pub db_root_path: Option<String>,
pub chain_cache_path: Option<String>,
/// 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<String>,
pub electrum_url: Option<ElectrumUrl>,
}

Expand Down
15 changes: 5 additions & 10 deletions lib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -43,7 +39,7 @@ pub struct Wallet {
}

impl Wallet {
pub fn init(mnemonic: &str) -> Result<Arc<Wallet>> {
pub fn init(mnemonic: &str, data_dir: Option<String>) -> Result<Arc<Wallet>> {
let signer = SwSigner::new(mnemonic, false)?;
let descriptor = singlesig_desc(
&signer,
Expand All @@ -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,
})
}
Expand All @@ -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);
Expand Down

0 comments on commit e6a8a84

Please sign in to comment.