light_token_interface/state/mint/
top_up.rs

1//! Optimized top-up lamports calculation for Mint accounts.
2
3use 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
11/// Minimum size for Mint with CompressionInfo.
12/// 166 (offset to CompressionInfo) + 96 (CompressionInfo size) = 262
13pub const MINT_MIN_SIZE_WITH_COMPRESSION: usize = COMPRESSION_INFO_OFFSET + COMPRESSION_INFO_SIZE;
14
15/// Offset to CompressionInfo in Mint.
16/// 82 (BaseMint) + 67 (metadata) + 16 (reserved) + 1 (account_type) = 166
17const COMPRESSION_INFO_OFFSET: usize = 166;
18
19/// Size of CompressionInfo struct (96 bytes).
20const COMPRESSION_INFO_SIZE: usize = 96;
21
22/// Offset to account_type field.
23const ACCOUNT_TYPE_OFFSET: usize = 165;
24
25/// Calculate top-up lamports directly from Mint account bytes.
26/// Returns None if account is not a valid Mint.
27#[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/// Calculate top-up lamports from a Mint AccountInfo.
49/// Verifies account owner is the Token program. Returns None if owner mismatch or invalid.
50/// Pass `current_slot` as 0 to fetch from Clock sysvar; non-zero values are used directly.
51#[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    // Check owner is Token program
61    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}