hopr_strategy/
lib.rs

1//! This crate contains all the Strategies for HOPRd.
2//! Strategies are vital for (partial) automation of ticket and HOPR channel operations
3//! during node runtime.
4//!
5//! - [passive strategy](crate::strategy::MultiStrategy)
6//! - [promiscuous strategy](crate::promiscuous)
7//! - [auto funding strategy](crate::auto_funding)
8//! - [auto redeeming strategy](crate::auto_redeeming)
9//! - [aggregating strategy](crate::aggregating)
10//! - [multiple strategy chains](crate::strategy)
11//!
12//! HOPRd can be configured to use any of the above strategies.
13//!
14//! ## Configuring strategies in HOPRd
15//!
16//! There are two ways of configuring strategies in HOPRd: via CLI and via a YAML config file.
17//!
18//! The configuration through CLI allows only fairly primitive single-strategy setting, through the `defaultStrategy`
19//! parameter. It can be set to any of the above strategies, however, the strategy parameters are not further
20//! configurable via the CLI and will always have their default values.
21//! In addition, if the ` disableTicketAutoRedeem ` CLI argument is `false`, the default Auto Redeem strategy is added
22//! to the strategy configured via the `defaultStrategy` argument (they execute together as Multi strategy).
23//!
24//! For more complex strategy configurations, the YAML configuration method is recommended via the `strategy` YAML
25//! section. In this case, the top-most strategy is always assumed to be Multi strategy:
26//!
27//! ```yaml
28//! strategy:
29//!   on_fail_continue: true
30//!   allow_recursive: true
31//!   execution_interval: 60
32//!   strategies:
33//!     - !Promiscuous
34//!       max_channels: 50
35//!       new_channel_stake: 20
36//!     - !AutoFunding
37//!       funding_amount: 20
38//!     - !Aggregating:
39//!       aggregation_threshold: 1000
40//! ```
41
42use std::str::FromStr;
43
44use hopr_primitive_types::prelude::*;
45use serde::{Deserialize, Serialize};
46use strum::{Display, EnumString, VariantNames};
47
48use crate::{
49    Strategy::{Aggregating, AutoRedeeming},
50    aggregating::AggregatingStrategyConfig,
51    auto_funding::AutoFundingStrategyConfig,
52    auto_redeeming::AutoRedeemingStrategyConfig,
53    channel_finalizer::ClosureFinalizerStrategyConfig,
54    promiscuous::PromiscuousStrategyConfig,
55    strategy::MultiStrategyConfig,
56};
57
58pub mod aggregating;
59pub mod auto_funding;
60pub mod auto_redeeming;
61mod channel_finalizer;
62pub mod errors;
63pub mod promiscuous;
64pub mod strategy;
65
66/// Lists all possible strategies with their respective configurations.
67#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Display, EnumString, VariantNames)]
68#[strum(serialize_all = "snake_case")]
69pub enum Strategy {
70    Promiscuous(PromiscuousStrategyConfig),
71    Aggregating(AggregatingStrategyConfig),
72    AutoRedeeming(AutoRedeemingStrategyConfig),
73    AutoFunding(AutoFundingStrategyConfig),
74    ClosureFinalizer(ClosureFinalizerStrategyConfig),
75    Multi(MultiStrategyConfig),
76    Passive,
77}
78
79/// Default HOPR node strategies (in order).
80///
81/// ## Aggregation strategy
82///  - aggregate every 100 tickets on all channels
83///  - or when unredeemed value in the channel is more than 90% of channel's current balance
84///  - aggregate unredeemed tickets when a channel transitions to `PendingToClose`
85/// ## Auto-redeem Strategy
86/// - redeem only aggregated tickets
87/// - redeem single tickets on channel close if worth at least 2 HOPR
88/// ## Auto-funding Strategy
89/// - funding amount: 10 HOPR
90/// - lower limit: 1 HOPR
91/// - the strategy will fund channels which fall below the lower limit with the funding amount
92pub fn hopr_default_strategies() -> MultiStrategyConfig {
93    MultiStrategyConfig {
94        on_fail_continue: true,
95        allow_recursive: false,
96        execution_interval: 60,
97        strategies: vec![
98            // AutoFunding(AutoFundingStrategyConfig {
99            // min_stake_threshold: Balance::new_from_str("1000000000000000000", BalanceType::HOPR),
100            // funding_amount: Balance::new_from_str("10000000000000000000", BalanceType::HOPR),
101            // }),
102            Aggregating(AggregatingStrategyConfig {
103                aggregation_threshold: Some(100),
104                unrealized_balance_ratio: Some(0.9),
105                aggregate_on_channel_close: true,
106            }),
107            AutoRedeeming(AutoRedeemingStrategyConfig {
108                redeem_only_aggregated: true,
109                redeem_all_on_close: true,
110                minimum_redeem_ticket_value: HoprBalance::from_str("0.09 wxHOPR").unwrap(),
111            }),
112        ],
113    }
114}
115
116impl Default for Strategy {
117    fn default() -> Self {
118        Self::Multi(hopr_default_strategies())
119    }
120}
121
122/// An alias for the strategy configuration type.
123pub type StrategyConfig = MultiStrategyConfig;