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