tokenomics_simulator/
report.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

use crate::{User, DECIMAL_PRECISION};

/// Report containing the results of a simulation.
#[derive(Debug, Deserialize, Serialize)]
pub struct SimulationReport {
    /// Profit or loss for the interval.
    pub profit_loss: Decimal,

    /// Number of trades made in the interval.
    pub trades: u64,

    /// Number of successful trades made in the interval.
    pub successful_trades: u64,

    /// Number of failed trades made in the interval.
    pub failed_trades: u64,

    /// Token distribution among participants.
    pub token_distribution: Vec<Decimal>,

    /// Market volatility during the simulation.
    pub market_volatility: Decimal,

    /// Liquidity of the token during the simulation.
    pub liquidity: Decimal,

    /// Adoption rate of the token.
    pub adoption_rate: Decimal,

    /// Burn rate of the token.
    pub burn_rate: Decimal,

    /// Inflation rate of the token.
    pub inflation_rate: Decimal,

    /// User retention rate.
    pub user_retention: Decimal,

    /// Network activity (e.g., transactions per second).
    pub network_activity: u64,
}

impl Default for SimulationReport {
    /// Create a new simulation report with default values.
    ///
    /// # Returns
    ///
    /// A new simulation report with default values.
    fn default() -> Self {
        Self {
            profit_loss: Decimal::default(),
            trades: 0,
            successful_trades: 0,
            failed_trades: 0,
            token_distribution: vec![],
            market_volatility: Decimal::default(),
            liquidity: Decimal::default(),
            adoption_rate: Decimal::default(),
            burn_rate: Decimal::default(),
            inflation_rate: Decimal::default(),
            user_retention: Decimal::default(),
            network_activity: 0,
        }
    }
}

impl SimulationReport {
    /// Calculate the liquidity of the token.
    /// Liquidity is the number of trades per second.
    ///
    /// # Arguments
    ///
    /// * `trades` - Number of trades made in the interval.
    /// * `interval_duration` - Duration of the interval in seconds.
    ///
    /// # Returns
    ///
    /// The liquidity of the token as trades per second.
    pub fn calculate_liquidity(&self, trades: Decimal, interval_duration: Decimal) -> Decimal {
        (trades / interval_duration).round_dp(DECIMAL_PRECISION)
    }

    /// Calculate the adoption rate.
    /// Adoption rate is the percentage of users who have a positive balance.
    ///
    /// # Arguments
    ///
    /// * `users` - A list of users.
    ///
    /// # Returns
    ///
    /// The adoption rate as a percentage.
    pub fn calculate_adoption_rate(&self, users: &[User]) -> Decimal {
        let total_users = Decimal::new(users.len() as i64, 0);
        let new_users = Decimal::new(
            users
                .iter()
                .filter(|u| u.balance > Decimal::default())
                .count() as i64,
            0,
        );

        (new_users / total_users).round_dp(DECIMAL_PRECISION)
    }

    /// Calculate the burn rate.
    /// Burn rate is the number of tokens burned per user.
    ///
    /// # Arguments
    ///
    /// * `total_burned` - Total number of tokens burned.
    /// * `total_users` - Total number of users.
    ///
    /// # Returns
    ///
    /// The burn rate as a percentage.
    pub fn calculate_burn_rate(&self, total_burned: Decimal, total_users: Decimal) -> Decimal {
        (total_burned / total_users).round_dp(DECIMAL_PRECISION)
    }

    /// Calculate the inflation rate.
    /// Inflation rate is the number of new tokens created per user.
    ///
    /// # Arguments
    ///
    /// * `total_new_tokens` - Total number of new tokens created.
    /// * `total_users` - Total number of users.
    ///
    /// # Returns
    ///
    /// The inflation rate as a percentage.
    pub fn calculate_inflation_rate(
        &self,
        total_new_tokens: Decimal,
        total_users: Decimal,
    ) -> Decimal {
        (total_new_tokens / total_users).round_dp(DECIMAL_PRECISION)
    }

    /// Calculate the user retention rate.
    /// User retention rate is the percentage of users who have a positive balance.
    ///
    /// # Arguments
    ///
    /// * `users` - A list of users.
    ///
    /// # Returns
    ///
    /// The user retention rate as a percentage.
    pub fn calculate_user_retention(&self, users: &[User]) -> Decimal {
        let total_users = Decimal::new(users.len() as i64, 0);
        let retained_users = Decimal::new(
            users
                .iter()
                .filter(|u| u.balance > Decimal::default())
                .count() as i64,
            0,
        );

        (retained_users / total_users).round_dp(DECIMAL_PRECISION)
    }
}

#[cfg(test)]
mod tests {
    use uuid::Uuid;

    use super::*;

    #[test]
    fn test_default() {
        let report = SimulationReport::default();

        assert_eq!(report.profit_loss, Decimal::default());
        assert_eq!(report.trades, 0);
        assert_eq!(report.successful_trades, 0);
        assert_eq!(report.failed_trades, 0);
        assert!(report.token_distribution.is_empty());
        assert_eq!(report.market_volatility, Decimal::default());
        assert_eq!(report.liquidity, Decimal::default());
        assert_eq!(report.adoption_rate, Decimal::default());
        assert_eq!(report.burn_rate, Decimal::default());
        assert_eq!(report.inflation_rate, Decimal::default());
        assert_eq!(report.user_retention, Decimal::default());
        assert_eq!(report.network_activity, 0);
    }

    #[test]
    fn test_calculate_liquidity() {
        let report = SimulationReport::default();
        let trades = Decimal::new(100, 0);
        let interval_duration = Decimal::new(10, 0);

        assert_eq!(
            report.calculate_liquidity(trades, interval_duration),
            Decimal::new(10, 0)
        );
    }

    #[test]
    fn test_calculate_adoption_rate() {
        let report = SimulationReport::default();
        let users = vec![
            User::new(Uuid::new_v4(), Decimal::default()),
            User::new(Uuid::new_v4(), Decimal::new(10, 0)),
            User::new(Uuid::new_v4(), Decimal::default()),
            User::new(Uuid::new_v4(), Decimal::new(5, 0)),
        ];

        assert_eq!(report.calculate_adoption_rate(&users), Decimal::new(5, 1));
    }

    #[test]
    fn test_calculate_burn_rate() {
        let report = SimulationReport::default();
        let total_burned = Decimal::new(100, 0);
        let total_users = Decimal::new(10, 0);

        assert_eq!(
            report.calculate_burn_rate(total_burned, total_users),
            Decimal::new(10, 0)
        );
    }

    #[test]
    fn test_calculate_inflation_rate() {
        let report = SimulationReport::default();
        let total_new_tokens = Decimal::new(100, 0);
        let total_users = Decimal::new(10, 0);

        assert_eq!(
            report.calculate_inflation_rate(total_new_tokens, total_users),
            Decimal::new(10, 0)
        );
    }

    #[test]
    fn test_calculate_user_retention() {
        let report = SimulationReport::default();
        let users = vec![
            User::new(Uuid::new_v4(), Decimal::default()),
            User::new(Uuid::new_v4(), Decimal::new(10, 0)),
            User::new(Uuid::new_v4(), Decimal::default()),
            User::new(Uuid::new_v4(), Decimal::new(5, 0)),
        ];

        assert_eq!(report.calculate_user_retention(&users), Decimal::new(5, 1));
    }
}