Skip to content

Commit f5d3df3

Browse files
Add documentation
Add documentation for the library, the blocking client and the non-blocking client.
1 parent c0c2451 commit f5d3df3

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

src/async.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct AsyncClient {
3232
}
3333

3434
impl AsyncClient {
35+
/// build an async client from a builder
3536
pub fn from_builder(builder: Builder) -> Result<Self, Error> {
3637
let mut client_builder = Client::builder();
3738

@@ -48,10 +49,12 @@ impl AsyncClient {
4849
Ok(Self::from_client(builder.base_url, client_builder.build()?))
4950
}
5051

52+
/// build an async client from the base url and [`Client`]
5153
pub fn from_client(url: String, client: Client) -> Self {
5254
AsyncClient { url, client }
5355
}
5456

57+
/// Get a [`Transaction`] option given its [`Txid`]
5558
pub async fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
5659
let resp = self
5760
.client
@@ -66,6 +69,7 @@ impl AsyncClient {
6669
Ok(Some(deserialize(&resp.error_for_status()?.bytes().await?)?))
6770
}
6871

72+
/// Get a [`Transaction`] given its [`Txid`]
6973
pub async fn get_tx_no_opt(&self, txid: &Txid) -> Result<Transaction, Error> {
7074
match self.get_tx(txid).await {
7175
Ok(Some(tx)) => Ok(tx),
@@ -74,6 +78,7 @@ impl AsyncClient {
7478
}
7579
}
7680

81+
/// Get a [`BlockHeader`] given a particular block height.
7782
pub async fn get_header(&self, block_height: u32) -> Result<BlockHeader, Error> {
7883
let resp = self
7984
.client
@@ -99,6 +104,7 @@ impl AsyncClient {
99104
Ok(header)
100105
}
101106

107+
/// Broadcast a [`Transaction`] to Esplora
102108
pub async fn broadcast(&self, transaction: &Transaction) -> Result<(), Error> {
103109
self.client
104110
.post(&format!("{}/tx", self.url))
@@ -110,6 +116,7 @@ impl AsyncClient {
110116
Ok(())
111117
}
112118

119+
/// Get the current height of the blockchain tip
113120
pub async fn get_height(&self) -> Result<u32, Error> {
114121
let req = self
115122
.client
@@ -120,6 +127,9 @@ impl AsyncClient {
120127
Ok(req.error_for_status()?.text().await?.parse()?)
121128
}
122129

130+
/// Get confirmed transaction history for the specified address/scripthash,
131+
/// sorted with newest first. Returns 25 transactions per page.
132+
/// More can be requested by specifying the last txid seen by the previous query.
123133
pub async fn scripthash_txs(
124134
&self,
125135
script: &Script,
@@ -143,6 +153,8 @@ impl AsyncClient {
143153
.await?)
144154
}
145155

156+
/// Get an map where the key is the confirmation target (in number of blocks)
157+
/// and the value is the estimated feerate (in sat/vB).
146158
pub async fn get_fee_estimates(&self) -> Result<HashMap<String, f64>, Error> {
147159
Ok(self
148160
.client

src/blocking.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct BlockingClient {
3535
}
3636

3737
impl BlockingClient {
38+
/// build a blocking client from a [`Builder`]
3839
pub fn from_builder(builder: Builder) -> Result<Self, Error> {
3940
let mut agent_builder = ureq::AgentBuilder::new();
4041

@@ -49,10 +50,12 @@ impl BlockingClient {
4950
Ok(Self::from_agent(builder.base_url, agent_builder.build()))
5051
}
5152

53+
/// build a blocking client from an [`Agent`]
5254
pub fn from_agent(url: String, agent: Agent) -> Self {
5355
BlockingClient { url, agent }
5456
}
5557

58+
/// Get a [`Transaction`] option given its [`Txid`]
5659
pub fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
5760
let resp = self
5861
.agent
@@ -71,6 +74,7 @@ impl BlockingClient {
7174
}
7275
}
7376

77+
/// Get a [`Transaction`] given its [`Txid`]
7478
pub fn get_tx_no_opt(&self, txid: &Txid) -> Result<Transaction, Error> {
7579
match self.get_tx(txid) {
7680
Ok(Some(tx)) => Ok(tx),
@@ -79,6 +83,7 @@ impl BlockingClient {
7983
}
8084
}
8185

86+
/// Get a [`BlockHeader`] given a particular block height.
8287
pub fn get_header(&self, block_height: u32) -> Result<BlockHeader, Error> {
8388
let resp = self
8489
.agent
@@ -106,6 +111,7 @@ impl BlockingClient {
106111
}
107112
}
108113

114+
/// Broadcast a [`Transaction`] to Esplora
109115
pub fn broadcast(&self, transaction: &Transaction) -> Result<(), Error> {
110116
let resp = self
111117
.agent
@@ -119,6 +125,7 @@ impl BlockingClient {
119125
}
120126
}
121127

128+
/// Get the current height of the blockchain tip
122129
pub fn get_height(&self) -> Result<u32, Error> {
123130
let resp = self
124131
.agent
@@ -132,6 +139,8 @@ impl BlockingClient {
132139
}
133140
}
134141

142+
/// Get an map where the key is the confirmation target (in number of blocks)
143+
/// and the value is the estimated feerate (in sat/vB).
135144
pub fn get_fee_estimates(&self) -> Result<HashMap<String, f64>, Error> {
136145
let resp = self
137146
.agent
@@ -150,6 +159,9 @@ impl BlockingClient {
150159
Ok(map)
151160
}
152161

162+
/// Get confirmed transaction history for the specified address/scripthash,
163+
/// sorted with newest first. Returns 25 transactions per page.
164+
/// More can be requested by specifying the last txid seen by the previous query.
153165
pub fn scripthash_txs(
154166
&self,
155167
script: &Script,

src/lib.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
1-
//! Esplora
1+
//! An extensible blocking/async Esplora client
22
//!
3-
//! This module defines a [`Builder`] struct that can create a blocking or
4-
//! async Esplora client to query an Esplora backend:
3+
//! This library provides an extensible blocking and
4+
//! async Esplora client to query Esplora's backend.
55
//!
6-
//! ## Examples
6+
//! The library provides the possibility to build a blocking
7+
//! client using [`ureq`] and an async client using [`reqwest`].
8+
//! The library supports communicating to Esplora via a proxy
9+
//! and also using TLS (SSL) for secure communication.
10+
//!
11+
//!
12+
//! ## Usage
13+
//!
14+
//! You can create a blocking client as follows:
715
//!
816
//! ```no_run
9-
//! # use esplora_client::Builder;
17+
//! use esplora_client::Builder;
1018
//! let builder = Builder::new("https://blockstream.info/testnet/api");
1119
//! let blocking_client = builder.build_blocking();
1220
//! # Ok::<(), esplora_client::Error>(());
1321
//! ```
22+
//!
23+
//! Here is an example of how to create an asynchronous client.
24+
//!
1425
//! ```no_run
15-
//! # use esplora_client::Builder;
26+
//! use esplora_client::Builder;
1627
//! let builder = Builder::new("https://blockstream.info/testnet/api");
1728
//! let async_client = builder.build_async();
1829
//! # Ok::<(), esplora_client::Error>(());
1930
//! ```
2031
//!
21-
//! Esplora client can use either `ureq` or `reqwest` for the HTTP client
22-
//! depending on your needs (blocking or async respectively).
32+
//! ## Features
33+
//!
34+
//! By default the library enables all features. To specify
35+
//! specific features, set `default-features` to `false` in your `Cargo.toml`
36+
//! and specify the features you want. This will look like this:
37+
//!
38+
//! `esplora_client = { version = "*", default-features = false, features = ["blocking"] }`
39+
//!
40+
//! * `blocking` enables [`ureq`], the blocking client with proxy and TLS (SSL) capabilities.
41+
//! * `async` enables [`reqwest`], the async client with proxy capabilities.
42+
//! * `async-https` enables [`reqwest`], the async client with support for proxying and TLS (SSL).
43+
//!
2344
//!
24-
//! Please note, to configure the Esplora HTTP client correctly use one of:
25-
//! Blocking: --features='blocking'
26-
//! Async: --features='async'
2745
use std::collections::HashMap;
2846
use std::fmt;
2947
use std::io;
@@ -44,6 +62,8 @@ pub use blocking::BlockingClient;
4462
#[cfg(any(feature = "async", feature = "async-https"))]
4563
pub use r#async::AsyncClient;
4664

65+
/// Get a fee value in sats/vbytes from the estimates
66+
/// that matches the confirmation target set as parameter.
4767
pub fn convert_fee_rate(target: usize, estimates: HashMap<String, f64>) -> Result<f32, Error> {
4868
let fee_val = {
4969
let mut pairs = estimates
@@ -79,6 +99,7 @@ pub struct Builder {
7999
}
80100

81101
impl Builder {
102+
/// Instantiate a new builder
82103
pub fn new(base_url: &str) -> Self {
83104
Builder {
84105
base_url: base_url.to_string(),
@@ -87,28 +108,32 @@ impl Builder {
87108
}
88109
}
89110

111+
/// Set the proxy of the builder
90112
pub fn proxy(mut self, proxy: &str) -> Self {
91113
self.proxy = Some(proxy.to_string());
92114
self
93115
}
94116

117+
/// Set the timeout of the builder
95118
pub fn timeout(mut self, timeout: u64) -> Self {
96119
self.timeout = Some(timeout);
97120
self
98121
}
99122

123+
/// build a blocking client from builder
100124
#[cfg(feature = "blocking")]
101125
pub fn build_blocking(self) -> Result<BlockingClient, Error> {
102126
BlockingClient::from_builder(self)
103127
}
104128

129+
// build an asynchronous client from builder
105130
#[cfg(feature = "async")]
106131
pub fn build_async(self) -> Result<AsyncClient, Error> {
107132
AsyncClient::from_builder(self)
108133
}
109134
}
110135

111-
/// Errors that can happen during a sync with [`EsploraBlockchain`]
136+
/// Errors that can happen during a sync with `Esplora`
112137
#[derive(Debug)]
113138
pub enum Error {
114139
/// Error during ureq HTTP request

0 commit comments

Comments
 (0)