Skip to main content

hopper_core/time/
mod.rs

1//! Time-based validation: deadlines, cooldowns, staleness.
2
3use hopper_runtime::error::ProgramError;
4
5/// Check that a deadline has passed (now >= deadline).
6#[inline(always)]
7pub fn check_deadline_passed(deadline: i64, now: i64) -> Result<(), ProgramError> {
8    if now < deadline {
9        return Err(ProgramError::InvalidAccountData);
10    }
11    Ok(())
12}
13
14/// Check that a deadline has NOT passed (now < deadline).
15#[inline(always)]
16pub fn check_deadline_not_passed(deadline: i64, now: i64) -> Result<(), ProgramError> {
17    if now >= deadline {
18        return Err(ProgramError::InvalidAccountData);
19    }
20    Ok(())
21}
22
23/// Check that enough time has elapsed since the last operation.
24#[inline(always)]
25pub fn check_cooldown_elapsed(
26    last_op_time: i64,
27    now: i64,
28    cooldown_seconds: i64,
29) -> Result<(), ProgramError> {
30    let elapsed = now.saturating_sub(last_op_time);
31    if elapsed < cooldown_seconds {
32        return Err(ProgramError::InvalidAccountData);
33    }
34    Ok(())
35}
36
37/// Check that data is not stale (updated recently enough).
38#[inline(always)]
39pub fn check_staleness(
40    updated_at: i64,
41    now: i64,
42    max_age_seconds: i64,
43) -> Result<(), ProgramError> {
44    let age = now.saturating_sub(updated_at);
45    if age > max_age_seconds {
46        return Err(ProgramError::InvalidAccountData);
47    }
48    Ok(())
49}
50
51/// Check that a timestamp is in the future.
52#[inline(always)]
53pub fn check_in_future(timestamp: i64, now: i64) -> Result<(), ProgramError> {
54    if timestamp <= now {
55        return Err(ProgramError::InvalidAccountData);
56    }
57    Ok(())
58}
59
60/// Check that a timestamp is in the past.
61#[inline(always)]
62pub fn check_in_past(timestamp: i64, now: i64) -> Result<(), ProgramError> {
63    if timestamp > now {
64        return Err(ProgramError::InvalidAccountData);
65    }
66    Ok(())
67}