hopr_chain_types/
actions.rs

1//! Types related to high-level actions that lead to Ethereum transactions.
2//!
3//! Defines an enumeration of action that can be done by a HOPR node.
4//!
5//! The actions eventually lead to an on-chain transaction.
6//!
7//! See the `chain-actions` crate for details.
8use hopr_internal_types::prelude::*;
9use hopr_primitive_types::prelude::*;
10use std::fmt::{Display, Formatter};
11
12/// Enumerates all possible on-chain state change requests.
13///
14/// An `Action` is an operation done by the HOPR node that leads
15/// to an on-chain transaction or a contract call. An `Action` is considered complete
16/// until the corresponding [SignificantChainEvent](crate::chain_events::SignificantChainEvent)
17/// is registered by the Indexer or a timeout.
18#[allow(clippy::large_enum_variant)] // TODO: Refactor the large enum variant
19#[derive(Clone, PartialEq, Debug, strum::VariantNames, strum::IntoStaticStr)]
20#[strum(serialize_all = "snake_case")]
21pub enum Action {
22    /// Redeem the given acknowledged ticket.
23    RedeemTicket(RedeemableTicket),
24
25    /// Open channel to the given destination with the given stake
26    OpenChannel(Address, Balance),
27
28    /// Fund channel with the given ID and amount
29    FundChannel(ChannelEntry, Balance),
30
31    /// Close channel with the given source and destination
32    CloseChannel(ChannelEntry, ChannelDirection),
33
34    /// Withdraw given balance to the given address
35    Withdraw(Address, Balance),
36
37    /// Announce node on-chain
38    Announce(AnnouncementData),
39
40    /// Register safe address with this node
41    RegisterSafe(Address),
42}
43
44impl Display for Action {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        match self {
47            Action::RedeemTicket(ack) => write!(f, "redeem action of {ack}"),
48            Action::OpenChannel(dst, amount) => write!(f, "open channel action to {dst} with {amount}"),
49            Action::FundChannel(channel, amount) => write!(
50                f,
51                "fund channel action for channel from {} to {} with {amount}",
52                channel.source, channel.destination
53            ),
54            Action::CloseChannel(channel, direction) => write!(
55                f,
56                "closure action of {} channel from {} to {}",
57                direction, channel.source, channel.destination
58            ),
59            Action::Withdraw(destination, amount) => write!(f, "withdraw action of {amount} to {destination}"),
60            Action::Announce(data) => write!(f, "announce action of {}", data.multiaddress()),
61            Action::RegisterSafe(safe_address) => write!(f, "register safe action {safe_address}"),
62        }
63    }
64}