hopr_chain_types/
actions.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Types related to high-level actions that lead to Ethereum transactions.
//!
//! Defines an enumeration of action that can be done by a HOPR node.
//!
//! The actions eventually lead to an on-chain transaction.
//!
//! See the `chain-actions` crate for details.
use hopr_internal_types::prelude::*;
use hopr_primitive_types::prelude::*;
use std::fmt::{Display, Formatter};

/// Enumerates all possible on-chain state change requests.
///
/// An `Action` is an operation done by the HOPR node that leads
/// to an on-chain transaction or a contract call. An `Action` is considered complete
/// until the corresponding [SignificantChainEvent](crate::chain_events::SignificantChainEvent)
/// is registered by the Indexer or a timeout.
#[allow(clippy::large_enum_variant)] // TODO: Refactor the large enum variant
#[derive(Clone, PartialEq, Debug, strum::VariantNames, strum::IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum Action {
    /// Redeem the given acknowledged ticket.
    RedeemTicket(RedeemableTicket),

    /// Open channel to the given destination with the given stake
    OpenChannel(Address, Balance),

    /// Fund channel with the given ID and amount
    FundChannel(ChannelEntry, Balance),

    /// Close channel with the given source and destination
    CloseChannel(ChannelEntry, ChannelDirection),

    /// Withdraw given balance to the given address
    Withdraw(Address, Balance),

    /// Announce node on-chain
    Announce(AnnouncementData),

    /// Register safe address with this node
    RegisterSafe(Address),
}

impl Display for Action {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Action::RedeemTicket(ack) => write!(f, "redeem action of {ack}"),
            Action::OpenChannel(dst, amount) => write!(f, "open channel action to {dst} with {amount}"),
            Action::FundChannel(channel, amount) => write!(
                f,
                "fund channel action for channel from {} to {} with {amount}",
                channel.source, channel.destination
            ),
            Action::CloseChannel(channel, direction) => write!(
                f,
                "closure action of {} channel from {} to {}",
                direction, channel.source, channel.destination
            ),
            Action::Withdraw(destination, amount) => write!(f, "withdraw action of {amount} to {destination}"),
            Action::Announce(data) => write!(f, "announce action of {}", data.multiaddress()),
            Action::RegisterSafe(safe_address) => write!(f, "register safe action {safe_address}"),
        }
    }
}