hopr_chain_types/
chain_events.rs

1//! Types that are related to events that are raised on-chain and extracted from chain logs.
2//!
3//! These events happen in response to actions (transactions, smart contract calls) done by a HOPR node on chain.
4//!
5//! See `chain-actions` and `chain-indexer` crates for details.
6use std::fmt::{Display, Formatter};
7
8use hopr_internal_types::prelude::*;
9use hopr_primitive_types::prelude::*;
10
11/// Enumeration of HOPR chain events.
12#[derive(Debug, Clone, strum::EnumTryAs, strum::EnumIs, strum::EnumDiscriminants)]
13pub enum ChainEvent {
14    /// Peer on-chain announcement event.
15    ///
16    /// The [`AccountEntry`] is guaranteed to be [announced](AccountEntry::has_announced).
17    Announcement(AccountEntry),
18    /// A new channel has been opened
19    ///
20    /// The [`ChannelEntry`] is guaranteed to be [opened](ChannelStatus::Open).
21    ChannelOpened(ChannelEntry),
22    /// Channel closure has been initiated.
23    ///
24    /// The [`ChannelEntry`] is guaranteed to be [pending to close](ChannelStatus::PendingToClose).
25    ChannelClosureInitiated(ChannelEntry),
26    /// Channel closure has been finalized.
27    ///
28    /// The [`ChannelEntry`] is guaranteed to be [closed](ChannelStatus::Closed).
29    ChannelClosed(ChannelEntry),
30    /// Channel balance has increased by an amount.
31    ///
32    /// The [`HoprBalance`] is never `0` and represents the difference from the current new balance on the
33    /// [`ChannelEntry`].
34    ChannelBalanceIncreased(ChannelEntry, HoprBalance),
35    /// Channel balance has decreased by an amount.
36    ///
37    /// The [`HoprBalance`] is never `0` and represents the difference from the current new balance on the
38    /// [`ChannelEntry`].
39    ChannelBalanceDecreased(ChannelEntry, HoprBalance),
40    /// Ticket has been redeemed on a channel.
41    ///
42    /// If the channel is a node's own, it also contains the ticket that has been redeemed.
43    TicketRedeemed(ChannelEntry, Option<Box<VerifiedTicket>>),
44
45    /// The minimum winning probability has been increased.
46    WinningProbabilityIncreased(WinningProbability),
47
48    /// The minimum winning probability has been decreased.
49    WinningProbabilityDecreased(WinningProbability),
50
51    /// A new ticket price has been set.
52    TicketPriceChanged(HoprBalance),
53}
54
55impl Display for ChainEvent {
56    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
57        match self {
58            ChainEvent::Announcement(a) => write!(f, "announcement event of {a}"),
59            ChainEvent::ChannelOpened(c) => write!(f, "open channel event {}", c.get_id()),
60            ChainEvent::ChannelClosureInitiated(c) => write!(f, "close channel initiation event {}", c.get_id()),
61            ChainEvent::ChannelClosed(c) => write!(f, "close channel event {}", c.get_id()),
62            ChainEvent::ChannelBalanceIncreased(c, _) => write!(f, "channel increase balance event {}", c.get_id()),
63            ChainEvent::ChannelBalanceDecreased(c, _) => write!(f, "channel decrease balance event {}", c.get_id()),
64            ChainEvent::TicketRedeemed(c, _) => write!(f, "ticket redeem event in channel {}", c.get_id()),
65            ChainEvent::WinningProbabilityIncreased(p) => write!(f, "winning probability increased to {p}"),
66            ChainEvent::WinningProbabilityDecreased(p) => write!(f, "winning probability decreased to {p}"),
67            ChainEvent::TicketPriceChanged(p) => write!(f, "ticket price changed to {p}"),
68        }
69    }
70}