diff --git a/swap/src/bitcoin/lock.rs b/swap/src/bitcoin/lock.rs index 43f68cee0..b0abae8ea 100644 --- a/swap/src/bitcoin/lock.rs +++ b/swap/src/bitcoin/lock.rs @@ -208,7 +208,7 @@ mod tests { #[tokio::test] async fn given_bob_sends_good_psbt_when_reconstructing_then_succeeeds() { let (A, B) = alice_and_bob(); - let wallet = WalletBuilder::new(50_000).build(); + let wallet = WalletBuilder::new(50_000).build().await; let agreed_amount = Amount::from_sat(10000); let psbt = bob_make_psbt(A, B, &wallet, agreed_amount).await; @@ -223,7 +223,7 @@ mod tests { let fees = 300; let agreed_amount = Amount::from_sat(10000); let amount = agreed_amount.to_sat() + fees; - let wallet = WalletBuilder::new(amount).build(); + let wallet = WalletBuilder::new(amount).build().await; let psbt = bob_make_psbt(A, B, &wallet, agreed_amount).await; assert_eq!( @@ -239,7 +239,7 @@ mod tests { #[tokio::test] async fn given_bob_is_sending_less_than_agreed_when_reconstructing_txlock_then_fails() { let (A, B) = alice_and_bob(); - let wallet = WalletBuilder::new(50_000).build(); + let wallet = WalletBuilder::new(50_000).build().await; let agreed_amount = Amount::from_sat(10000); let bad_amount = Amount::from_sat(5000); @@ -252,7 +252,7 @@ mod tests { #[tokio::test] async fn given_bob_is_sending_to_a_bad_output_reconstructing_txlock_then_fails() { let (A, B) = alice_and_bob(); - let wallet = WalletBuilder::new(50_000).build(); + let wallet = WalletBuilder::new(50_000).build().await; let agreed_amount = Amount::from_sat(10000); let E = eve(); diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index 8b9d61ae6..1fd050104 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -819,17 +819,18 @@ impl Client { impl EstimateFeeRate for Client { fn estimate_feerate(&self, target_block: usize) -> Result { // Get the fee rate in BTC/kvB - let fee_rate_btc_kvb = self.electrum.inner.estimate_fee(target_block)?; - // Convert to sat/vb - let fee_rate_sat_vb = Amount::from_btc(fee_rate_btc_kvb / 1_000.)?; + let btc_per_kvb = self.electrum.inner.estimate_fee(target_block)?; + let amount_per_kvb = Amount::from_btc(btc_per_kvb)?; + // Convert to sat/kwu + let amount_per_kwu = amount_per_kvb.checked_div(4).context("fee rate overflow")?; - FeeRate::from_sat_per_vb(fee_rate_sat_vb.to_sat()).ok_or(anyhow!("Fee rate overflow")) + Ok(FeeRate::from_sat_per_kwu(amount_per_kwu.to_sat())) } fn min_relay_fee(&self) -> Result { let relay_fee_btc = self.electrum.inner.relay_fee()?; - Amount::from_btc(relay_fee_btc).context("fee out of range") + Amount::from_btc(relay_fee_btc).context("relay fee out of range") } } diff --git a/swap/src/cli/api/request.rs b/swap/src/cli/api/request.rs index 2cdc97fe1..937b3a72b 100644 --- a/swap/src/cli/api/request.rs +++ b/swap/src/cli/api/request.rs @@ -11,6 +11,7 @@ use crate::network::swarm; use crate::protocol::bob::{BobState, Swap}; use crate::protocol::{bob, State}; use crate::{bitcoin, cli, monero, rpc}; +use ::bitcoin::address::NetworkUnchecked; use ::bitcoin::Txid; use ::monero::Network; use anyhow::{bail, Context as AnyContext, Result}; @@ -57,8 +58,7 @@ pub struct BuyXmrArgs { #[typeshare(serialized_as = "string")] pub seller: Multiaddr, #[typeshare(serialized_as = "Option")] - #[serde(with = "crate::bitcoin::address_serde::option")] - pub bitcoin_change_address: Option, + pub bitcoin_change_address: Option>, #[typeshare(serialized_as = "string")] pub monero_receive_address: monero::Address, } @@ -591,7 +591,9 @@ pub async fn buy_xmr( ); let bitcoin_change_address = match bitcoin_change_address { - Some(addr) => addr, + Some(addr) => addr + .require_network(bitcoin_wallet.network()) + .context("Address is not on the correct network")?, None => { let internal_wallet_address = bitcoin_wallet.new_address().await?; diff --git a/swap/src/cli/command.rs b/swap/src/cli/command.rs index 982b5a27c..1f98bc0e9 100644 --- a/swap/src/cli/command.rs +++ b/swap/src/cli/command.rs @@ -80,7 +80,8 @@ where let bitcoin_change_address = bitcoin_change_address .map(|address| bitcoin_address::validate(address, is_testnet)) - .transpose()?; + .transpose()? + .map(|address| address.into_unchecked()); let context = Arc::new( ContextBuilder::new(is_testnet) @@ -93,7 +94,7 @@ where .build() .await?, ); - + BuyXmrArgs { seller, bitcoin_change_address, diff --git a/swap/src/rpc/methods.rs b/swap/src/rpc/methods.rs index 80ff41690..6aab6a1b7 100644 --- a/swap/src/rpc/methods.rs +++ b/swap/src/rpc/methods.rs @@ -95,10 +95,10 @@ pub fn register_modules(outer_context: Context) -> Result> { params.bitcoin_change_address = params .bitcoin_change_address .map(|address| { - bitcoin_address::revalidate_network( + bitcoin_address::validate_network( address, context.config.env_config.bitcoin_network, - ) + ).map(|a| a.into_unchecked()) }) .transpose() .to_jsonrpsee_result()?;