Simplify arguments to init_XYZ_wallet functions

This makes the function calls fit onto one line.
This commit is contained in:
Thomas Eizinger 2021-03-04 16:54:26 +11:00
parent d0db6cba10
commit 4642e6c0e3
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96

View File

@ -14,10 +14,8 @@
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use prettytable::{row, Table}; use prettytable::{row, Table};
use reqwest::Url;
use std::cmp::min; use std::cmp::min;
use std::future::Future; use std::future::Future;
use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use structopt::StructOpt; use structopt::StructOpt;
@ -79,7 +77,6 @@ async fn main() -> Result<()> {
let db = Database::open(config.data.dir.join("database").as_path()) let db = Database::open(config.data.dir.join("database").as_path())
.context("Could not open database")?; .context("Could not open database")?;
let wallet_data_dir = config.data.dir.join("wallet");
let seed = let seed =
Seed::from_file_or_generate(&config.data.dir).expect("Could not retrieve/initialize 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 monero_network = monero::Network::Stagenet;
let execution_params = execution_params::Testnet::get_execution_params(); 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 { match args.cmd {
Command::BuyXmr { Command::BuyXmr {
receive_monero_address, receive_monero_address,
@ -108,10 +99,8 @@ async fn main() -> Result<()> {
) )
} }
let bitcoin_wallet = let bitcoin_wallet = init_bitcoin_wallet(bitcoin_network, &config, seed).await?;
init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?; let (monero_wallet, _process) = init_monero_wallet(monero_network, &config).await?;
let monero_wallet =
init_monero_wallet(monero_network, monero_wallet_rpc_process.endpoint()).await?;
let bitcoin_wallet = Arc::new(bitcoin_wallet); let bitcoin_wallet = Arc::new(bitcoin_wallet);
let (event_loop, mut event_loop_handle) = EventLoop::new( let (event_loop, mut event_loop_handle) = EventLoop::new(
&seed.derive_libp2p_identity(), &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) bail!("The given monero address is on network {:?}, expected address of network {:?}.", receive_monero_address.network, monero_network)
} }
let bitcoin_wallet = let bitcoin_wallet = init_bitcoin_wallet(bitcoin_network, &config, seed).await?;
init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?; let (monero_wallet, _process) = init_monero_wallet(monero_network, &config).await?;
let monero_wallet =
init_monero_wallet(monero_network, monero_wallet_rpc_process.endpoint()).await?;
let bitcoin_wallet = Arc::new(bitcoin_wallet); let bitcoin_wallet = Arc::new(bitcoin_wallet);
let (event_loop, event_loop_handle) = EventLoop::new( let (event_loop, event_loop_handle) = EventLoop::new(
@ -218,8 +205,7 @@ async fn main() -> Result<()> {
} }
} }
Command::Cancel { swap_id, force } => { Command::Cancel { swap_id, force } => {
let bitcoin_wallet = let bitcoin_wallet = init_bitcoin_wallet(bitcoin_network, &config, seed).await?;
init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?;
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into(); let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
let cancel = let cancel =
@ -239,8 +225,7 @@ async fn main() -> Result<()> {
} }
} }
Command::Refund { swap_id, force } => { Command::Refund { swap_id, force } => {
let bitcoin_wallet = let bitcoin_wallet = init_bitcoin_wallet(bitcoin_network, &config, seed).await?;
init_bitcoin_wallet(config, bitcoin_network, &wallet_data_dir, seed).await?;
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into(); 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( async fn init_bitcoin_wallet(
config: Config, network: bitcoin::Network,
bitcoin_network: bitcoin::Network, config: &Config,
bitcoin_wallet_data_dir: &Path,
seed: Seed, seed: Seed,
) -> Result<bitcoin::Wallet> { ) -> Result<bitcoin::Wallet> {
let bitcoin_wallet = bitcoin::Wallet::new( let wallet_dir = config.data.dir.join("wallet");
config.bitcoin.electrum_rpc_url,
config.bitcoin.electrum_http_url, let wallet = bitcoin::Wallet::new(
bitcoin_network, config.bitcoin.electrum_rpc_url.clone(),
bitcoin_wallet_data_dir, config.bitcoin.electrum_http_url.clone(),
seed.derive_extended_private_key(bitcoin_network)?, network,
&wallet_dir,
seed.derive_extended_private_key(network)?,
) )
.await?; .await?;
bitcoin_wallet wallet
.sync_wallet() .sync_wallet()
.await .await
.context("failed to sync balance of bitcoin wallet")?; .context("failed to sync balance of bitcoin wallet")?;
Ok(bitcoin_wallet) Ok(wallet)
} }
async fn init_monero_wallet( async fn init_monero_wallet(
monero_network: monero::Network, monero_network: monero::Network,
monero_wallet_rpc_url: Url, config: &Config,
) -> Result<monero::Wallet> { ) -> 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( let monero_wallet = monero::Wallet::new(
monero_wallet_rpc_url.clone(), monero_wallet_rpc_process.endpoint(),
monero_network, monero_network,
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(), MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
); );
@ -298,7 +290,7 @@ async fn init_monero_wallet(
.await .await
.context("failed to validate connection to monero-wallet-rpc")?; .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( async fn determine_btc_to_swap(