hopr_strategy/
lib.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! This crate contains all the Strategies for HOPRd.
//! Strategies are vital for (partial) automation of ticket and HOPR channel operations
//! during node runtime.
//!
//! - [passive strategy](crate::strategy::MultiStrategy)
//! - [promiscuous strategy](crate::promiscuous)
//! - [auto funding strategy](crate::auto_funding)
//! - [auto redeeming strategy](crate::auto_redeeming)
//! - [aggregating strategy](crate::aggregating)
//! - [multiple strategy chains](crate::strategy)
//!
//! HOPRd can be configured to use any of the above strategies.
//!
//! ## Configuring strategies in HOPRd
//!
//! There are two ways of configuring strategies in HOPRd: via CLI and via a YAML config file.
//!
//! The configuration through CLI allows only fairly primitive single-strategy setting, through the `defaultStrategy`
//! parameter. It can be set to any of the above strategies, however, the strategy parameters are not further
//! configurable via the CLI and will always have their default values.
//! In addition, if the ` disableTicketAutoRedeem ` CLI argument is `false`, the default Auto Redeem strategy is added to the
//! strategy configured via the `defaultStrategy` argument (they execute together as Multi strategy).
//!
//! For more complex strategy configurations, the YAML configuration method is recommended via the `strategy` YAML section.
//! In this case, the top-most strategy is always assumed to be Multi strategy:
//!
//! ```yaml
//! strategy:
//!   on_fail_continue: true
//!   allow_recursive: true
//!   execution_interval: 60
//!   strategies:
//!     - !Promiscuous
//!       max_channels: 50
//!       new_channel_stake: 20
//!     - !AutoFunding
//!       funding_amount: 20
//!     - !Aggregating:
//!       aggregation_threshold: 1000
//! ```

use hopr_primitive_types::prelude::*;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString, VariantNames};

use crate::aggregating::AggregatingStrategyConfig;
use crate::auto_funding::AutoFundingStrategyConfig;
use crate::auto_redeeming::AutoRedeemingStrategyConfig;
use crate::channel_finalizer::ClosureFinalizerStrategyConfig;
use crate::promiscuous::PromiscuousStrategyConfig;
use crate::strategy::MultiStrategyConfig;
use crate::Strategy::{Aggregating, AutoRedeeming};

pub mod aggregating;
pub mod auto_funding;
pub mod auto_redeeming;
mod channel_finalizer;
pub mod errors;
pub mod promiscuous;
pub mod strategy;

/// Lists all possible strategies with their respective configurations.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Display, EnumString, VariantNames)]
#[strum(serialize_all = "snake_case")]
pub enum Strategy {
    Promiscuous(PromiscuousStrategyConfig),
    Aggregating(AggregatingStrategyConfig),
    AutoRedeeming(AutoRedeemingStrategyConfig),
    AutoFunding(AutoFundingStrategyConfig),
    ClosureFinalizer(ClosureFinalizerStrategyConfig),
    Multi(MultiStrategyConfig),
    Passive,
}

/// Default HOPR node strategies (in order).
///
/// ## Aggregation strategy
///  - aggregate every 100 tickets on all channels
///  - or when unredeemed value in the channel is more than 90% of channel's current balance
///  - aggregate unredeemed tickets when a channel transitions to `PendingToClose`
/// ## Auto-redeem Strategy
/// - redeem only aggregated tickets
/// - redeem single tickets on channel close if worth at least 2 HOPR
/// ## Auto-funding Strategy
/// - funding amount: 10 HOPR
/// - lower limit: 1 HOPR
/// - the strategy will fund channels which fall below the lower limit with the funding amount
pub fn hopr_default_strategies() -> MultiStrategyConfig {
    MultiStrategyConfig {
        on_fail_continue: true,
        allow_recursive: false,
        execution_interval: 60,
        strategies: vec![
            /*AutoFunding(AutoFundingStrategyConfig {
                min_stake_threshold: Balance::new_from_str("1000000000000000000", BalanceType::HOPR),
                funding_amount: Balance::new_from_str("10000000000000000000", BalanceType::HOPR),
            }),*/
            Aggregating(AggregatingStrategyConfig {
                aggregation_threshold: Some(100),
                unrealized_balance_ratio: Some(0.9),
                aggregate_on_channel_close: true,
            }),
            AutoRedeeming(AutoRedeemingStrategyConfig {
                redeem_only_aggregated: true,
                redeem_all_on_close: true,
                minimum_redeem_ticket_value: Balance::new_from_str("90000000000000000", BalanceType::HOPR),
            }),
        ],
    }
}

impl Default for Strategy {
    fn default() -> Self {
        Self::Multi(hopr_default_strategies())
    }
}

/// An alias for the strategy configuration type.
pub type StrategyConfig = MultiStrategyConfig;