solana_program/sysvar/clock.rs
1//! Information about the network’s clock, ticks, slots, etc.
2//!
3//! The _clock sysvar_ provides access to the [`Clock`] type, which includes the
4//! current slot, the current epoch, and the approximate real-world time of the
5//! slot.
6//!
7//! [`Clock`] implements [`Sysvar::get`] and can be loaded efficiently without
8//! passing the sysvar account ID to the program.
9//!
10//! See also the Solana [documentation on the clock sysvar][sdoc].
11//!
12//! [sdoc]: https://docs.solanalabs.com/runtime/sysvars#clock
13//!
14//! # Examples
15//!
16//! Accessing via on-chain program directly:
17//!
18//! ```no_run
19//! # use solana_program::{
20//! # account_info::{AccountInfo, next_account_info},
21//! # entrypoint::ProgramResult,
22//! # msg,
23//! # pubkey::Pubkey,
24//! # sysvar::clock::{self, Clock},
25//! # sysvar::Sysvar,
26//! # };
27//! # use solana_program::program_error::ProgramError;
28//! #
29//! fn process_instruction(
30//! program_id: &Pubkey,
31//! accounts: &[AccountInfo],
32//! instruction_data: &[u8],
33//! ) -> ProgramResult {
34//!
35//! let clock = Clock::get()?;
36//! msg!("clock: {:#?}", clock);
37//!
38//! Ok(())
39//! }
40//! #
41//! # use solana_sysvar_id::SysvarId;
42//! # let p = Clock::id();
43//! # let l = &mut 1169280;
44//! # let d = &mut vec![240, 153, 233, 7, 0, 0, 0, 0, 11, 115, 118, 98, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 0, 0, 52, 1, 0, 0, 0, 0, 0, 0, 121, 50, 119, 98, 0, 0, 0, 0];
45//! # let a = AccountInfo::new(&p, false, false, l, d, &p, false, 0);
46//! # let accounts = &[a.clone(), a];
47//! # process_instruction(
48//! # &Pubkey::new_unique(),
49//! # accounts,
50//! # &[],
51//! # )?;
52//! # Ok::<(), ProgramError>(())
53//! ```
54//!
55//! Accessing via on-chain program's account parameters:
56//!
57//! ```
58//! # use solana_program::{
59//! # account_info::{AccountInfo, next_account_info},
60//! # entrypoint::ProgramResult,
61//! # msg,
62//! # pubkey::Pubkey,
63//! # sysvar::clock::{self, Clock},
64//! # sysvar::Sysvar,
65//! # };
66//! # use solana_program::program_error::ProgramError;
67//! #
68//! fn process_instruction(
69//! program_id: &Pubkey,
70//! accounts: &[AccountInfo],
71//! instruction_data: &[u8],
72//! ) -> ProgramResult {
73//! let account_info_iter = &mut accounts.iter();
74//! let clock_account_info = next_account_info(account_info_iter)?;
75//!
76//! assert!(clock::check_id(clock_account_info.key));
77//!
78//! let clock = Clock::from_account_info(clock_account_info)?;
79//! msg!("clock: {:#?}", clock);
80//!
81//! Ok(())
82//! }
83//! #
84//! # use solana_sysvar_id::SysvarId;
85//! # let p = Clock::id();
86//! # let l = &mut 1169280;
87//! # let d = &mut vec![240, 153, 233, 7, 0, 0, 0, 0, 11, 115, 118, 98, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 0, 0, 52, 1, 0, 0, 0, 0, 0, 0, 121, 50, 119, 98, 0, 0, 0, 0];
88//! # let a = AccountInfo::new(&p, false, false, l, d, &p, false, 0);
89//! # let accounts = &[a.clone(), a];
90//! # process_instruction(
91//! # &Pubkey::new_unique(),
92//! # accounts,
93//! # &[],
94//! # )?;
95//! # Ok::<(), ProgramError>(())
96//! ```
97//!
98//! Accessing via the RPC client:
99//!
100//! ```
101//! # use solana_program::example_mocks::solana_sdk;
102//! # use solana_program::example_mocks::solana_rpc_client;
103//! # use solana_sdk::account::Account;
104//! # use solana_rpc_client::rpc_client::RpcClient;
105//! # use solana_sdk::sysvar::clock::{self, Clock};
106//! # use anyhow::Result;
107//! #
108//! fn print_sysvar_clock(client: &RpcClient) -> Result<()> {
109//! # client.set_get_account_response(clock::ID, Account {
110//! # lamports: 1169280,
111//! # data: vec![240, 153, 233, 7, 0, 0, 0, 0, 11, 115, 118, 98, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 0, 0, 52, 1, 0, 0, 0, 0, 0, 0, 121, 50, 119, 98, 0, 0, 0, 0],
112//! # owner: solana_sdk::system_program::ID,
113//! # executable: false,
114//! # rent_epoch: 307,
115//! # });
116//! #
117//! let clock = client.get_account(&clock::ID)?;
118//! let data: Clock = bincode::deserialize(&clock.data)?;
119//!
120//! Ok(())
121//! }
122//! #
123//! # let client = RpcClient::new(String::new());
124//! # print_sysvar_clock(&client)?;
125//! #
126//! # Ok::<(), anyhow::Error>(())
127//! ```
128
129use crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar};
130pub use solana_clock::{
131 sysvar::{check_id, id, ID},
132 Clock,
133};
134
135impl Sysvar for Clock {
136 impl_sysvar_get!(sol_get_clock_sysvar);
137}