use light_compressible::compression_info::CompressionInfo;
use light_program_profiler::profile;
#[cfg(target_os = "solana")]
use pinocchio::account_info::AccountInfo;
use super::ACCOUNT_TYPE_TOKEN_ACCOUNT;
use crate::state::ExtensionType;
pub const MIN_SIZE_WITH_COMPRESSIBLE: usize = COMPRESSION_INFO_OFFSET + COMPRESSION_INFO_SIZE;
const COMPRESSION_INFO_OFFSET: usize = 176;
const COMPRESSION_INFO_SIZE: usize = 96;
const ACCOUNT_TYPE_OFFSET: usize = 165;
const OPTION_DISCRIMINATOR_OFFSET: usize = 166;
const FIRST_EXT_DISCRIMINATOR_OFFSET: usize = 171;
const OPTION_SOME: u8 = 1;
#[inline(always)]
#[profile]
pub fn top_up_lamports_from_slice(
data: &[u8],
current_lamports: u64,
current_slot: u64,
) -> Option<u64> {
if data.len() < MIN_SIZE_WITH_COMPRESSIBLE
|| data[ACCOUNT_TYPE_OFFSET] != ACCOUNT_TYPE_TOKEN_ACCOUNT
|| data[OPTION_DISCRIMINATOR_OFFSET] != OPTION_SOME
|| data[FIRST_EXT_DISCRIMINATOR_OFFSET] != ExtensionType::Compressible as u8
{
return None;
}
let info: &CompressionInfo = bytemuck::from_bytes(
&data[COMPRESSION_INFO_OFFSET..COMPRESSION_INFO_OFFSET + COMPRESSION_INFO_SIZE],
);
info.calculate_top_up_lamports(data.len() as u64, current_slot, current_lamports)
.ok()
}
#[cfg(target_os = "solana")]
#[inline(always)]
#[profile]
pub fn top_up_lamports_from_account_info_unchecked(
account_info: &AccountInfo,
current_slot: &mut u64,
) -> Option<u64> {
use pinocchio::sysvars::{clock::Clock, Sysvar};
let data = account_info.try_borrow_data().ok()?;
let current_lamports = account_info.lamports();
if *current_slot == 0 {
*current_slot = Clock::get().ok()?.slot;
}
top_up_lamports_from_slice(&data, current_lamports, *current_slot)
}