sentrix_grpc_wasm/
client.rs1use tonic_web_wasm_client::Client as WebClient;
11
12use super::pb::{
13 get_block_request::Selector, sentrix_client::SentrixClient, BlockHeight, EventFilter,
14 GetBalanceRequest, GetBlockRequest, GetMempoolRequest, GetSupplyRequest,
15 GetValidatorSetRequest, Mempool, StreamEventsRequest, Supply, ValidatorSet,
16};
17
18pub type Inner = SentrixClient<WebClient>;
20
21#[derive(Clone)]
22pub struct SentrixGrpcClient {
23 inner: Inner,
24}
25
26impl SentrixGrpcClient {
27 pub fn new(endpoint: impl Into<String>) -> Self {
30 let transport = WebClient::new(endpoint.into());
31 Self {
32 inner: SentrixClient::new(transport),
33 }
34 }
35
36 pub async fn get_latest_block(&mut self) -> Result<super::pb::Block, tonic::Status> {
39 let req = GetBlockRequest {
40 selector: Some(Selector::Latest(true)),
41 };
42 let resp = self.inner.get_block(req).await?;
43 Ok(resp.into_inner())
44 }
45
46 pub async fn get_block_by_height(
47 &mut self,
48 height: u64,
49 ) -> Result<super::pb::Block, tonic::Status> {
50 let req = GetBlockRequest {
51 selector: Some(Selector::Height(BlockHeight { value: height })),
52 };
53 let resp = self.inner.get_block(req).await?;
54 Ok(resp.into_inner())
55 }
56
57 pub async fn get_balance(
58 &mut self,
59 addr: [u8; 20],
60 ) -> Result<super::pb::Account, tonic::Status> {
61 let req = GetBalanceRequest {
62 address: Some(super::pb::Address {
63 value: addr.to_vec(),
64 }),
65 at_height: None,
66 };
67 let resp = self.inner.get_balance(req).await?;
68 Ok(resp.into_inner())
69 }
70
71 pub async fn get_validator_set(&mut self) -> Result<ValidatorSet, tonic::Status> {
73 let req = GetValidatorSetRequest { at_height: None };
74 let resp = self.inner.get_validator_set(req).await?;
75 Ok(resp.into_inner())
76 }
77
78 pub async fn get_supply(&mut self) -> Result<Supply, tonic::Status> {
80 let req = GetSupplyRequest { at_height: None };
81 let resp = self.inner.get_supply(req).await?;
82 Ok(resp.into_inner())
83 }
84
85 pub async fn get_mempool(&mut self, limit: u32) -> Result<Mempool, tonic::Status> {
90 let req = GetMempoolRequest { limit };
91 let resp = self.inner.get_mempool(req).await?;
92 Ok(resp.into_inner())
93 }
94
95 pub async fn subscribe_events(
99 &mut self,
100 filters: Vec<EventFilter>,
101 ) -> Result<tonic::Streaming<super::pb::ChainEvent>, tonic::Status> {
102 let req = StreamEventsRequest {
103 filters: filters.into_iter().map(|f| f as i32).collect(),
104 from_sequence: 0,
105 };
106 let resp = self.inner.stream_events(req).await?;
107 Ok(resp.into_inner())
108 }
109}
110
111pub fn hash_short(h: &super::pb::Hash) -> String {
115 if h.value.len() != 32 {
116 return "—".into();
117 }
118 let hex = hex::encode(&h.value);
119 format!("{}…{}", &hex[..6], &hex[hex.len() - 4..])
120}