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
use std::iter::FromIterator;
use solana_program::{account_info::AccountInfo, program_error::ProgramError, program_pack::Pack};
pub fn duration_sanity(now: u64, start: u64, end: u64, cliff: u64) -> bool {
let cliff_cond = if cliff == 0 {
true
} else {
start <= cliff && cliff <= end
};
now < start && start < end && cliff_cond
}
pub fn unpack_token_account(
account_info: &AccountInfo,
) -> Result<spl_token::state::Account, ProgramError> {
if account_info.owner != &spl_token::id() {
return Err(ProgramError::InvalidAccountData);
}
spl_token::state::Account::unpack(&account_info.data.borrow())
}
pub fn unpack_mint_account(
account_info: &AccountInfo,
) -> Result<spl_token::state::Mint, ProgramError> {
spl_token::state::Mint::unpack(&account_info.data.borrow())
}
pub fn pretty_time(t: u64) -> String {
let seconds = t % 60;
let minutes = (t / 60) % 60;
let hours = (t / (60 * 60)) % 24;
let days = t / (60 * 60 * 24);
format!(
"{} days, {} hours, {} minutes, {} seconds",
days, hours, minutes, seconds
)
}
pub fn encode_base10(amount: u64, decimal_places: usize) -> String {
let mut s: Vec<char> = format!("{:0width$}", amount, width = 1 + decimal_places)
.chars()
.collect();
s.insert(s.len() - decimal_places, '.');
String::from_iter(&s)
.trim_end_matches('0')
.trim_end_matches('.')
.to_string()
}
#[allow(unused_imports)]
mod tests {
use crate::utils::duration_sanity;
#[test]
fn test_duration_sanity() {
assert_eq!(true, duration_sanity(100, 110, 130, 120));
assert_eq!(true, duration_sanity(100, 110, 130, 0));
assert_eq!(false, duration_sanity(100, 140, 130, 130));
assert_eq!(false, duration_sanity(100, 130, 130, 130));
assert_eq!(false, duration_sanity(130, 130, 130, 130));
assert_eq!(false, duration_sanity(100, 110, 130, 140));
}
}