1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! ProgramMetadata Account

use {
    crate::state::enums::GovernanceAccountType,
    borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
    solana_program::{
        account_info::AccountInfo, clock::Slot, program_error::ProgramError,
        program_pack::IsInitialized, pubkey::Pubkey,
    },
    spl_governance_tools::account::{get_account_data, AccountMaxSize},
};

/// Program metadata account. It stores information about the particular
/// SPL-Governance program instance
#[derive(Clone, Debug, PartialEq, Eq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct ProgramMetadata {
    /// Governance account type
    pub account_type: GovernanceAccountType,

    /// The slot when the metadata was captured
    pub updated_at: Slot,

    /// The version of the program
    /// Max 11 characters XXX.YYY.ZZZ
    pub version: String,

    /// Reserved
    pub reserved: [u8; 64],
}

impl AccountMaxSize for ProgramMetadata {
    fn get_max_size(&self) -> Option<usize> {
        Some(88)
    }
}

impl IsInitialized for ProgramMetadata {
    fn is_initialized(&self) -> bool {
        self.account_type == GovernanceAccountType::ProgramMetadata
    }
}

/// Returns ProgramMetadata PDA address
pub fn get_program_metadata_address(program_id: &Pubkey) -> Pubkey {
    Pubkey::find_program_address(&get_program_metadata_seeds(), program_id).0
}

/// Returns ProgramMetadata PDA seeds
pub fn get_program_metadata_seeds<'a>() -> [&'a [u8]; 1] {
    [b"metadata"]
}

/// Deserializes account and checks owner program
pub fn get_program_metadata_data(
    program_id: &Pubkey,
    program_metadata_info: &AccountInfo,
) -> Result<ProgramMetadata, ProgramError> {
    get_account_data::<ProgramMetadata>(program_id, program_metadata_info)
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn test_max_size() {
        let program_metadata_data = ProgramMetadata {
            account_type: GovernanceAccountType::TokenOwnerRecordV2,
            updated_at: 10,
            reserved: [0; 64],
            version: "111.122.155".to_string(),
        };

        let size = program_metadata_data.try_to_vec().unwrap().len();

        assert_eq!(program_metadata_data.get_max_size(), Some(size));
    }
}