1#![allow(clippy::integer_arithmetic)]
6
7use {crate::clock::DEFAULT_SLOTS_PER_EPOCH, cbe_sdk_macro::CloneZeroed};
8
9#[repr(C)]
11#[derive(Serialize, Deserialize, PartialEq, CloneZeroed, Copy, Debug, AbiExample)]
12pub struct Rent {
13 pub scoobies_per_byte_year: u64,
15
16 pub exemption_threshold: f64,
19
20 pub burn_percent: u8,
25}
26
27pub const DEFAULT_SCOOBIES_PER_BYTE_YEAR: u64 = 1_000_000_000 / 100 * 365 / (1024 * 1024);
35
36pub const DEFAULT_EXEMPTION_THRESHOLD: f64 = 2.0;
39
40pub const DEFAULT_BURN_PERCENT: u8 = 50;
45
46pub const ACCOUNT_STORAGE_OVERHEAD: u64 = 128;
51
52impl Default for Rent {
53 fn default() -> Self {
54 Self {
55 scoobies_per_byte_year: DEFAULT_SCOOBIES_PER_BYTE_YEAR,
56 exemption_threshold: DEFAULT_EXEMPTION_THRESHOLD,
57 burn_percent: DEFAULT_BURN_PERCENT,
58 }
59 }
60}
61
62impl Rent {
63 pub fn calculate_burn(&self, rent_collected: u64) -> (u64, u64) {
68 let burned_portion = (rent_collected * u64::from(self.burn_percent)) / 100;
69 (burned_portion, rent_collected - burned_portion)
70 }
71
72 pub fn minimum_balance(&self, data_len: usize) -> u64 {
79 let bytes = data_len as u64;
80 (((ACCOUNT_STORAGE_OVERHEAD + bytes) * self.scoobies_per_byte_year) as f64
81 * self.exemption_threshold) as u64
82 }
83
84 pub fn is_exempt(&self, balance: u64, data_len: usize) -> bool {
86 balance >= self.minimum_balance(data_len)
87 }
88
89 pub fn due(&self, balance: u64, data_len: usize, years_elapsed: f64) -> RentDue {
91 if self.is_exempt(balance, data_len) {
92 RentDue::Exempt
93 } else {
94 RentDue::Paying(self.due_amount(data_len, years_elapsed))
95 }
96 }
97
98 pub fn due_amount(&self, data_len: usize, years_elapsed: f64) -> u64 {
100 let actual_data_len = data_len as u64 + ACCOUNT_STORAGE_OVERHEAD;
101 let scoobies_per_year = self.scoobies_per_byte_year * actual_data_len;
102 (scoobies_per_year as f64 * years_elapsed) as u64
103 }
104
105 pub fn free() -> Self {
109 Self {
110 scoobies_per_byte_year: 0,
111 ..Rent::default()
112 }
113 }
114
115 pub fn with_slots_per_epoch(slots_per_epoch: u64) -> Self {
119 let ratio = slots_per_epoch as f64 / DEFAULT_SLOTS_PER_EPOCH as f64;
120 let exemption_threshold = DEFAULT_EXEMPTION_THRESHOLD * ratio;
121 let scoobies_per_byte_year = (DEFAULT_SCOOBIES_PER_BYTE_YEAR as f64 / ratio) as u64;
122 Self {
123 scoobies_per_byte_year,
124 exemption_threshold,
125 ..Self::default()
126 }
127 }
128}
129
130#[derive(Debug, Copy, Clone, Eq, PartialEq)]
132pub enum RentDue {
133 Exempt,
135 Paying(u64),
137}
138
139impl RentDue {
140 pub fn scoobies(&self) -> u64 {
142 match self {
143 RentDue::Exempt => 0,
144 RentDue::Paying(x) => *x,
145 }
146 }
147
148 pub fn is_exempt(&self) -> bool {
150 match self {
151 RentDue::Exempt => true,
152 RentDue::Paying(_) => false,
153 }
154 }
155}
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160
161 #[test]
162 fn test_due() {
163 let default_rent = Rent::default();
164
165 assert_eq!(
166 default_rent.due(0, 2, 1.2),
167 RentDue::Paying(
168 (((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_SCOOBIES_PER_BYTE_YEAR) as f64 * 1.2)
169 as u64
170 ),
171 );
172 assert_eq!(
173 default_rent.due(
174 (((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_SCOOBIES_PER_BYTE_YEAR) as f64
175 * DEFAULT_EXEMPTION_THRESHOLD) as u64,
176 2,
177 1.2
178 ),
179 RentDue::Exempt,
180 );
181
182 let custom_rent = Rent {
183 scoobies_per_byte_year: 5,
184 exemption_threshold: 2.5,
185 ..Rent::default()
186 };
187
188 assert_eq!(
189 custom_rent.due(0, 2, 1.2),
190 RentDue::Paying(
191 (((2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.scoobies_per_byte_year) as f64 * 1.2)
192 as u64,
193 )
194 );
195
196 assert_eq!(
197 custom_rent.due(
198 (((2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.scoobies_per_byte_year) as f64
199 * custom_rent.exemption_threshold) as u64,
200 2,
201 1.2
202 ),
203 RentDue::Exempt
204 );
205 }
206
207 #[test]
208 fn test_rent_due_scoobies() {
209 assert_eq!(RentDue::Exempt.scoobies(), 0);
210
211 let amount = 123;
212 assert_eq!(RentDue::Paying(amount).scoobies(), amount);
213 }
214
215 #[test]
216 fn test_rent_due_is_exempt() {
217 assert!(RentDue::Exempt.is_exempt());
218 assert!(!RentDue::Paying(0).is_exempt());
219 }
220
221 #[test]
222 fn test_clone() {
223 let rent = Rent {
224 scoobies_per_byte_year: 1,
225 exemption_threshold: 2.2,
226 burn_percent: 3,
227 };
228 #[allow(clippy::clone_on_copy)]
229 let cloned_rent = rent.clone();
230 assert_eq!(cloned_rent, rent);
231 }
232}