2021-02-11 15:07:01 +11:00
|
|
|
#![warn(
|
|
|
|
unused_extern_crates,
|
|
|
|
missing_copy_implementations,
|
|
|
|
rust_2018_idioms,
|
|
|
|
clippy::cast_possible_truncation,
|
|
|
|
clippy::cast_sign_loss,
|
|
|
|
clippy::fallible_impl_from,
|
|
|
|
clippy::cast_precision_loss,
|
|
|
|
clippy::cast_possible_wrap,
|
|
|
|
clippy::dbg_macro
|
|
|
|
)]
|
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
2021-03-23 16:56:04 +11:00
|
|
|
use libp2p::Swarm;
|
2021-02-11 15:07:01 +11:00
|
|
|
use prettytable::{row, Table};
|
2021-03-04 11:28:58 +11:00
|
|
|
use std::sync::Arc;
|
2021-02-11 15:07:01 +11:00
|
|
|
use structopt::StructOpt;
|
2021-03-04 11:28:58 +11:00
|
|
|
use swap::asb::command::{Arguments, Command};
|
|
|
|
use swap::asb::config::{
|
|
|
|
initial_setup, query_user_for_initial_testnet_config, read_config, Config, ConfigNotInitialized,
|
2021-02-11 15:07:01 +11:00
|
|
|
};
|
2021-03-04 11:28:58 +11:00
|
|
|
use swap::database::Database;
|
2021-03-17 14:55:42 +11:00
|
|
|
use swap::env::GetConfig;
|
2021-03-04 11:28:58 +11:00
|
|
|
use swap::fs::default_config_path;
|
|
|
|
use swap::monero::Amount;
|
2021-03-23 16:56:04 +11:00
|
|
|
use swap::network::swarm;
|
|
|
|
use swap::protocol::alice::{run, Behaviour, EventLoop};
|
2021-03-04 11:28:58 +11:00
|
|
|
use swap::seed::Seed;
|
|
|
|
use swap::trace::init_tracing;
|
2021-03-17 14:55:42 +11:00
|
|
|
use swap::{bitcoin, env, kraken, monero};
|
2021-02-11 15:07:01 +11:00
|
|
|
use tracing::{info, warn};
|
2021-02-22 12:41:04 +11:00
|
|
|
use tracing_subscriber::filter::LevelFilter;
|
2021-02-11 15:07:01 +11:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate prettytable;
|
|
|
|
|
2021-02-19 15:31:39 +11:00
|
|
|
const DEFAULT_WALLET_NAME: &str = "asb-wallet";
|
2021-02-11 15:07:01 +11:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-02-22 12:41:04 +11:00
|
|
|
init_tracing(LevelFilter::DEBUG).expect("initialize tracing");
|
2021-02-11 15:07:01 +11:00
|
|
|
|
|
|
|
let opt = Arguments::from_args();
|
|
|
|
|
|
|
|
let config_path = if let Some(config_path) = opt.config {
|
|
|
|
config_path
|
|
|
|
} else {
|
|
|
|
default_config_path()?
|
|
|
|
};
|
|
|
|
|
|
|
|
let config = match read_config(config_path.clone())? {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(ConfigNotInitialized {}) => {
|
|
|
|
initial_setup(config_path.clone(), query_user_for_initial_testnet_config)?;
|
|
|
|
read_config(config_path)?.expect("after initial setup config can be read")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
info!(
|
|
|
|
"Database and Seed will be stored in directory: {}",
|
|
|
|
config.data.dir.display()
|
|
|
|
);
|
|
|
|
|
2021-02-02 10:39:34 +11:00
|
|
|
let db_path = config.data.dir.join("database");
|
|
|
|
|
|
|
|
let db = Database::open(config.data.dir.join(db_path).as_path())
|
2021-02-11 15:07:01 +11:00
|
|
|
.context("Could not open database")?;
|
|
|
|
|
|
|
|
match opt.cmd {
|
2021-03-02 16:22:39 +11:00
|
|
|
Command::Start { max_buy } => {
|
2021-02-11 15:07:01 +11:00
|
|
|
let seed = Seed::from_file_or_generate(&config.data.dir)
|
|
|
|
.expect("Could not retrieve/initialize seed");
|
|
|
|
|
2021-03-17 14:55:42 +11:00
|
|
|
let env_config = env::Testnet::get_config();
|
2021-02-11 15:07:01 +11:00
|
|
|
|
2021-03-31 16:38:54 +11:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
|
|
|
|
let monero_wallet = init_monero_wallet(&config, env_config).await?;
|
2021-02-11 15:07:01 +11:00
|
|
|
|
2021-03-09 15:35:22 +11:00
|
|
|
let kraken_rate_updates = kraken::connect()?;
|
2021-02-16 16:37:44 +11:00
|
|
|
|
2021-03-23 16:56:04 +11:00
|
|
|
let mut swarm = swarm::new::<Behaviour>(&seed)?;
|
|
|
|
Swarm::listen_on(&mut swarm, config.network.listen)
|
|
|
|
.context("Failed to listen network interface")?;
|
|
|
|
|
2021-03-16 17:08:19 +11:00
|
|
|
let (event_loop, mut swap_receiver) = EventLoop::new(
|
2021-03-23 16:56:04 +11:00
|
|
|
swarm,
|
2021-03-17 14:55:42 +11:00
|
|
|
env_config,
|
2021-02-11 15:07:01 +11:00
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
|
|
|
Arc::new(db),
|
2021-03-05 13:52:24 +11:00
|
|
|
kraken_rate_updates,
|
2021-03-02 16:22:39 +11:00
|
|
|
max_buy,
|
2021-02-11 15:07:01 +11:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-03-16 17:08:19 +11:00
|
|
|
tokio::spawn(async move {
|
|
|
|
while let Some(swap) = swap_receiver.recv().await {
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let swap_id = swap.swap_id;
|
|
|
|
match run(swap).await {
|
|
|
|
Ok(state) => {
|
|
|
|
tracing::debug!(%swap_id, "Swap finished with state {}", state)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!(%swap_id, "Swap failed with {:#}", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-02-11 15:07:01 +11:00
|
|
|
info!("Our peer id is {}", event_loop.peer_id());
|
|
|
|
|
|
|
|
event_loop.run().await;
|
|
|
|
}
|
|
|
|
Command::History => {
|
|
|
|
let mut table = Table::new();
|
|
|
|
|
|
|
|
table.add_row(row!["SWAP ID", "STATE"]);
|
|
|
|
|
2021-03-26 15:16:19 +11:00
|
|
|
for (swap_id, state) in db.all_alice()? {
|
2021-02-11 15:07:01 +11:00
|
|
|
table.add_row(row![swap_id, state]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the table to stdout
|
|
|
|
table.printstd();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:38:54 +11:00
|
|
|
async fn init_bitcoin_wallet(
|
|
|
|
config: &Config,
|
|
|
|
seed: &Seed,
|
|
|
|
env_config: swap::env::Config,
|
|
|
|
) -> Result<bitcoin::Wallet> {
|
|
|
|
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)?,
|
2021-03-17 14:55:42 +11:00
|
|
|
env_config,
|
2021-02-11 15:07:01 +11:00
|
|
|
)
|
2021-03-31 16:38:54 +11:00
|
|
|
.await
|
|
|
|
.context("Failed to initialize Bitcoin wallet")?;
|
2021-02-17 10:56:39 +11:00
|
|
|
|
2021-03-31 16:38:54 +11:00
|
|
|
wallet.sync().await?;
|
2021-02-17 10:56:39 +11:00
|
|
|
|
2021-03-31 16:38:54 +11:00
|
|
|
let balance = wallet.balance().await?;
|
2021-02-11 15:07:01 +11:00
|
|
|
info!(
|
|
|
|
"Connection to Bitcoin wallet succeeded, balance: {}",
|
2021-03-31 16:38:54 +11:00
|
|
|
balance
|
2021-02-11 15:07:01 +11:00
|
|
|
);
|
|
|
|
|
2021-03-31 16:38:54 +11:00
|
|
|
Ok(wallet)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn init_monero_wallet(
|
|
|
|
config: &Config,
|
|
|
|
env_config: swap::env::Config,
|
|
|
|
) -> Result<monero::Wallet> {
|
|
|
|
let wallet = monero::Wallet::open_or_create(
|
2021-02-24 18:00:07 +11:00
|
|
|
config.monero.wallet_rpc_url.clone(),
|
|
|
|
DEFAULT_WALLET_NAME.to_string(),
|
2021-03-17 14:55:42 +11:00
|
|
|
env_config,
|
2021-03-16 19:24:41 +11:00
|
|
|
)
|
|
|
|
.await?;
|
2021-02-11 15:07:01 +11:00
|
|
|
|
2021-03-31 16:38:54 +11:00
|
|
|
let balance = wallet.get_balance().await?;
|
2021-02-19 15:31:39 +11:00
|
|
|
if balance == Amount::ZERO {
|
2021-03-31 16:38:54 +11:00
|
|
|
let deposit_address = wallet.get_main_address();
|
2021-02-19 15:31:39 +11:00
|
|
|
warn!(
|
|
|
|
"The Monero balance is 0, make sure to deposit funds at: {}",
|
|
|
|
deposit_address
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
info!("Monero balance: {}", balance);
|
|
|
|
}
|
2021-02-11 15:07:01 +11:00
|
|
|
|
2021-03-31 16:38:54 +11:00
|
|
|
Ok(wallet)
|
2021-02-11 15:07:01 +11:00
|
|
|
}
|