2021-02-11 04:07:01 +00: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};
|
|
|
|
use prettytable::{row, Table};
|
2021-02-01 23:39:34 +00:00
|
|
|
use std::{path::Path, sync::Arc};
|
2021-02-11 04:07:01 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
use swap::{
|
2021-02-21 23:24:11 +00:00
|
|
|
asb::{
|
|
|
|
command::{Arguments, Command},
|
|
|
|
config::{
|
|
|
|
initial_setup, query_user_for_initial_testnet_config, read_config, Config,
|
|
|
|
ConfigNotInitialized,
|
|
|
|
},
|
2021-02-16 05:37:44 +00:00
|
|
|
kraken,
|
2021-02-21 23:24:11 +00:00
|
|
|
},
|
2021-02-11 04:07:01 +00:00
|
|
|
bitcoin,
|
|
|
|
database::Database,
|
|
|
|
execution_params,
|
|
|
|
execution_params::GetExecutionParams,
|
|
|
|
fs::default_config_path,
|
|
|
|
monero,
|
2021-02-24 23:34:22 +00:00
|
|
|
monero::{Amount, CreateWallet, OpenWallet},
|
2021-02-11 04:07:01 +00:00
|
|
|
protocol::alice::EventLoop,
|
|
|
|
seed::Seed,
|
|
|
|
trace::init_tracing,
|
|
|
|
};
|
|
|
|
use tracing::{info, warn};
|
2021-02-22 01:41:04 +00:00
|
|
|
use tracing_subscriber::filter::LevelFilter;
|
2021-02-11 04:07:01 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate prettytable;
|
|
|
|
|
2021-02-19 04:31:39 +00:00
|
|
|
const DEFAULT_WALLET_NAME: &str = "asb-wallet";
|
2021-02-11 04:07:01 +00:00
|
|
|
const BITCOIN_NETWORK: bitcoin::Network = bitcoin::Network::Testnet;
|
|
|
|
const MONERO_NETWORK: monero::Network = monero::Network::Stagenet;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-02-22 01:41:04 +00:00
|
|
|
init_tracing(LevelFilter::DEBUG).expect("initialize tracing");
|
2021-02-11 04:07:01 +00: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-01 23:39:34 +00:00
|
|
|
let db_path = config.data.dir.join("database");
|
|
|
|
|
|
|
|
let db = Database::open(config.data.dir.join(db_path).as_path())
|
2021-02-11 04:07:01 +00:00
|
|
|
.context("Could not open database")?;
|
|
|
|
|
2021-02-01 23:39:34 +00:00
|
|
|
let wallet_data_dir = config.data.dir.join("wallet");
|
|
|
|
|
2021-02-11 04:07:01 +00:00
|
|
|
match opt.cmd {
|
|
|
|
Command::Start => {
|
|
|
|
let seed = Seed::from_file_or_generate(&config.data.dir)
|
|
|
|
.expect("Could not retrieve/initialize seed");
|
|
|
|
|
|
|
|
let execution_params = execution_params::Testnet::get_execution_params();
|
|
|
|
|
2021-02-09 06:23:13 +00:00
|
|
|
let (bitcoin_wallet, monero_wallet) = init_wallets(
|
|
|
|
config.clone(),
|
|
|
|
&wallet_data_dir,
|
|
|
|
seed.extended_private_key(BITCOIN_NETWORK)?.private_key,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-02-11 04:07:01 +00:00
|
|
|
|
2021-02-15 09:01:57 +00:00
|
|
|
info!(
|
|
|
|
"BTC deposit address: {}",
|
|
|
|
bitcoin_wallet.new_address().await?
|
|
|
|
);
|
|
|
|
|
2021-02-16 05:37:44 +00:00
|
|
|
let rate_service = kraken::RateService::new().await?;
|
|
|
|
|
2021-02-15 04:00:47 +00:00
|
|
|
let (mut event_loop, _) = EventLoop::new(
|
2021-02-11 04:07:01 +00:00
|
|
|
config.network.listen,
|
|
|
|
seed,
|
|
|
|
execution_params,
|
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
Arc::new(monero_wallet),
|
|
|
|
Arc::new(db),
|
2021-02-16 05:37:44 +00:00
|
|
|
rate_service,
|
2021-02-11 04:07:01 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
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"]);
|
|
|
|
|
|
|
|
for (swap_id, state) in db.all()? {
|
|
|
|
table.add_row(row![swap_id, state]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the table to stdout
|
|
|
|
table.printstd();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-01 23:39:34 +00:00
|
|
|
async fn init_wallets(
|
|
|
|
config: Config,
|
|
|
|
bitcoin_wallet_data_dir: &Path,
|
2021-02-09 06:23:13 +00:00
|
|
|
private_key: ::bitcoin::PrivateKey,
|
2021-02-01 23:39:34 +00:00
|
|
|
) -> Result<(bitcoin::Wallet, monero::Wallet)> {
|
2021-02-11 04:07:01 +00:00
|
|
|
let bitcoin_wallet = bitcoin::Wallet::new(
|
2021-02-01 23:39:34 +00:00
|
|
|
config.bitcoin.electrum_rpc_url,
|
|
|
|
config.bitcoin.electrum_http_url,
|
2021-02-11 04:07:01 +00:00
|
|
|
BITCOIN_NETWORK,
|
2021-02-01 23:39:34 +00:00
|
|
|
bitcoin_wallet_data_dir,
|
2021-02-09 06:23:13 +00:00
|
|
|
private_key,
|
2021-02-11 04:07:01 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-02-16 23:56:39 +00:00
|
|
|
|
|
|
|
bitcoin_wallet
|
|
|
|
.sync_wallet()
|
|
|
|
.await
|
|
|
|
.expect("Could not sync btc wallet");
|
|
|
|
|
2021-02-11 04:07:01 +00:00
|
|
|
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
|
|
|
info!(
|
|
|
|
"Connection to Bitcoin wallet succeeded, balance: {}",
|
|
|
|
bitcoin_balance
|
|
|
|
);
|
|
|
|
|
2021-02-24 07:00:07 +00:00
|
|
|
let monero_wallet = monero::Wallet::new(
|
|
|
|
config.monero.wallet_rpc_url.clone(),
|
|
|
|
MONERO_NETWORK,
|
|
|
|
DEFAULT_WALLET_NAME.to_string(),
|
|
|
|
);
|
2021-02-11 04:07:01 +00:00
|
|
|
|
2021-02-19 04:31:39 +00:00
|
|
|
// Setup the Monero wallet
|
|
|
|
let open_wallet_response = monero_wallet.open_wallet(DEFAULT_WALLET_NAME).await;
|
|
|
|
if open_wallet_response.is_err() {
|
2021-02-11 04:07:01 +00:00
|
|
|
monero_wallet
|
2021-02-19 04:31:39 +00:00
|
|
|
.create_wallet(DEFAULT_WALLET_NAME)
|
2021-02-11 04:07:01 +00:00
|
|
|
.await
|
|
|
|
.context(format!(
|
2021-02-19 04:31:39 +00:00
|
|
|
"Unable to create Monero wallet.\
|
2021-02-11 04:07:01 +00:00
|
|
|
Please ensure that the monero-wallet-rpc is available at {}",
|
|
|
|
config.monero.wallet_rpc_url
|
|
|
|
))?;
|
|
|
|
|
2021-02-19 04:31:39 +00:00
|
|
|
info!("Created Monero wallet {}", DEFAULT_WALLET_NAME);
|
2021-02-11 04:07:01 +00:00
|
|
|
} else {
|
2021-02-19 04:31:39 +00:00
|
|
|
info!("Opened Monero wallet {}", DEFAULT_WALLET_NAME);
|
2021-02-11 04:07:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 04:31:39 +00:00
|
|
|
let balance = monero_wallet.get_balance().await?;
|
|
|
|
if balance == Amount::ZERO {
|
2021-02-24 05:34:04 +00:00
|
|
|
let deposit_address = monero_wallet.get_main_address().await?;
|
2021-02-19 04:31:39 +00:00
|
|
|
warn!(
|
|
|
|
"The Monero balance is 0, make sure to deposit funds at: {}",
|
|
|
|
deposit_address
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
info!("Monero balance: {}", balance);
|
|
|
|
}
|
2021-02-11 04:07:01 +00:00
|
|
|
|
|
|
|
Ok((bitcoin_wallet, monero_wallet))
|
|
|
|
}
|