mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Separate bitcoin and monero wallet initialization
This commit is contained in:
parent
d144405182
commit
d9d697821e
@ -13,11 +13,8 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use bdk::descriptor::Segwitv0;
|
|
||||||
use bdk::keys::DerivableKey;
|
|
||||||
use libp2p::Swarm;
|
use libp2p::Swarm;
|
||||||
use prettytable::{row, Table};
|
use prettytable::{row, Table};
|
||||||
use std::path::Path;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use swap::asb::command::{Arguments, Command};
|
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())
|
let db = Database::open(config.data.dir.join(db_path).as_path())
|
||||||
.context("Could not open database")?;
|
.context("Could not open database")?;
|
||||||
|
|
||||||
let wallet_data_dir = config.data.dir.join("wallet");
|
|
||||||
|
|
||||||
match opt.cmd {
|
match opt.cmd {
|
||||||
Command::Start { max_buy } => {
|
Command::Start { max_buy } => {
|
||||||
let seed = Seed::from_file_or_generate(&config.data.dir)
|
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 env_config = env::Testnet::get_config();
|
||||||
|
|
||||||
let (bitcoin_wallet, monero_wallet) = init_wallets(
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
||||||
config.clone(),
|
let monero_wallet = init_monero_wallet(&config, env_config).await?;
|
||||||
&wallet_data_dir,
|
|
||||||
seed.derive_extended_private_key(env_config.bitcoin_network)?,
|
|
||||||
env_config,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let kraken_rate_updates = kraken::connect()?;
|
let kraken_rate_updates = kraken::connect()?;
|
||||||
|
|
||||||
@ -142,38 +132,47 @@ async fn main() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn init_wallets(
|
async fn init_bitcoin_wallet(
|
||||||
config: Config,
|
config: &Config,
|
||||||
bitcoin_wallet_data_dir: &Path,
|
seed: &Seed,
|
||||||
key: impl DerivableKey<Segwitv0> + Clone,
|
env_config: swap::env::Config,
|
||||||
env_config: env::Config,
|
) -> Result<bitcoin::Wallet> {
|
||||||
) -> Result<(bitcoin::Wallet, monero::Wallet)> {
|
let wallet_dir = config.data.dir.join("wallet");
|
||||||
let bitcoin_wallet = bitcoin::Wallet::new(
|
|
||||||
config.bitcoin.electrum_rpc_url,
|
let wallet = bitcoin::Wallet::new(
|
||||||
bitcoin_wallet_data_dir,
|
config.bitcoin.electrum_rpc_url.clone(),
|
||||||
key,
|
&wallet_dir,
|
||||||
|
seed.derive_extended_private_key(env_config.bitcoin_network)?,
|
||||||
env_config,
|
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!(
|
info!(
|
||||||
"Connection to Bitcoin wallet succeeded, balance: {}",
|
"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<monero::Wallet> {
|
||||||
|
let wallet = monero::Wallet::open_or_create(
|
||||||
config.monero.wallet_rpc_url.clone(),
|
config.monero.wallet_rpc_url.clone(),
|
||||||
DEFAULT_WALLET_NAME.to_string(),
|
DEFAULT_WALLET_NAME.to_string(),
|
||||||
env_config,
|
env_config,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let balance = monero_wallet.get_balance().await?;
|
let balance = wallet.get_balance().await?;
|
||||||
if balance == Amount::ZERO {
|
if balance == Amount::ZERO {
|
||||||
let deposit_address = monero_wallet.get_main_address();
|
let deposit_address = wallet.get_main_address();
|
||||||
warn!(
|
warn!(
|
||||||
"The Monero balance is 0, make sure to deposit funds at: {}",
|
"The Monero balance is 0, make sure to deposit funds at: {}",
|
||||||
deposit_address
|
deposit_address
|
||||||
@ -182,5 +181,5 @@ async fn init_wallets(
|
|||||||
info!("Monero balance: {}", balance);
|
info!("Monero balance: {}", balance);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((bitcoin_wallet, monero_wallet))
|
Ok(wallet)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user