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-03-15 01:02:42 +00:00
|
|
|
use swap::cli::command::{
|
|
|
|
AliceConnectParams, Arguments, BitcoinParams, Command, Data, MoneroParams,
|
|
|
|
};
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::database::Database;
|
2021-03-11 05:06:28 +00:00
|
|
|
use swap::execution_params::{ExecutionParams, GetExecutionParams};
|
2021-03-04 02:43:06 +00:00
|
|
|
use swap::network::quote::BidQuote;
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::protocol::bob;
|
|
|
|
use swap::protocol::bob::{Builder, EventLoop};
|
|
|
|
use swap::seed::Seed;
|
|
|
|
use swap::{bitcoin, execution_params, monero};
|
2021-03-01 01:19:05 +00:00
|
|
|
use tracing::{debug, error, info, warn, Level};
|
2021-02-26 05:22:24 +00:00
|
|
|
use tracing_subscriber::FmtSubscriber;
|
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-03-01 01:19:05 +00:00
|
|
|
let args = Arguments::from_args();
|
|
|
|
|
2021-02-26 05:22:24 +00:00
|
|
|
let is_terminal = atty::is(atty::Stream::Stderr);
|
2021-03-01 01:19:05 +00:00
|
|
|
let base_subscriber = |level| {
|
|
|
|
FmtSubscriber::builder()
|
|
|
|
.with_writer(std::io::stderr)
|
|
|
|
.with_ansi(is_terminal)
|
|
|
|
.with_target(false)
|
|
|
|
.with_env_filter(format!("swap={}", level))
|
|
|
|
};
|
2020-12-04 05:27:17 +00:00
|
|
|
|
2021-03-01 01:19:05 +00:00
|
|
|
if args.debug {
|
|
|
|
let subscriber = base_subscriber(Level::DEBUG)
|
|
|
|
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::with_format(
|
|
|
|
"%F %T".to_owned(),
|
|
|
|
))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
tracing::subscriber::set_global_default(subscriber)?;
|
|
|
|
} else {
|
|
|
|
let subscriber = base_subscriber(Level::INFO)
|
|
|
|
.without_time()
|
|
|
|
.with_level(false)
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
tracing::subscriber::set_global_default(subscriber)?;
|
|
|
|
}
|
2021-01-27 02:33:32 +00:00
|
|
|
|
2021-03-15 01:02:42 +00:00
|
|
|
let data: Data = args.data;
|
|
|
|
let data_dir = data.0;
|
2020-12-04 05:27:17 +00:00
|
|
|
|
2021-03-15 01:02:42 +00:00
|
|
|
let db =
|
|
|
|
Database::open(data_dir.join("database").as_path()).context("Failed to open database")?;
|
2021-02-11 04:21:34 +00:00
|
|
|
|
|
|
|
let seed =
|
2021-03-15 01:02:42 +00:00
|
|
|
Seed::from_file_or_generate(data_dir.as_path()).context("Failed to read in seed file")?;
|
2020-12-15 10:26:02 +00:00
|
|
|
|
2021-01-27 02:33:32 +00:00
|
|
|
// hardcode to testnet/stagenet
|
|
|
|
let bitcoin_network = bitcoin::Network::Testnet;
|
|
|
|
let monero_network = monero::Network::Stagenet;
|
2021-01-29 06:27:50 +00:00
|
|
|
let execution_params = execution_params::Testnet::get_execution_params();
|
2021-01-27 02:33:32 +00:00
|
|
|
|
2021-03-02 11:07:48 +00:00
|
|
|
match args.cmd {
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::BuyXmr {
|
2021-03-05 05:00:52 +00:00
|
|
|
connect_params:
|
2021-03-05 05:16:18 +00:00
|
|
|
AliceConnectParams {
|
|
|
|
peer_id: alice_peer_id,
|
|
|
|
multiaddr: alice_addr,
|
2021-03-05 05:00:52 +00:00
|
|
|
},
|
2021-03-05 05:10:45 +00:00
|
|
|
monero_params:
|
|
|
|
MoneroParams {
|
|
|
|
receive_monero_address,
|
|
|
|
monero_daemon_host,
|
|
|
|
},
|
2021-03-15 01:02:42 +00:00
|
|
|
bitcoin_params:
|
|
|
|
BitcoinParams {
|
|
|
|
electrum_http_url,
|
|
|
|
electrum_rpc_url,
|
|
|
|
},
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
2021-03-03 23:46:12 +00:00
|
|
|
if receive_monero_address.network != monero_network {
|
|
|
|
bail!(
|
|
|
|
"Given monero address is on network {:?}, expected address on network {:?}",
|
|
|
|
receive_monero_address.network,
|
|
|
|
monero_network
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-15 01:02:42 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
bitcoin_network,
|
|
|
|
electrum_rpc_url,
|
|
|
|
electrum_http_url,
|
|
|
|
seed,
|
|
|
|
data_dir.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-11 05:06:28 +00:00
|
|
|
let (monero_wallet, _process) = init_monero_wallet(
|
|
|
|
monero_network,
|
2021-03-15 01:02:42 +00:00
|
|
|
data_dir,
|
2021-03-11 05:06:28 +00:00
|
|
|
monero_daemon_host,
|
|
|
|
execution_params,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-03 02:56:25 +00:00
|
|
|
let bitcoin_wallet = Arc::new(bitcoin_wallet);
|
2021-03-03 05:54:47 +00:00
|
|
|
let (event_loop, mut event_loop_handle) = EventLoop::new(
|
|
|
|
&seed.derive_libp2p_identity(),
|
|
|
|
alice_peer_id,
|
|
|
|
alice_addr,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
)?;
|
|
|
|
let handle = tokio::spawn(event_loop.run());
|
|
|
|
|
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
|
|
|
|
|
|
|
let swap = Builder::new(
|
2021-02-11 04:21:34 +00:00
|
|
|
db,
|
2021-03-03 05:54:47 +00:00
|
|
|
Uuid::new_v4(),
|
2021-03-03 02:56:25 +00:00
|
|
|
bitcoin_wallet.clone(),
|
2021-02-28 23:53:43 +00:00
|
|
|
Arc::new(monero_wallet),
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-03 02:56:25 +00:00
|
|
|
event_loop_handle,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2021-03-03 02:56:25 +00:00
|
|
|
)
|
|
|
|
.with_init_params(send_bitcoin)
|
|
|
|
.build()?;
|
2021-02-28 23:53:43 +00:00
|
|
|
|
2021-02-26 05:11:14 +00:00
|
|
|
let swap = bob::run(swap);
|
|
|
|
tokio::select! {
|
|
|
|
event_loop_result = handle => {
|
|
|
|
event_loop_result??;
|
|
|
|
},
|
|
|
|
swap_result = swap => {
|
|
|
|
swap_result?;
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 05:27:17 +00:00
|
|
|
}
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::History => {
|
2020-12-04 05:27:17 +00:00
|
|
|
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();
|
|
|
|
}
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::Resume {
|
2020-12-21 03:43:44 +00:00
|
|
|
swap_id,
|
2021-03-05 05:00:52 +00:00
|
|
|
connect_params:
|
2021-03-05 05:16:18 +00:00
|
|
|
AliceConnectParams {
|
|
|
|
peer_id: alice_peer_id,
|
|
|
|
multiaddr: alice_addr,
|
2021-03-05 05:00:52 +00:00
|
|
|
},
|
2021-03-05 05:10:45 +00:00
|
|
|
monero_params:
|
|
|
|
MoneroParams {
|
|
|
|
receive_monero_address,
|
|
|
|
monero_daemon_host,
|
|
|
|
},
|
2021-03-15 01:02:42 +00:00
|
|
|
bitcoin_params:
|
|
|
|
BitcoinParams {
|
|
|
|
electrum_http_url,
|
|
|
|
electrum_rpc_url,
|
|
|
|
},
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
2021-03-03 23:46:12 +00:00
|
|
|
if receive_monero_address.network != monero_network {
|
|
|
|
bail!("The given monero address is on network {:?}, expected address of network {:?}.", receive_monero_address.network, monero_network)
|
|
|
|
}
|
|
|
|
|
2021-03-15 01:02:42 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
bitcoin_network,
|
|
|
|
electrum_rpc_url,
|
|
|
|
electrum_http_url,
|
|
|
|
seed,
|
|
|
|
data_dir.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-11 05:06:28 +00:00
|
|
|
let (monero_wallet, _process) = init_monero_wallet(
|
|
|
|
monero_network,
|
2021-03-15 01:02:42 +00:00
|
|
|
data_dir,
|
2021-03-11 05:06:28 +00:00
|
|
|
monero_daemon_host,
|
|
|
|
execution_params,
|
|
|
|
)
|
|
|
|
.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-03 02:56:25 +00:00
|
|
|
let (event_loop, event_loop_handle) = EventLoop::new(
|
|
|
|
&seed.derive_libp2p_identity(),
|
|
|
|
alice_peer_id,
|
|
|
|
alice_addr,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
)?;
|
|
|
|
let handle = tokio::spawn(event_loop.run());
|
|
|
|
|
|
|
|
let swap = Builder::new(
|
2021-02-11 04:21:34 +00:00
|
|
|
db,
|
2020-12-21 03:43:44 +00:00
|
|
|
swap_id,
|
2021-03-03 02:56:25 +00:00
|
|
|
bitcoin_wallet.clone(),
|
2021-01-20 02:36:38 +00:00
|
|
|
Arc::new(monero_wallet),
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-03 02:56:25 +00:00
|
|
|
event_loop_handle,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2021-03-03 02:56:25 +00:00
|
|
|
)
|
|
|
|
.build()?;
|
|
|
|
|
2021-02-26 05:11:14 +00:00
|
|
|
let swap = bob::run(swap);
|
|
|
|
tokio::select! {
|
|
|
|
event_loop_result = handle => {
|
|
|
|
event_loop_result??;
|
|
|
|
},
|
|
|
|
swap_result = swap => {
|
|
|
|
swap_result?;
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 10:26:02 +00:00
|
|
|
}
|
2021-03-15 01:02:42 +00:00
|
|
|
Command::Cancel {
|
|
|
|
swap_id,
|
|
|
|
force,
|
|
|
|
bitcoin_params:
|
|
|
|
BitcoinParams {
|
|
|
|
electrum_http_url,
|
|
|
|
electrum_rpc_url,
|
|
|
|
},
|
|
|
|
} => {
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
bitcoin_network,
|
|
|
|
electrum_rpc_url,
|
|
|
|
electrum_http_url,
|
|
|
|
seed,
|
|
|
|
data_dir,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-02-01 05:10:43 +00:00
|
|
|
|
2021-03-03 02:17:09 +00:00
|
|
|
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
|
|
|
|
let cancel =
|
|
|
|
bob::cancel(swap_id, resume_state, 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-03-04 06:02:03 +00:00
|
|
|
Err(bob::cancel::Error::CancelTxAlreadyPublished) => {
|
2021-03-03 02:17:09 +00:00
|
|
|
warn!("The Cancel Transaction has already been published.")
|
2021-02-01 05:25:33 +00:00
|
|
|
}
|
2021-02-01 05:10:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 01:02:42 +00:00
|
|
|
Command::Refund {
|
|
|
|
swap_id,
|
|
|
|
force,
|
|
|
|
bitcoin_params:
|
|
|
|
BitcoinParams {
|
|
|
|
electrum_http_url,
|
|
|
|
electrum_rpc_url,
|
|
|
|
},
|
|
|
|
} => {
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
bitcoin_network,
|
|
|
|
electrum_rpc_url,
|
|
|
|
electrum_http_url,
|
|
|
|
seed,
|
|
|
|
data_dir,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-02-01 05:25:33 +00:00
|
|
|
|
2021-03-03 02:17:09 +00:00
|
|
|
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
|
|
|
|
|
|
|
|
bob::refund(
|
2021-02-01 05:25:33 +00:00
|
|
|
swap_id,
|
2021-03-03 02:17:09 +00:00
|
|
|
resume_state,
|
2021-02-01 05:25:33 +00:00
|
|
|
execution_params,
|
2021-03-03 02:17:09 +00:00
|
|
|
Arc::new(bitcoin_wallet),
|
|
|
|
db,
|
2021-02-01 11:32:54 +00:00
|
|
|
force,
|
2021-03-03 02:17:09 +00:00
|
|
|
)
|
|
|
|
.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-04 05:54:26 +00:00
|
|
|
network: bitcoin::Network,
|
2021-03-15 01:02:42 +00:00
|
|
|
electrum_rpc_url: Url,
|
|
|
|
electrum_http_url: Url,
|
2021-02-09 06:23:13 +00:00
|
|
|
seed: Seed,
|
2021-03-15 01:02:42 +00:00
|
|
|
data_dir: PathBuf,
|
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(),
|
|
|
|
electrum_http_url.clone(),
|
2021-03-04 05:54:26 +00:00
|
|
|
network,
|
|
|
|
&wallet_dir,
|
|
|
|
seed.derive_extended_private_key(network)?,
|
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(
|
|
|
|
monero_network: monero::Network,
|
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-11 05:06:28 +00:00
|
|
|
execution_params: ExecutionParams,
|
2021-03-04 05:54:26 +00:00
|
|
|
) -> Result<(monero::Wallet, monero::WalletRpcProcess)> {
|
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-05 05:10:45 +00:00
|
|
|
.run(monero_network, monero_daemon_host.as_str())
|
2021-03-04 05:54:26 +00:00
|
|
|
.await?;
|
|
|
|
|
2021-02-24 07:00:07 +00:00
|
|
|
let monero_wallet = monero::Wallet::new(
|
2021-03-04 05:54:26 +00:00
|
|
|
monero_wallet_rpc_process.endpoint(),
|
2021-02-24 07:00:07 +00:00
|
|
|
monero_network,
|
|
|
|
MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(),
|
2021-03-11 06:03:10 +00:00
|
|
|
execution_params.monero_avg_block_time,
|
2021-02-24 07:00:07 +00:00
|
|
|
);
|
2021-02-09 04:34:12 +00:00
|
|
|
|
2021-03-03 04:30:58 +00:00
|
|
|
monero_wallet.open_or_create().await?;
|
2021-02-09 04:34:12 +00:00
|
|
|
|
2021-02-26 06:04:51 +00:00
|
|
|
let _test_wallet_connection = monero_wallet
|
|
|
|
.block_height()
|
|
|
|
.await
|
2021-03-04 06:25:05 +00:00
|
|
|
.context("Failed to validate connection to monero-wallet-rpc")?;
|
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-04 06:25:05 +00:00
|
|
|
let bid_quote = request_quote.await.context("Failed to request quote")?;
|
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?;
|
|
|
|
|
|
|
|
if initial_balance == Amount::ZERO {
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
info!("Found {} in wallet", initial_balance);
|
|
|
|
}
|
|
|
|
|
2021-03-04 06:39:17 +00:00
|
|
|
let max_giveable = max_giveable
|
|
|
|
.await
|
|
|
|
.context("Failed to compute max 'giveable' Bitcoin amount")?;
|
2021-03-04 02:43:06 +00:00
|
|
|
let max_accepted = bid_quote.max_quantity;
|
|
|
|
|
|
|
|
if max_giveable > max_accepted {
|
|
|
|
info!(
|
|
|
|
"Max giveable amount {} exceeds max accepted amount {}!",
|
|
|
|
max_giveable, max_accepted
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(min(max_giveable, max_accepted))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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()?)
|
|
|
|
}
|
|
|
|
}
|