Skip to content

Add get_txid_at_block_index API method #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
24 changes: 24 additions & 0 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ impl AsyncClient {
}
}

/// Get a [`Txid`] of a transaction given its index in a block with a given hash.
pub async fn get_txid_at_block_index(
&self,
block_hash: &BlockHash,
index: usize,
) -> Result<Option<Txid>, Error> {
let resp = self
.client
.get(&format!(
"{}/block/{}/txid/{}",
self.url,
block_hash.to_string(),
index
))
.send()
.await?;

if let StatusCode::NOT_FOUND = resp.status() {
return Ok(None);
}

Ok(Some(Txid::from_str(&resp.text().await?)?))
}

/// Get the status of a [`Transaction`] given its [`Txid`].
pub async fn get_tx_status(&self, txid: &Txid) -> Result<Option<TxStatus>, Error> {
let resp = self
Expand Down
28 changes: 28 additions & 0 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ impl BlockingClient {
}
}

/// Get a [`Txid`] of a transaction given its index in a block with a given hash.
pub fn get_txid_at_block_index(
&self,
block_hash: &BlockHash,
index: usize,
) -> Result<Option<Txid>, Error> {
let resp = self
.agent
.get(&format!(
"{}/block/{}/txid/{}",
self.url,
block_hash.to_string(),
index
))
.call();

match resp {
Ok(resp) => Ok(Some(Txid::from_str(&resp.into_string()?)?)),
Err(ureq::Error::Status(code, _)) => {
if is_status_not_found(code) {
return Ok(None);
}
Err(Error::HttpResponse(code))
}
Err(e) => Err(Error::Ureq(e)),
}
}

/// Get the status of a [`Transaction`] given its [`Txid`].
pub fn get_tx_status(&self, txid: &Txid) -> Result<Option<TxStatus>, Error> {
let resp = self
Expand Down