2020-10-15 22:14:39 +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)]
|
2020-12-23 03:33:29 +00:00
|
|
|
#![allow(non_snake_case)]
|
2020-10-15 22:14:39 +00:00
|
|
|
|
2021-03-03 23:46:12 +00:00
|
|
|
use anyhow::{bail, Context, Result};
|
2021-02-04 05:42:14 +00:00
|
|
|
use prettytable::{row, Table};
|
2021-03-03 05:54:47 +00:00
|
|
|
use std::cmp::min;
|
2021-03-04 02:43:06 +00:00
|
|
|
use std::future::Future;
|
2021-03-15 01:02:42 +00:00
|
|
|
use std::path::PathBuf;
|
2021-03-04 00:28:58 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
2021-02-04 05:42:14 +00:00
|
|
|
use structopt::StructOpt;
|
2021-05-07 09:06:58 +00:00
|
|
|
use swap::bitcoin::TxLock;
|
2021-04-02 07:22:51 +00:00
|
|
|
use swap::cli::command::{Arguments, Command, MoneroParams};
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::database::Database;
|
2021-03-17 03:55:42 +00:00
|
|
|
use swap::env::{Config, GetConfig};
|
2021-03-04 02:43:06 +00:00
|
|
|
use swap::network::quote::BidQuote;
|
2021-03-23 05:56:04 +00:00
|
|
|
use swap::network::swarm;
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::protocol::bob;
|
2021-04-16 02:00:11 +00:00
|
|
|
use swap::protocol::bob::{EventLoop, Swap};
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::seed::Seed;
|
2021-04-08 03:09:52 +00:00
|
|
|
use swap::{bitcoin, cli, env, monero};
|
2021-04-02 07:22:51 +00:00
|
|
|
use tracing::{debug, error, info, warn};
|
2021-03-15 01:02:42 +00:00
|
|
|
use url::Url;
|
2021-01-21 02:43:25 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2020-12-04 05:27:17 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate prettytable;
|
2020-10-15 22:14:39 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-04-02 07:22:51 +00:00
|
|
|
let Arguments { data, debug, cmd } = Arguments::from_args();
|
2020-12-04 05:27:17 +00:00
|
|
|
|
2021-04-02 07:22:51 +00:00
|
|
|
match cmd {
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::BuyXmr {
|
2021-03-30 04:40:59 +00:00
|
|
|
alice_peer_id,
|
2021-04-06 01:05:36 +00:00
|
|
|
alice_multiaddr,
|
2021-03-05 05:10:45 +00:00
|
|
|
monero_params:
|
|
|
|
MoneroParams {
|
|
|
|
receive_monero_address,
|
|
|
|
monero_daemon_host,
|
|
|
|
},
|
2021-03-11 07:16:00 +00:00
|
|
|
electrum_rpc_url,
|
2021-04-22 01:33:36 +00:00
|
|
|
tor_socks5_port,
|
2021-05-02 23:48:40 +00:00
|
|
|
bitcoin_target_block,
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
2021-04-02 07:22:51 +00:00
|
|
|
let swap_id = Uuid::new_v4();
|
|
|
|
|
|
|
|
let data_dir = data.0;
|
2021-04-08 03:09:52 +00:00
|
|
|
cli::tracing::init(debug, data_dir.join("logs"), swap_id)?;
|
2021-04-02 07:22:51 +00:00
|
|
|
let db = Database::open(data_dir.join("database").as_path())
|
|
|
|
.context("Failed to open database")?;
|
|
|
|
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
|
|
.context("Failed to read in seed file")?;
|
|
|
|
let env_config = env::Testnet::get_config();
|
|
|
|
|
2021-03-17 03:55:42 +00:00
|
|
|
if receive_monero_address.network != env_config.monero_network {
|
2021-03-03 23:46:12 +00:00
|
|
|
bail!(
|
|
|
|
"Given monero address is on network {:?}, expected address on network {:?}",
|
|
|
|
receive_monero_address.network,
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config.monero_network
|
2021-03-03 23:46:12 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:48:40 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
electrum_rpc_url,
|
|
|
|
&seed,
|
|
|
|
data_dir.clone(),
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-17 02:36:43 +00:00
|
|
|
let (monero_wallet, _process) =
|
2021-03-17 03:55:42 +00:00
|
|
|
init_monero_wallet(data_dir, monero_daemon_host, env_config).await?;
|
2021-03-03 02:56:25 +00:00
|
|
|
let bitcoin_wallet = Arc::new(bitcoin_wallet);
|
2021-03-23 05:56:04 +00:00
|
|
|
|
2021-04-22 01:33:36 +00:00
|
|
|
let mut swarm = swarm::bob(&seed, alice_peer_id, tor_socks5_port).await?;
|
2021-04-15 07:44:21 +00:00
|
|
|
swarm
|
|
|
|
.behaviour_mut()
|
|
|
|
.add_address(alice_peer_id, alice_multiaddr);
|
2021-03-23 05:56:04 +00:00
|
|
|
|
2021-04-08 08:56:26 +00:00
|
|
|
let swap_id = Uuid::new_v4();
|
2021-03-23 05:56:04 +00:00
|
|
|
let (event_loop, mut event_loop_handle) =
|
2021-04-08 08:56:26 +00:00
|
|
|
EventLoop::new(swap_id, swarm, alice_peer_id, bitcoin_wallet.clone())?;
|
2021-03-18 06:48:54 +00:00
|
|
|
let event_loop = tokio::spawn(event_loop.run());
|
2021-03-03 05:54:47 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let max_givable = || bitcoin_wallet.max_giveable(TxLock::script_size());
|
|
|
|
let (send_bitcoin, fees) = determine_btc_to_swap(
|
2021-03-04 02:43:06 +00:00
|
|
|
event_loop_handle.request_quote(),
|
2021-05-07 09:06:58 +00:00
|
|
|
max_givable().await?,
|
2021-03-04 02:43:06 +00:00
|
|
|
bitcoin_wallet.new_address(),
|
2021-05-07 09:06:58 +00:00
|
|
|
|| bitcoin_wallet.balance(),
|
|
|
|
max_givable,
|
|
|
|
|| bitcoin_wallet.sync(),
|
2021-03-04 02:43:06 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-03-03 02:56:25 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
info!("Swapping {} with {} fees", send_bitcoin, fees);
|
|
|
|
|
2021-03-30 04:40:59 +00:00
|
|
|
db.insert_peer_id(swap_id, alice_peer_id).await?;
|
|
|
|
|
2021-04-16 02:00:11 +00:00
|
|
|
let swap = Swap::new(
|
2021-02-11 04:21:34 +00:00
|
|
|
db,
|
2021-03-30 04:40:59 +00:00
|
|
|
swap_id,
|
2021-04-16 02:00:11 +00:00
|
|
|
bitcoin_wallet,
|
2021-02-28 23:53:43 +00:00
|
|
|
Arc::new(monero_wallet),
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config,
|
2021-03-03 02:56:25 +00:00
|
|
|
event_loop_handle,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2021-04-16 02:00:11 +00:00
|
|
|
send_bitcoin,
|
|
|
|
);
|
2021-02-28 23:53:43 +00:00
|
|
|
|
2021-02-26 05:11:14 +00:00
|
|
|
tokio::select! {
|
2021-03-18 06:48:54 +00:00
|
|
|
result = event_loop => {
|
|
|
|
result
|
2021-03-18 07:00:02 +00:00
|
|
|
.context("EventLoop panicked")?;
|
2021-02-26 05:11:14 +00:00
|
|
|
},
|
2021-03-18 06:48:54 +00:00
|
|
|
result = bob::run(swap) => {
|
|
|
|
result.context("Failed to complete swap")?;
|
2021-02-26 05:11:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 05:27:17 +00:00
|
|
|
}
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::History => {
|
2021-04-02 07:22:51 +00:00
|
|
|
let data_dir = data.0;
|
|
|
|
|
|
|
|
let db = Database::open(data_dir.join("database").as_path())
|
|
|
|
.context("Failed to open database")?;
|
|
|
|
|
2020-12-04 05:27:17 +00:00
|
|
|
let mut table = Table::new();
|
|
|
|
|
|
|
|
table.add_row(row!["SWAP ID", "STATE"]);
|
|
|
|
|
2021-03-26 04:16:19 +00:00
|
|
|
for (swap_id, state) in db.all_bob()? {
|
2020-12-04 05:27:17 +00:00
|
|
|
table.add_row(row![swap_id, state]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the table to stdout
|
|
|
|
table.printstd();
|
|
|
|
}
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::Resume {
|
2020-12-21 03:43:44 +00:00
|
|
|
swap_id,
|
2021-04-06 01:05:36 +00:00
|
|
|
alice_multiaddr,
|
2021-03-05 05:10:45 +00:00
|
|
|
monero_params:
|
|
|
|
MoneroParams {
|
|
|
|
receive_monero_address,
|
|
|
|
monero_daemon_host,
|
|
|
|
},
|
2021-03-11 07:16:00 +00:00
|
|
|
electrum_rpc_url,
|
2021-04-22 01:33:36 +00:00
|
|
|
tor_socks5_port,
|
2021-05-02 23:48:40 +00:00
|
|
|
bitcoin_target_block,
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
2021-04-02 07:22:51 +00:00
|
|
|
let data_dir = data.0;
|
2021-04-08 03:09:52 +00:00
|
|
|
cli::tracing::init(debug, data_dir.join("logs"), swap_id)?;
|
2021-04-02 07:22:51 +00:00
|
|
|
let db = Database::open(data_dir.join("database").as_path())
|
|
|
|
.context("Failed to open database")?;
|
|
|
|
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
|
|
.context("Failed to read in seed file")?;
|
|
|
|
let env_config = env::Testnet::get_config();
|
|
|
|
|
2021-03-17 03:55:42 +00:00
|
|
|
if receive_monero_address.network != env_config.monero_network {
|
|
|
|
bail!("The given monero address is on network {:?}, expected address of network {:?}.", receive_monero_address.network, env_config.monero_network)
|
2021-03-03 23:46:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 23:48:40 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
electrum_rpc_url,
|
|
|
|
&seed,
|
|
|
|
data_dir.clone(),
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-17 02:36:43 +00:00
|
|
|
let (monero_wallet, _process) =
|
2021-03-17 03:55:42 +00:00
|
|
|
init_monero_wallet(data_dir, monero_daemon_host, env_config).await?;
|
2021-03-03 02:56:25 +00:00
|
|
|
let bitcoin_wallet = Arc::new(bitcoin_wallet);
|
2021-01-18 10:57:17 +00:00
|
|
|
|
2021-03-30 04:40:59 +00:00
|
|
|
let alice_peer_id = db.get_peer_id(swap_id)?;
|
2021-04-22 01:33:36 +00:00
|
|
|
|
|
|
|
let mut swarm = swarm::bob(&seed, alice_peer_id, tor_socks5_port).await?;
|
2021-04-26 01:51:03 +00:00
|
|
|
let bob_peer_id = swarm.local_peer_id();
|
|
|
|
tracing::debug!("Our peer-id: {}", bob_peer_id);
|
2021-04-15 07:44:21 +00:00
|
|
|
swarm
|
|
|
|
.behaviour_mut()
|
|
|
|
.add_address(alice_peer_id, alice_multiaddr);
|
2021-03-23 05:56:04 +00:00
|
|
|
|
|
|
|
let (event_loop, event_loop_handle) =
|
2021-04-08 08:56:26 +00:00
|
|
|
EventLoop::new(swap_id, swarm, alice_peer_id, bitcoin_wallet.clone())?;
|
2021-03-03 02:56:25 +00:00
|
|
|
let handle = tokio::spawn(event_loop.run());
|
|
|
|
|
2021-04-16 02:00:11 +00:00
|
|
|
let swap = Swap::from_db(
|
2021-02-11 04:21:34 +00:00
|
|
|
db,
|
2020-12-21 03:43:44 +00:00
|
|
|
swap_id,
|
2021-04-16 02:00:11 +00:00
|
|
|
bitcoin_wallet,
|
2021-01-20 02:36:38 +00:00
|
|
|
Arc::new(monero_wallet),
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config,
|
2021-03-03 02:56:25 +00:00
|
|
|
event_loop_handle,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2021-04-16 02:00:11 +00:00
|
|
|
)?;
|
2021-03-03 02:56:25 +00:00
|
|
|
|
2021-02-26 05:11:14 +00:00
|
|
|
tokio::select! {
|
|
|
|
event_loop_result = handle => {
|
2021-03-18 07:00:02 +00:00
|
|
|
event_loop_result?;
|
2021-02-26 05:11:14 +00:00
|
|
|
},
|
2021-03-18 06:48:54 +00:00
|
|
|
swap_result = bob::run(swap) => {
|
2021-02-26 05:11:14 +00:00
|
|
|
swap_result?;
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 10:26:02 +00:00
|
|
|
}
|
2021-03-15 01:02:42 +00:00
|
|
|
Command::Cancel {
|
|
|
|
swap_id,
|
|
|
|
force,
|
2021-03-11 07:16:00 +00:00
|
|
|
electrum_rpc_url,
|
2021-05-02 23:48:40 +00:00
|
|
|
bitcoin_target_block,
|
2021-03-15 01:02:42 +00:00
|
|
|
} => {
|
2021-04-02 07:22:51 +00:00
|
|
|
let data_dir = data.0;
|
2021-04-08 03:09:52 +00:00
|
|
|
cli::tracing::init(debug, data_dir.join("logs"), swap_id)?;
|
2021-04-02 07:22:51 +00:00
|
|
|
let db = Database::open(data_dir.join("database").as_path())
|
|
|
|
.context("Failed to open database")?;
|
|
|
|
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
|
|
.context("Failed to read in seed file")?;
|
|
|
|
let env_config = env::Testnet::get_config();
|
|
|
|
|
2021-05-02 23:48:40 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
electrum_rpc_url,
|
|
|
|
&seed,
|
|
|
|
data_dir,
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-02-01 05:10:43 +00:00
|
|
|
|
2021-04-29 01:02:26 +00:00
|
|
|
let cancel = bob::cancel(swap_id, Arc::new(bitcoin_wallet), db, force).await?;
|
2021-02-26 05:11:14 +00:00
|
|
|
|
2021-03-03 02:17:09 +00:00
|
|
|
match cancel {
|
|
|
|
Ok((txid, _)) => {
|
|
|
|
debug!("Cancel transaction successfully published with id {}", txid)
|
|
|
|
}
|
2021-03-04 06:02:03 +00:00
|
|
|
Err(bob::cancel::Error::CancelTimelockNotExpiredYet) => error!(
|
2021-03-03 02:17:09 +00:00
|
|
|
"The Cancel Transaction cannot be published yet, \
|
|
|
|
because the timelock has not expired. Please try again later."
|
|
|
|
),
|
2021-02-01 05:10:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 01:02:42 +00:00
|
|
|
Command::Refund {
|
|
|
|
swap_id,
|
|
|
|
force,
|
2021-03-11 07:16:00 +00:00
|
|
|
electrum_rpc_url,
|
2021-05-02 23:48:40 +00:00
|
|
|
bitcoin_target_block,
|
2021-03-15 01:02:42 +00:00
|
|
|
} => {
|
2021-04-02 07:22:51 +00:00
|
|
|
let data_dir = data.0;
|
2021-04-08 03:09:52 +00:00
|
|
|
cli::tracing::init(debug, data_dir.join("logs"), swap_id)?;
|
2021-04-02 07:22:51 +00:00
|
|
|
let db = Database::open(data_dir.join("database").as_path())
|
|
|
|
.context("Failed to open database")?;
|
|
|
|
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
|
|
.context("Failed to read in seed file")?;
|
|
|
|
let env_config = env::Testnet::get_config();
|
|
|
|
|
2021-05-02 23:48:40 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
electrum_rpc_url,
|
|
|
|
&seed,
|
|
|
|
data_dir,
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-02-01 05:25:33 +00:00
|
|
|
|
2021-04-29 01:02:26 +00:00
|
|
|
bob::refund(swap_id, Arc::new(bitcoin_wallet), db, force).await??;
|
2021-02-01 05:25:33 +00:00
|
|
|
}
|
2020-12-21 03:43:44 +00:00
|
|
|
};
|
2020-12-04 05:27:17 +00:00
|
|
|
Ok(())
|
2020-10-15 22:14:39 +00:00
|
|
|
}
|
2020-12-15 10:26:02 +00:00
|
|
|
|
2021-03-03 02:17:09 +00:00
|
|
|
async fn init_bitcoin_wallet(
|
2021-03-15 01:02:42 +00:00
|
|
|
electrum_rpc_url: Url,
|
2021-03-23 05:53:25 +00:00
|
|
|
seed: &Seed,
|
2021-03-15 01:02:42 +00:00
|
|
|
data_dir: PathBuf,
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config: Config,
|
2021-05-02 23:48:40 +00:00
|
|
|
bitcoin_target_block: usize,
|
2021-03-03 02:17:09 +00:00
|
|
|
) -> Result<bitcoin::Wallet> {
|
2021-03-15 01:02:42 +00:00
|
|
|
let wallet_dir = data_dir.join("wallet");
|
2021-03-04 05:54:26 +00:00
|
|
|
|
|
|
|
let wallet = bitcoin::Wallet::new(
|
2021-03-15 01:02:42 +00:00
|
|
|
electrum_rpc_url.clone(),
|
2021-03-04 05:54:26 +00:00
|
|
|
&wallet_dir,
|
2021-03-17 03:55:42 +00:00
|
|
|
seed.derive_extended_private_key(env_config.bitcoin_network)?,
|
|
|
|
env_config,
|
2021-05-02 23:48:40 +00:00
|
|
|
bitcoin_target_block,
|
2021-01-27 02:33:32 +00:00
|
|
|
)
|
2021-03-04 06:16:18 +00:00
|
|
|
.await
|
|
|
|
.context("Failed to initialize Bitcoin wallet")?;
|
2021-02-01 23:39:34 +00:00
|
|
|
|
2021-03-04 06:07:02 +00:00
|
|
|
wallet.sync().await?;
|
2020-12-15 10:26:02 +00:00
|
|
|
|
2021-03-04 05:54:26 +00:00
|
|
|
Ok(wallet)
|
2021-03-03 02:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn init_monero_wallet(
|
2021-03-15 01:02:42 +00:00
|
|
|
data_dir: PathBuf,
|
2021-03-05 05:10:45 +00:00
|
|
|
monero_daemon_host: String,
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config: Config,
|
2021-03-04 05:54:26 +00:00
|
|
|
) -> Result<(monero::Wallet, monero::WalletRpcProcess)> {
|
2021-03-17 03:55:42 +00:00
|
|
|
let network = env_config.monero_network;
|
2021-03-17 02:36:43 +00:00
|
|
|
|
2021-03-04 05:55:40 +00:00
|
|
|
const MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME: &str = "swap-tool-blockchain-monitoring-wallet";
|
|
|
|
|
2021-03-15 01:02:42 +00:00
|
|
|
let monero_wallet_rpc = monero::WalletRpc::new(data_dir.join("monero")).await?;
|
2021-03-04 05:54:26 +00:00
|
|
|
|
|
|
|
let monero_wallet_rpc_process = monero_wallet_rpc
|
2021-03-17 02:36:43 +00:00
|
|
|
.run(network, monero_daemon_host.as_str())
|
2021-03-04 05:54:26 +00:00
|
|
|
.await?;
|
|
|
|
|
2021-03-16 08:24:41 +00:00
|
|
|
let monero_wallet = monero::Wallet::open_or_create(
|
2021-03-04 05:54:26 +00:00
|
|
|
monero_wallet_rpc_process.endpoint(),
|
2021-02-24 07:00:07 +00:00
|
|
|
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config,
|
2021-03-16 08:24:41 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2020-12-15 10:26:02 +00:00
|
|
|
|
2021-03-04 05:54:26 +00:00
|
|
|
Ok((monero_wallet, monero_wallet_rpc_process))
|
2020-12-15 10:26:02 +00:00
|
|
|
}
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS>(
|
|
|
|
bid_quote: impl Future<Output = Result<BidQuote>>,
|
|
|
|
mut current_maximum_giveable: bitcoin::Amount,
|
2021-03-04 02:43:06 +00:00
|
|
|
get_new_address: impl Future<Output = Result<bitcoin::Address>>,
|
2021-05-07 09:06:58 +00:00
|
|
|
balance: FB,
|
|
|
|
max_giveable: FMG,
|
|
|
|
sync: FS,
|
|
|
|
) -> Result<(bitcoin::Amount, bitcoin::Amount)>
|
|
|
|
where
|
|
|
|
TB: Future<Output = Result<bitcoin::Amount>>,
|
|
|
|
FB: Fn() -> TB,
|
|
|
|
TMG: Future<Output = Result<bitcoin::Amount>>,
|
|
|
|
FMG: Fn() -> TMG,
|
|
|
|
TS: Future<Output = Result<()>>,
|
|
|
|
FS: Fn() -> TS,
|
|
|
|
{
|
2021-03-04 02:43:06 +00:00
|
|
|
debug!("Requesting quote");
|
2021-05-07 09:06:58 +00:00
|
|
|
let bid_quote = bid_quote.await?;
|
2021-03-04 02:43:06 +00:00
|
|
|
info!("Received quote: 1 XMR ~ {}", bid_quote.price);
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let max_giveable = if current_maximum_giveable == bitcoin::Amount::ZERO
|
|
|
|
|| current_maximum_giveable < bid_quote.min_quantity
|
|
|
|
{
|
|
|
|
let deposit_address = get_new_address.await?;
|
|
|
|
let minimum_amount = bid_quote.min_quantity;
|
|
|
|
let maximum_amount = bid_quote.max_quantity;
|
2021-03-04 02:43:06 +00:00
|
|
|
|
|
|
|
info!(
|
2021-05-07 09:06:58 +00:00
|
|
|
%deposit_address,
|
|
|
|
%current_maximum_giveable,
|
|
|
|
%minimum_amount,
|
|
|
|
%maximum_amount,
|
|
|
|
"Please deposit BTC you want to swap to",
|
2021-03-04 02:43:06 +00:00
|
|
|
);
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
loop {
|
|
|
|
sync().await?;
|
|
|
|
|
|
|
|
let new_max_givable = max_giveable().await?;
|
|
|
|
|
|
|
|
if new_max_givable != current_maximum_giveable {
|
|
|
|
current_maximum_giveable = new_max_givable;
|
|
|
|
|
|
|
|
let new_balance = balance().await?;
|
|
|
|
tracing::info!(
|
|
|
|
%new_balance,
|
|
|
|
%current_maximum_giveable,
|
|
|
|
"Received BTC",
|
|
|
|
);
|
|
|
|
|
|
|
|
if current_maximum_giveable >= bid_quote.min_quantity {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
tracing::info!(
|
|
|
|
%minimum_amount,
|
|
|
|
%deposit_address,
|
|
|
|
"Please deposit more, not enough BTC to trigger swap with",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
|
|
}
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
current_maximum_giveable
|
2021-03-04 02:43:06 +00:00
|
|
|
} else {
|
2021-05-07 09:06:58 +00:00
|
|
|
current_maximum_giveable
|
2021-03-19 06:40:14 +00:00
|
|
|
};
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let balance = balance().await?;
|
2021-03-19 06:40:14 +00:00
|
|
|
let fees = balance - max_giveable;
|
|
|
|
|
2021-03-04 02:43:06 +00:00
|
|
|
let max_accepted = bid_quote.max_quantity;
|
|
|
|
|
2021-03-19 06:40:14 +00:00
|
|
|
let btc_swap_amount = min(max_giveable, max_accepted);
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
Ok((btc_swap_amount, fees))
|
2021-03-04 02:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::determine_btc_to_swap;
|
|
|
|
use ::bitcoin::Amount;
|
|
|
|
use tracing::subscriber;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_no_balance_and_transfers_less_than_max_swaps_max_giveable() {
|
|
|
|
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-03-04 02:43:06 +00:00
|
|
|
async { Ok(quote_with_max(0.01)) },
|
2021-05-07 09:06:58 +00:00
|
|
|
Amount::ZERO,
|
2021-03-04 02:43:06 +00:00
|
|
|
get_dummy_address(),
|
2021-05-07 09:06:58 +00:00
|
|
|
|| async { Ok(Amount::from_btc(0.001)?) },
|
|
|
|
|| async { Ok(Amount::from_btc(0.0009)?) },
|
|
|
|
|| async { Ok(()) },
|
2021-03-04 02:43:06 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let expected_amount = Amount::from_btc(0.0009).unwrap();
|
|
|
|
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
|
|
|
|
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
2021-03-04 02:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_no_balance_and_transfers_more_then_swaps_max_quantity_from_quote() {
|
|
|
|
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-03-04 02:43:06 +00:00
|
|
|
async { Ok(quote_with_max(0.01)) },
|
2021-05-07 09:06:58 +00:00
|
|
|
Amount::ZERO,
|
2021-03-04 02:43:06 +00:00
|
|
|
get_dummy_address(),
|
2021-05-07 09:06:58 +00:00
|
|
|
|| async { Ok(Amount::from_btc(0.1001)?) },
|
|
|
|
|| async { Ok(Amount::from_btc(0.1)?) },
|
|
|
|
|| async { Ok(()) },
|
2021-03-04 02:43:06 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let expected_amount = Amount::from_btc(0.01).unwrap();
|
|
|
|
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
|
|
|
|
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
2021-03-04 02:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_initial_balance_below_max_quantity_swaps_max_givable() {
|
|
|
|
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-03-04 02:43:06 +00:00
|
|
|
async { Ok(quote_with_max(0.01)) },
|
2021-05-07 09:06:58 +00:00
|
|
|
Amount::from_btc(0.0049).unwrap(),
|
2021-03-04 02:43:06 +00:00
|
|
|
async { panic!("should not request new address when initial balance is > 0") },
|
2021-05-07 09:06:58 +00:00
|
|
|
|| async { Ok(Amount::from_btc(0.005)?) },
|
|
|
|
|| async { panic!("should not wait for deposit when initial balance > 0") },
|
|
|
|
|| async { Ok(()) },
|
2021-03-04 02:43:06 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let expected_amount = Amount::from_btc(0.0049).unwrap();
|
|
|
|
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
|
|
|
|
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
2021-03-04 02:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_initial_balance_above_max_quantity_swaps_max_quantity() {
|
|
|
|
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-03-04 02:43:06 +00:00
|
|
|
async { Ok(quote_with_max(0.01)) },
|
2021-05-07 09:06:58 +00:00
|
|
|
Amount::from_btc(0.1).unwrap(),
|
2021-03-04 02:43:06 +00:00
|
|
|
async { panic!("should not request new address when initial balance is > 0") },
|
2021-05-07 09:06:58 +00:00
|
|
|
|| async { Ok(Amount::from_btc(0.1001)?) },
|
|
|
|
|| async { panic!("should not wait for deposit when initial balance > 0") },
|
|
|
|
|| async { Ok(()) },
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let expected_amount = Amount::from_btc(0.01).unwrap();
|
|
|
|
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
|
|
|
|
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_no_initial_balance_then_min_wait_for_sufficient_deposit() {
|
|
|
|
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
|
|
|
|
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
|
|
|
async { Ok(quote_with_min(0.01)) },
|
|
|
|
Amount::ZERO,
|
|
|
|
get_dummy_address(),
|
|
|
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
|
|
|
|| async { Ok(Amount::from_btc(0.01)?) },
|
|
|
|
|| async { Ok(()) },
|
2021-03-04 02:43:06 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let expected_amount = Amount::from_btc(0.01).unwrap();
|
|
|
|
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
|
|
|
|
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_balance_less_then_min_wait_for_sufficient_deposit() {
|
|
|
|
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
|
|
|
|
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
|
|
|
async { Ok(quote_with_min(0.01)) },
|
|
|
|
Amount::from_btc(0.0001).unwrap(),
|
|
|
|
get_dummy_address(),
|
|
|
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
|
|
|
|| async { Ok(Amount::from_btc(0.01)?) },
|
|
|
|
|| async { Ok(()) },
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let expected_amount = Amount::from_btc(0.01).unwrap();
|
|
|
|
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
|
|
|
|
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_no_initial_balance_and_transfers_less_than_min_keep_waiting() {
|
|
|
|
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
|
|
|
|
|
|
|
let error = tokio::time::timeout(
|
|
|
|
Duration::from_secs(1),
|
|
|
|
determine_btc_to_swap(
|
|
|
|
async { Ok(quote_with_min(0.1)) },
|
|
|
|
Amount::ZERO,
|
|
|
|
get_dummy_address(),
|
|
|
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
|
|
|
|| async { Ok(Amount::from_btc(0.01)?) },
|
|
|
|
|| async { Ok(()) },
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap_err();
|
|
|
|
|
|
|
|
assert!(matches!(error, tokio::time::error::Elapsed { .. }))
|
2021-03-04 02:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn quote_with_max(btc: f64) -> BidQuote {
|
|
|
|
BidQuote {
|
|
|
|
price: Amount::from_btc(0.001).unwrap(),
|
|
|
|
max_quantity: Amount::from_btc(btc).unwrap(),
|
2021-05-07 09:06:58 +00:00
|
|
|
min_quantity: Amount::ZERO,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn quote_with_min(btc: f64) -> BidQuote {
|
|
|
|
BidQuote {
|
|
|
|
price: Amount::from_btc(0.001).unwrap(),
|
|
|
|
max_quantity: Amount::max_value(),
|
|
|
|
min_quantity: Amount::from_btc(btc).unwrap(),
|
2021-03-04 02:43:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_dummy_address() -> Result<bitcoin::Address> {
|
|
|
|
Ok("1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6".parse()?)
|
|
|
|
}
|
|
|
|
}
|