Skip to main content

antegen_thread_program/
utils.rs

1use anchor_lang::prelude::*;
2use antegen_cron::Schedule;
3use chrono::{DateTime, Utc};
4use std::collections::hash_map::DefaultHasher;
5use std::hash::{Hash, Hasher};
6use std::str::FromStr;
7
8/// Calculate the next timestamp for a cron schedule
9pub fn next_timestamp(after: i64, schedule: String) -> Option<i64> {
10    Schedule::from_str(&schedule)
11        .unwrap()
12        .next_after(&DateTime::<Utc>::from_timestamp(after, 0).unwrap())
13        .take()
14        .map(|datetime| datetime.timestamp())
15}
16
17/// Calculate deterministic jitter offset using prev timestamp and thread pubkey
18/// This creates a feedback loop where each execution's timing affects the next jitter
19pub fn calculate_jitter_offset(prev_timestamp: i64, thread_pubkey: &Pubkey, jitter: u64) -> i64 {
20    if jitter == 0 {
21        return 0;
22    }
23
24    let mut hasher = DefaultHasher::new();
25    prev_timestamp.hash(&mut hasher);
26    thread_pubkey.hash(&mut hasher);
27    let hash = hasher.finish();
28
29    (hash % jitter) as i64
30}
31
32/// Safely transfer lamports from one account to another
33pub fn transfer_lamports(from: &AccountInfo, to: &AccountInfo, amount: u64) -> Result<()> {
34    if amount == 0 {
35        return Ok(());
36    }
37
38    // Deduct from source
39    **from.try_borrow_mut_lamports()? = from
40        .lamports()
41        .checked_sub(amount)
42        .ok_or(ProgramError::InsufficientFunds)?;
43
44    // Add to destination
45    **to.try_borrow_mut_lamports()? = to
46        .lamports()
47        .checked_add(amount)
48        .ok_or(ProgramError::ArithmeticOverflow)?;
49
50    Ok(())
51}