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-03-04 00:28:58 +00:00
|
|
|
use swap::bitcoin::{Amount, 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-03-04 02:43:06 +00:00
|
|
|
let send_bitcoin = determine_btc_to_swap(
|
|
|
|
event_loop_handle.request_quote(),
|
|
|
|
bitcoin_wallet.balance(),
|
|
|
|
bitcoin_wallet.new_address(),
|
|
|
|
async {
|
|
|
|
while bitcoin_wallet.balance().await? == Amount::ZERO {
|
2021-03-04 06:07:02 +00:00
|
|
|
bitcoin_wallet.sync().await?;
|
2021-02-28 23:53:43 +00:00
|
|
|
|
2021-03-04 02:43:06 +00:00
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
|
|
}
|
2021-02-28 23:53:43 +00:00
|
|
|
|
2021-03-04 02:43:06 +00:00
|
|
|
bitcoin_wallet.balance().await
|
|
|
|
},
|
|
|
|
bitcoin_wallet.max_giveable(TxLock::script_size()),
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-03 02:56:25 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
async fn determine_btc_to_swap(
|
|
|
|
request_quote: impl Future<Output = Result<BidQuote>>,
|
|
|
|
initial_balance: impl Future<Output = Result<bitcoin::Amount>>,
|
|
|
|
get_new_address: impl Future<Output = Result<bitcoin::Address>>,
|
|
|
|
wait_for_deposit: impl Future<Output = Result<bitcoin::Amount>>,
|
|
|
|
max_giveable: impl Future<Output = Result<bitcoin::Amount>>,
|
|
|
|
) -> Result<bitcoin::Amount> {
|
|
|
|
debug!("Requesting quote");
|
|
|
|
|
2021-03-18 06:48:54 +00:00
|
|
|
let bid_quote = request_quote.await?;
|
2021-03-04 02:43:06 +00:00
|
|
|
|
|
|
|
info!("Received quote: 1 XMR ~ {}", bid_quote.price);
|
|
|
|
|
|
|
|
// TODO: Also wait for more funds if balance < dust
|
|
|
|
let initial_balance = initial_balance.await?;
|
|
|
|
|
2021-03-19 06:40:14 +00:00
|
|
|
let balance = if initial_balance == Amount::ZERO {
|
2021-03-04 02:43:06 +00:00
|
|
|
info!(
|
|
|
|
"Please deposit the BTC you want to swap to {} (max {})",
|
|
|
|
get_new_address.await?,
|
|
|
|
bid_quote.max_quantity
|
|
|
|
);
|
|
|
|
|
2021-03-04 06:39:17 +00:00
|
|
|
let new_balance = wait_for_deposit
|
|
|
|
.await
|
|
|
|
.context("Failed to wait for Bitcoin deposit")?;
|
2021-03-04 02:43:06 +00:00
|
|
|
|
|
|
|
info!("Received {}", new_balance);
|
2021-03-19 06:40:14 +00:00
|
|
|
new_balance
|
2021-03-04 02:43:06 +00:00
|
|
|
} else {
|
|
|
|
info!("Found {} in wallet", initial_balance);
|
2021-03-19 06:40:14 +00:00
|
|
|
initial_balance
|
|
|
|
};
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-03-04 06:39:17 +00:00
|
|
|
let max_giveable = max_giveable
|
|
|
|
.await
|
|
|
|
.context("Failed to compute max 'giveable' Bitcoin amount")?;
|
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);
|
|
|
|
info!("Swapping {} with {} fees", btc_swap_amount, fees);
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-03-19 06:40:14 +00:00
|
|
|
Ok(btc_swap_amount)
|
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());
|
|
|
|
|
|
|
|
let amount = determine_btc_to_swap(
|
|
|
|
async { Ok(quote_with_max(0.01)) },
|
|
|
|
async { Ok(Amount::ZERO) },
|
|
|
|
get_dummy_address(),
|
|
|
|
async { Ok(Amount::from_btc(0.0001)?) },
|
|
|
|
async { Ok(Amount::from_btc(0.00009)?) },
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(amount, Amount::from_btc(0.00009).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
|
|
|
|
let amount = determine_btc_to_swap(
|
|
|
|
async { Ok(quote_with_max(0.01)) },
|
|
|
|
async { Ok(Amount::ZERO) },
|
|
|
|
get_dummy_address(),
|
|
|
|
async { Ok(Amount::from_btc(0.1)?) },
|
|
|
|
async { Ok(Amount::from_btc(0.09)?) },
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(amount, Amount::from_btc(0.01).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
|
|
|
|
let amount = determine_btc_to_swap(
|
|
|
|
async { Ok(quote_with_max(0.01)) },
|
|
|
|
async { Ok(Amount::from_btc(0.005)?) },
|
|
|
|
async { panic!("should not request new address when initial balance is > 0") },
|
|
|
|
async { panic!("should not wait for deposit when initial balance > 0") },
|
|
|
|
async { Ok(Amount::from_btc(0.0049)?) },
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(amount, Amount::from_btc(0.0049).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
|
|
|
|
let amount = determine_btc_to_swap(
|
|
|
|
async { Ok(quote_with_max(0.01)) },
|
|
|
|
async { Ok(Amount::from_btc(0.1)?) },
|
|
|
|
async { panic!("should not request new address when initial balance is > 0") },
|
|
|
|
async { panic!("should not wait for deposit when initial balance > 0") },
|
|
|
|
async { Ok(Amount::from_btc(0.09)?) },
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(amount, Amount::from_btc(0.01).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn quote_with_max(btc: f64) -> BidQuote {
|
|
|
|
BidQuote {
|
|
|
|
price: Amount::from_btc(0.001).unwrap(),
|
|
|
|
max_quantity: Amount::from_btc(btc).unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_dummy_address() -> Result<bitcoin::Address> {
|
|
|
|
Ok("1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6".parse()?)
|
|
|
|
}
|
|
|
|
}
|