use {
crate::{error::SinglePoolError, find_pool_address},
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
solana_program::{
account_info::AccountInfo, borsh0_10::try_from_slice_unchecked,
program_error::ProgramError, pubkey::Pubkey,
},
};
#[derive(Clone, Debug, Default, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub enum SinglePoolAccountType {
#[default]
Uninitialized,
Pool,
}
#[derive(Clone, Debug, Default, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct SinglePool {
pub account_type: SinglePoolAccountType,
pub vote_account_address: Pubkey,
}
impl SinglePool {
pub fn from_account_info(
account_info: &AccountInfo,
program_id: &Pubkey,
) -> Result<Self, ProgramError> {
if account_info.data_len() == 0 || account_info.owner != program_id {
return Err(SinglePoolError::InvalidPoolAccount.into());
}
let pool = try_from_slice_unchecked::<SinglePool>(&account_info.data.borrow())?;
if pool.account_type != SinglePoolAccountType::Pool {
return Err(SinglePoolError::InvalidPoolAccount.into());
}
if *account_info.key != find_pool_address(program_id, &pool.vote_account_address) {
return Err(SinglePoolError::InvalidPoolAccount.into());
}
Ok(pool)
}
}