tokenomics_simulator/
engine.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
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
350
351
352
353
354
use std::{collections::HashMap, hash::BuildHasherDefault};

use chrono::{DateTime, Utc};
use rand::Rng;
use rust_decimal::{prelude::*, Decimal};
use serde::{Deserialize, Serialize};
use twox_hash::XxHash64;
use uuid::Uuid;

use crate::{SimulationReport, Token, User, DECIMAL_PRECISION};

/// Input parameters for a simulation.
#[derive(Debug, Deserialize, Serialize)]
pub struct SimulationOptions {
    /// Duration of the simulation, depending on the interval type.
    pub duration: u64,

    /// Number of users in the simulation.
    pub total_users: u64,

    /// Volatility level. 0.0 is no volatility, 1.0 is maximum volatility.
    pub market_volatility: Decimal,

    /// Interval type for the simulation.
    pub interval_type: SimulationInterval,

    /// Transaction fee for each trade.
    pub transaction_fee: Option<Decimal>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Simulation {
    /// ID of the simulation.
    pub id: Uuid,

    /// Name of the simulation.
    pub name: String,

    /// Token used in the simulation.
    pub token: Token,

    /// Description of the simulation.
    pub description: Option<String>,

    /// Status of the simulation.
    pub status: SimulationStatus,

    /// Input parameters for the simulation.
    pub options: SimulationOptions,

    /// Report of the results for each interval of the simulation.
    pub interval_reports: HashMap<u64, SimulationReport, BuildHasherDefault<XxHash64>>,

    /// Report of the total results of the simulation.
    pub report: SimulationReport,

    /// Date and time the simulation was created.
    pub created_at: DateTime<Utc>,

    /// Date and time the simulation was last updated.
    pub updated_at: DateTime<Utc>,
}

/// Status of a simulation.
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
pub enum SimulationStatus {
    /// Simulation is currently running.
    #[serde(rename = "running")]
    Running,

    /// Simulation has completed.
    #[serde(rename = "completed")]
    Completed,
}

/// Interval type for the simulation.
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
pub enum SimulationInterval {
    /// Hourly interval.
    #[serde(rename = "hourly")]
    Hourly,

    /// Daily interval.
    #[serde(rename = "daily")]
    Daily,

    /// Weekly interval.
    #[serde(rename = "weekly")]
    Weekly,

    /// Monthly interval.
    #[serde(rename = "monthly")]
    Monthly,
}

impl Simulation {
    /// Update the status of the simulation.   
    ///
    /// # Arguments
    ///
    /// * `status` - The new status of the simulation.
    pub fn update_status(&mut self, status: SimulationStatus) {
        self.status = status;
        self.updated_at = Utc::now();
    }

    /// Run the simulation.
    ///
    /// # Arguments
    ///
    /// * `token` - The token used in the simulation.
    pub fn run(&mut self, token: &mut Token) {
        self.update_status(SimulationStatus::Running);

        // Apply airdrop, if available
        let airdrop_amount = match token.airdrop_percentage {
            Some(percentage) => token.airdrop(percentage),
            None => Decimal::default(),
        };

        let mut users = User::generate(
            self.options.total_users,
            token.initial_supply(),
            token.initial_price,
        );

        // Distribute airdrop amount among users, if available
        if airdrop_amount.is_zero() {
            let airdrop_per_user = airdrop_amount / Decimal::new(users.len() as i64, 0);

            for user in &mut users {
                user.balance += airdrop_per_user.round_dp(DECIMAL_PRECISION);
            }
        }

        self.interval_reports = HashMap::default();

        let interval = self.get_interval();

        for time in (0..self.options.duration * interval).step_by(interval as usize) {
            // Process unlock events up to the current time
            let current_date = Utc::now() + chrono::Duration::hours(time as i64);
            token.process_unlocks(current_date);

            let report = self.process_interval(&mut users, interval);
            self.interval_reports.insert(time, report);
        }

        self.generate_report(&users);

        self.update_status(SimulationStatus::Completed);
    }

    /// Simulate trades for a given interval.
    ///
    /// # Arguments
    ///
    /// * `users` - A list of users.
    /// * `interval` - Duration of the interval.
    ///
    /// # Returns
    ///
    /// A report of the simulation results for the interval.
    pub fn process_interval(&self, users: &mut [User], interval: u64) -> SimulationReport {
        let mut rng = rand::thread_rng();

        let mut trades = 0;
        let total_users = Decimal::new(users.len() as i64, 0);
        let mut total_burned = Decimal::default();
        let mut total_new_tokens = Decimal::default();
        let mut report = SimulationReport::default();

        for _ in 0..interval {
            for user in users.iter_mut() {
                // Skip users with zero balance
                if user.balance.is_zero() {
                    continue;
                }

                // Simulate a trade, 50% chance of success
                if rng.gen_bool(0.5) {
                    // Simulate a successful trade and randomise the fraction between 1% and 10% of the user's balance
                    let trade_fraction = rng.gen_range(0.01..0.1); //
                    let trade_amount = Decimal::from_f64(
                        rng.gen_range(0.0..(user.balance.to_f64().unwrap() * trade_fraction)),
                    )
                    .unwrap()
                    .round_dp(DECIMAL_PRECISION);

                    user.balance -= trade_amount;
                    report.profit_loss += trade_amount;
                    report.successful_trades += 1;

                    if let Some(burn_rate) = self.token.burn_rate {
                        let burned = trade_amount * burn_rate;
                        user.balance -= burned;
                        total_burned += burned;
                    }

                    if let Some(inflation_rate) = self.token.inflation_rate {
                        let new_tokens = trade_amount * inflation_rate;
                        user.balance += new_tokens;
                        total_new_tokens += new_tokens;
                    }

                    if let Some(fee) = self.options.transaction_fee {
                        user.balance -= trade_amount * fee.round_dp(DECIMAL_PRECISION);
                    }
                } else {
                    report.failed_trades += 1;
                }
                trades += 1;
            }
        }

        report.trades = trades;
        report.liquidity = report.calculate_liquidity(
            Decimal::new(trades as i64, 0),
            Decimal::new(interval as i64, 0),
        );
        report.adoption_rate = report.calculate_adoption_rate(users);
        report.burn_rate = report.calculate_burn_rate(total_burned, total_users);
        report.user_retention = report.calculate_user_retention(users);
        report.market_volatility = self.options.market_volatility;
        report.network_activity = trades / interval;
        report.token_distribution = users.iter().map(|u| u.balance).collect();
        report.inflation_rate = report.calculate_inflation_rate(total_new_tokens, total_users);

        report
    }

    /// Calculate the total report for the simulation.
    ///
    /// # Arguments
    ///
    /// * `users` - A list of users.
    pub fn generate_report(&mut self, users: &[User]) {
        let mut report = SimulationReport {
            market_volatility: self.options.market_volatility,
            ..Default::default()
        };

        let mut total_burned = Decimal::default();
        let mut total_new_tokens = Decimal::default();
        let total_users = Decimal::new(self.options.total_users as i64, 0);

        for result in self.interval_reports.values() {
            report.profit_loss += result.profit_loss;
            report.trades += result.trades;
            report.successful_trades += result.successful_trades;
            report.failed_trades += result.failed_trades;

            total_burned += result.burn_rate * total_users;
            total_new_tokens += result.inflation_rate * total_users;
            report.liquidity += result.liquidity;
            report.adoption_rate += result.adoption_rate;
            report.user_retention += result.user_retention;
        }

        let total_trades = Decimal::new(report.trades as i64, 0);
        let total_intervals = Decimal::new(self.interval_reports.len() as i64, 0);

        report.liquidity = (report.liquidity / total_intervals).round_dp(DECIMAL_PRECISION);
        report.adoption_rate = (report.adoption_rate / total_intervals).round_dp(DECIMAL_PRECISION);
        report.user_retention =
            (report.user_retention / total_intervals).round_dp(DECIMAL_PRECISION);
        report.token_distribution = users
            .iter()
            .map(|u| u.balance.round_dp(DECIMAL_PRECISION))
            .collect();
        report.burn_rate = report.calculate_burn_rate(total_burned, total_trades);
        report.inflation_rate = (total_new_tokens / total_trades).round_dp(DECIMAL_PRECISION);
        report.network_activity = report.trades / self.options.duration;

        self.report = report;
    }

    /// Get the interval for the simulation.
    ///
    /// # Returns
    ///
    /// The duration of the simulation interval.
    pub fn get_interval(&self) -> u64 {
        match self.options.interval_type {
            SimulationInterval::Hourly => 1,
            SimulationInterval::Daily => 24,
            SimulationInterval::Weekly => 24 * 7,
            SimulationInterval::Monthly => 24 * 30,
        }
    }
}

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

    fn setup() -> Simulation {
        Simulation {
            id: Uuid::new_v4(),
            name: "Test Simulation".to_string(),
            token: Token::default(),
            description: None,
            status: SimulationStatus::Running,
            options: SimulationOptions {
                duration: 30,
                total_users: 100,
                market_volatility: Decimal::new(5, 1),
                transaction_fee: None,
                interval_type: SimulationInterval::Daily,
            },
            interval_reports: HashMap::default(),
            report: SimulationReport::default(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
        }
    }

    #[test]
    fn test_get_interval() {
        let daily_simulation = setup();
        assert_eq!(daily_simulation.get_interval(), 24);

        let mut hourly_simulation = setup();
        hourly_simulation.options.interval_type = SimulationInterval::Hourly;
        assert_eq!(hourly_simulation.get_interval(), 1);

        let mut weekly_simulation = setup();
        weekly_simulation.options.interval_type = SimulationInterval::Weekly;
        assert_eq!(weekly_simulation.get_interval(), 24 * 7);

        let mut monthly_simulation = setup();
        monthly_simulation.options.interval_type = SimulationInterval::Monthly;
        assert_eq!(monthly_simulation.get_interval(), 24 * 30);
    }

    #[test]
    fn test_update_status() {
        let mut simulation = setup();
        simulation.update_status(SimulationStatus::Completed);

        assert_eq!(simulation.status, SimulationStatus::Completed);
    }

    #[test]
    fn test_run() {
        let mut token = Token::default();
        let mut simulation = setup();

        simulation.run(&mut token);

        assert_eq!(simulation.status, SimulationStatus::Completed);
        assert_eq!(simulation.interval_reports.len(), 30);
    }
}