Skip to content

Commit b9908f4

Browse files
authored
Merge pull request #3 from rustaceanrob/26-1-25-taproot-dn-client
Add `estimate_smart_fee` endpoint
2 parents 11c5cfc + 63385f2 commit b9908f4

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/lib.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#![warn(missing_docs)]
33
use std::{borrow::Cow, io::Cursor, net::SocketAddr};
44

5-
use bitcoin::{Block, BlockHash, bip158::BlockFilter, block::Header, consensus::Decodable};
5+
use bitcoin::{
6+
Block, BlockHash, FeeRate, bip158::BlockFilter, block::Header, consensus::Decodable,
7+
};
68
use models::{Html, ServerStatus, TapTweaks};
79

810
/// Errors that may occur when querying.
@@ -19,8 +21,8 @@ pub struct Endpoint<'e>(Cow<'e, str>);
1921
impl<'e> Endpoint<'e> {
2022
/// The original `block-dn` server hosted at `block-dn.org`.
2123
pub const BLOCK_DN_ORG: Self = Self(Cow::Borrowed("https://block-dn.org"));
22-
// Taproot-specific filters hosted by `2140.dev`.
23-
// pub const TAPROOT_DN: Self = Self(Cow::Borrowed("https://taprootdn.xyz"));
24+
/// Server with additional capabilities hosted by `2140.dev`.
25+
pub const DEV_2140: Self = Self(Cow::Borrowed("https://taprootdn.xyz"));
2426
/// Local host at port 8080.
2527
pub const LOCAL_HOST: Self = Self(Cow::Borrowed("https://127.0.0.1:8080"));
2628

@@ -170,6 +172,23 @@ impl<'e> Client<'e> {
170172
let block = bitcoin::consensus::deserialize::<Block>(response.as_bytes())?;
171173
Ok(block)
172174
}
175+
176+
/// Get the fee rate estimated for inclusion in the target block. Only available on
177+
/// [`Endpoint::DEV_2140`].
178+
pub fn estimate_smart_fee(&self, conf_target: u32) -> Result<FeeRate, Error> {
179+
let route = self
180+
.endpoint
181+
.append_route(format!("fees/estimate-fee/{conf_target}"));
182+
let response = bitreq::get(route).with_timeout(self.timeout.0).send()?;
183+
let sats_vb: [u8; 8] = response.as_bytes().try_into().map_err(|_| {
184+
Error::Decoder(bitcoin::consensus::encode::Error::ParseFailed(
185+
"cannot fit response into 8 byte little endian.",
186+
))
187+
})?;
188+
Ok(FeeRate::from_sat_per_vb_unchecked(u64::from_le_bytes(
189+
sats_vb,
190+
)))
191+
}
173192
}
174193

175194
#[cfg(test)]

tests/client.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use block_dn_client::{Builder, Client, Timeout};
1+
use block_dn_client::{Builder, Client, Endpoint, Timeout};
22

33
fn default_client() -> Client<'static> {
44
Builder::default().build()
@@ -50,3 +50,9 @@ fn test_block() {
5050
.is_ok()
5151
)
5252
}
53+
54+
#[test]
55+
fn test_estimate_fee() {
56+
let client = Builder::new().endpoint(Endpoint::DEV_2140).build();
57+
assert!(client.estimate_smart_fee(1).is_ok());
58+
}

0 commit comments

Comments
 (0)