use {
crate::{error::SinglePoolError, find_pool_address},
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
solana_account_info::AccountInfo,
solana_borsh::v1::try_from_slice_unchecked,
solana_program_error::ProgramError,
solana_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 const fn size_of() -> usize {
33
}
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)
}
}
#[cfg(test)]
mod tests {
use {super::*, solana_borsh::v1::get_packed_len};
#[test]
fn single_pool_size_of() {
assert_eq!(SinglePool::size_of(), get_packed_len::<SinglePool>());
}
}