Skip to content

Commit 81ff242

Browse files
authored
Merge pull request #79 from staratlasmeta/sysvars
Feat: sysvar account set
2 parents 48e34a8 + 4947039 commit 81ff242

File tree

5 files changed

+70
-23
lines changed

5 files changed

+70
-23
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ daggy = "0.8.0"
2828
derivative = "2.2.0"
2929
derive_more = "0.99.17"
3030
easy_proc = "0.3.0"
31-
fixed = "1.26.0"
31+
fixed = "=1.26.0"
3232
half = "2.4.0"
3333
heck = "0.5.0"
3434
itertools = "0.12.1"

framework/star_frame/src/account_set/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod program;
55
mod rest;
66
mod single_set;
77
mod system_account;
8+
mod sysvar;
89

910
pub use data_account::*;
1011
pub use impls::*;
@@ -14,6 +15,7 @@ pub use rest::*;
1415
pub use single_set::*;
1516
pub use star_frame_proc::AccountSet;
1617
pub use system_account::*;
18+
pub use sysvar::*;
1719

1820
use crate::syscalls::{SyscallAccountCache, SyscallInvoke};
1921
use crate::Result;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use solana_program::sysvar::{Sysvar as SysvarTrait, SysvarId};
2+
use star_frame::prelude::*;
3+
use std::marker::PhantomData;
4+
5+
#[derive(AccountSet, Debug, Clone)]
6+
#[idl(generics = [])]
7+
#[validate(generics = [])]
8+
pub struct Sysvar<'info, T>
9+
where
10+
T: SysvarId,
11+
{
12+
#[single_account_set]
13+
#[idl(arg = T::id())]
14+
#[validate(arg = &T::id())]
15+
info: AccountInfo<'info>,
16+
#[account_set(skip = PhantomData)]
17+
phantom_t: PhantomData<fn() -> T>,
18+
}
19+
20+
impl<'info, T> Sysvar<'info, T>
21+
where
22+
T: SysvarTrait,
23+
{
24+
pub fn from_account_info(info: &AccountInfo<'info>) -> Result<T> {
25+
let sysvar = T::from_account_info(info)?;
26+
Ok(sysvar)
27+
}
28+
}

framework/star_frame/src/program/system_program.rs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::empty_star_frame_instruction;
22
use crate::prelude::*;
33
use borsh::{BorshDeserialize, BorshSerialize};
4+
use solana_program::rent::Rent;
45
use solana_program::system_program;
6+
#[allow(deprecated)]
7+
use solana_program::sysvar::recent_blockhashes::RecentBlockhashes;
58

69
/// Solana's system program.
710
#[derive(Debug, Copy, Clone, Align1)]
@@ -95,43 +98,57 @@ empty_star_frame_instruction!(Transfer, TransferAccounts);
9598
#[instruction_to_idl(program = SystemProgram)]
9699
pub struct AdvanceNonceAccount;
97100
/// Accounts for the [`AdvanceNonceAccount`] instruction.
98-
#[derive(Debug, Clone, AccountSet)]
99-
pub struct AdvanceNonceAccountAccounts<'info> {
100-
pub nonce_account: Mut<AccountInfo<'info>>,
101-
// todo: sysvars!
102-
pub recent_blockhashes: AccountInfo<'info>,
103-
pub nonce_authority: Signer<AccountInfo<'info>>,
101+
#[allow(deprecated)]
102+
mod advance_nonce {
103+
use super::*;
104+
#[derive(Debug, Clone, AccountSet)]
105+
pub struct AdvanceNonceAccountAccounts<'info> {
106+
pub nonce_account: Mut<AccountInfo<'info>>,
107+
pub recent_blockhashes: Sysvar<'info, RecentBlockhashes>,
108+
pub nonce_authority: Signer<AccountInfo<'info>>,
109+
}
104110
}
111+
pub use advance_nonce::*;
105112
empty_star_frame_instruction!(AdvanceNonceAccount, AdvanceNonceAccountAccounts);
106113

107114
// WithdrawNonceAccount
108115
/// Withdraws funds from a nonce account.
109116
#[derive(Copy, Clone, Debug, Eq, PartialEq, InstructionToIdl, BorshDeserialize, BorshSerialize)]
110117
#[instruction_to_idl(program = SystemProgram)]
111118
pub struct WithdrawNonceAccount(pub u64);
112-
/// Accounts for the [`WithdrawNonceAccount`] instruction.
113-
#[derive(Debug, Clone, AccountSet)]
114-
pub struct WithdrawNonceAccountAccounts<'info> {
115-
pub nonce_account: Mut<AccountInfo<'info>>,
116-
pub recipient: Mut<AccountInfo<'info>>,
117-
pub recent_blockhashes: AccountInfo<'info>,
118-
pub rent: AccountInfo<'info>,
119-
pub nonce_authority: Signer<AccountInfo<'info>>,
119+
#[allow(deprecated)]
120+
mod withdraw_nonce {
121+
use super::*;
122+
/// Accounts for the [`WithdrawNonceAccount`] instruction.
123+
#[derive(Debug, Clone, AccountSet)]
124+
pub struct WithdrawNonceAccountAccounts<'info> {
125+
pub nonce_account: Mut<AccountInfo<'info>>,
126+
pub recipient: Mut<AccountInfo<'info>>,
127+
pub recent_blockhashes: Sysvar<'info, RecentBlockhashes>,
128+
pub rent: Sysvar<'info, Rent>,
129+
pub nonce_authority: Signer<AccountInfo<'info>>,
130+
}
120131
}
132+
pub use withdraw_nonce::*;
121133
empty_star_frame_instruction!(WithdrawNonceAccount, WithdrawNonceAccountAccounts);
122134

123135
// InitializeNonceAccount
124136
/// Drives the state of an uninitialized nonce account to initialized, setting the nonce value.
125137
#[derive(Copy, Clone, Debug, Eq, PartialEq, InstructionToIdl, BorshDeserialize, BorshSerialize)]
126138
#[instruction_to_idl(program = SystemProgram)]
127139
pub struct InitializeNonceAccount(pub Pubkey);
128-
/// Accounts for the [`InitializeNonceAccount`] instruction.
129-
#[derive(Debug, Clone, AccountSet)]
130-
pub struct InitializeNonceAccountAccounts<'info> {
131-
pub nonce_account: Mut<AccountInfo<'info>>,
132-
pub recent_blockhashes: AccountInfo<'info>,
133-
pub rent: AccountInfo<'info>,
140+
#[allow(deprecated)]
141+
mod initialize_nonce {
142+
use super::*;
143+
/// Accounts for the [`InitializeNonceAccount`] instruction.
144+
#[derive(Debug, Clone, AccountSet)]
145+
pub struct InitializeNonceAccountAccounts<'info> {
146+
pub nonce_account: Mut<AccountInfo<'info>>,
147+
pub recent_blockhashes: Sysvar<'info, RecentBlockhashes>,
148+
pub rent: Sysvar<'info, Rent>,
149+
}
134150
}
151+
pub use initialize_nonce::*;
135152
empty_star_frame_instruction!(InitializeNonceAccount, InitializeNonceAccountAccounts);
136153

137154
// AuthorizeNonceAccount

0 commit comments

Comments
 (0)