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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! configuration for network inflation

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug, Copy, AbiExample)]
#[serde(rename_all = "camelCase")]
pub struct Inflation {
    /// Initial inflation percentage, from time=0
    pub initial: f64,

    /// Terminal inflation percentage, to time=INF
    pub terminal: f64,

    /// Rate per year, at which inflation is lowered until reaching terminal
    ///  i.e. inflation(year) == MAX(terminal, initial*((1-taper)^year))
    pub taper: f64,

    /// Percentage of total inflation allocated to the foundation
    pub foundation: f64,
    /// Duration of foundation pool inflation, in years
    pub foundation_term: f64,

    /// DEPRECATED, this field is currently unused
    __unused: f64,
}

const DEFAULT_INITIAL: f64 = 0.04;
const DEFAULT_TERMINAL: f64 = 0.0;
const DEFAULT_TAPER: f64 = 0.25;
const DEFAULT_FOUNDATION: f64 = 0.0;
const DEFAULT_FOUNDATION_TERM: f64 = 0.0;

impl Default for Inflation {
    fn default() -> Self {
        Self {
            initial: DEFAULT_INITIAL,
            terminal: DEFAULT_TERMINAL,
            taper: DEFAULT_TAPER,
            foundation: DEFAULT_FOUNDATION,
            foundation_term: DEFAULT_FOUNDATION_TERM,
            __unused: 0.0,
        }
    }
}

impl Inflation {
    pub fn new_disabled() -> Self {
        Self {
            initial: 0.0,
            terminal: 0.0,
            taper: 0.0,
            foundation: 0.0,
            foundation_term: 0.0,
            __unused: 0.0,
        }
    }

    // fixed inflation rate at `validator` percentage for staking rewards, and none for foundation
    pub fn new_fixed(validator: f64) -> Self {
        Self {
            initial: validator,
            terminal: validator,
            taper: 1.0,
            foundation: 0.0,
            foundation_term: 0.0,
            __unused: 0.0,
        }
    }

    pub fn pico() -> Self {
        Self::new_fixed(0.0001) // 0.01% inflation
    }

    pub fn full() -> Self {
        Self {
            initial: DEFAULT_INITIAL,
            terminal: DEFAULT_TERMINAL,
            taper: DEFAULT_TAPER,
            foundation: 0.0,
            foundation_term: 0.0,
            __unused: 0.0,
        }
    }

    /// inflation rate at year
    pub fn total(&self, year: f64) -> f64 {
        assert!(year >= 0.0);
        let tapered = self.initial * ((1.0 - self.taper).powf(year));

        if tapered > self.terminal {
            tapered
        } else {
            self.terminal
        }
    }

    /// portion of total that goes to validators
    pub fn validator(&self, year: f64) -> f64 {
        self.total(year) - self.foundation(year)
    }

    /// portion of total that goes to foundation
    pub fn foundation(&self, year: f64) -> f64 {
        if year < self.foundation_term {
            self.total(year) * self.foundation
        } else {
            0.0
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_inflation_basic() {
        let inflation = Inflation::default();

        let mut last = inflation.total(0.0);

        for year in &[0.1, 0.5, 1.0, DEFAULT_FOUNDATION_TERM, 100.0] {
            let total = inflation.total(*year);
            assert_eq!(
                total,
                inflation.validator(*year) + inflation.foundation(*year)
            );
            assert!(total < last);
            assert!(total >= inflation.terminal);
            last = total;
        }
        assert_eq!(last, inflation.terminal);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_inflation_fixed() {
        let inflation = Inflation::new_fixed(0.001);
        for year in &[0.1, 0.5, 1.0, DEFAULT_FOUNDATION_TERM, 100.0] {
            assert_eq!(inflation.total(*year), 0.001);
        }
    }
}