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
use solana_sdk::{clock::Epoch, epoch_schedule::EpochSchedule, timing::years_as_slots};
use std::time::Duration;
#[derive(Debug)]
pub struct UnlockInfo {
pub cliff_fraction: f64,
pub cliff_years: f64,
pub unlocks: usize,
pub unlock_years: f64,
pub custodian: &'static str,
}
#[derive(Debug, Default, Clone)]
pub struct Unlocks {
i: usize,
unlocks: usize,
prev_fraction: f64,
cliff_fraction: f64,
cliff_epoch: Epoch,
unlock_fraction: f64,
unlock_epochs: Epoch,
}
impl Unlocks {
pub fn new(
cliff_fraction: f64,
cliff_year: f64,
unlocks: usize,
unlock_years: f64,
epoch_schedule: &EpochSchedule,
tick_duration: &Duration,
ticks_per_slot: u64,
) -> Self {
let cliff_slot = years_as_slots(cliff_year, tick_duration, ticks_per_slot) as u64;
let cliff_epoch = epoch_schedule.get_epoch(cliff_slot);
let first_unlock_slot =
years_as_slots(cliff_year + unlock_years, tick_duration, ticks_per_slot) as u64;
let unlock_epochs = epoch_schedule.get_epoch(first_unlock_slot) - cliff_epoch;
Self::from_epochs(cliff_fraction, cliff_epoch, unlocks, unlock_epochs)
}
pub fn from_epochs(
cliff_fraction: f64,
cliff_epoch: Epoch,
unlocks: usize,
unlock_epochs: Epoch,
) -> Self {
let unlock_fraction = if unlocks != 0 {
(1.0 - cliff_fraction) / unlocks as f64
} else {
0.0
};
Self {
prev_fraction: 0.0,
i: 0,
unlocks,
cliff_fraction,
cliff_epoch,
unlock_fraction,
unlock_epochs,
}
}
}
impl Iterator for Unlocks {
type Item = Unlock;
fn next(&mut self) -> Option<Self::Item> {
let i = self.i;
if i == 0 {
self.i += 1;
self.prev_fraction = self.cliff_fraction;
Some(Unlock {
prev_fraction: 0.0,
fraction: self.cliff_fraction,
epoch: self.cliff_epoch,
})
} else if i <= self.unlocks {
self.i += 1;
let prev_fraction = self.prev_fraction;
self.prev_fraction = 1.0 - (self.unlocks - i) as f64 * self.unlock_fraction;
Some(Unlock {
prev_fraction,
fraction: self.prev_fraction,
epoch: self.cliff_epoch + i as u64 * self.unlock_epochs,
})
} else {
None
}
}
}
#[derive(Debug, Default)]
pub struct Unlock {
pub epoch: Epoch,
pub prev_fraction: f64,
pub fraction: f64,
}
impl Unlock {
#[allow(clippy::float_cmp)]
pub fn amount(&self, total: u64) -> u64 {
if self.fraction == 1.0 {
total - (self.prev_fraction * total as f64) as u64
} else {
(self.fraction * total as f64) as u64 - (self.prev_fraction * total as f64) as u64
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[allow(clippy::float_cmp)]
fn test_make_lockups() {
let total_lamports: u64 = 1_725_987_234_408_923;
const EPOCHS_PER_MONTH: Epoch = 2;
assert_eq!(
Unlocks::from_epochs(0.20, 6 * EPOCHS_PER_MONTH, 24, EPOCHS_PER_MONTH)
.map(|unlock| unlock.amount(total_lamports))
.sum::<u64>(),
total_lamports
);
let tick_duration = Duration::new(1, 0);
let ticks_per_slot = 1;
let epoch_schedule = EpochSchedule::custom(14 * 24 * 60 * 60, 0, false);
assert_eq!(
Unlocks::new(
0.20,
0.5,
24,
1.0 / 12.0,
&epoch_schedule,
&tick_duration,
ticks_per_slot,
)
.map(|unlock| {
if unlock.prev_fraction == 0.0 {
assert_eq!(unlock.epoch, 13);
} else if unlock.prev_fraction == 0.2 {
assert_eq!(unlock.epoch, 15);
}
unlock.amount(total_lamports)
})
.sum::<u64>(),
total_lamports
);
assert_eq!(
Unlocks::new(
0.20,
1.5,
24,
1.0 / 12.0,
&epoch_schedule,
&tick_duration,
ticks_per_slot,
)
.map(|unlock| {
if unlock.prev_fraction == 0.0 {
assert_eq!(unlock.epoch, 26 + 13);
} else if unlock.prev_fraction == 0.2 {
assert_eq!(unlock.epoch, 26 + 15);
}
unlock.amount(total_lamports)
})
.sum::<u64>(),
total_lamports
);
}
}