hopr_api/chain/
accounts.rs1use std::fmt::Formatter;
2
3use futures::{future::BoxFuture, stream::BoxStream};
4use hopr_crypto_types::prelude::{OffchainKeypair, OffchainPublicKey};
5pub use hopr_internal_types::prelude::AccountEntry;
6use hopr_primitive_types::prelude::Address;
7pub use hopr_primitive_types::prelude::{Balance, Currency};
8pub use multiaddr::Multiaddr;
9
10use crate::chain::ChainReceipt;
11
12#[derive(Debug, strum::EnumIs, strum::EnumTryAs)]
16pub enum AnnouncementError<E> {
17 AlreadyAnnounced,
19 ProcessingError(E),
21}
22
23impl<E: std::fmt::Display> std::fmt::Display for AnnouncementError<E> {
24 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25 match self {
26 AnnouncementError::AlreadyAnnounced => f.write_str("already announced"),
27 AnnouncementError::ProcessingError(e) => write!(f, "account processing error: {e}"),
28 }
29 }
30}
31
32impl<E: std::error::Error> std::error::Error for AnnouncementError<E> {}
33
34#[async_trait::async_trait]
36pub trait ChainWriteAccountOperations {
37 type Error: std::error::Error + Send + Sync + 'static;
38
39 async fn announce(
41 &self,
42 multiaddrs: &[Multiaddr],
43 key: &OffchainKeypair,
44 ) -> Result<BoxFuture<'_, Result<ChainReceipt, Self::Error>>, AnnouncementError<Self::Error>>;
45
46 async fn withdraw<C: Currency + Send>(
48 &self,
49 balance: Balance<C>,
50 recipient: &Address,
51 ) -> Result<BoxFuture<'_, Result<ChainReceipt, Self::Error>>, Self::Error>;
52
53 async fn register_safe(
55 &self,
56 safe_address: &Address,
57 ) -> Result<BoxFuture<'_, Result<ChainReceipt, Self::Error>>, Self::Error>;
58}
59
60#[derive(Clone, Debug, PartialEq, Eq, Default)]
64pub struct AccountSelector {
65 pub public_only: bool,
67 pub chain_key: Option<Address>,
69 pub offchain_key: Option<OffchainPublicKey>,
71}
72
73#[async_trait::async_trait]
75pub trait ChainReadAccountOperations {
76 type Error: std::error::Error + Send + Sync + 'static;
77
78 async fn node_balance<C: Currency>(&self) -> Result<Balance<C>, Self::Error>;
80
81 async fn safe_balance<C: Currency>(&self) -> Result<Balance<C>, Self::Error>;
83
84 async fn safe_allowance<C: Currency>(&self) -> Result<Balance<C>, Self::Error>;
86
87 async fn find_account_by_address(&self, address: &Address) -> Result<Option<AccountEntry>, Self::Error>;
89
90 async fn find_account_by_packet_key(
92 &self,
93 packet_key: &OffchainPublicKey,
94 ) -> Result<Option<AccountEntry>, Self::Error>;
95
96 async fn check_node_safe_module_status(&self) -> Result<bool, Self::Error>;
98
99 async fn can_register_with_safe(&self, safe_address: &Address) -> Result<bool, Self::Error>;
101
102 async fn stream_accounts<'a>(
104 &'a self,
105 selector: AccountSelector,
106 ) -> Result<BoxStream<'a, AccountEntry>, Self::Error>;
107
108 async fn count_accounts(&self, selector: AccountSelector) -> Result<usize, Self::Error>;
113}