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
use arrayref::{array_ref, array_refs};
use solana_program::{
account_info::AccountInfo, program_error::ProgramError, program_option::COption, pubkey::Pubkey,
};
fn unpack_coption_key(src: &[u8; 36]) -> Result<COption<Pubkey>, ProgramError> {
let (tag, body) = array_refs![src, 4, 32];
match *tag {
[0, 0, 0, 0] => Ok(COption::None),
[1, 0, 0, 0] => Ok(COption::Some(Pubkey::new_from_array(*body))),
_ => Err(ProgramError::InvalidAccountData),
}
}
pub fn get_owner_from_token_account(
token_account_info: &AccountInfo,
) -> Result<Pubkey, ProgramError> {
let data = token_account_info.try_borrow_data()?;
let owner_data = array_ref![data, 32, 32];
Ok(Pubkey::new_from_array(*owner_data))
}
pub fn get_mint_authority(account_info: &AccountInfo) -> Result<COption<Pubkey>, ProgramError> {
let data = account_info.try_borrow_data().unwrap();
let authority_bytes = array_ref![data, 0, 36];
unpack_coption_key(authority_bytes)
}
pub fn get_mint_freeze_authority(
account_info: &AccountInfo,
) -> Result<COption<Pubkey>, ProgramError> {
let data = account_info.try_borrow_data().unwrap();
let authority_bytes = array_ref![data, 36 + 8 + 1 + 1, 36];
unpack_coption_key(authority_bytes)
}
pub fn get_mint_supply(account_info: &AccountInfo) -> Result<u64, ProgramError> {
let data = account_info.try_borrow_data()?;
if data.is_empty() {
return Err(ProgramError::InvalidAccountData);
}
let bytes = array_ref![data, 36, 8];
Ok(u64::from_le_bytes(*bytes))
}
pub fn get_mint_decimals(account_info: &AccountInfo) -> Result<u8, ProgramError> {
let data = account_info.try_borrow_data()?;
if data.is_empty() {
return Err(ProgramError::InvalidAccountData);
}
Ok(data[44])
}