123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- use crate::{
- AccountDeserialize, AccountSerialize, Accounts, AccountsExit, CpiAccount, ToAccountInfo,
- ToAccountInfos, ToAccountMetas,
- };
- use solana_program::account_info::AccountInfo;
- use solana_program::entrypoint::ProgramResult;
- use solana_program::instruction::AccountMeta;
- use solana_program::program_error::ProgramError;
- use solana_program::pubkey::Pubkey;
- use std::ops::{Deref, DerefMut};
- /// Boxed container for the program state singleton.
- #[derive(Clone)]
- pub struct ProgramState<'info, T: AccountSerialize + AccountDeserialize + Clone> {
- inner: Box<Inner<'info, T>>,
- }
- #[derive(Clone)]
- struct Inner<'info, T: AccountSerialize + AccountDeserialize + Clone> {
- info: AccountInfo<'info>,
- account: T,
- }
- impl<'a, T: AccountSerialize + AccountDeserialize + Clone> ProgramState<'a, T> {
- pub fn new(info: AccountInfo<'a>, account: T) -> ProgramState<'a, T> {
- Self {
- inner: Box::new(Inner { info, account }),
- }
- }
- /// Deserializes the given `info` into a `ProgramState`.
- #[inline(never)]
- pub fn try_from(info: &AccountInfo<'a>) -> Result<ProgramState<'a, T>, ProgramError> {
- let mut data: &[u8] = &info.try_borrow_data()?;
- Ok(ProgramState::new(
- info.clone(),
- T::try_deserialize(&mut data)?,
- ))
- }
- pub fn seed() -> &'static str {
- "unversioned"
- }
- pub fn address(program_id: &Pubkey) -> Pubkey {
- let (base, _nonce) = Pubkey::find_program_address(&[], program_id);
- let seed = Self::seed();
- let owner = program_id;
- Pubkey::create_with_seed(&base, seed, owner).unwrap()
- }
- }
- impl<'info, T> Accounts<'info> for ProgramState<'info, T>
- where
- T: AccountSerialize + AccountDeserialize + Clone,
- {
- #[inline(never)]
- fn try_accounts(
- program_id: &Pubkey,
- accounts: &mut &[AccountInfo<'info>],
- ) -> Result<Self, ProgramError> {
- if accounts.len() == 0 {
- return Err(ProgramError::NotEnoughAccountKeys);
- }
- let account = &accounts[0];
- *accounts = &accounts[1..];
- if account.key != &Self::address(program_id) {
- return Err(ProgramError::Custom(1)); // todo: proper error.
- }
- let pa = ProgramState::try_from(account)?;
- if pa.inner.info.owner != program_id {
- return Err(ProgramError::Custom(1)); // todo: proper error.
- }
- Ok(pa)
- }
- }
- impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountMetas
- for ProgramState<'info, T>
- {
- fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
- let is_signer = is_signer.unwrap_or(self.inner.info.is_signer);
- let meta = match self.inner.info.is_writable {
- false => AccountMeta::new_readonly(*self.inner.info.key, is_signer),
- true => AccountMeta::new(*self.inner.info.key, is_signer),
- };
- vec![meta]
- }
- }
- impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfos<'info>
- for ProgramState<'info, T>
- {
- fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
- vec![self.inner.info.clone()]
- }
- }
- impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfo<'info>
- for ProgramState<'info, T>
- {
- fn to_account_info(&self) -> AccountInfo<'info> {
- self.inner.info.clone()
- }
- }
- impl<'a, T: AccountSerialize + AccountDeserialize + Clone> Deref for ProgramState<'a, T> {
- type Target = T;
- fn deref(&self) -> &Self::Target {
- &(*self.inner).account
- }
- }
- impl<'a, T: AccountSerialize + AccountDeserialize + Clone> DerefMut for ProgramState<'a, T> {
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut DerefMut::deref_mut(&mut self.inner).account
- }
- }
- impl<'info, T> From<CpiAccount<'info, T>> for ProgramState<'info, T>
- where
- T: AccountSerialize + AccountDeserialize + Clone,
- {
- fn from(a: CpiAccount<'info, T>) -> Self {
- Self::new(a.to_account_info(), Deref::deref(&a).clone())
- }
- }
- impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info>
- for ProgramState<'info, T>
- {
- fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
- let info = self.to_account_info();
- let mut data = info.try_borrow_mut_data()?;
- let dst: &mut [u8] = &mut data;
- let mut cursor = std::io::Cursor::new(dst);
- self.inner.account.try_serialize(&mut cursor)?;
- Ok(())
- }
- }
|