diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index 2c5751f1..7b590007 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -186,7 +186,7 @@ impl GetRawTransaction for Wallet { #[async_trait] impl GetBlockHeight for Wallet { async fn get_block_height(&self) -> Result { - let url = self.http_url.join("blocks/tip/height")?; + let url = blocks_tip_height_url(&self.http_url)?; let height = retry(ConstantBackoff::new(Duration::from_secs(1)), || async { let height = reqwest::Client::new() .request(Method::GET, url.clone()) @@ -210,8 +210,7 @@ impl GetBlockHeight for Wallet { #[async_trait] impl TransactionBlockHeight for Wallet { async fn transaction_block_height(&self, txid: Txid) -> Result { - let url = self.http_url.join(&format!("tx/{}/status", txid))?; - + let url = tx_status_url(txid, &self.http_url)?; #[derive(Serialize, Deserialize, Debug, Clone)] struct TransactionStatus { block_height: Option, @@ -278,3 +277,42 @@ impl GetNetwork for Wallet { self.inner.lock().await.network() } } + +fn tx_status_url(txid: Txid, base_url: &Url) -> Result { + let url = base_url.join(&format!("tx/{}/status", txid))?; + Ok(url) +} + +fn blocks_tip_height_url(base_url: &Url) -> Result { + let url = base_url.join("blocks/tip/height")?; + Ok(url) +} + +#[cfg(test)] +mod tests { + use crate::{ + bitcoin::{ + wallet::{blocks_tip_height_url, tx_status_url}, + Txid, + }, + cli::config::DEFAULT_ELECTRUM_HTTP_URL, + }; + use reqwest::Url; + + #[test] + fn create_tx_status_url_from_default_base_url_success() { + let txid: Txid = Txid::default(); + let base_url = Url::parse(DEFAULT_ELECTRUM_HTTP_URL).expect("Could not parse url"); + let url = tx_status_url(txid, &base_url).expect("Could not create url"); + let expected = format!("https://blockstream.info/testnet/api/tx/{}/status", txid); + assert_eq!(url.as_str(), expected); + } + + #[test] + fn create_block_tip_height_url_from_default_base_url_success() { + let base_url = Url::parse(DEFAULT_ELECTRUM_HTTP_URL).expect("Could not parse url"); + let url = blocks_tip_height_url(&base_url).expect("Could not create url"); + let expected = "https://blockstream.info/testnet/api/blocks/tip/height"; + assert_eq!(url.as_str(), expected); + } +} diff --git a/swap/src/cli/config.rs b/swap/src/cli/config.rs index 1d04f917..2252f181 100644 --- a/swap/src/cli/config.rs +++ b/swap/src/cli/config.rs @@ -11,7 +11,7 @@ use std::{ use tracing::info; use url::Url; -const DEFAULT_ELECTRUM_HTTP_URL: &str = "https://blockstream.info/testnet/api/"; +pub const DEFAULT_ELECTRUM_HTTP_URL: &str = "https://blockstream.info/testnet/api/"; 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";