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
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use solana_program::{
account_info::AccountInfo, clock::Slot, program_error::ProgramError,
program_pack::IsInitialized, pubkey::Pubkey,
};
use spl_governance_tools::account::{get_account_data, AccountMaxSize};
use crate::state::enums::GovernanceAccountType;
#[repr(C)]
#[derive(Clone, Debug, PartialEq, BorshDeserialize, BorshSerialize, BorshSchema)]
pub struct ProgramMetadata {
pub account_type: GovernanceAccountType,
pub updated_at: Slot,
pub version: String,
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
}
}
pub fn get_program_metadata_address(program_id: &Pubkey) -> Pubkey {
Pubkey::find_program_address(&get_program_metadata_seeds(), program_id).0
}
pub fn get_program_metadata_seeds<'a>() -> [&'a [u8]; 1] {
[b"metadata"]
}
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));
}
}