Add config/argument to swap/asb to configure bitcoin tx fees.

This commit is contained in:
Philipp Hoenisch 2021-05-03 09:48:40 +10:00
parent 9e8b788aa9
commit 46e0449b8e
No known key found for this signature in database
GPG Key ID: E5F8E74C672BC666
4 changed files with 65 additions and 11 deletions

View File

@ -16,6 +16,7 @@ const DEFAULT_LISTEN_ADDRESS_TCP: &str = "/ip4/0.0.0.0/tcp/9939";
const DEFAULT_LISTEN_ADDRESS_WS: &str = "/ip4/0.0.0.0/tcp/9940/ws";
const DEFAULT_ELECTRUM_RPC_URL: &str = "ssl://electrum.blockstream.info:60002";
const DEFAULT_MONERO_WALLET_RPC_TESTNET_URL: &str = "http://127.0.0.1:38083/json_rpc";
const DEFAULT_BITCOIN_CONFIRMATION_TARGET: usize = 3;
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct Config {
@ -55,6 +56,7 @@ pub struct Network {
#[serde(deny_unknown_fields)]
pub struct Bitcoin {
pub electrum_rpc_url: Url,
pub target_block: usize,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
@ -148,6 +150,10 @@ pub fn query_user_for_initial_testnet_config() -> Result<Config> {
.interact_text()?;
let data_dir = data_dir.as_str().parse()?;
let target_block = Input::with_theme(&ColorfulTheme::default())
.with_prompt("How fast should your Bitcoin transactions be confirmed? Your transaction fee will be calculated based on this target. Hit return to use default")
.default(DEFAULT_BITCOIN_CONFIRMATION_TARGET)
.interact_text()?;
let listen_addresses = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter multiaddresses (comma separated) on which asb should list for peer-to-peer communications or hit return to use default")
.default( format!("{},{}", DEFAULT_LISTEN_ADDRESS_TCP, DEFAULT_LISTEN_ADDRESS_WS))
@ -186,7 +192,10 @@ pub fn query_user_for_initial_testnet_config() -> Result<Config> {
network: Network {
listen: listen_addresses,
},
bitcoin: Bitcoin { electrum_rpc_url },
bitcoin: Bitcoin {
electrum_rpc_url,
target_block,
},
monero: Monero {
wallet_rpc_url: monero_wallet_rpc_url,
},
@ -214,6 +223,7 @@ mod tests {
},
bitcoin: Bitcoin {
electrum_rpc_url: Url::from_str(DEFAULT_ELECTRUM_RPC_URL).unwrap(),
target_block: DEFAULT_BITCOIN_CONFIRMATION_TARGET,
},
network: Network {
listen: vec![

View File

@ -211,7 +211,7 @@ async fn init_bitcoin_wallet(
&wallet_dir,
seed.derive_extended_private_key(env_config.bitcoin_network)?,
env_config,
6, // TODO move this into config
config.bitcoin.target_block,
)
.await
.context("Failed to initialize Bitcoin wallet")?;

View File

@ -52,6 +52,7 @@ async fn main() -> Result<()> {
},
electrum_rpc_url,
tor_socks5_port,
bitcoin_target_block,
} => {
let swap_id = Uuid::new_v4();
@ -71,8 +72,14 @@ async fn main() -> Result<()> {
)
}
let bitcoin_wallet =
init_bitcoin_wallet(electrum_rpc_url, &seed, data_dir.clone(), env_config).await?;
let bitcoin_wallet = init_bitcoin_wallet(
electrum_rpc_url,
&seed,
data_dir.clone(),
env_config,
bitcoin_target_block,
)
.await?;
let (monero_wallet, _process) =
init_monero_wallet(data_dir, monero_daemon_host, env_config).await?;
let bitcoin_wallet = Arc::new(bitcoin_wallet);
@ -154,6 +161,7 @@ async fn main() -> Result<()> {
},
electrum_rpc_url,
tor_socks5_port,
bitcoin_target_block,
} => {
let data_dir = data.0;
cli::tracing::init(debug, data_dir.join("logs"), swap_id)?;
@ -167,8 +175,14 @@ async fn main() -> Result<()> {
bail!("The given monero address is on network {:?}, expected address of network {:?}.", receive_monero_address.network, env_config.monero_network)
}
let bitcoin_wallet =
init_bitcoin_wallet(electrum_rpc_url, &seed, data_dir.clone(), env_config).await?;
let bitcoin_wallet = init_bitcoin_wallet(
electrum_rpc_url,
&seed,
data_dir.clone(),
env_config,
bitcoin_target_block,
)
.await?;
let (monero_wallet, _process) =
init_monero_wallet(data_dir, monero_daemon_host, env_config).await?;
let bitcoin_wallet = Arc::new(bitcoin_wallet);
@ -209,6 +223,7 @@ async fn main() -> Result<()> {
swap_id,
force,
electrum_rpc_url,
bitcoin_target_block,
} => {
let data_dir = data.0;
cli::tracing::init(debug, data_dir.join("logs"), swap_id)?;
@ -218,8 +233,14 @@ async fn main() -> Result<()> {
.context("Failed to read in seed file")?;
let env_config = env::Testnet::get_config();
let bitcoin_wallet =
init_bitcoin_wallet(electrum_rpc_url, &seed, data_dir, env_config).await?;
let bitcoin_wallet = init_bitcoin_wallet(
electrum_rpc_url,
&seed,
data_dir,
env_config,
bitcoin_target_block,
)
.await?;
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
let cancel =
@ -239,6 +260,7 @@ async fn main() -> Result<()> {
swap_id,
force,
electrum_rpc_url,
bitcoin_target_block,
} => {
let data_dir = data.0;
cli::tracing::init(debug, data_dir.join("logs"), swap_id)?;
@ -248,8 +270,14 @@ async fn main() -> Result<()> {
.context("Failed to read in seed file")?;
let env_config = env::Testnet::get_config();
let bitcoin_wallet =
init_bitcoin_wallet(electrum_rpc_url, &seed, data_dir, env_config).await?;
let bitcoin_wallet = init_bitcoin_wallet(
electrum_rpc_url,
&seed,
data_dir,
env_config,
bitcoin_target_block,
)
.await?;
let resume_state = db.get_state(swap_id)?.try_into_bob()?.into();
@ -264,6 +292,7 @@ async fn init_bitcoin_wallet(
seed: &Seed,
data_dir: PathBuf,
env_config: Config,
bitcoin_target_block: usize,
) -> Result<bitcoin::Wallet> {
let wallet_dir = data_dir.join("wallet");
@ -272,7 +301,7 @@ async fn init_bitcoin_wallet(
&wallet_dir,
seed.derive_extended_private_key(env_config.bitcoin_network)?,
env_config,
6, // TODO move this into config
bitcoin_target_block,
)
.await
.context("Failed to initialize Bitcoin wallet")?;

View File

@ -15,6 +15,9 @@ const DEFAULT_ELECTRUM_RPC_URL: &str = "ssl://electrum.blockstream.info:60002";
const DEFAULT_TOR_SOCKS5_PORT: &str = "9050";
// Bitcoin transactions should be confirmed within X blocks
const DEFAUL_BITCOIN_CONFIRMATION_TARGET: &str = "3";
#[derive(structopt::StructOpt, Debug)]
#[structopt(name = "swap", about = "CLI for swapping BTC for XMR", author)]
pub struct Arguments {
@ -53,6 +56,9 @@ pub enum Command {
#[structopt(long = "tor-socks5-port", help = "Your local Tor socks5 proxy port", default_value = DEFAULT_TOR_SOCKS5_PORT)]
tor_socks5_port: u16,
#[structopt(long = "bitcoin-target-block", help = "Within how many blocks should the Bitcoin transactions be confirmed.", default_value = DEFAUL_BITCOIN_CONFIRMATION_TARGET)]
bitcoin_target_block: usize,
},
/// Show a list of past ongoing and completed swaps
History,
@ -78,6 +84,9 @@ pub enum Command {
#[structopt(long = "tor-socks5-port", help = "Your local Tor socks5 proxy port", default_value = DEFAULT_TOR_SOCKS5_PORT)]
tor_socks5_port: u16,
#[structopt(long = "bitcoin-target-block", help = "Within how many blocks should the Bitcoin transactions be confirmed.", default_value = DEFAUL_BITCOIN_CONFIRMATION_TARGET)]
bitcoin_target_block: usize,
},
/// Try to cancel an ongoing swap (expert users only)
Cancel {
@ -95,6 +104,9 @@ pub enum Command {
default_value = DEFAULT_ELECTRUM_RPC_URL
)]
electrum_rpc_url: Url,
#[structopt(long = "bitcoin-target-block", help = "Within how many blocks should the Bitcoin transactions be confirmed.", default_value = DEFAUL_BITCOIN_CONFIRMATION_TARGET)]
bitcoin_target_block: usize,
},
/// Try to cancel a swap and refund my BTC (expert users only)
Refund {
@ -112,6 +124,9 @@ pub enum Command {
default_value = DEFAULT_ELECTRUM_RPC_URL
)]
electrum_rpc_url: Url,
#[structopt(long = "bitcoin-target-block", help = "Within how many blocks should the Bitcoin transactions be confirmed.", default_value = DEFAUL_BITCOIN_CONFIRMATION_TARGET)]
bitcoin_target_block: usize,
},
}