Skip to content

Commit e8476bc

Browse files
committed
Add get_txid_at_block_index API method
1 parent 93c515c commit e8476bc

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/async.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,30 @@ impl AsyncClient {
7979
}
8080
}
8181

82+
/// Get a [`Txid`] of a transaction given its index in a block with a given hash.
83+
pub async fn get_txid_at_block_index(
84+
&self,
85+
block_hash: &BlockHash,
86+
index: usize,
87+
) -> Result<Option<Txid>, Error> {
88+
let resp = self
89+
.client
90+
.get(&format!(
91+
"{}/block/{}/txid/{}",
92+
self.url,
93+
block_hash.to_string(),
94+
index
95+
))
96+
.send()
97+
.await?;
98+
99+
if let StatusCode::NOT_FOUND = resp.status() {
100+
return Ok(None);
101+
}
102+
103+
Ok(Some(deserialize(&Vec::from_hex(&resp.text().await?)?)?))
104+
}
105+
82106
/// Get the status of a [`Transaction`] given its [`Txid`].
83107
pub async fn get_tx_status(&self, txid: &Txid) -> Result<Option<TxStatus>, Error> {
84108
let resp = self

src/blocking.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ impl BlockingClient {
8484
}
8585
}
8686

87+
/// Get a [`Txid`] of a transaction given its index in a block with a given hash.
88+
pub fn get_txid_at_block_index(
89+
&self,
90+
block_hash: &BlockHash,
91+
index: usize,
92+
) -> Result<Option<Txid>, Error> {
93+
let resp = self
94+
.agent
95+
.get(&format!(
96+
"{}/block/{}/txid/{}",
97+
self.url,
98+
block_hash.to_string(),
99+
index
100+
))
101+
.call();
102+
103+
match resp {
104+
Ok(resp) => Ok(Some(deserialize(&Vec::from_hex(&resp.into_string()?)?)?)),
105+
Err(ureq::Error::Status(code, _)) => {
106+
if is_status_not_found(code) {
107+
return Ok(None);
108+
}
109+
Err(Error::HttpResponse(code))
110+
}
111+
Err(e) => Err(Error::Ureq(e)),
112+
}
113+
}
114+
87115
/// Get the status of a [`Transaction`] given its [`Txid`].
88116
pub fn get_tx_status(&self, txid: &Txid) -> Result<Option<TxStatus>, Error> {
89117
let resp = self

0 commit comments

Comments
 (0)