From 4642e6c0e3f3c376df9baec277e122c5813c9dee Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 4 Mar 2021 16:54:26 +1100 Subject: [PATCH] Simplify arguments to `init_XYZ_wallet` functions This makes the function calls fit onto one line. --- swap/src/bin/swap_cli.rs | 64 ++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/swap/src/bin/swap_cli.rs b/swap/src/bin/swap_cli.rs index dd9d88b7..404c82a6 100644 --- a/swap/src/bin/swap_cli.rs +++ b/swap/src/bin/swap_cli.rs @@ -14,10 +14,8 @@ use anyhow::{bail, Context, Result}; use prettytable::{row, Table}; -use reqwest::Url; use std::cmp::min; use std::future::Future; -use std::path::Path; use std::sync::Arc; use std::time::Duration; use structopt::StructOpt; @@ -79,7 +77,6 @@ async fn main() -> Result<()> { let db = Database::open(config.data.dir.join("database").as_path()) .context("Could not open database")?; - let wallet_data_dir = config.data.dir.join("wallet"); let seed = Seed::from_file_or_generate(&config.data.dir).expect("Could not retrieve/initialize seed"); @@ -88,12 +85,6 @@ async fn main() -> Result<()> { let monero_network = monero::Network::Stagenet; let execution_params = execution_params::Testnet::get_execution_params(); - let monero_wallet_rpc = monero::WalletRpc::new(config.data.dir.join("monero")).await?; - - let monero_wallet_rpc_process = monero_wallet_rpc - .run(monero_network, "monero-stagenet.exan.tech") - .await?; - match args.cmd { Command::BuyXmr { receive_monero_address, @@ -108,10 +99,8 @@ async fn main() -> Result<()> { ) } - let bitcoin_wallet = - init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?; - let monero_wallet = - init_monero_wallet(monero_network, monero_wallet_rpc_process.endpoint()).await?; + let bitcoin_wallet = init_bitcoin_wallet(bitcoin_network, &config, seed).await?; + let (monero_wallet, _process) = init_monero_wallet(monero_network, &config).await?; let bitcoin_wallet = Arc::new(bitcoin_wallet); let (event_loop, mut event_loop_handle) = EventLoop::new( &seed.derive_libp2p_identity(), @@ -182,10 +171,8 @@ async fn main() -> Result<()> { bail!("The given monero address is on network {:?}, expected address of network {:?}.", receive_monero_address.network, monero_network) } - let bitcoin_wallet = - init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?; - let monero_wallet = - init_monero_wallet(monero_network, monero_wallet_rpc_process.endpoint()).await?; + let bitcoin_wallet = init_bitcoin_wallet(bitcoin_network, &config, seed).await?; + let (monero_wallet, _process) = init_monero_wallet(monero_network, &config).await?; let bitcoin_wallet = Arc::new(bitcoin_wallet); let (event_loop, event_loop_handle) = EventLoop::new( @@ -218,8 +205,7 @@ async fn main() -> Result<()> { } } Command::Cancel { swap_id, force } => { - let bitcoin_wallet = - init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?; + let bitcoin_wallet = init_bitcoin_wallet(bitcoin_network, &config, seed).await?; let resume_state = db.get_state(swap_id)?.try_into_bob()?.into(); let cancel = @@ -239,8 +225,7 @@ async fn main() -> Result<()> { } } Command::Refund { swap_id, force } => { - let bitcoin_wallet = - init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?; + let bitcoin_wallet = init_bitcoin_wallet(bitcoin_network, &config, seed).await?; let resume_state = db.get_state(swap_id)?.try_into_bob()?.into(); @@ -259,34 +244,41 @@ async fn main() -> Result<()> { } async fn init_bitcoin_wallet( - config: Config, - bitcoin_network: bitcoin::Network, - bitcoin_wallet_data_dir: &Path, + network: bitcoin::Network, + config: &Config, seed: Seed, ) -> Result { - let bitcoin_wallet = bitcoin::Wallet::new( - config.bitcoin.electrum_rpc_url, - config.bitcoin.electrum_http_url, - bitcoin_network, - bitcoin_wallet_data_dir, - seed.derive_extended_private_key(bitcoin_network)?, + let wallet_dir = config.data.dir.join("wallet"); + + let wallet = bitcoin::Wallet::new( + config.bitcoin.electrum_rpc_url.clone(), + config.bitcoin.electrum_http_url.clone(), + network, + &wallet_dir, + seed.derive_extended_private_key(network)?, ) .await?; - bitcoin_wallet + wallet .sync_wallet() .await .context("failed to sync balance of bitcoin wallet")?; - Ok(bitcoin_wallet) + Ok(wallet) } async fn init_monero_wallet( monero_network: monero::Network, - monero_wallet_rpc_url: Url, -) -> Result { + config: &Config, +) -> Result<(monero::Wallet, monero::WalletRpcProcess)> { + let monero_wallet_rpc = monero::WalletRpc::new(config.data.dir.join("monero")).await?; + + let monero_wallet_rpc_process = monero_wallet_rpc + .run(monero_network, "monero-stagenet.exan.tech") + .await?; + let monero_wallet = monero::Wallet::new( - monero_wallet_rpc_url.clone(), + monero_wallet_rpc_process.endpoint(), monero_network, MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(), ); @@ -298,7 +290,7 @@ async fn init_monero_wallet( .await .context("failed to validate connection to monero-wallet-rpc")?; - Ok(monero_wallet) + Ok((monero_wallet, monero_wallet_rpc_process)) } async fn determine_btc_to_swap(