Implement Wrap for solitaire::Sysvar<...>

Change-Id: I066e425d1e1549ea296a4b54ef019872064a8acd
This commit is contained in:
Stan Drozd 2021-06-16 12:09:06 +02:00
parent 834b26b773
commit e1c2f73e29
1 changed files with 26 additions and 2 deletions

View File

@ -5,6 +5,7 @@
//! Client-specific code
pub use solana_program::pubkey::Pubkey;
use solana_program::sysvar::Sysvar as SolSysvar;
pub use solana_sdk;
pub use solana_sdk::{
@ -20,7 +21,10 @@ pub use solana_sdk::{
use borsh::BorshSerialize;
use solitaire::AccountState;
use solitaire::{
AccountState,
Sysvar,
};
pub use solitaire::{
Data,
Derive,
@ -52,7 +56,7 @@ pub enum AccEntry {
CPIProgramSigner(Keypair),
/// Key decided from SPL constants
Sysvar,
Sysvar(Pubkey),
/// Key derived from constants and/or program address
Derived(Pubkey),
@ -146,6 +150,26 @@ where
}
}
impl<'b, Var> Wrap for Sysvar<'b, Var>
where
Var: SolSysvar,
{
fn wrap(a: &AccEntry) -> StdResult<Vec<AccountMeta>, ErrBox> {
if let AccEntry::Sysvar(k) = a {
if Var::check_id(k) {
Ok(vec![AccountMeta::new_readonly(k.clone(), false)])
} else {
Err(format!("{} does not point at sysvar {}", k, std::any::type_name::<Var>()).into())
}
} else {
Err(format!(
"{} must be passed as Sysvar",
std::any::type_name::<Self>()
).into())
}
}
}
/// Trait used on client side to easily validate a program accounts + ix_data for a bare Solana call
pub trait ToInstruction {
fn to_ix(