Expand description
Access to special accounts with dynamically-updated data.
Sysvars are special accounts that contain dynamically-updated data about the
network cluster, the blockchain history, and the executing transaction. Each
sysvar is defined in its own submodule within this module. The clock
,
epoch_schedule
, instructions
, and rent
sysvars are most useful
to on-chain programs.
Simple sysvars implement the Sysvar::get
method, which loads a sysvar
directly from the runtime, as in this example that logs the clock
sysvar:
use solana_program::{
account_info::AccountInfo,
clock,
entrypoint::ProgramResult,
msg,
pubkey::Pubkey,
sysvar::Sysvar,
};
fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
let clock = clock::Clock::get()?;
msg!("clock: {:#?}", clock);
Ok(())
}
Since Solana sysvars are accounts, if the AccountInfo
is provided to the
program, then the program can deserialize the sysvar with
Sysvar::from_account_info
to access its data, as in this example that
again logs the clock
sysvar.
use solana_program::{
account_info::{next_account_info, AccountInfo},
clock,
entrypoint::ProgramResult,
msg,
pubkey::Pubkey,
sysvar::Sysvar,
};
fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let clock_account = next_account_info(account_info_iter)?;
let clock = clock::Clock::from_account_info(&clock_account)?;
msg!("clock: {:#?}", clock);
Ok(())
}
When possible, programs should prefer to call Sysvar::get
instead of
deserializing with Sysvar::from_account_info
, as the latter imposes extra
overhead of deserialization while also requiring the sysvar account address
be passed to the program, wasting the limited space available to
transactions. Deserializing sysvars that can instead be retrieved with
Sysvar::get
should be only be considered for compatibility with older
programs that pass around sysvar accounts.
Some sysvars are too large to deserialize within a program, and
Sysvar::from_account_info
returns an error, or the serialization attempt
will exhaust the program’s compute budget. Some sysvars do not implement
Sysvar::get
and return an error. Some sysvars have custom deserializers
that do not implement the Sysvar
trait. These cases are documented in the
modules for individual sysvars.
All sysvar accounts are owned by the account identified by sysvar::ID
.
For more details see the Solana documentation on sysvars.
Re-exports§
pub use sysvar_ids::ALL_IDS;
Deprecated pub use sysvar_ids::ALL_IDS;
Deprecated
Modules§
- clock
- Information about the network’s clock, ticks, slots, etc.
- epoch_
rewards - Epoch rewards for current epoch
- epoch_
schedule - Information about epoch duration.
- fees
- Current cluster fees.
- instructions
- The serialized instructions of the current transaction.
- last_
restart_ slot - Information about the last restart slot (hard fork).
- recent_
blockhashes - Information about recent blocks and their fee calculators.
- rent
- Configuration for network rent.
- rewards
- This sysvar is deprecated and unused.
- slot_
hashes - The most recent hashes of a slot’s parent banks.
- slot_
history - A bitvector of slots present over the last epoch.
- stake_
history - History of stake activations and de-activations.
Macros§
- declare_
deprecated_ sysvar_ id - Same as
declare_sysvar_id
except that it reports that this ID has been deprecated. - declare_
sysvar_ id - Declares an ID that implements
SysvarId
.
Structs§
- ALL_IDS
Deprecated
Constants§
- ID
- The const program ID.
Traits§
- Sysvar
- A type that holds sysvar data.
- Sysvar
Id - A type that holds sysvar data and has an associated sysvar
Pubkey
.
Functions§
- check_
id - Returns
true
if given pubkey is the program ID. - id
- Returns the program ID.
- is_
sysvar_ id Deprecated - Returns
true
of the givenPubkey
is a sysvar account.