1use {
4 crate::{error::SinglePoolError, find_pool_address},
5 borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
6 solana_program::{
7 account_info::AccountInfo, borsh1::try_from_slice_unchecked, program_error::ProgramError,
8 pubkey::Pubkey,
9 },
10};
11
12#[derive(Clone, Debug, Default, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
14pub enum SinglePoolAccountType {
15 #[default]
17 Uninitialized,
18 Pool,
20}
21
22#[derive(Clone, Debug, Default, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
24pub struct SinglePool {
25 pub account_type: SinglePoolAccountType,
27 pub vote_account_address: Pubkey,
29}
30impl SinglePool {
31 pub fn from_account_info(
33 account_info: &AccountInfo,
34 program_id: &Pubkey,
35 ) -> Result<Self, ProgramError> {
36 if account_info.data_len() == 0 || account_info.owner != program_id {
38 return Err(SinglePoolError::InvalidPoolAccount.into());
39 }
40
41 let pool = try_from_slice_unchecked::<SinglePool>(&account_info.data.borrow())?;
42
43 if pool.account_type != SinglePoolAccountType::Pool {
45 return Err(SinglePoolError::InvalidPoolAccount.into());
46 }
47
48 if *account_info.key != find_pool_address(program_id, &pool.vote_account_address) {
52 return Err(SinglePoolError::InvalidPoolAccount.into());
53 }
54
55 Ok(pool)
56 }
57}