use dashmap::DashMap;
use once_cell::sync::Lazy;
use solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
};
use std::sync::Arc;
use crate::common::{
spl_associated_token_account::get_associated_token_address_with_program_id,
spl_token::close_account,
};
use crate::perf::compiler_optimization::CompileTimeOptimizedEventProcessor;
static COMPILE_TIME_HASH: CompileTimeOptimizedEventProcessor =
CompileTimeOptimizedEventProcessor::new();
const MAX_PDA_CACHE_SIZE: usize = 100_000;
const MAX_ATA_CACHE_SIZE: usize = 100_000;
const MAX_INSTRUCTION_CACHE_SIZE: usize = 100_000;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum InstructionCacheKey {
CreateAssociatedTokenAccount {
payer: Pubkey,
owner: Pubkey,
mint: Pubkey,
token_program: Pubkey,
use_seed: bool,
},
CloseWsolAccount { payer: Pubkey, wsol_token_account: Pubkey },
}
static INSTRUCTION_CACHE: Lazy<DashMap<InstructionCacheKey, Arc<Vec<Instruction>>>> =
Lazy::new(|| DashMap::with_capacity(MAX_INSTRUCTION_CACHE_SIZE));
#[inline]
pub fn get_cached_instructions<F>(
cache_key: InstructionCacheKey,
compute_fn: F,
) -> Arc<Vec<Instruction>>
where
F: FnOnce() -> Vec<Instruction>,
{
let _hash = match &cache_key {
InstructionCacheKey::CreateAssociatedTokenAccount { payer, .. } => {
let bytes = payer.to_bytes();
COMPILE_TIME_HASH.hash_lookup_optimized(bytes[0])
}
InstructionCacheKey::CloseWsolAccount { payer, .. } => {
let bytes = payer.to_bytes();
COMPILE_TIME_HASH.hash_lookup_optimized(bytes[0])
}
};
INSTRUCTION_CACHE.entry(cache_key).or_insert_with(|| Arc::new(compute_fn())).clone()
}
pub fn create_associated_token_account_idempotent_fast_use_seed(
payer: &Pubkey,
owner: &Pubkey,
mint: &Pubkey,
token_program: &Pubkey,
use_seed: bool,
) -> Vec<Instruction> {
_create_associated_token_account_idempotent_fast(payer, owner, mint, token_program, use_seed)
}
pub fn create_associated_token_account_idempotent_fast(
payer: &Pubkey,
owner: &Pubkey,
mint: &Pubkey,
token_program: &Pubkey,
) -> Vec<Instruction> {
_create_associated_token_account_idempotent_fast(payer, owner, mint, token_program, false)
}
pub fn _create_associated_token_account_idempotent_fast(
payer: &Pubkey,
owner: &Pubkey,
mint: &Pubkey,
token_program: &Pubkey,
use_seed: bool,
) -> Vec<Instruction> {
let cache_key = InstructionCacheKey::CreateAssociatedTokenAccount {
payer: *payer,
owner: *owner,
mint: *mint,
token_program: *token_program,
use_seed,
};
let arc_instructions = if use_seed
&& !mint.eq(&crate::constants::WSOL_TOKEN_ACCOUNT)
&& !mint.eq(&crate::constants::SOL_TOKEN_ACCOUNT)
&& (token_program.eq(&crate::constants::TOKEN_PROGRAM)
|| token_program.eq(&crate::constants::TOKEN_PROGRAM_2022))
{
get_cached_instructions(cache_key, || {
super::seed::create_associated_token_account_use_seed(payer, owner, mint, token_program)
.unwrap()
})
} else {
get_cached_instructions(cache_key, || {
let associated_token_address =
get_associated_token_address_with_program_id_fast(owner, mint, token_program);
vec![Instruction {
program_id: crate::constants::ASSOCIATED_TOKEN_PROGRAM_ID,
accounts: vec![
AccountMeta::new(*payer, true), AccountMeta::new(associated_token_address, false), AccountMeta::new_readonly(*owner, false), AccountMeta::new_readonly(*mint, false), crate::constants::SYSTEM_PROGRAM_META,
AccountMeta::new_readonly(*token_program, false), ],
data: vec![1],
}]
})
};
Arc::try_unwrap(arc_instructions).unwrap_or_else(|arc| (*arc).clone())
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PdaCacheKey {
PumpFunUserVolume(Pubkey),
PumpFunBondingCurve(Pubkey),
PumpFunBondingCurveV2(Pubkey),
PumpFunCreatorVault(Pubkey),
BonkPool(Pubkey, Pubkey),
BonkVault(Pubkey, Pubkey),
PumpSwapUserVolume(Pubkey),
PumpSwapPoolV2(Pubkey),
}
static PDA_CACHE: Lazy<DashMap<PdaCacheKey, Pubkey>> =
Lazy::new(|| DashMap::with_capacity(MAX_PDA_CACHE_SIZE));
#[inline]
pub fn get_cached_pda<F>(cache_key: PdaCacheKey, compute_fn: F) -> Option<Pubkey>
where
F: FnOnce() -> Option<Pubkey>,
{
if let Some(pda) = PDA_CACHE.get(&cache_key) {
return Some(*pda);
}
let pda_result = compute_fn();
if let Some(pda) = pda_result {
PDA_CACHE.insert(cache_key, pda);
}
pda_result
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct AtaCacheKey {
wallet_address: Pubkey,
token_mint_address: Pubkey,
token_program_id: Pubkey,
use_seed: bool,
}
static ATA_CACHE: Lazy<DashMap<AtaCacheKey, Pubkey>> =
Lazy::new(|| DashMap::with_capacity(MAX_ATA_CACHE_SIZE));
#[inline]
pub fn get_associated_token_address_with_program_id_fast_use_seed(
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
token_program_id: &Pubkey,
use_seed: bool,
) -> Pubkey {
_get_associated_token_address_with_program_id_fast(
wallet_address,
token_mint_address,
token_program_id,
use_seed,
)
}
#[inline]
pub fn get_associated_token_address_with_program_id_fast(
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
token_program_id: &Pubkey,
) -> Pubkey {
_get_associated_token_address_with_program_id_fast(
wallet_address,
token_mint_address,
token_program_id,
false,
)
}
fn _get_associated_token_address_with_program_id_fast(
wallet_address: &Pubkey,
token_mint_address: &Pubkey,
token_program_id: &Pubkey,
use_seed: bool,
) -> Pubkey {
let cache_key = AtaCacheKey {
wallet_address: *wallet_address,
token_mint_address: *token_mint_address,
token_program_id: *token_program_id,
use_seed,
};
if let Some(cached_ata) = ATA_CACHE.get(&cache_key) {
return *cached_ata;
}
let ata = if use_seed
&& !token_mint_address.eq(&crate::constants::WSOL_TOKEN_ACCOUNT)
&& !token_mint_address.eq(&crate::constants::SOL_TOKEN_ACCOUNT)
&& (token_program_id.eq(&crate::constants::TOKEN_PROGRAM)
|| token_program_id.eq(&crate::constants::TOKEN_PROGRAM_2022))
{
super::seed::get_associated_token_address_with_program_id_use_seed(
wallet_address,
token_mint_address,
token_program_id,
)
.unwrap()
} else {
get_associated_token_address_with_program_id(
wallet_address,
token_mint_address,
token_program_id,
)
};
ATA_CACHE.insert(cache_key, ata);
ata
}
pub fn fast_init(payer: &Pubkey) {
crate::instruction::utils::pumpfun::get_user_volume_accumulator_pda(payer);
crate::instruction::utils::pumpswap::get_user_volume_accumulator_pda(payer);
let wsol_token_account = get_associated_token_address_with_program_id_fast(
payer,
&crate::constants::WSOL_TOKEN_ACCOUNT,
&crate::constants::TOKEN_PROGRAM,
);
get_cached_instructions(
crate::common::fast_fn::InstructionCacheKey::CloseWsolAccount {
payer: *payer,
wsol_token_account,
},
|| {
vec![close_account(
&crate::constants::TOKEN_PROGRAM,
&wsol_token_account,
&payer,
&payer,
&[],
)
.unwrap()]
},
);
}