Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 813aa33

Browse files
authored
Update sysvar example (#1795)
1 parent 929dd59 commit 813aa33

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

examples/rust/sysvar/src/processor.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,28 @@ pub fn process_instruction(
1717
// Create in iterator to safety reference accounts in the slice
1818
let account_info_iter = &mut accounts.iter();
1919

20-
// The first account is the clock sysvar
20+
// Get the clock sysvar via syscall
21+
let clock_via_sysvar = Clock::get()?;
22+
// Or deserialize the account into a clock struct
2123
let clock_sysvar_info = next_account_info(account_info_iter)?;
22-
// The second account is the rent sysvar
23-
let rent_sysvar_info = next_account_info(account_info_iter)?;
24-
25-
// Deserialize the account into a clock struct
26-
let clock = Clock::from_account_info(&clock_sysvar_info)?;
27-
28-
// Deserialize the account into a rent struct
29-
let rent = Rent::from_account_info(&rent_sysvar_info)?;
30-
24+
let clock_via_account = Clock::from_account_info(&clock_sysvar_info)?;
25+
// Both produce the same sysvar
26+
assert_eq!(clock_via_sysvar, clock_via_account);
3127
// Note: `format!` can be very expensive, use cautiously
32-
msg!("{:?}", clock);
28+
msg!("{:?}", clock_via_sysvar);
29+
30+
// Get the rent sysvar via syscall
31+
let rent_via_sysvar = Rent::get()?;
32+
// Or deserialize the account into a rent struct
33+
let rent_sysvar_info = next_account_info(account_info_iter)?;
34+
let rent_via_account = Rent::from_account_info(&rent_sysvar_info)?;
35+
// Both produce the same sysvar
36+
assert_eq!(rent_via_sysvar, rent_via_account);
3337
// Can't print `exemption_threshold` because BPF does not support printing floats
3438
msg!(
3539
"Rent: lamports_per_byte_year: {:?}, burn_percent: {:?}",
36-
rent.lamports_per_byte_year,
37-
rent.burn_percent
40+
rent_via_sysvar.lamports_per_byte_year,
41+
rent_via_sysvar.burn_percent
3842
);
3943

4044
Ok(())

0 commit comments

Comments
 (0)