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 to the
22//! 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 section.
25//! 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 hopr_primitive_types::prelude::*;
43use serde::{Deserialize, Serialize};
44use strum::{Display, EnumString, VariantNames};
45
46use crate::aggregating::AggregatingStrategyConfig;
47use crate::auto_funding::AutoFundingStrategyConfig;
48use crate::auto_redeeming::AutoRedeemingStrategyConfig;
49use crate::channel_finalizer::ClosureFinalizerStrategyConfig;
50use crate::promiscuous::PromiscuousStrategyConfig;
51use crate::strategy::MultiStrategyConfig;
52use crate::Strategy::{Aggregating, AutoRedeeming};
53
54pub mod aggregating;
55pub mod auto_funding;
56pub mod auto_redeeming;
57mod channel_finalizer;
58pub mod errors;
59pub mod promiscuous;
60pub mod strategy;
61
62/// Lists all possible strategies with their respective configurations.
63#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Display, EnumString, VariantNames)]
64#[strum(serialize_all = "snake_case")]
65pub enum Strategy {
66    Promiscuous(PromiscuousStrategyConfig),
67    Aggregating(AggregatingStrategyConfig),
68    AutoRedeeming(AutoRedeemingStrategyConfig),
69    AutoFunding(AutoFundingStrategyConfig),
70    ClosureFinalizer(ClosureFinalizerStrategyConfig),
71    Multi(MultiStrategyConfig),
72    Passive,
73}
74
75/// Default HOPR node strategies (in order).
76///
77/// ## Aggregation strategy
78///  - aggregate every 100 tickets on all channels
79///  - or when unredeemed value in the channel is more than 90% of channel's current balance
80///  - aggregate unredeemed tickets when a channel transitions to `PendingToClose`
81/// ## Auto-redeem Strategy
82/// - redeem only aggregated tickets
83/// - redeem single tickets on channel close if worth at least 2 HOPR
84/// ## Auto-funding Strategy
85/// - funding amount: 10 HOPR
86/// - lower limit: 1 HOPR
87/// - the strategy will fund channels which fall below the lower limit with the funding amount
88pub fn hopr_default_strategies() -> MultiStrategyConfig {
89    MultiStrategyConfig {
90        on_fail_continue: true,
91        allow_recursive: false,
92        execution_interval: 60,
93        strategies: vec![
94            /*AutoFunding(AutoFundingStrategyConfig {
95                min_stake_threshold: Balance::new_from_str("1000000000000000000", BalanceType::HOPR),
96                funding_amount: Balance::new_from_str("10000000000000000000", BalanceType::HOPR),
97            }),*/
98            Aggregating(AggregatingStrategyConfig {
99                aggregation_threshold: Some(100),
100                unrealized_balance_ratio: Some(0.9),
101                aggregate_on_channel_close: true,
102            }),
103            AutoRedeeming(AutoRedeemingStrategyConfig {
104                redeem_only_aggregated: true,
105                redeem_all_on_close: true,
106                minimum_redeem_ticket_value: Balance::new_from_str("90000000000000000", BalanceType::HOPR),
107            }),
108        ],
109    }
110}
111
112impl Default for Strategy {
113    fn default() -> Self {
114        Self::Multi(hopr_default_strategies())
115    }
116}
117
118/// An alias for the strategy configuration type.
119pub type StrategyConfig = MultiStrategyConfig;