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-07-06 09:34:02 +00:00
|
|
|
use anyhow::{Context, Result};
|
2021-07-06 04:19:15 +00:00
|
|
|
use comfy_table::Table;
|
2021-05-30 11:53:43 +00:00
|
|
|
use qrcode::render::unicode;
|
|
|
|
use qrcode::QrCode;
|
2021-03-03 05:54:47 +00:00
|
|
|
use std::cmp::min;
|
2021-05-11 06:52:40 +00:00
|
|
|
use std::env;
|
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-05-07 09:06:58 +00:00
|
|
|
use swap::bitcoin::TxLock;
|
2021-05-21 02:35:22 +00:00
|
|
|
use swap::cli::command::{parse_args_and_apply_defaults, Arguments, Command, ParseResult};
|
2021-07-07 03:32:44 +00:00
|
|
|
use swap::cli::{list_sellers, EventLoop, SellerStatus};
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::database::Database;
|
2021-05-11 06:52:40 +00:00
|
|
|
use swap::env::Config;
|
2021-07-06 04:39:05 +00:00
|
|
|
use swap::libp2p_ext::MultiAddrExt;
|
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-06-25 03:40:33 +00:00
|
|
|
use swap::protocol::bob::Swap;
|
2021-03-04 00:28:58 +00:00
|
|
|
use swap::seed::Seed;
|
2021-05-11 06:52:40 +00:00
|
|
|
use swap::{bitcoin, cli, monero};
|
2021-03-15 01:02:42 +00:00
|
|
|
use url::Url;
|
2021-01-21 02:43:25 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2020-10-15 22:14:39 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-05-11 06:52:40 +00:00
|
|
|
let Arguments {
|
|
|
|
env_config,
|
|
|
|
data_dir,
|
|
|
|
debug,
|
2021-05-20 06:36:01 +00:00
|
|
|
json,
|
2021-05-11 06:52:40 +00:00
|
|
|
cmd,
|
2021-05-21 02:35:22 +00:00
|
|
|
} = match parse_args_and_apply_defaults(env::args_os())? {
|
|
|
|
ParseResult::Arguments(args) => args,
|
|
|
|
ParseResult::PrintAndExitZero { message } => {
|
|
|
|
println!("{}", message);
|
|
|
|
std::process::exit(0);
|
2021-05-21 01:46:11 +00:00
|
|
|
}
|
|
|
|
};
|
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-07-06 04:39:05 +00:00
|
|
|
seller,
|
2021-05-11 06:52:40 +00:00
|
|
|
bitcoin_electrum_rpc_url,
|
|
|
|
bitcoin_target_block,
|
2021-07-06 09:17:17 +00:00
|
|
|
bitcoin_change_address,
|
2021-05-11 06:52:40 +00:00
|
|
|
monero_receive_address,
|
|
|
|
monero_daemon_address,
|
|
|
|
tor_socks5_port,
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
2021-04-02 07:22:51 +00:00
|
|
|
let swap_id = Uuid::new_v4();
|
|
|
|
|
2021-06-28 06:45:40 +00:00
|
|
|
cli::tracing::init(debug, json, data_dir.join("logs"), Some(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")?;
|
2021-03-03 23:46:12 +00:00
|
|
|
|
2021-05-02 23:48:40 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
2021-05-11 06:52:40 +00:00
|
|
|
bitcoin_electrum_rpc_url,
|
2021-05-02 23:48:40 +00:00
|
|
|
&seed,
|
|
|
|
data_dir.clone(),
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-17 02:36:43 +00:00
|
|
|
let (monero_wallet, _process) =
|
2021-05-11 02:37:13 +00:00
|
|
|
init_monero_wallet(data_dir, monero_daemon_address, 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-07-06 04:39:05 +00:00
|
|
|
let seller_peer_id = seller
|
|
|
|
.extract_peer_id()
|
|
|
|
.context("Seller address must contain peer ID")?;
|
|
|
|
db.insert_address(seller_peer_id, seller.clone()).await?;
|
|
|
|
|
2021-06-28 10:49:02 +00:00
|
|
|
let behaviour = cli::Behaviour::new(seller_peer_id, env_config, bitcoin_wallet.clone());
|
|
|
|
let mut swarm =
|
|
|
|
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
|
2021-07-06 04:39:05 +00:00
|
|
|
swarm.behaviour_mut().add_address(seller_peer_id, seller);
|
2021-03-23 05:56:04 +00:00
|
|
|
|
2021-06-09 04:13:04 +00:00
|
|
|
tracing::debug!(peer_id = %swarm.local_peer_id(), "Network layer initialized");
|
|
|
|
|
2021-06-24 03:54:26 +00:00
|
|
|
let (event_loop, mut event_loop_handle) =
|
|
|
|
EventLoop::new(swap_id, swarm, seller_peer_id, env_config)?;
|
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());
|
2021-05-20 23:53:06 +00:00
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-05-30 11:53:43 +00:00
|
|
|
json,
|
2021-03-04 02:43:06 +00:00
|
|
|
event_loop_handle.request_quote(),
|
|
|
|
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-07-06 06:42:05 +00:00
|
|
|
tracing::info!(%amount, %fees, %swap_id, "Starting new swap");
|
2021-05-07 09:06:58 +00:00
|
|
|
|
2021-05-11 02:00:11 +00:00
|
|
|
db.insert_peer_id(swap_id, seller_peer_id).await?;
|
2021-07-06 09:34:02 +00:00
|
|
|
db.insert_monero_address(swap_id, monero_receive_address)
|
|
|
|
.await?;
|
2021-03-30 04:40:59 +00:00
|
|
|
|
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-05-11 06:52:40 +00:00
|
|
|
monero_receive_address,
|
2021-07-06 09:17:17 +00:00
|
|
|
bitcoin_change_address,
|
2021-05-20 23:53:06 +00:00
|
|
|
amount,
|
2021-04-16 02:00:11 +00:00
|
|
|
);
|
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 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();
|
|
|
|
|
2021-07-06 04:19:15 +00:00
|
|
|
table.set_header(vec!["SWAP ID", "STATE"]);
|
2020-12-04 05:27:17 +00:00
|
|
|
|
2021-03-26 04:16:19 +00:00
|
|
|
for (swap_id, state) in db.all_bob()? {
|
2021-07-06 04:19:15 +00:00
|
|
|
table.add_row(vec![swap_id.to_string(), state.to_string()]);
|
2020-12-04 05:27:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 04:19:15 +00:00
|
|
|
println!("{}", table);
|
2020-12-04 05:27:17 +00:00
|
|
|
}
|
2021-08-29 16:31:41 +00:00
|
|
|
|
|
|
|
Command::WithdrawBtc {
|
|
|
|
bitcoin_electrum_rpc_url,
|
|
|
|
bitcoin_target_block,
|
|
|
|
amount,
|
|
|
|
address,
|
|
|
|
} => {
|
|
|
|
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
|
|
|
|
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
|
|
.context("Failed to read in seed file")?;
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
bitcoin_electrum_rpc_url,
|
|
|
|
&seed,
|
|
|
|
data_dir.clone(),
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let amount = match amount {
|
|
|
|
Some(amount) => amount,
|
|
|
|
None => {
|
|
|
|
bitcoin_wallet
|
|
|
|
.max_giveable(address.script_pubkey().len())
|
|
|
|
.await?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let psbt = bitcoin_wallet
|
|
|
|
.send_to_address(address, amount, None)
|
|
|
|
.await?;
|
|
|
|
let signed_tx = bitcoin_wallet.sign_and_finalize(psbt).await?;
|
|
|
|
|
|
|
|
bitcoin_wallet.broadcast(signed_tx, "withdraw").await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Command::Balance {
|
|
|
|
bitcoin_electrum_rpc_url,
|
|
|
|
bitcoin_target_block,
|
|
|
|
} => {
|
|
|
|
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
|
|
|
|
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
|
|
.context("Failed to read in seed file")?;
|
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
|
|
|
bitcoin_electrum_rpc_url,
|
|
|
|
&seed,
|
|
|
|
data_dir.clone(),
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
|
|
|
println!("Bitcoin balance is {}", bitcoin_balance);
|
|
|
|
}
|
2021-02-28 23:53:43 +00:00
|
|
|
Command::Resume {
|
2021-05-11 06:52:40 +00:00
|
|
|
swap_id,
|
|
|
|
bitcoin_electrum_rpc_url,
|
|
|
|
bitcoin_target_block,
|
|
|
|
monero_daemon_address,
|
|
|
|
tor_socks5_port,
|
2021-02-28 23:53:43 +00:00
|
|
|
} => {
|
2021-06-28 06:45:40 +00:00
|
|
|
cli::tracing::init(debug, json, data_dir.join("logs"), Some(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")?;
|
|
|
|
|
2021-05-02 23:48:40 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
2021-05-11 06:52:40 +00:00
|
|
|
bitcoin_electrum_rpc_url,
|
2021-05-02 23:48:40 +00:00
|
|
|
&seed,
|
|
|
|
data_dir.clone(),
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-17 02:36:43 +00:00
|
|
|
let (monero_wallet, _process) =
|
2021-05-11 02:37:13 +00:00
|
|
|
init_monero_wallet(data_dir, monero_daemon_address, 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-05-11 02:00:11 +00:00
|
|
|
let seller_peer_id = db.get_peer_id(swap_id)?;
|
2021-07-06 04:39:05 +00:00
|
|
|
let seller_addresses = db.get_addresses(seller_peer_id)?;
|
2021-04-22 01:33:36 +00:00
|
|
|
|
2021-06-28 10:49:02 +00:00
|
|
|
let behaviour = cli::Behaviour::new(seller_peer_id, env_config, bitcoin_wallet.clone());
|
|
|
|
let mut swarm =
|
|
|
|
swarm::cli(seed.derive_libp2p_identity(), tor_socks5_port, behaviour).await?;
|
2021-05-20 23:53:06 +00:00
|
|
|
let our_peer_id = swarm.local_peer_id();
|
2021-07-06 06:42:05 +00:00
|
|
|
tracing::debug!(peer_id = %our_peer_id, "Network layer initialized");
|
2021-07-06 04:39:05 +00:00
|
|
|
|
|
|
|
for seller_address in seller_addresses {
|
|
|
|
swarm
|
|
|
|
.behaviour_mut()
|
|
|
|
.add_address(seller_peer_id, seller_address);
|
|
|
|
}
|
2021-03-23 05:56:04 +00:00
|
|
|
|
2021-06-24 03:54:26 +00:00
|
|
|
let (event_loop, event_loop_handle) =
|
|
|
|
EventLoop::new(swap_id, swarm, seller_peer_id, env_config)?;
|
2021-03-03 02:56:25 +00:00
|
|
|
let handle = tokio::spawn(event_loop.run());
|
|
|
|
|
2021-07-06 09:34:02 +00:00
|
|
|
let monero_receive_address = db.get_monero_address(swap_id)?;
|
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-05-11 06:52:40 +00:00
|
|
|
monero_receive_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 {
|
2021-05-11 06:52:40 +00:00
|
|
|
swap_id,
|
2021-03-15 01:02:42 +00:00
|
|
|
force,
|
2021-05-11 06:52:40 +00:00
|
|
|
bitcoin_electrum_rpc_url,
|
|
|
|
bitcoin_target_block,
|
2021-03-15 01:02:42 +00:00
|
|
|
} => {
|
2021-06-28 06:45:40 +00:00
|
|
|
cli::tracing::init(debug, json, data_dir.join("logs"), Some(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")?;
|
|
|
|
|
2021-05-02 23:48:40 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
2021-05-11 06:52:40 +00:00
|
|
|
bitcoin_electrum_rpc_url,
|
2021-05-02 23:48:40 +00:00
|
|
|
&seed,
|
|
|
|
data_dir,
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-02-01 05:10:43 +00:00
|
|
|
|
2021-06-25 03:40:33 +00:00
|
|
|
let cancel = cli::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, _)) => {
|
2021-07-06 06:08:43 +00:00
|
|
|
tracing::debug!("Cancel transaction successfully published with id {}", txid)
|
2021-03-03 02:17:09 +00:00
|
|
|
}
|
2021-07-06 06:08:43 +00:00
|
|
|
Err(cli::cancel::Error::CancelTimelockNotExpiredYet) => tracing::error!(
|
2021-07-06 06:42:05 +00:00
|
|
|
"The cancel transaction cannot be published yet, because the timelock has not expired. Please try again later"
|
2021-03-03 02:17:09 +00:00
|
|
|
),
|
2021-02-01 05:10:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 01:02:42 +00:00
|
|
|
Command::Refund {
|
2021-05-11 06:52:40 +00:00
|
|
|
swap_id,
|
2021-03-15 01:02:42 +00:00
|
|
|
force,
|
2021-05-11 06:52:40 +00:00
|
|
|
bitcoin_electrum_rpc_url,
|
|
|
|
bitcoin_target_block,
|
2021-03-15 01:02:42 +00:00
|
|
|
} => {
|
2021-06-28 06:45:40 +00:00
|
|
|
cli::tracing::init(debug, json, data_dir.join("logs"), Some(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")?;
|
|
|
|
|
2021-05-02 23:48:40 +00:00
|
|
|
let bitcoin_wallet = init_bitcoin_wallet(
|
2021-05-11 06:52:40 +00:00
|
|
|
bitcoin_electrum_rpc_url,
|
2021-05-02 23:48:40 +00:00
|
|
|
&seed,
|
|
|
|
data_dir,
|
|
|
|
env_config,
|
|
|
|
bitcoin_target_block,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-02-01 05:25:33 +00:00
|
|
|
|
2021-06-25 03:40:33 +00:00
|
|
|
cli::refund(swap_id, Arc::new(bitcoin_wallet), db, force).await??;
|
2021-02-01 05:25:33 +00:00
|
|
|
}
|
2021-06-28 06:45:40 +00:00
|
|
|
Command::ListSellers {
|
2021-07-07 04:17:57 +00:00
|
|
|
rendezvous_point,
|
2021-06-28 06:45:40 +00:00
|
|
|
namespace,
|
|
|
|
tor_socks5_port,
|
|
|
|
} => {
|
2021-07-07 04:17:57 +00:00
|
|
|
let rendezvous_node_peer_id = rendezvous_point
|
2021-06-28 06:45:40 +00:00
|
|
|
.extract_peer_id()
|
|
|
|
.context("Rendezvous node address must contain peer ID")?;
|
|
|
|
|
|
|
|
cli::tracing::init(debug, json, data_dir.join("logs"), None)?;
|
|
|
|
let seed = Seed::from_file_or_generate(data_dir.as_path())
|
|
|
|
.context("Failed to read in seed file")?;
|
|
|
|
let identity = seed.derive_libp2p_identity();
|
|
|
|
|
2021-07-08 02:42:26 +00:00
|
|
|
let sellers = list_sellers(
|
2021-06-28 06:45:40 +00:00
|
|
|
rendezvous_node_peer_id,
|
2021-07-07 04:17:57 +00:00
|
|
|
rendezvous_point,
|
2021-06-28 06:45:40 +00:00
|
|
|
namespace,
|
|
|
|
tor_socks5_port,
|
|
|
|
identity,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if json {
|
|
|
|
for seller in sellers {
|
|
|
|
println!("{}", serde_json::to_string(&seller)?);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut table = Table::new();
|
|
|
|
|
2021-07-07 03:32:44 +00:00
|
|
|
table.set_header(vec![
|
|
|
|
"PRICE",
|
|
|
|
"MIN_QUANTITY",
|
|
|
|
"MAX_QUANTITY",
|
|
|
|
"STATUS",
|
|
|
|
"ADDRESS",
|
|
|
|
]);
|
2021-06-28 06:45:40 +00:00
|
|
|
|
|
|
|
for seller in sellers {
|
2021-07-07 03:32:44 +00:00
|
|
|
let row = match seller.status {
|
|
|
|
SellerStatus::Online(quote) => {
|
|
|
|
vec![
|
|
|
|
quote.price.to_string(),
|
|
|
|
quote.min_quantity.to_string(),
|
|
|
|
quote.max_quantity.to_string(),
|
|
|
|
"Online".to_owned(),
|
|
|
|
seller.multiaddr.to_string(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
SellerStatus::Unreachable => {
|
|
|
|
vec![
|
|
|
|
"???".to_owned(),
|
|
|
|
"???".to_owned(),
|
|
|
|
"???".to_owned(),
|
|
|
|
"Unreachable".to_owned(),
|
|
|
|
seller.multiaddr.to_string(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
table.add_row(row);
|
2021-06-28 06:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
println!("{}", table);
|
|
|
|
}
|
|
|
|
}
|
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-05-11 02:37:13 +00:00
|
|
|
monero_daemon_address: 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-05-11 12:22:59 +00:00
|
|
|
.run(network, monero_daemon_address.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-30 11:53:43 +00:00
|
|
|
fn qr_code(value: &impl ToString) -> Result<String> {
|
|
|
|
let code = QrCode::new(value.to_string())?;
|
|
|
|
let qr_code = code
|
|
|
|
.render::<unicode::Dense1x2>()
|
|
|
|
.dark_color(unicode::Dense1x2::Light)
|
|
|
|
.light_color(unicode::Dense1x2::Dark)
|
|
|
|
.build();
|
|
|
|
Ok(qr_code)
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS>(
|
2021-05-30 11:53:43 +00:00
|
|
|
json: bool,
|
2021-05-07 09:06:58 +00:00
|
|
|
bid_quote: impl Future<Output = Result<BidQuote>>,
|
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,
|
2021-06-21 01:56:04 +00:00
|
|
|
max_giveable_fn: FMG,
|
2021-05-07 09:06:58 +00:00
|
|
|
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-07-06 06:08:43 +00:00
|
|
|
tracing::debug!("Requesting quote");
|
2021-05-07 09:06:58 +00:00
|
|
|
let bid_quote = bid_quote.await?;
|
2021-07-06 06:08:43 +00:00
|
|
|
tracing::info!(
|
2021-05-20 23:53:06 +00:00
|
|
|
price = %bid_quote.price,
|
2021-05-12 05:50:00 +00:00
|
|
|
minimum_amount = %bid_quote.min_quantity,
|
|
|
|
maximum_amount = %bid_quote.max_quantity,
|
2021-07-08 03:40:11 +00:00
|
|
|
"Received quote",
|
2021-05-12 05:50:00 +00:00
|
|
|
);
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-06-21 01:56:04 +00:00
|
|
|
let mut max_giveable = max_giveable_fn().await?;
|
2021-05-12 05:44:22 +00:00
|
|
|
|
2021-06-21 01:56:04 +00:00
|
|
|
if max_giveable == bitcoin::Amount::ZERO || max_giveable < bid_quote.min_quantity {
|
2021-05-07 09:06:58 +00:00
|
|
|
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
|
|
|
|
2021-05-30 11:53:43 +00:00
|
|
|
if !json {
|
|
|
|
eprintln!("{}", qr_code(&deposit_address)?);
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
loop {
|
2021-07-06 06:42:05 +00:00
|
|
|
tracing::info!(
|
|
|
|
%deposit_address,
|
|
|
|
%max_giveable,
|
|
|
|
%minimum_amount,
|
|
|
|
%maximum_amount,
|
|
|
|
"Waiting for Bitcoin deposit",
|
|
|
|
);
|
|
|
|
|
2021-07-08 02:14:30 +00:00
|
|
|
max_giveable = loop {
|
|
|
|
sync().await?;
|
|
|
|
let new_max_givable = max_giveable_fn().await?;
|
2021-05-07 09:06:58 +00:00
|
|
|
|
2021-07-08 02:14:30 +00:00
|
|
|
if new_max_givable > max_giveable {
|
|
|
|
break new_max_givable;
|
|
|
|
}
|
2021-05-07 09:06:58 +00:00
|
|
|
|
2021-07-08 02:14:30 +00:00
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
|
|
};
|
2021-05-07 09:06:58 +00:00
|
|
|
|
2021-07-08 02:14:30 +00:00
|
|
|
let new_balance = balance().await?;
|
|
|
|
tracing::info!(%new_balance, %max_giveable, "Received Bitcoin");
|
2021-05-07 09:06:58 +00:00
|
|
|
|
2021-07-08 02:14:30 +00:00
|
|
|
if max_giveable < bid_quote.min_quantity {
|
|
|
|
tracing::info!("Deposited amount is less than `min_quantity`");
|
|
|
|
continue;
|
2021-05-07 09:06:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 02:14:30 +00:00
|
|
|
break;
|
2021-05-07 09:06:58 +00:00
|
|
|
}
|
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 {
|
2021-07-08 01:50:37 +00:00
|
|
|
use super::*;
|
|
|
|
use crate::determine_btc_to_swap;
|
2021-06-25 03:40:33 +00:00
|
|
|
use ::bitcoin::Amount;
|
2021-07-08 01:50:37 +00:00
|
|
|
use std::sync::Mutex;
|
2021-07-08 02:28:04 +00:00
|
|
|
use swap::tracing_ext::capture_logs;
|
|
|
|
use tracing::level_filters::LevelFilter;
|
2021-05-12 05:44:22 +00:00
|
|
|
|
2021-03-04 02:43:06 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn given_no_balance_and_transfers_less_than_max_swaps_max_giveable() {
|
2021-07-08 02:28:04 +00:00
|
|
|
let writer = capture_logs(LevelFilter::INFO);
|
2021-05-12 05:44:22 +00:00
|
|
|
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::from_btc(0.0009).unwrap(),
|
|
|
|
])));
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-05-30 11:53:43 +00:00
|
|
|
true,
|
2021-03-04 02:43:06 +00:00
|
|
|
async { Ok(quote_with_max(0.01)) },
|
|
|
|
get_dummy_address(),
|
2021-05-07 09:06:58 +00:00
|
|
|
|| async { Ok(Amount::from_btc(0.001)?) },
|
2021-05-12 05:44:22 +00:00
|
|
|
|| async {
|
|
|
|
let mut result = givable.lock().unwrap();
|
|
|
|
result.give()
|
|
|
|
},
|
2021-05-07 09:06:58 +00:00
|
|
|
|| 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();
|
|
|
|
|
2021-07-08 01:50:37 +00:00
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
|
|
|
assert_eq!(
|
|
|
|
writer.captured(),
|
2021-07-08 03:40:11 +00:00
|
|
|
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
2021-07-08 01:50:37 +00:00
|
|
|
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00000000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
|
|
|
INFO swap: Received Bitcoin new_balance=0.00100000 BTC max_giveable=0.00090000 BTC
|
|
|
|
"
|
|
|
|
);
|
2021-03-04 02:43:06 +00:00
|
|
|
}
|
2021-07-08 01:50:37 +00:00
|
|
|
|
2021-03-04 02:43:06 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn given_no_balance_and_transfers_more_then_swaps_max_quantity_from_quote() {
|
2021-07-08 02:28:04 +00:00
|
|
|
let writer = capture_logs(LevelFilter::INFO);
|
2021-05-12 05:44:22 +00:00
|
|
|
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::from_btc(0.1).unwrap(),
|
|
|
|
])));
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-05-30 11:53:43 +00:00
|
|
|
true,
|
2021-03-04 02:43:06 +00:00
|
|
|
async { Ok(quote_with_max(0.01)) },
|
|
|
|
get_dummy_address(),
|
2021-05-07 09:06:58 +00:00
|
|
|
|| async { Ok(Amount::from_btc(0.1001)?) },
|
2021-05-12 05:44:22 +00:00
|
|
|
|| async {
|
|
|
|
let mut result = givable.lock().unwrap();
|
|
|
|
result.give()
|
|
|
|
},
|
2021-05-07 09:06:58 +00:00
|
|
|
|| 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();
|
|
|
|
|
2021-07-08 01:50:37 +00:00
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
|
|
|
assert_eq!(
|
|
|
|
writer.captured(),
|
2021-07-08 03:40:11 +00:00
|
|
|
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
2021-07-08 01:50:37 +00:00
|
|
|
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00000000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
|
|
|
INFO swap: Received Bitcoin new_balance=0.10010000 BTC max_giveable=0.10000000 BTC
|
|
|
|
"
|
|
|
|
);
|
2021-03-04 02:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_initial_balance_below_max_quantity_swaps_max_givable() {
|
2021-07-08 02:28:04 +00:00
|
|
|
let writer = capture_logs(LevelFilter::INFO);
|
2021-05-12 05:44:22 +00:00
|
|
|
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
|
|
|
Amount::from_btc(0.0049).unwrap(),
|
|
|
|
Amount::from_btc(99.9).unwrap(),
|
|
|
|
])));
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-05-30 11:53:43 +00:00
|
|
|
true,
|
2021-03-04 02:43:06 +00:00
|
|
|
async { Ok(quote_with_max(0.01)) },
|
2021-05-12 05:44:22 +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)?) },
|
2021-05-12 05:44:22 +00:00
|
|
|
|| async {
|
|
|
|
let mut result = givable.lock().unwrap();
|
|
|
|
result.give()
|
|
|
|
},
|
2021-05-07 09:06:58 +00:00
|
|
|
|| 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();
|
|
|
|
|
2021-07-08 01:50:37 +00:00
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
|
|
|
assert_eq!(
|
|
|
|
writer.captured(),
|
2021-07-08 03:40:11 +00:00
|
|
|
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
2021-07-08 01:50:37 +00:00
|
|
|
"
|
|
|
|
);
|
2021-03-04 02:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_initial_balance_above_max_quantity_swaps_max_quantity() {
|
2021-07-08 02:28:04 +00:00
|
|
|
let writer = capture_logs(LevelFilter::INFO);
|
2021-05-12 05:44:22 +00:00
|
|
|
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
|
|
|
Amount::from_btc(0.1).unwrap(),
|
|
|
|
Amount::from_btc(99.9).unwrap(),
|
|
|
|
])));
|
2021-03-04 02:43:06 +00:00
|
|
|
|
2021-05-07 09:06:58 +00:00
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-05-30 11:53:43 +00:00
|
|
|
true,
|
2021-03-04 02:43:06 +00:00
|
|
|
async { Ok(quote_with_max(0.01)) },
|
|
|
|
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)?) },
|
2021-05-12 05:44:22 +00:00
|
|
|
|| async {
|
|
|
|
let mut result = givable.lock().unwrap();
|
|
|
|
result.give()
|
|
|
|
},
|
2021-05-07 09:06:58 +00:00
|
|
|
|| async { Ok(()) },
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let expected_amount = Amount::from_btc(0.01).unwrap();
|
|
|
|
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
|
|
|
|
2021-07-08 01:50:37 +00:00
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
|
|
|
assert_eq!(
|
|
|
|
writer.captured(),
|
2021-07-08 03:40:11 +00:00
|
|
|
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
2021-07-08 01:50:37 +00:00
|
|
|
"
|
|
|
|
);
|
2021-05-07 09:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_no_initial_balance_then_min_wait_for_sufficient_deposit() {
|
2021-07-08 02:28:04 +00:00
|
|
|
let writer = capture_logs(LevelFilter::INFO);
|
2021-05-12 05:44:22 +00:00
|
|
|
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::from_btc(0.01).unwrap(),
|
|
|
|
])));
|
2021-05-07 09:06:58 +00:00
|
|
|
|
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-05-30 11:53:43 +00:00
|
|
|
true,
|
2021-05-07 09:06:58 +00:00
|
|
|
async { Ok(quote_with_min(0.01)) },
|
|
|
|
get_dummy_address(),
|
|
|
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
2021-05-12 05:44:22 +00:00
|
|
|
|| async {
|
|
|
|
let mut result = givable.lock().unwrap();
|
|
|
|
result.give()
|
|
|
|
},
|
2021-05-07 09:06:58 +00:00
|
|
|
|| 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();
|
|
|
|
|
2021-07-08 01:50:37 +00:00
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
|
|
|
assert_eq!(
|
|
|
|
writer.captured(),
|
2021-07-08 03:40:11 +00:00
|
|
|
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
2021-07-08 01:50:37 +00:00
|
|
|
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00000000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
|
|
|
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
|
|
|
|
"
|
|
|
|
);
|
2021-05-07 09:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_balance_less_then_min_wait_for_sufficient_deposit() {
|
2021-07-08 02:28:04 +00:00
|
|
|
let writer = capture_logs(LevelFilter::INFO);
|
2021-05-12 05:44:22 +00:00
|
|
|
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
|
|
|
Amount::from_btc(0.0001).unwrap(),
|
|
|
|
Amount::from_btc(0.01).unwrap(),
|
|
|
|
])));
|
2021-05-07 09:06:58 +00:00
|
|
|
|
|
|
|
let (amount, fees) = determine_btc_to_swap(
|
2021-05-30 11:53:43 +00:00
|
|
|
true,
|
2021-05-07 09:06:58 +00:00
|
|
|
async { Ok(quote_with_min(0.01)) },
|
|
|
|
get_dummy_address(),
|
|
|
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
2021-05-12 05:44:22 +00:00
|
|
|
|| async {
|
|
|
|
let mut result = givable.lock().unwrap();
|
|
|
|
result.give()
|
|
|
|
},
|
2021-05-07 09:06:58 +00:00
|
|
|
|| async { Ok(()) },
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let expected_amount = Amount::from_btc(0.01).unwrap();
|
|
|
|
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
|
|
|
|
2021-07-08 01:50:37 +00:00
|
|
|
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
|
|
|
assert_eq!(
|
|
|
|
writer.captured(),
|
2021-07-08 03:40:11 +00:00
|
|
|
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
2021-07-08 01:50:37 +00:00
|
|
|
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00010000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
|
|
|
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
|
|
|
|
"
|
|
|
|
);
|
2021-05-07 09:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn given_no_initial_balance_and_transfers_less_than_min_keep_waiting() {
|
2021-07-08 02:28:04 +00:00
|
|
|
let writer = capture_logs(LevelFilter::INFO);
|
2021-05-12 05:44:22 +00:00
|
|
|
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::from_btc(0.01).unwrap(),
|
|
|
|
Amount::from_btc(0.01).unwrap(),
|
|
|
|
Amount::from_btc(0.01).unwrap(),
|
|
|
|
Amount::from_btc(0.01).unwrap(),
|
|
|
|
])));
|
2021-05-07 09:06:58 +00:00
|
|
|
|
|
|
|
let error = tokio::time::timeout(
|
|
|
|
Duration::from_secs(1),
|
|
|
|
determine_btc_to_swap(
|
2021-05-30 11:53:43 +00:00
|
|
|
true,
|
2021-05-07 09:06:58 +00:00
|
|
|
async { Ok(quote_with_min(0.1)) },
|
|
|
|
get_dummy_address(),
|
|
|
|
|| async { Ok(Amount::from_btc(0.0101)?) },
|
2021-05-12 05:44:22 +00:00
|
|
|
|| async {
|
|
|
|
let mut result = givable.lock().unwrap();
|
|
|
|
result.give()
|
|
|
|
},
|
2021-05-07 09:06:58 +00:00
|
|
|
|| async { Ok(()) },
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap_err();
|
|
|
|
|
2021-07-08 01:50:37 +00:00
|
|
|
assert!(matches!(error, tokio::time::error::Elapsed { .. }));
|
|
|
|
assert_eq!(
|
|
|
|
writer.captured(),
|
2021-07-08 03:40:11 +00:00
|
|
|
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
2021-07-08 01:50:37 +00:00
|
|
|
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
|
|
|
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
|
|
|
|
INFO swap: Deposited amount is less than `min_quantity`
|
|
|
|
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.01000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-08 02:12:55 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn given_longer_delay_until_deposit_should_not_spam_user() {
|
2021-07-08 02:28:04 +00:00
|
|
|
let writer = capture_logs(LevelFilter::INFO);
|
2021-07-08 02:12:55 +00:00
|
|
|
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::ZERO,
|
|
|
|
Amount::from_btc(0.2).unwrap(),
|
|
|
|
])));
|
|
|
|
|
|
|
|
tokio::time::timeout(
|
|
|
|
Duration::from_secs(10),
|
|
|
|
determine_btc_to_swap(
|
|
|
|
true,
|
|
|
|
async { Ok(quote_with_min(0.1)) },
|
|
|
|
get_dummy_address(),
|
|
|
|
|| async { Ok(Amount::from_btc(0.21)?) },
|
|
|
|
|| async {
|
|
|
|
let mut result = givable.lock().unwrap();
|
|
|
|
|
|
|
|
result.give()
|
|
|
|
},
|
|
|
|
|| async { Ok(()) },
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
writer.captured(),
|
2021-07-08 03:40:11 +00:00
|
|
|
r" INFO swap: Received quote price=0.00100000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
2021-07-08 02:12:55 +00:00
|
|
|
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
|
|
|
INFO swap: Received Bitcoin new_balance=0.21000000 BTC max_giveable=0.20000000 BTC
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-08 01:15:14 +00:00
|
|
|
struct MaxGiveable {
|
|
|
|
amounts: Vec<Amount>,
|
|
|
|
call_counter: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MaxGiveable {
|
|
|
|
fn new(amounts: Vec<Amount>) -> Self {
|
|
|
|
Self {
|
|
|
|
amounts,
|
|
|
|
call_counter: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn give(&mut self) -> Result<Amount> {
|
|
|
|
let amount = self
|
|
|
|
.amounts
|
|
|
|
.get(self.call_counter)
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("No more balances available"))?;
|
|
|
|
self.call_counter += 1;
|
|
|
|
Ok(*amount)
|
|
|
|
}
|
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()?)
|
|
|
|
}
|
|
|
|
}
|