use crate::blob_info::BlobInfo;
use crate::errors::EigenClientError;
use super::{config::EigenConfig, sdk::RawEigenClient};
use crate::rust_eigenda_signers::{signers::private_key::Signer as PrivateKeySigner, Sign};
use async_trait::async_trait;
use std::error::Error;
use std::sync::Arc;
#[async_trait]
pub trait BlobProvider: std::fmt::Debug + Send + Sync {
async fn get_blob(
&self,
blob_id: &str,
) -> Result<Option<Vec<u8>>, Box<dyn Error + Send + Sync>>;
}
#[derive(Debug, Clone)]
pub struct EigenClient<S = PrivateKeySigner> {
pub(crate) client: Arc<RawEigenClient<S>>,
}
impl<S> EigenClient<S> {
pub async fn new(
config: EigenConfig,
signer: S,
blob_provider: Arc<dyn BlobProvider>,
) -> Result<Self, EigenClientError> {
let client = RawEigenClient::new(signer, config, blob_provider).await?;
Ok(Self {
client: Arc::new(client),
})
}
pub async fn dispatch_blob(&self, data: Vec<u8>) -> Result<String, EigenClientError>
where
S: Sign,
{
let blob_id = self.client.dispatch_blob(data).await?;
Ok(blob_id)
}
pub async fn get_inclusion_data(
&self,
blob_id: &str,
) -> Result<Option<Vec<u8>>, EigenClientError> {
let inclusion_data = self.client.get_inclusion_data(blob_id).await?;
Ok(inclusion_data)
}
pub async fn get_blob_info(&self, blob_id: &str) -> Result<Option<BlobInfo>, EigenClientError> {
self.client.get_blob_info(blob_id).await
}
pub async fn check_finality(&self, blob_id: &str) -> Result<bool, EigenClientError> {
let blob_info = self
.client
.try_get_inclusion_data(blob_id.to_string())
.await?;
Ok(blob_info.is_some())
}
pub fn blob_size_limit(&self) -> Option<usize> {
Some(RawEigenClient::<S>::blob_size_limit())
}
pub async fn get_blob(
&self,
blob_index: u32,
batch_header_hash: Vec<u8>,
) -> Result<Option<Vec<u8>>, EigenClientError> {
self.client.get_blob(blob_index, batch_header_hash).await
}
}