#![allow(clippy::arithmetic_side_effects)]
#![no_std]
#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
#[cfg(feature = "frozen-abi")]
extern crate std;
#[cfg(feature = "sysvar")]
pub mod sysvar;
use solana_sdk_macro::CloneZeroed;
const DEFAULT_SLOTS_PER_EPOCH: u64 = 432_000;
#[cfg(test)]
static_assertions::const_assert_eq!(
DEFAULT_SLOTS_PER_EPOCH,
solana_clock::DEFAULT_SLOTS_PER_EPOCH
);
#[repr(C)]
#[cfg_attr(feature = "frozen-abi", derive(solana_frozen_abi_macro::AbiExample))]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize, serde_derive::Serialize)
)]
#[derive(PartialEq, CloneZeroed, Debug)]
pub struct Rent {
pub lamports_per_byte_year: u64,
pub exemption_threshold: f64,
pub burn_percent: u8,
}
pub const DEFAULT_LAMPORTS_PER_BYTE_YEAR: u64 = 1_000_000_000 / 100 * 365 / (1024 * 1024);
pub const DEFAULT_EXEMPTION_THRESHOLD: f64 = 2.0;
pub const DEFAULT_BURN_PERCENT: u8 = 50;
pub const ACCOUNT_STORAGE_OVERHEAD: u64 = 128;
impl Default for Rent {
fn default() -> Self {
Self {
lamports_per_byte_year: DEFAULT_LAMPORTS_PER_BYTE_YEAR,
exemption_threshold: DEFAULT_EXEMPTION_THRESHOLD,
burn_percent: DEFAULT_BURN_PERCENT,
}
}
}
impl Rent {
pub fn calculate_burn(&self, rent_collected: u64) -> (u64, u64) {
let burned_portion = (rent_collected * u64::from(self.burn_percent)) / 100;
(burned_portion, rent_collected - burned_portion)
}
pub fn minimum_balance(&self, data_len: usize) -> u64 {
let bytes = data_len as u64;
(((ACCOUNT_STORAGE_OVERHEAD + bytes) * self.lamports_per_byte_year) as f64
* self.exemption_threshold) as u64
}
pub fn is_exempt(&self, balance: u64, data_len: usize) -> bool {
balance >= self.minimum_balance(data_len)
}
pub fn due(&self, balance: u64, data_len: usize, years_elapsed: f64) -> RentDue {
if self.is_exempt(balance, data_len) {
RentDue::Exempt
} else {
RentDue::Paying(self.due_amount(data_len, years_elapsed))
}
}
pub fn due_amount(&self, data_len: usize, years_elapsed: f64) -> u64 {
let actual_data_len = data_len as u64 + ACCOUNT_STORAGE_OVERHEAD;
let lamports_per_year = self.lamports_per_byte_year * actual_data_len;
(lamports_per_year as f64 * years_elapsed) as u64
}
pub fn free() -> Self {
Self {
lamports_per_byte_year: 0,
..Rent::default()
}
}
pub fn with_slots_per_epoch(slots_per_epoch: u64) -> Self {
let ratio = slots_per_epoch as f64 / DEFAULT_SLOTS_PER_EPOCH as f64;
let exemption_threshold = DEFAULT_EXEMPTION_THRESHOLD * ratio;
let lamports_per_byte_year = (DEFAULT_LAMPORTS_PER_BYTE_YEAR as f64 / ratio) as u64;
Self {
lamports_per_byte_year,
exemption_threshold,
..Self::default()
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum RentDue {
Exempt,
Paying(u64),
}
impl RentDue {
pub fn lamports(&self) -> u64 {
match self {
RentDue::Exempt => 0,
RentDue::Paying(x) => *x,
}
}
pub fn is_exempt(&self) -> bool {
match self {
RentDue::Exempt => true,
RentDue::Paying(_) => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_due() {
let default_rent = Rent::default();
assert_eq!(
default_rent.due(0, 2, 1.2),
RentDue::Paying(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64 * 1.2)
as u64
),
);
assert_eq!(
default_rent.due(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64
* DEFAULT_EXEMPTION_THRESHOLD) as u64,
2,
1.2
),
RentDue::Exempt,
);
let custom_rent = Rent {
lamports_per_byte_year: 5,
exemption_threshold: 2.5,
..Rent::default()
};
assert_eq!(
custom_rent.due(0, 2, 1.2),
RentDue::Paying(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.lamports_per_byte_year) as f64 * 1.2)
as u64,
)
);
assert_eq!(
custom_rent.due(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.lamports_per_byte_year) as f64
* custom_rent.exemption_threshold) as u64,
2,
1.2
),
RentDue::Exempt
);
}
#[test]
fn test_rent_due_lamports() {
assert_eq!(RentDue::Exempt.lamports(), 0);
let amount = 123;
assert_eq!(RentDue::Paying(amount).lamports(), amount);
}
#[test]
fn test_rent_due_is_exempt() {
assert!(RentDue::Exempt.is_exempt());
assert!(!RentDue::Paying(0).is_exempt());
}
#[test]
fn test_clone() {
let rent = Rent {
lamports_per_byte_year: 1,
exemption_threshold: 2.2,
burn_percent: 3,
};
#[allow(clippy::clone_on_copy)]
let cloned_rent = rent.clone();
assert_eq!(cloned_rent, rent);
}
}