diff --git a/src/api.rs b/src/api.rs index 588efcbb..227784de 100644 --- a/src/api.rs +++ b/src/api.rs @@ -103,7 +103,7 @@ pub struct BlockStatus { /// A [`Transaction`] in the format returned by Esplora. #[derive(Deserialize, Clone, Debug, PartialEq, Eq)] -pub struct Tx { +pub struct EsploraTx { /// The [`Txid`] of the [`Transaction`]. pub txid: Txid, /// The version number of the [`Transaction`]. @@ -358,9 +358,8 @@ pub struct MempoolFeesSubmitPackage { pub effective_includes: Option>, } -impl Tx { - /// Convert a transaction from the format returned by Esplora into a `rust-bitcoin` - /// [`Transaction`]. +impl EsploraTx { + /// Convert a transaction from the format returned by Esplora into a [`Transaction`]. pub fn to_tx(&self) -> Transaction { Transaction { version: transaction::Version::non_standard(self.version), @@ -391,7 +390,7 @@ impl Tx { } } - /// Get the confirmation time from a [`Tx`]. + /// Get the confirmation time from an [`EsploraTx`]. pub fn confirmation_time(&self) -> Option { match self.status { TxStatus { @@ -404,7 +403,7 @@ impl Tx { } } - /// Get a list of the [`Tx`]'s previous outputs. + /// Get a list of the [`EsploraTx`]'s previous outputs. pub fn previous_outputs(&self) -> Vec> { self.vin .iter() @@ -417,10 +416,17 @@ impl Tx { }) .collect() } +} + +impl From for Transaction { + fn from(tx: EsploraTx) -> Self { + tx.to_tx() + } +} - /// Get the fee paid by a [`Tx`]. - pub fn fee(&self) -> Amount { - self.fee +impl From<&EsploraTx> for Transaction { + fn from(tx: &EsploraTx) -> Self { + tx.to_tx() } } diff --git a/src/async.rs b/src/async.rs index 12b16d2e..39d6d165 100644 --- a/src/async.rs +++ b/src/async.rs @@ -19,8 +19,8 @@ use bitreq::{Client, Method, Proxy, Request, RequestExt, Response}; use crate::{ is_retryable, is_success, AddressStats, BlockInfo, BlockStatus, BlockSummary, Builder, Error, - MempoolRecentTx, MempoolStats, MerkleProof, OutputStatus, ScriptHashStats, SubmitPackageResult, - Tx, TxStatus, Utxo, BASE_BACKOFF_MILLIS, + EsploraTx, MempoolRecentTx, MempoolStats, MerkleProof, OutputStatus, ScriptHashStats, + SubmitPackageResult, TxStatus, Utxo, BASE_BACKOFF_MILLIS, }; /// An async client for interacting with an Esplora API server. @@ -324,7 +324,7 @@ impl AsyncClient { } /// Get transaction info given its [`Txid`]. - pub async fn get_tx_info(&self, txid: &Txid) -> Result, Error> { + pub async fn get_tx_info(&self, txid: &Txid) -> Result, Error> { self.get_opt_response_json(&format!("/tx/{txid}")).await } @@ -466,7 +466,7 @@ impl AsyncClient { &self, address: &Address, last_seen: Option, - ) -> Result, Error> { + ) -> Result, Error> { let path = match last_seen { Some(last_seen) => format!("/address/{address}/txs/chain/{last_seen}"), None => format!("/address/{address}/txs"), @@ -476,7 +476,10 @@ impl AsyncClient { } /// Get mempool [`Transaction`]s for the specified [`Address`], sorted with newest first. - pub async fn get_mempool_address_txs(&self, address: &Address) -> Result, Error> { + pub async fn get_mempool_address_txs( + &self, + address: &Address, + ) -> Result, Error> { let path = format!("/address/{address}/txs/mempool"); self.get_response_json(&path).await @@ -490,7 +493,7 @@ impl AsyncClient { &self, script: &Script, last_seen: Option, - ) -> Result, Error> { + ) -> Result, Error> { let script_hash = sha256::Hash::hash(script.as_bytes()); let path = match last_seen { Some(last_seen) => format!("/scripthash/{script_hash:x}/txs/chain/{last_seen}"), @@ -502,7 +505,10 @@ impl AsyncClient { /// Get mempool [`Transaction`] history for the /// specified [`Script`] hash, sorted with newest first. - pub async fn get_mempool_scripthash_txs(&self, script: &Script) -> Result, Error> { + pub async fn get_mempool_scripthash_txs( + &self, + script: &Script, + ) -> Result, Error> { let script_hash = sha256::Hash::hash(script.as_bytes()); let path = format!("/scripthash/{script_hash:x}/txs/mempool"); @@ -555,7 +561,7 @@ impl AsyncClient { &self, blockhash: &BlockHash, start_index: Option, - ) -> Result, Error> { + ) -> Result, Error> { let path = match start_index { None => format!("/block/{blockhash}/txs"), Some(start_index) => format!("/block/{blockhash}/txs/{start_index}"), diff --git a/src/blocking.rs b/src/blocking.rs index 9897929c..2cb0ab81 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -20,8 +20,8 @@ use bitcoin::{Address, Block, BlockHash, MerkleBlock, Script, Transaction, Txid} use crate::{ is_retryable, is_success, AddressStats, BlockInfo, BlockStatus, BlockSummary, Builder, Error, - MempoolRecentTx, MempoolStats, MerkleProof, OutputStatus, ScriptHashStats, SubmitPackageResult, - Tx, TxStatus, Utxo, BASE_BACKOFF_MILLIS, + EsploraTx, MempoolRecentTx, MempoolStats, MerkleProof, OutputStatus, ScriptHashStats, + SubmitPackageResult, TxStatus, Utxo, BASE_BACKOFF_MILLIS, }; /// A blocking client for interacting with an Esplora API server. @@ -306,7 +306,7 @@ impl BlockingClient { } /// Get transaction info given its [`Txid`]. - pub fn get_tx_info(&self, txid: &Txid) -> Result, Error> { + pub fn get_tx_info(&self, txid: &Txid) -> Result, Error> { self.get_opt_response_json(&format!("/tx/{txid}")) } @@ -462,7 +462,7 @@ impl BlockingClient { &self, address: &Address, last_seen: Option, - ) -> Result, Error> { + ) -> Result, Error> { let path = match last_seen { Some(last_seen) => format!("/address/{address}/txs/chain/{last_seen}"), None => format!("/address/{address}/txs"), @@ -472,7 +472,7 @@ impl BlockingClient { } /// Get mempool [`Transaction`]s for the specified [`Address`], sorted with newest first. - pub fn get_mempool_address_txs(&self, address: &Address) -> Result, Error> { + pub fn get_mempool_address_txs(&self, address: &Address) -> Result, Error> { let path = format!("/address/{address}/txs/mempool"); self.get_response_json(&path) @@ -486,7 +486,7 @@ impl BlockingClient { &self, script: &Script, last_seen: Option, - ) -> Result, Error> { + ) -> Result, Error> { let script_hash = sha256::Hash::hash(script.as_bytes()); let path = match last_seen { Some(last_seen) => format!("/scripthash/{script_hash:x}/txs/chain/{last_seen}"), @@ -497,7 +497,7 @@ impl BlockingClient { /// Get mempool [`Transaction`] history for the /// specified [`Script`] hash, sorted with newest first. - pub fn get_mempool_scripthash_txs(&self, script: &Script) -> Result, Error> { + pub fn get_mempool_scripthash_txs(&self, script: &Script) -> Result, Error> { let script_hash = sha256::Hash::hash(script.as_bytes()); let path = format!("/scripthash/{script_hash:x}/txs/mempool"); @@ -527,7 +527,7 @@ impl BlockingClient { &self, blockhash: &BlockHash, start_index: Option, - ) -> Result, Error> { + ) -> Result, Error> { let path = match start_index { None => format!("/block/{blockhash}/txs"), Some(start_index) => format!("/block/{blockhash}/txs/{start_index}"), diff --git a/src/lib.rs b/src/lib.rs index ac10ce92..b79bb247 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -684,7 +684,7 @@ mod test { 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_eq!(tx_info.fee, tx_res.fee.unwrap().unsigned_abs()); assert!(tx_info.status.confirmed); assert_eq!(tx_info.status.block_height, Some(tx_block_height)); assert_eq!(tx_info.status.block_hash, tx_res.block_hash);