use crate::{client::Client, error::Result};
use ev_types::v1::{
get_block_request::Identifier, store_service_client::StoreServiceClient, Block,
GetBlockRequest, GetBlockResponse, GetMetadataRequest, GetMetadataResponse, GetStateResponse,
State,
};
use tonic::Request;
pub struct StoreClient {
inner: StoreServiceClient<tonic::transport::Channel>,
}
impl StoreClient {
pub fn new(client: &Client) -> Self {
let inner = StoreServiceClient::new(client.channel().clone());
Self { inner }
}
pub async fn get_block_by_height(&self, height: u64) -> Result<Option<Block>> {
let request = Request::new(GetBlockRequest {
identifier: Some(Identifier::Height(height)),
});
let response = self.inner.clone().get_block(request).await?;
Ok(response.into_inner().block)
}
pub async fn get_block_by_hash(&self, hash: Vec<u8>) -> Result<Option<Block>> {
let request = Request::new(GetBlockRequest {
identifier: Some(Identifier::Hash(hash)),
});
let response = self.inner.clone().get_block(request).await?;
Ok(response.into_inner().block)
}
pub async fn get_state(&self) -> Result<Option<State>> {
let request = Request::new(());
let response = self.inner.clone().get_state(request).await?;
Ok(response.into_inner().state)
}
pub async fn get_metadata(&self, key: String) -> Result<Vec<u8>> {
let request = Request::new(GetMetadataRequest { key });
let response = self.inner.clone().get_metadata(request).await?;
Ok(response.into_inner().value)
}
pub async fn get_block_full_by_height(&self, height: u64) -> Result<GetBlockResponse> {
let request = Request::new(GetBlockRequest {
identifier: Some(Identifier::Height(height)),
});
let response = self.inner.clone().get_block(request).await?;
Ok(response.into_inner())
}
pub async fn get_block_full_by_hash(&self, hash: Vec<u8>) -> Result<GetBlockResponse> {
let request = Request::new(GetBlockRequest {
identifier: Some(Identifier::Hash(hash)),
});
let response = self.inner.clone().get_block(request).await?;
Ok(response.into_inner())
}
pub async fn get_state_full(&self) -> Result<GetStateResponse> {
let request = Request::new(());
let response = self.inner.clone().get_state(request).await?;
Ok(response.into_inner())
}
pub async fn get_metadata_full(&self, key: String) -> Result<GetMetadataResponse> {
let request = Request::new(GetMetadataRequest { key });
let response = self.inner.clone().get_metadata(request).await?;
Ok(response.into_inner())
}
}