1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use super::{get_http, BitcoinFeed};
use crate::Error;
use async_trait::async_trait;
use clap::Parser;
use reqwest::Url;
#[derive(Parser, Debug, Clone)]
pub struct BlockstreamCli {
#[clap(long)]
blockstream_url: Option<Url>,
}
pub struct BlockstreamApi {
url: Url,
}
impl Default for BlockstreamApi {
fn default() -> Self {
Self {
url: Url::parse("https://blockstream.info/api/").unwrap(),
}
}
}
impl BlockstreamApi {
pub fn from_opts(opts: BlockstreamCli) -> Option<Self> {
opts.blockstream_url.map(Self::new)
}
pub fn new(url: Url) -> Self {
Self { url }
}
pub async fn get_fee_estimate(&self, confirmation_target: u32) -> Result<f64, Error> {
let mut url = self.url.clone();
url.set_path(&format!("{}/fee-estimates", self.url.path()));
let fee_estimate = get_http(url)
.await?
.get(&confirmation_target.to_string())
.ok_or(Error::InvalidResponse)?
.as_f64()
.ok_or(Error::InvalidResponse)?;
Ok(fee_estimate)
}
}
#[async_trait]
impl BitcoinFeed for BlockstreamApi {
async fn get_fee_estimate(&self, confirmation_target: u32) -> Result<f64, Error> {
self.get_fee_estimate(confirmation_target).await
}
}