tokenomics_simulator/
token.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
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Token.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Token {
    /// ID for the token.
    pub id: Uuid,

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

    /// Symbol of the token.
    pub symbol: Option<String>,

    /// Maximum supply of the token.
    pub maximum_supply: Decimal,

    /// Current supply of the token.
    pub current_supply: Decimal,

    /// Initial supply of the token, in percentage of maximum supply.
    pub initial_supply_percentage: Decimal,

    /// Annual percentage increase in supply, if supply is inflationary.
    pub inflation_rate: Option<Decimal>,

    /// Percentage of tokens burned during each transaction, if deflationary.
    pub burn_rate: Option<Decimal>,

    /// Initial price of the token in simulation
    pub initial_price: Option<Decimal>,

    /// Airdrop amount of the token, in percentage of maximum supply.
    pub airdrop_percentage: Option<Decimal>,

    /// Unlock schedule.
    pub unlock_schedule: Option<Vec<UnlockEvent>>,
}

/// Unlock event.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct UnlockEvent {
    /// Date and time of the unlock event.
    pub date: DateTime<Utc>,

    /// Amount of tokens to unlock.
    pub amount: Decimal,
}

impl Default for Token {
    /// Create a new token with default values.
    ///
    /// # Returns
    ///
    /// New token with default values.
    fn default() -> Self {
        Self {
            id: Uuid::new_v4(),
            name: "Token".to_string(),
            symbol: Some("TKN".to_string()),
            maximum_supply: Decimal::new(1_000_000, 0),
            current_supply: Decimal::default(),
            initial_supply_percentage: Decimal::new(100, 0),
            inflation_rate: None,
            burn_rate: None,
            initial_price: Some(Decimal::new(1, 0)),
            airdrop_percentage: None,
            unlock_schedule: None,
        }
    }
}

impl Token {
    /// Perform an airdrop.
    ///
    /// # Arguments
    ///
    /// * `percentage` - The percentage of the maximum supply to airdrop.
    ///
    /// # Returns
    ///
    /// The amount of tokens airdropped.
    pub fn airdrop(&mut self, percentage: Decimal) -> Decimal {
        let airdrop_amount = (self.maximum_supply * percentage / Decimal::new(100, 0)).round();
        let remaining_supply = self.maximum_supply - self.current_supply;
        let final_airdrop_amount = if airdrop_amount > remaining_supply {
            remaining_supply
        } else {
            airdrop_amount
        };

        self.current_supply += final_airdrop_amount;

        final_airdrop_amount
    }

    /// Add an unlock event to the schedule.
    ///
    /// # Arguments
    ///
    /// * `date` - The date and time of the unlock event.
    /// * `amount` - The amount of tokens to unlock.
    pub fn add_unlock_event(&mut self, date: DateTime<Utc>, amount: Decimal) {
        let event = UnlockEvent { date, amount };

        if let Some(schedule) = &mut self.unlock_schedule {
            schedule.push(event);
        } else {
            self.unlock_schedule = Some(vec![event]);
        }
    }

    /// Process unlock events up to the current date.
    ///
    /// # Arguments
    ///
    /// * `current_date` - The current date and time.
    pub fn process_unlocks(&mut self, current_date: DateTime<Utc>) {
        if let Some(schedule) = &mut self.unlock_schedule {
            schedule.retain(|event| {
                if event.date <= current_date {
                    self.current_supply += event.amount;
                    false
                } else {
                    true
                }
            });
        }
    }

    /// Calculate the initial supply based on the initial supply percentage.
    ///
    /// # Returns
    ///
    /// Initial supply of the token.
    pub fn initial_supply(&self) -> Decimal {
        (self.maximum_supply * self.initial_supply_percentage / Decimal::new(100, 0)).round()
    }
}

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

    #[test]
    fn test_token_default() {
        let token = Token::default();

        assert_eq!(token.name, "Token");
        assert_eq!(token.symbol, Some("TKN".to_string()));
        assert_eq!(token.maximum_supply, Decimal::new(1_000_000, 0));
        assert_eq!(token.current_supply, Decimal::default());
        assert_eq!(token.initial_supply_percentage, Decimal::new(100, 0));
        assert_eq!(token.inflation_rate, None);
        assert_eq!(token.burn_rate, None);
        assert_eq!(token.initial_price, Some(Decimal::new(1, 0)));
        assert_eq!(token.airdrop_percentage, None);
        assert_eq!(token.unlock_schedule, None);
    }

    #[test]
    fn test_token_airdrop() {
        let mut token = Token::default();
        let final_amount = Decimal::new(100000, 0);

        let airdrop_amount = token.airdrop(Decimal::new(10, 0));

        assert_eq!(airdrop_amount, final_amount);
        assert_eq!(token.current_supply, final_amount);

        let airdrop_amount = token.airdrop(Decimal::new(100, 0));

        assert_eq!(airdrop_amount, Decimal::new(900000, 0));
        assert_eq!(token.current_supply, Decimal::new(1_000_000, 0));
    }
}