light_token_interface/state/mint/
top_up.rs1use light_compressible::compression_info::CompressionInfo;
4use light_program_profiler::profile;
5use light_zero_copy::traits::ZeroCopyAt;
6#[cfg(target_os = "solana")]
7use pinocchio::account_info::AccountInfo;
8
9use super::compressed_mint::ACCOUNT_TYPE_MINT;
10
11pub const MINT_MIN_SIZE_WITH_COMPRESSION: usize = COMPRESSION_INFO_OFFSET + COMPRESSION_INFO_SIZE;
14
15const COMPRESSION_INFO_OFFSET: usize = 166;
18
19const COMPRESSION_INFO_SIZE: usize = 96;
21
22const ACCOUNT_TYPE_OFFSET: usize = 165;
24
25#[inline(always)]
28#[profile]
29pub fn mint_top_up_lamports_from_slice(
30 data: &[u8],
31 current_lamports: u64,
32 current_slot: u64,
33) -> Option<u64> {
34 if data.len() < MINT_MIN_SIZE_WITH_COMPRESSION || data[ACCOUNT_TYPE_OFFSET] != ACCOUNT_TYPE_MINT
35 {
36 return None;
37 }
38
39 let (info, _) = CompressionInfo::zero_copy_at(
40 &data[COMPRESSION_INFO_OFFSET..COMPRESSION_INFO_OFFSET + COMPRESSION_INFO_SIZE],
41 )
42 .ok()?;
43
44 info.calculate_top_up_lamports(data.len() as u64, current_slot, current_lamports)
45 .ok()
46}
47
48#[cfg(target_os = "solana")]
52#[inline(always)]
53#[profile]
54pub fn mint_top_up_lamports_from_account_info(
55 account_info: &AccountInfo,
56 current_slot: &mut u64,
57) -> Option<u64> {
58 use pinocchio::sysvars::{clock::Clock, Sysvar};
59
60 if !account_info.is_owned_by(&crate::LIGHT_TOKEN_PROGRAM_ID) {
62 return None;
63 }
64
65 let data = account_info.try_borrow_data().ok()?;
66
67 if data.len() < MINT_MIN_SIZE_WITH_COMPRESSION || data[ACCOUNT_TYPE_OFFSET] != ACCOUNT_TYPE_MINT
68 {
69 return None;
70 }
71
72 let current_lamports = account_info.lamports();
73 if *current_slot == 0 {
74 *current_slot = Clock::get().ok()?.slot;
75 }
76
77 let (info, _) = CompressionInfo::zero_copy_at(
78 &data[COMPRESSION_INFO_OFFSET..COMPRESSION_INFO_OFFSET + COMPRESSION_INFO_SIZE],
79 )
80 .ok()?;
81
82 info.calculate_top_up_lamports(data.len() as u64, *current_slot, current_lamports)
83 .ok()
84}