#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
use web_time::{SystemTime, UNIX_EPOCH};
mod indexed;
use std::fmt;
use super::wire;
use crate::{
chains::cardano::{BalanceReader, UtxoReader},
config::HttpConfig,
domain::{
Source, Timestamp,
cardano::{
AddressBalance, Context, Network, Observation, Operation, Order, PageRequest,
PaymentAddress, UtxoPage,
},
},
error::{Error, ValidationError},
transport::{HttpClient, OperationBudget},
};
#[derive(Clone, Debug)]
pub struct BlockfrostHttpConfig {
http: HttpConfig,
network: Network,
}
impl BlockfrostHttpConfig {
#[must_use]
pub const fn new(network: Network, http: HttpConfig) -> Self {
Self { http, network }
}
#[must_use]
pub const fn http_config(&self) -> &HttpConfig {
&self.http
}
#[must_use]
pub const fn network(&self) -> &Network {
&self.network
}
}
pub struct BlockfrostClient {
config: BlockfrostHttpConfig,
http: HttpClient,
}
impl fmt::Debug for BlockfrostClient {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("BlockfrostClient")
.field("config", &self.config)
.finish_non_exhaustive()
}
}
impl BlockfrostClient {
pub async fn connect(config: BlockfrostHttpConfig) -> Result<Self, Error> {
let budget = OperationBudget::new(config.http_config().limits())?;
if config.http_config().limits().max_response_bytes() > 2 * 1024 * 1024 {
return Err(Error::Configuration);
}
let http = HttpClient::new(config.http_config())?;
let value = Self { config, http };
budget.run(value.verify_network(&budget)).await?;
Ok(value)
}
#[must_use]
pub const fn config(&self) -> &BlockfrostHttpConfig {
&self.config
}
#[must_use]
pub const fn network(&self) -> &Network {
self.config.network()
}
async fn verify_network(&self, budget: &OperationBudget) -> Result<(), Error> {
let bytes = self
.http
.read(&["genesis"], &[], None, budget)
.await?
.into_success()?;
wire::genesis(&bytes, self.network().identity())
}
pub async fn get_balance(
&self,
address: PaymentAddress,
) -> Result<Observation<AddressBalance>, Error> {
if !address.is_compatible_with(self.network().identity()) {
return Err(ValidationError::NetworkMismatch.into());
}
let budget = OperationBudget::new(self.config.http_config().limits())?;
budget
.run(async {
self.verify_network(&budget).await?;
let response = self
.http
.read(&["addresses", address.as_str()], &[], None, &budget)
.await?;
if response.status.as_u16() == 404 {
return Err(Error::UnavailableData);
}
let bytes = response.into_success()?;
let value = wire::balance(&bytes, self.network(), &address)?;
let context = self.context(Operation::Balance)?;
Observation::balance(value, context)
})
.await
}
pub async fn get_utxos(
&self,
address: PaymentAddress,
page: PageRequest,
) -> Result<Observation<UtxoPage>, Error> {
if !address.is_compatible_with(self.network().identity()) {
return Err(ValidationError::NetworkMismatch.into());
}
let budget = OperationBudget::new(self.config.http_config().limits())?;
budget
.run(async {
self.verify_network(&budget).await?;
let number = page.page().to_string();
let count = page.count().to_string();
let order = match page.order() {
Order::Asc => "asc",
Order::Desc => "desc",
};
let response = self
.http
.read(
&["addresses", address.as_str(), "utxos"],
&[("page", &number), ("count", &count), ("order", order)],
None,
&budget,
)
.await?;
if response.status.as_u16() == 404 {
return Err(Error::UnavailableData);
}
let bytes = response.into_success()?;
let value = wire::utxos(&bytes, self.network(), &address, page)?;
Observation::utxos(value, self.context(Operation::Utxos)?)
})
.await
}
fn context(&self, operation: Operation) -> Result<Context, Error> {
let seconds = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|_| Error::Configuration)?
.as_secs();
let method = match operation {
Operation::Balance | Operation::AddressDetails => "addresses",
Operation::Utxos => "addresses-utxos",
Operation::AddressTransactions => "addresses-transactions",
Operation::NetworkData => "network",
Operation::Epoch => "epochs",
Operation::ProtocolParameters => "epochs-parameters",
Operation::StakeAccount => "accounts",
Operation::Rewards => "accounts-rewards",
Operation::Assets => "assets",
Operation::AssetDetails => "assets-asset",
Operation::AssetTransactions => "assets-transactions",
Operation::AssetHolders => "assets-addresses",
Operation::Transaction => "txs-and-cbor",
Operation::TransactionUtxos => "txs-utxos",
Operation::TransactionStatus => "txs-status-and-cbor",
Operation::PaymentEstimate => "epochs-parameters-ordinary-estimate",
Operation::Submission => "tx-submit",
};
let source = Source::new(
self.config.http_config().provider_id(),
method,
env!("CARGO_PKG_VERSION"),
)?;
Ok(Context::new(
operation,
self.network().clone(),
source,
Timestamp::from_unix_seconds(seconds),
))
}
}
impl BalanceReader for BlockfrostClient {
fn network(&self) -> &Network {
Self::network(self)
}
async fn get_balance(
&self,
address: PaymentAddress,
) -> Result<Observation<AddressBalance>, Error> {
Self::get_balance(self, address).await
}
}
impl UtxoReader for BlockfrostClient {
fn network(&self) -> &Network {
Self::network(self)
}
async fn get_utxos(
&self,
address: PaymentAddress,
page: PageRequest,
) -> Result<Observation<UtxoPage>, Error> {
Self::get_utxos(self, address, page).await
}
}