hopr_chain_connector/connector/
values.rs1use std::time::Duration;
2
3use blokli_client::api::BlokliQueryClient;
4use futures::TryFutureExt;
5use hopr_api::chain::{ChainInfo, DomainSeparators};
6use hopr_internal_types::prelude::WinningProbability;
7use hopr_primitive_types::prelude::*;
8
9use crate::{
10 connector::{
11 HoprBlockchainConnector,
12 utils::{ParsedChainInfo, model_to_chain_info},
13 },
14 errors::ConnectorError,
15};
16
17pub(crate) const CHAIN_INFO_CACHE_KEY: u32 = 0;
18
19impl<B, C, P, R> HoprBlockchainConnector<C, R, B, P>
20where
21 C: BlokliQueryClient + Send + Sync + 'static,
22{
23 pub(crate) async fn query_cached_chain_info(&self) -> Result<ParsedChainInfo, ConnectorError> {
24 Ok(self
25 .values
26 .try_get_with(
27 CHAIN_INFO_CACHE_KEY,
28 self.client
29 .query_chain_info()
30 .map_err(ConnectorError::from)
31 .and_then(|model| futures::future::ready(model_to_chain_info(model))),
32 )
33 .await?)
34 }
35}
36
37#[async_trait::async_trait]
38impl<B, R, C, P> hopr_api::chain::ChainValues for HoprBlockchainConnector<C, B, P, R>
39where
40 B: Send + Sync,
41 C: BlokliQueryClient + Send + Sync + 'static,
42 P: Send + Sync,
43 R: Send + Sync,
44{
45 type Error = ConnectorError;
46
47 async fn domain_separators(&self) -> Result<DomainSeparators, Self::Error> {
48 self.check_connection_state()?;
49
50 Ok(self.query_cached_chain_info().await?.domain_separators)
51 }
52
53 async fn minimum_incoming_ticket_win_prob(&self) -> Result<WinningProbability, Self::Error> {
54 self.check_connection_state()?;
55
56 Ok(self.query_cached_chain_info().await?.ticket_win_prob)
57 }
58
59 async fn minimum_ticket_price(&self) -> Result<HoprBalance, Self::Error> {
60 self.check_connection_state()?;
61
62 Ok(self.query_cached_chain_info().await?.ticket_price)
63 }
64
65 async fn channel_closure_notice_period(&self) -> Result<Duration, Self::Error> {
66 self.check_connection_state()?;
67
68 Ok(self.query_cached_chain_info().await?.channel_closure_grace_period)
69 }
70
71 async fn chain_info(&self) -> Result<ChainInfo, Self::Error> {
72 self.check_connection_state()?;
73
74 Ok(self.query_cached_chain_info().await?.info)
75 }
76}