use std::collections::HashMap;
use std::str::FromStr;
use bpstd::hashes::{sha256, Hash};
use bpstd::{BlockHash, ScriptPubkey, Txid};
#[allow(unused_imports)]
use log::{debug, error, info, trace};
use reqwest::{Client, StatusCode};
use crate::{BlockStatus, BlockSummary, Builder, Error, OutputStatus, Tx, TxStatus};
#[derive(Debug, Clone)]
pub struct AsyncClient {
url: String,
client: Client,
}
impl AsyncClient {
pub fn from_builder(builder: Builder) -> Result<Self, Error> {
let mut client_builder = Client::builder();
#[cfg(not(target_arch = "wasm32"))]
if let Some(proxy) = &builder.proxy {
client_builder = client_builder.proxy(reqwest::Proxy::all(proxy)?);
}
#[cfg(not(target_arch = "wasm32"))]
if let Some(timeout) = builder.timeout {
client_builder = client_builder.timeout(core::time::Duration::from_secs(timeout));
}
Ok(Self::from_client(builder.base_url, client_builder.build()?))
}
pub fn from_client(url: String, client: Client) -> Self {
AsyncClient { url, client }
}
pub async fn 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, index))
.send()
.await?;
if let StatusCode::NOT_FOUND = resp.status() {
return Ok(None);
}
Ok(Some(Txid::from_str(&resp.text().await?)?))
}
pub async fn tx_status(&self, txid: &Txid) -> Result<TxStatus, Error> {
let resp = self
.client
.get(&format!("{}/tx/{}/status", self.url, txid))
.send()
.await?;
Ok(resp.error_for_status()?.json().await?)
}
pub async fn block_status(&self, block_hash: &BlockHash) -> Result<BlockStatus, Error> {
let resp = self
.client
.get(&format!("{}/block/{}/status", self.url, block_hash))
.send()
.await?;
Ok(resp.error_for_status()?.json().await?)
}
pub async fn output_status(
&self,
txid: &Txid,
index: u64,
) -> Result<Option<OutputStatus>, Error> {
let resp = self
.client
.get(&format!("{}/tx/{}/outspend/{}", self.url, txid, index))
.send()
.await?;
if let StatusCode::NOT_FOUND = resp.status() {
return Ok(None);
}
Ok(Some(resp.error_for_status()?.json().await?))
}
pub async fn height(&self) -> Result<u32, Error> {
let resp = self
.client
.get(&format!("{}/blocks/tip/height", self.url))
.send()
.await?;
Ok(resp.error_for_status()?.text().await?.parse()?)
}
pub async fn tip_hash(&self) -> Result<BlockHash, Error> {
let resp = self
.client
.get(&format!("{}/blocks/tip/hash", self.url))
.send()
.await?;
Ok(BlockHash::from_str(
&resp.error_for_status()?.text().await?,
)?)
}
pub async fn block_hash(&self, block_height: u32) -> Result<BlockHash, Error> {
let resp = self
.client
.get(&format!("{}/block-height/{}", self.url, block_height))
.send()
.await?;
if let StatusCode::NOT_FOUND = resp.status() {
return Err(Error::HeaderHeightNotFound(block_height));
}
Ok(BlockHash::from_str(
&resp.error_for_status()?.text().await?,
)?)
}
pub async fn scripthash_txs(
&self,
script: &ScriptPubkey,
last_seen: Option<Txid>,
) -> Result<Vec<Tx>, Error> {
let script_hash = sha256::Hash::hash(script.as_ref());
let url = match last_seen {
Some(last_seen) => format!(
"{}/scripthash/{:x}/txs/chain/{}",
self.url, script_hash, last_seen
),
None => format!("{}/scripthash/{:x}/txs", self.url, script_hash),
};
Ok(self
.client
.get(url)
.send()
.await?
.error_for_status()?
.json::<Vec<Tx>>()
.await?)
}
pub async fn fee_estimates(&self) -> Result<HashMap<String, f64>, Error> {
Ok(self
.client
.get(&format!("{}/fee-estimates", self.url,))
.send()
.await?
.error_for_status()?
.json::<HashMap<String, f64>>()
.await?)
}
pub async fn blocks(&self, height: Option<u32>) -> Result<Vec<BlockSummary>, Error> {
let url = match height {
Some(height) => format!("{}/blocks/{}", self.url, height),
None => format!("{}/blocks", self.url),
};
Ok(self
.client
.get(&url)
.send()
.await?
.error_for_status()?
.json()
.await?)
}
pub fn url(&self) -> &str {
&self.url
}
pub fn client(&self) -> &Client {
&self.client
}
}