ev_client/
store.rs

1use crate::{client::Client, error::Result};
2use ev_types::v1::{
3    get_block_request::Identifier, store_service_client::StoreServiceClient, Block,
4    GetBlockRequest, GetBlockResponse, GetMetadataRequest, GetMetadataResponse, GetStateResponse,
5    State,
6};
7use tonic::Request;
8
9pub struct StoreClient {
10    inner: StoreServiceClient<tonic::transport::Channel>,
11}
12
13impl StoreClient {
14    /// Create a new StoreClient from a Client
15    pub fn new(client: &Client) -> Self {
16        let inner = StoreServiceClient::new(client.channel().clone());
17        Self { inner }
18    }
19
20    /// Get a block by height
21    pub async fn get_block_by_height(&self, height: u64) -> Result<Option<Block>> {
22        let request = Request::new(GetBlockRequest {
23            identifier: Some(Identifier::Height(height)),
24        });
25        let response = self.inner.clone().get_block(request).await?;
26
27        Ok(response.into_inner().block)
28    }
29
30    /// Get a block by hash
31    pub async fn get_block_by_hash(&self, hash: Vec<u8>) -> Result<Option<Block>> {
32        let request = Request::new(GetBlockRequest {
33            identifier: Some(Identifier::Hash(hash)),
34        });
35        let response = self.inner.clone().get_block(request).await?;
36
37        Ok(response.into_inner().block)
38    }
39
40    /// Get the current state
41    pub async fn get_state(&self) -> Result<Option<State>> {
42        let request = Request::new(());
43        let response = self.inner.clone().get_state(request).await?;
44
45        Ok(response.into_inner().state)
46    }
47
48    /// Get metadata by key
49    pub async fn get_metadata(&self, key: String) -> Result<Vec<u8>> {
50        let request = Request::new(GetMetadataRequest { key });
51        let response = self.inner.clone().get_metadata(request).await?;
52
53        Ok(response.into_inner().value)
54    }
55
56    /// Get the full block response by height
57    pub async fn get_block_full_by_height(&self, height: u64) -> Result<GetBlockResponse> {
58        let request = Request::new(GetBlockRequest {
59            identifier: Some(Identifier::Height(height)),
60        });
61        let response = self.inner.clone().get_block(request).await?;
62
63        Ok(response.into_inner())
64    }
65
66    /// Get the full block response by hash
67    pub async fn get_block_full_by_hash(&self, hash: Vec<u8>) -> Result<GetBlockResponse> {
68        let request = Request::new(GetBlockRequest {
69            identifier: Some(Identifier::Hash(hash)),
70        });
71        let response = self.inner.clone().get_block(request).await?;
72
73        Ok(response.into_inner())
74    }
75
76    /// Get the full state response
77    pub async fn get_state_full(&self) -> Result<GetStateResponse> {
78        let request = Request::new(());
79        let response = self.inner.clone().get_state(request).await?;
80
81        Ok(response.into_inner())
82    }
83
84    /// Get the full metadata response
85    pub async fn get_metadata_full(&self, key: String) -> Result<GetMetadataResponse> {
86        let request = Request::new(GetMetadataRequest { key });
87        let response = self.inner.clone().get_metadata(request).await?;
88
89        Ok(response.into_inner())
90    }
91}