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::AutoRedeeming, aggregating::AggregatingStrategyConfig, auto_funding::AutoFundingStrategyConfig,
50 auto_redeeming::AutoRedeemingStrategyConfig, channel_finalizer::ClosureFinalizerStrategyConfig,
51 promiscuous::PromiscuousStrategyConfig, strategy::MultiStrategyConfig,
52};
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/// ## Auto-redeem Strategy
78/// - redeem only aggregated tickets
79/// - redeem single tickets on channel close if worth at least 2 HOPR
80pub fn hopr_default_strategies() -> MultiStrategyConfig {
81 MultiStrategyConfig {
82 on_fail_continue: true,
83 allow_recursive: false,
84 execution_interval: 60,
85 strategies: vec![
86 // AutoFunding(AutoFundingStrategyConfig {
87 // min_stake_threshold: Balance::new_from_str("1000000000000000000", BalanceType::HOPR),
88 // funding_amount: Balance::new_from_str("10000000000000000000", BalanceType::HOPR),
89 // }),
90 // Aggregating(AggregatingStrategyConfig {
91 // aggregation_threshold: Some(100),
92 // unrealized_balance_ratio: Some(0.9),
93 // aggregate_on_channel_close: true,
94 //}),
95 AutoRedeeming(AutoRedeemingStrategyConfig {
96 redeem_only_aggregated: true,
97 redeem_all_on_close: true,
98 minimum_redeem_ticket_value: HoprBalance::from_str("0.09 wxHOPR").unwrap(),
99 }),
100 ],
101 }
102}
103
104impl Default for Strategy {
105 fn default() -> Self {
106 Self::Multi(hopr_default_strategies())
107 }
108}
109
110/// An alias for the strategy configuration type.
111pub type StrategyConfig = MultiStrategyConfig;