Skip to content

Commit

Permalink
Make network a mandatory arg of init()
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Mar 26, 2024
1 parent e6a8a84 commit 018219c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
8 changes: 6 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod persist;
use std::{fs, path::PathBuf};

use anyhow::{anyhow, Result};
use breez_sdk_liquid::Wallet;
use breez_sdk_liquid::{Network, Wallet};
use clap::Parser;
use commands::{handle_command, CliHelper, Command};
use log::{error, info};
Expand Down Expand Up @@ -45,7 +45,11 @@ fn main() -> Result<()> {
}

let mnemonic = persistence.get_or_create_mnemonic()?;
let wallet = Wallet::init(&mnemonic.to_string(), Some(data_dir_str))?;
let wallet = Wallet::init(
&mnemonic.to_string(),
Some(data_dir_str),
Network::LiquidTestnet,
)?;

loop {
let readline = rl.readline("breez-liquid> ");
Expand Down
1 change: 1 addition & 0 deletions lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use boltz_client::util::error::S5Error;
use lwk_signer::SwSigner;
use lwk_wollet::{ElectrumUrl, ElementsNetwork};

#[derive(Copy, Clone, PartialEq)]
pub enum Network {
Liquid,
LiquidTestnet,
Expand Down
35 changes: 15 additions & 20 deletions lib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ use crate::{
pub struct Wallet {
signer: SwSigner,
electrum_url: ElectrumUrl,
network: ElementsNetwork,
network: Network,
wallet: Arc<Mutex<LwkWollet>>,
active_address: Option<u32>,
swap_persister: Persister,
}

impl Wallet {
pub fn init(mnemonic: &str, data_dir: Option<String>) -> Result<Arc<Wallet>> {
let signer = SwSigner::new(mnemonic, false)?;
pub fn init(mnemonic: &str, data_dir: Option<String>, network: Network) -> Result<Arc<Wallet>> {
let signer = SwSigner::new(mnemonic, network == Network::Liquid)?;
let descriptor = singlesig_desc(
&signer,
Singlesig::Wpkh,
Expand All @@ -54,27 +54,24 @@ impl Wallet {
descriptor,
electrum_url: None,
data_dir_path: data_dir,
network: Network::LiquidTestnet,
network,
})
}

fn new(opts: WalletOptions) -> Result<Arc<Self>> {
let network: ElementsNetwork = opts.network.into();
let network = opts.network;
let elements_network: ElementsNetwork = opts.network.into();

let lwk_persister = NoPersist::new();
let wallet = Arc::new(Mutex::new(LwkWollet::new(
network,
elements_network,
lwk_persister,
&opts.descriptor,
)?));

let electrum_url = opts.electrum_url.unwrap_or(match network {
ElementsNetwork::Liquid | ElementsNetwork::LiquidTestnet => {
ElectrumUrl::new(DEFAULT_ELECTRUM_URL, true, false)
}
ElementsNetwork::ElementsRegtest { .. } => todo!(),
});

let electrum_url =
opts.electrum_url
.unwrap_or(ElectrumUrl::new(DEFAULT_ELECTRUM_URL, true, false));
let persister_path = opts.data_dir_path.unwrap_or(DEFAULT_DATA_DIR.to_string());
fs::create_dir_all(&persister_path)?;

Expand Down Expand Up @@ -113,7 +110,7 @@ impl Wallet {
} = swap;
match cloned.try_claim(&preimage, &redeem_script, &blinding_key, None) {
Ok(_) => cloned.swap_persister.resolve_ongoing_swap(swap.id).unwrap(),
Err(e) => warn!("Could not claim yet. Err: {}", e),
Err(e) => warn!("Could not claim yet. Err: {e}"),
}
});
}
Expand Down Expand Up @@ -156,19 +153,17 @@ impl Wallet {

fn boltz_client(&self) -> BoltzApiClient {
let base_url = match self.network {
ElementsNetwork::LiquidTestnet => BOLTZ_TESTNET_URL,
ElementsNetwork::Liquid => BOLTZ_MAINNET_URL,
ElementsNetwork::ElementsRegtest { .. } => todo!(),
Network::LiquidTestnet => BOLTZ_TESTNET_URL,
Network::Liquid => BOLTZ_MAINNET_URL,
};

BoltzApiClient::new(base_url)
}

fn get_chain(&self) -> Chain {
match self.network {
ElementsNetwork::Liquid => Chain::Liquid,
ElementsNetwork::LiquidTestnet => Chain::LiquidTestnet,
ElementsNetwork::ElementsRegtest { .. } => todo!(),
Network::Liquid => Chain::Liquid,
Network::LiquidTestnet => Chain::LiquidTestnet,
}
}

Expand Down

0 comments on commit 018219c

Please sign in to comment.