Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

pub use bitcoin::consensus::{deserialize, serialize};
pub use bitcoin::hex::FromHex;
use bitcoin::Weight;
pub use bitcoin::{
transaction, Amount, BlockHash, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Txid, Witness,
};
Expand Down Expand Up @@ -65,13 +66,17 @@ pub struct BlockStatus {
pub next_best: Option<BlockHash>,
}

#[derive(Deserialize, Clone, Debug)]
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Tx {
pub txid: Txid,
pub version: i32,
pub locktime: u32,
pub vin: Vec<Vin>,
pub vout: Vec<Vout>,
/// Transaction size in raw bytes (NOT virtual bytes).
pub size: usize,
/// Transaction weight units.
pub weight: u64,
pub status: TxStatus,
pub fee: u64,
}
Expand Down Expand Up @@ -147,6 +152,14 @@ impl Tx {
})
.collect()
}

pub fn weight(&self) -> Weight {
Weight::from_wu(self.weight)
}

pub fn fee(&self) -> Amount {
Amount::from_sat(self.fee)
}
}

fn deserialize_witness<'de, D>(d: D) -> Result<Vec<Vec<u8>>, D::Error>
Expand Down
20 changes: 20 additions & 0 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ impl AsyncClient {
}
}

/// Get transaction info given it's [`Txid`].
pub async fn get_tx_info(&self, txid: &Txid) -> Result<Option<Tx>, Error> {
let resp = self
.client
.get(&format!("{}/tx/{}", self.url, txid))
.send()
.await?;
if resp.status() == StatusCode::NOT_FOUND {
return Ok(None);
}
if resp.status().is_server_error() || resp.status().is_client_error() {
Err(Error::HttpResponse {
status: resp.status().as_u16(),
message: resp.text().await?,
})
} else {
Ok(Some(resp.json().await?))
}
}

/// Get a [`BlockHeader`] given a particular block hash.
pub async fn get_header_by_hash(&self, block_hash: &BlockHash) -> Result<BlockHeader, Error> {
let resp = self
Expand Down
13 changes: 12 additions & 1 deletion src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ impl BlockingClient {
}
}

fn get_request(&self, path: &str) -> Result<Request, Error> {
/// Get the underlying base URL.
pub fn url(&self) -> &str {
&self.url
}

/// Perform a raw HTTP GET request with the given URI `path`.
pub fn get_request(&self, path: &str) -> Result<Request, Error> {
let mut request = minreq::get(format!("{}{}", self.url, path));

if let Some(proxy) = &self.proxy {
Expand Down Expand Up @@ -207,6 +213,11 @@ impl BlockingClient {
self.get_response_json(&format!("/tx/{}/status", txid))
}

/// Get transaction info given it's [`Txid`].
pub fn get_tx_info(&self, txid: &Txid) -> Result<Option<Tx>, Error> {
self.get_opt_response_json(&format!("/tx/{}", txid))
}

/// Get a [`BlockHeader`] given a particular block hash.
pub fn get_header_by_hash(&self, block_hash: &BlockHash) -> Result<BlockHeader, Error> {
self.get_response_hex(&format!("/block/{}/header", block_hash))
Expand Down
54 changes: 54 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,60 @@ mod test {
assert!(tx_status.block_time.is_none());
}

#[cfg(all(feature = "blocking", feature = "async"))]
#[tokio::test]
async fn test_get_tx_info() {
let (blocking_client, async_client) = setup_clients().await;

let address = BITCOIND
.client
.get_new_address(Some("test"), Some(AddressType::Legacy))
.unwrap()
.assume_checked();
let txid = BITCOIND
.client
.send_to_address(
&address,
Amount::from_sat(1000),
None,
None,
None,
None,
None,
None,
)
.unwrap();
let _miner = MINER.lock().await;
generate_blocks_and_wait(1);

let tx_res = BITCOIND.client.get_transaction(&txid, None).unwrap();
let tx_exp = tx_res.transaction().expect("must decode");

let tx_info = blocking_client
.get_tx_info(&txid)
.unwrap()
.expect("must get tx");
let tx_info_async = async_client
.get_tx_info(&txid)
.await
.unwrap()
.expect("must get tx");
assert_eq!(tx_info, tx_info_async);
assert_eq!(tx_info.txid, txid);
assert_eq!(tx_info.to_tx(), tx_exp);
assert_eq!(tx_info.size, tx_exp.total_size());
assert_eq!(tx_info.weight(), tx_exp.weight());
assert_eq!(tx_info.fee(), tx_res.fee.unwrap().unsigned_abs());
assert!(tx_info.status.confirmed);
assert_eq!(tx_info.status.block_height, tx_res.info.blockheight);
assert_eq!(tx_info.status.block_hash, tx_res.info.blockhash);
assert_eq!(tx_info.status.block_time, tx_res.info.blocktime);

let txid = Txid::hash(b"not exist");
assert_eq!(blocking_client.get_tx_info(&txid).unwrap(), None);
assert_eq!(async_client.get_tx_info(&txid).await.unwrap(), None);
}

#[cfg(all(feature = "blocking", feature = "async"))]
#[tokio::test]
async fn test_get_header_by_hash() {
Expand Down