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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! # Report module
//!
//! This module contains the simulation report struct and its methods.
//! The simulation report contains the results of a simulation.
use chrono::Utc;
use rust_decimal::Decimal;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::User;
/// Report containing the results of a simulation.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct SimulationReport {
/// Timestamp of the simulation interval.
pub interval: i64,
/// List of users and their balances, behaviors, etc.
/// Only available in the final report.
#[cfg_attr(feature = "serde", serde(skip))]
pub users: Option<Vec<User>>,
/// Profit or loss for the interval.
/// Positive value indicates profit, negative value indicates loss.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub profit_loss: Decimal,
/// Number of trades made in the interval.
/// This includes both successful and failed trades.
pub trades: u64,
/// Number of successful trades made in the interval.
/// A trade is considered successful if the user has a positive balance.
pub successful_trades: u64,
/// Number of failed trades made in the interval.
/// A trade is considered failed if the user has a zero balance.
pub failed_trades: u64,
/// Market volatility during the simulation.
/// This is the standard deviation of token prices.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub market_volatility: Decimal,
/// Liquidity of the token during the simulation.
/// Liquidity is the number of trades per second.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub liquidity: Decimal,
/// Adoption rate of the token.
/// Adoption rate is the percentage of users who have a positive balance.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub adoption_rate: Decimal,
/// Total number of tokens burned during the simulation.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub total_burned: Decimal,
/// Burn rate of the token.
/// Burn rate is the number of tokens burned per user.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub burn_rate: Decimal,
/// Inflation rate of the token.
/// Inflation rate is the number of new tokens created per user.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub inflation_rate: Decimal,
/// User retention rate.
/// User retention rate is the percentage of users who have a positive balance.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub user_retention: Decimal,
/// Network activity (e.g., transactions per second).
/// This is the number of transactions made in the interval.
pub network_activity: u64,
/// Actual token price during the simulation.
/// This is the price of the token at the end of the simulation.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub token_price: Decimal,
/// Total number of new tokens created during the simulation.
#[cfg_attr(feature = "serde", serde(with = "rust_decimal::serde::float"))]
pub total_new_tokens: Decimal,
}
impl Default for SimulationReport {
/// Create a new simulation report with default values.
///
/// # Returns
///
/// A new simulation report with default values.
fn default() -> Self {
Self {
users: None,
interval: Utc::now().timestamp(),
profit_loss: Decimal::default(),
trades: 0,
successful_trades: 0,
failed_trades: 0,
market_volatility: Decimal::default(),
liquidity: Decimal::default(),
adoption_rate: Decimal::default(),
total_burned: Decimal::default(),
burn_rate: Decimal::default(),
inflation_rate: Decimal::default(),
user_retention: Decimal::default(),
token_price: Decimal::default(),
total_new_tokens: 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.
/// * `decimals` - Number of decimal places to round to.
///
/// # Returns
///
/// The liquidity of the token as trades per second.
pub fn calculate_liquidity(
&self,
trades: Decimal,
interval_duration: Decimal,
decimals: u32,
) -> Decimal {
#[cfg(feature = "log")]
log::debug!(
"Calculating liquidity: trades={}, interval_duration={}",
trades,
interval_duration
);
(trades / interval_duration).round_dp(decimals)
}
/// Calculate the adoption rate.
/// Adoption rate is the percentage of users who have a positive balance.
///
/// # Arguments
///
/// * `users` - A list of users.
/// * `decimals` - Number of decimal places to round to.
///
/// # Returns
///
/// The adoption rate as a percentage.
pub fn calculate_adoption_rate(&self, users: &[User], decimals: u32) -> Decimal {
#[cfg(feature = "log")]
log::debug!("Calculating adoption rate: users={:?}", users.len());
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(decimals)
}
/// 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.
/// * `decimals` - Number of decimal places to round to.
///
/// # Returns
///
/// The burn rate as a percentage.
pub fn calculate_burn_rate(
&self,
total_burned: Decimal,
total_users: Decimal,
decimals: u32,
) -> Decimal {
#[cfg(feature = "log")]
log::debug!(
"Calculating burn rate: total_burned={}, total_users={}",
total_burned,
total_users
);
(total_burned / total_users).round_dp(decimals)
}
/// 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.
/// * `decimals` - Number of decimal places to round to.
///
/// # Returns
///
/// The inflation rate as a percentage.
pub fn calculate_inflation_rate(
&self,
total_new_tokens: Decimal,
total_users: Decimal,
decimals: u32,
) -> Decimal {
#[cfg(feature = "log")]
log::debug!(
"Calculating inflation rate: total_new_tokens={}, total_users={}",
total_new_tokens,
total_users
);
(total_new_tokens / total_users).round_dp(decimals)
}
/// Calculate the user retention rate.
/// User retention rate is the percentage of users who have a positive balance.
///
/// # Arguments
///
/// * `users` - A list of users.
/// * `decimals` - Number of decimal places to round to.
///
/// # Returns
///
/// The user retention rate as a percentage.
pub fn calculate_user_retention(&self, users: &[User], decimals: u32) -> Decimal {
#[cfg(feature = "log")]
log::debug!("Calculating user retention rate: users={:?}", users.len());
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(decimals)
}
}
#[cfg(test)]
mod tests {
use uuid::Uuid;
use super::*;
#[test]
fn test_default() {
let report = SimulationReport::default();
assert!(report.users.is_none());
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_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, 4),
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, 4),
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, 4),
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, 4),
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, 4),
Decimal::new(5, 1),
);
}
}