diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index d77bf271..28b1e671 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -13,11 +13,8 @@ #![allow(non_snake_case)] use anyhow::{Context, Result}; -use bdk::descriptor::Segwitv0; -use bdk::keys::DerivableKey; use libp2p::Swarm; use prettytable::{row, Table}; -use std::path::Path; use std::sync::Arc; use structopt::StructOpt; use swap::asb::command::{Arguments, Command}; @@ -71,8 +68,6 @@ async fn main() -> Result<()> { let db = Database::open(config.data.dir.join(db_path).as_path()) .context("Could not open database")?; - let wallet_data_dir = config.data.dir.join("wallet"); - match opt.cmd { Command::Start { max_buy } => { let seed = Seed::from_file_or_generate(&config.data.dir) @@ -80,13 +75,8 @@ async fn main() -> Result<()> { let env_config = env::Testnet::get_config(); - let (bitcoin_wallet, monero_wallet) = init_wallets( - config.clone(), - &wallet_data_dir, - seed.derive_extended_private_key(env_config.bitcoin_network)?, - env_config, - ) - .await?; + let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?; + let monero_wallet = init_monero_wallet(&config, env_config).await?; let kraken_rate_updates = kraken::connect()?; @@ -142,38 +132,47 @@ async fn main() -> Result<()> { Ok(()) } -async fn init_wallets( - config: Config, - bitcoin_wallet_data_dir: &Path, - key: impl DerivableKey + Clone, - env_config: env::Config, -) -> Result<(bitcoin::Wallet, monero::Wallet)> { - let bitcoin_wallet = bitcoin::Wallet::new( - config.bitcoin.electrum_rpc_url, - bitcoin_wallet_data_dir, - key, +async fn init_bitcoin_wallet( + config: &Config, + seed: &Seed, + env_config: swap::env::Config, +) -> Result { + let wallet_dir = config.data.dir.join("wallet"); + + let wallet = bitcoin::Wallet::new( + config.bitcoin.electrum_rpc_url.clone(), + &wallet_dir, + seed.derive_extended_private_key(env_config.bitcoin_network)?, env_config, ) - .await?; + .await + .context("Failed to initialize Bitcoin wallet")?; - bitcoin_wallet.sync().await?; + wallet.sync().await?; - let bitcoin_balance = bitcoin_wallet.balance().await?; + let balance = wallet.balance().await?; info!( "Connection to Bitcoin wallet succeeded, balance: {}", - bitcoin_balance + balance ); - let monero_wallet = monero::Wallet::open_or_create( + Ok(wallet) +} + +async fn init_monero_wallet( + config: &Config, + env_config: swap::env::Config, +) -> Result { + let wallet = monero::Wallet::open_or_create( config.monero.wallet_rpc_url.clone(), DEFAULT_WALLET_NAME.to_string(), env_config, ) .await?; - let balance = monero_wallet.get_balance().await?; + let balance = wallet.get_balance().await?; if balance == Amount::ZERO { - let deposit_address = monero_wallet.get_main_address(); + let deposit_address = wallet.get_main_address(); warn!( "The Monero balance is 0, make sure to deposit funds at: {}", deposit_address @@ -182,5 +181,5 @@ async fn init_wallets( info!("Monero balance: {}", balance); } - Ok((bitcoin_wallet, monero_wallet)) + Ok(wallet) }