From 1ddab6818871e4b130c7016e2dc583f6db60eeb5 Mon Sep 17 00:00:00 2001 From: Mariusz Klochowicz Date: Mon, 29 Nov 2021 14:24:54 +1030 Subject: [PATCH] Extract code generating mempool.space URLs to avoid duplication --- daemon/src/routes_taker.rs | 11 ++--------- daemon/src/tx.rs | 17 +++++++++++------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/daemon/src/routes_taker.rs b/daemon/src/routes_taker.rs index 0fe9513..475b91c 100644 --- a/daemon/src/routes_taker.rs +++ b/daemon/src/routes_taker.rs @@ -5,7 +5,7 @@ use daemon::model::{Leverage, Price, Usd, WalletInfo}; use daemon::projection::{CfdAction, Feeds}; use daemon::routes::EmbeddedFileExt; use daemon::to_sse_event::{CfdsWithAuxData, ToSseEvent}; -use daemon::{bitmex_price_feed, monitor, oracle, taker_cfd, wallet}; +use daemon::{bitmex_price_feed, monitor, oracle, taker_cfd, tx, wallet}; use http_api_problem::{HttpApiProblem, StatusCode}; use rocket::http::{ContentType, Status}; use rocket::response::stream::EventStream; @@ -263,12 +263,5 @@ pub async fn post_withdraw_request( .detail(e.to_string()) })?; - let url = match network.inner() { - Network::Bitcoin => format!("https://mempool.space/tx/{}", txid), - Network::Testnet => format!("https://mempool.space/testnet/tx/{}", txid), - Network::Signet => format!("https://mempool.space/signet/tx/{}", txid), - Network::Regtest => txid.to_string(), - }; - - Ok(url) + Ok(tx::to_mempool_url(txid, *network.inner())) } diff --git a/daemon/src/tx.rs b/daemon/src/tx.rs index 8d452b0..b404552 100644 --- a/daemon/src/tx.rs +++ b/daemon/src/tx.rs @@ -9,16 +9,21 @@ pub struct TxUrl { pub url: String, } +/// Construct a mempool.space URL for a given txid +pub fn to_mempool_url(txid: Txid, network: Network) -> String { + match network { + Network::Bitcoin => format!("https://mempool.space/tx/{}", txid), + Network::Testnet => format!("https://mempool.space/testnet/tx/{}", txid), + Network::Signet => format!("https://mempool.space/signet/tx/{}", txid), + Network::Regtest => txid.to_string(), + } +} + impl TxUrl { pub fn new(txid: Txid, network: Network, label: TxLabel) -> Self { Self { label, - url: match network { - Network::Bitcoin => format!("https://mempool.space/tx/{}", txid), - Network::Testnet => format!("https://mempool.space/testnet/tx/{}", txid), - Network::Signet => format!("https://mempool.space/signet/tx/{}", txid), - Network::Regtest => txid.to_string(), - }, + url: to_mempool_url(txid, network), } } }