diff --git a/src/boltz/api.rs b/src/boltz/api.rs index 8fe4d6f..4025c04 100644 --- a/src/boltz/api.rs +++ b/src/boltz/api.rs @@ -13,7 +13,9 @@ pub struct Client { impl Client { pub fn new(endpoint: String) -> Self { - Client { endpoint } + Client { + endpoint: crate::utils::string::trim_suffix(endpoint, '/'), + } } pub async fn send_raw_transaction(&self, hex: String) -> Result> { @@ -65,6 +67,12 @@ mod client_test { const ENDPOINT: &str = "https://api.testnet.boltz.exchange/v2"; + #[test] + fn test_trim_suffix() { + assert_eq!(Client::new(ENDPOINT.to_string() + "/").endpoint, ENDPOINT); + assert_eq!(Client::new(ENDPOINT.to_string()).endpoint, ENDPOINT); + } + #[tokio::test] async fn test_send_raw_transaction_error_handling() { let client = Client::new(ENDPOINT.to_string()); diff --git a/src/chain/esplora.rs b/src/chain/esplora.rs index 7e5160e..2becefb 100644 --- a/src/chain/esplora.rs +++ b/src/chain/esplora.rs @@ -39,12 +39,6 @@ impl EsploraClient { max_reqs_per_second: u64, boltz_endpoint: String, ) -> Result> { - // TODO: also do in boltz - let trimmed_endpoint = match endpoint.strip_suffix('/') { - Some(s) => s.to_string(), - None => endpoint, - }; - let (tx_sender, tx_receiver) = crossbeam_channel::bounded::(1); let (block_sender, block_receiver) = crossbeam_channel::unbounded::(); @@ -79,7 +73,7 @@ impl EsploraClient { boltz_client, poll_interval, block_receiver, - endpoint: trimmed_endpoint, + endpoint: crate::utils::string::trim_suffix(endpoint, '/'), }) } diff --git a/src/main.rs b/src/main.rs index af3d1a0..94f60ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ mod boltz; mod chain; mod claimer; mod db; +mod utils; pub mod built_info { include!(concat!(env!("OUT_DIR"), "/built.rs")); diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..d245b85 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod string; diff --git a/src/utils/string.rs b/src/utils/string.rs new file mode 100644 index 0000000..4763434 --- /dev/null +++ b/src/utils/string.rs @@ -0,0 +1,6 @@ +pub fn trim_suffix(string: String, suffix: char) -> String { + match string.strip_suffix(suffix) { + Some(s) => s.to_string(), + None => string, + } +}