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
use AccountInfo;
use Clock;
use cratecheck_pubkey_eq;
/// Optimized function to parse the Clock sysvar from an AccountInfo.
///
/// This function extracts a `Clock` reference from any type that implements
/// `AsRef<AccountInfo>`, making it compatible with Anchor's `Sysvar<Clock>` wrapper.
///
/// # Arguments
/// * `clock_sysvar` - Any type that implements `AsRef<AccountInfo>` (e.g., `Sysvar<Clock>`, direct `AccountInfo` reference)
///
/// # Returns
/// A reference to the parsed `Clock` with the same lifetime as the input AccountInfo.
///
/// # Example with Anchor
/// ```rust,ignore
/// use anchor_lang::prelude::*;
/// use switchboard_on_demand::clock::parse_clock;
///
/// pub fn my_function(ctx: Context<MyCtx>) -> Result<()> {
/// let MyCtx { sysvars, .. } = ctx.accounts;
/// let clock = parse_clock(&sysvars.clock); // Works with Sysvar<Clock>
///
/// // Use the parsed clock
/// msg!("Current slot: {}", clock.slot);
/// Ok(())
/// }
/// ```
///
/// # Safety
/// This function uses unsafe operations to directly cast the sysvar data to a `Clock`.
/// It is safe because it validates the account key against the Clock sysvar ID first.