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        .map(|datetime| datetime.timestamp())
14}
15
16/// Calculate deterministic jitter offset using prev timestamp and thread pubkey
17/// This creates a feedback loop where each execution's timing affects the next jitter
18pub fn calculate_jitter_offset(prev_timestamp: i64, thread_pubkey: &Pubkey, jitter: u64) -> i64 {
19    if jitter == 0 {
20        return 0;
21    }
22
23    let mut hasher = DefaultHasher::new();
24    prev_timestamp.hash(&mut hasher);
25    thread_pubkey.hash(&mut hasher);
26    let hash = hasher.finish();
27
28    (hash % jitter) as i64
29}
30
31/// Safely transfer lamports from one account to another
32pub fn transfer_lamports(from: &AccountInfo, to: &AccountInfo, amount: u64) -> Result<()> {
33    if amount == 0 {
34        return Ok(());
35    }
36
37    // Deduct from source
38    **from.try_borrow_mut_lamports()? = from
39        .lamports()
40        .checked_sub(amount)
41        .ok_or(ProgramError::InsufficientFunds)?;
42
43    // Add to destination
44    **to.try_borrow_mut_lamports()? = to
45        .lamports()
46        .checked_add(amount)
47        .ok_or(ProgramError::ArithmeticOverflow)?;
48
49    Ok(())
50}