hopr_api/chain/
events.rs

1pub use hopr_chain_types::chain_events::ChainEvent;
2
3/// Indicates if the current state should be emitted in the form of events
4/// in the [subscription stream](ChainEvents::subscribe).
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
6pub enum StateSyncOptions {
7    /// The current state of channels is emitted as [`ChainEvent::ChannelOpened`] events
8    /// in the stream, preceding all the future events.
9    OpenedChannels,
10    /// The current state of **public** accounts is emitted as [`ChainEvent::Announcement`] events
11    /// in the stream, preceding all the future events of that kind.
12    PublicAccounts,
13    /// The current state of all accounts (also private ones) is emitted as [`ChainEvent::Announcement`] events
14    /// in the stream, preceding all the future events of that kind.
15    AllAccounts,
16}
17
18/// Allows subscribing to on-chain events.
19#[auto_impl::auto_impl(&, Box, Arc)]
20pub trait ChainEvents {
21    type Error: std::error::Error + Send + Sync;
22
23    /// Convenience method for subscribing to on-chain events without specifying any state sync options.
24    ///
25    /// See [`ChainEvents::subscribe_with_state_sync`].
26    fn subscribe(&self) -> Result<impl futures::Stream<Item = ChainEvent> + Send + 'static, Self::Error> {
27        self.subscribe_with_state_sync(None)
28    }
29
30    /// Subscribe to on-chain events.
31    ///
32    /// The [`options`](StateSyncOptions) specify which parts of the current state should be streamed
33    /// in the form on [`ChainEvents`](ChainEvent) before any future events are streamed.
34    ///
35    /// When an empty iterator (or simply `None`) is specified, only all future events are streamed from this point.
36    fn subscribe_with_state_sync<I: IntoIterator<Item = StateSyncOptions>>(
37        &self,
38        options: I,
39    ) -> Result<impl futures::Stream<Item = ChainEvent> + Send + 'static, Self::Error>;
40}