2025-09-08 17:12:29 +08:00
|
|
|
use clru::CLruCache;
|
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
use parking_lot::RwLock;
|
|
|
|
|
use solana_sdk::{
|
|
|
|
|
instruction::{AccountMeta, Instruction},
|
|
|
|
|
pubkey::Pubkey,
|
|
|
|
|
};
|
|
|
|
|
use spl_associated_token_account::{
|
2025-09-08 17:33:53 +08:00
|
|
|
get_associated_token_address_with_program_id, ID as ASSOCIATED_TOKEN_PROGRAM_ID,
|
2025-09-08 17:12:29 +08:00
|
|
|
};
|
|
|
|
|
use std::num::NonZeroUsize;
|
|
|
|
|
|
|
|
|
|
const MAX_PDA_CACHE_SIZE: usize = 10000;
|
|
|
|
|
const MAX_ATA_CACHE_SIZE: usize = 10000;
|
|
|
|
|
const MAX_INSTRUCTION_CACHE_SIZE: usize = 10000;
|
|
|
|
|
|
|
|
|
|
// --------------------- Instruction Cache ---------------------
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
/// Instruction cache key for uniquely identifying instruction types and parameters
|
2025-09-08 17:12:29 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub enum InstructionCacheKey {
|
2025-09-09 00:50:24 +08:00
|
|
|
/// Associated Token Account creation instruction
|
2025-09-08 17:12:29 +08:00
|
|
|
CreateAssociatedTokenAccount {
|
|
|
|
|
payer: Pubkey,
|
|
|
|
|
owner: Pubkey,
|
|
|
|
|
mint: Pubkey,
|
|
|
|
|
token_program: Pubkey,
|
|
|
|
|
},
|
2025-09-08 18:29:05 +08:00
|
|
|
/// Close wSOL Account
|
|
|
|
|
CloseWsolAccount { payer: Pubkey, wsol_token_account: Pubkey },
|
2025-09-08 17:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
/// Global instruction cache for storing common instructions
|
2025-09-08 17:12:29 +08:00
|
|
|
static INSTRUCTION_CACHE: Lazy<RwLock<CLruCache<InstructionCacheKey, Instruction>>> =
|
|
|
|
|
Lazy::new(|| {
|
|
|
|
|
RwLock::new(CLruCache::new(NonZeroUsize::new(MAX_INSTRUCTION_CACHE_SIZE).unwrap()))
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
/// Get cached instruction, compute and cache if not exists
|
2025-09-08 17:12:29 +08:00
|
|
|
pub fn get_cached_instruction<F>(cache_key: InstructionCacheKey, compute_fn: F) -> Instruction
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce() -> Instruction,
|
|
|
|
|
{
|
2025-09-09 00:50:24 +08:00
|
|
|
// Try to get from cache (using read lock)
|
2025-09-08 17:12:29 +08:00
|
|
|
{
|
|
|
|
|
let cache = INSTRUCTION_CACHE.read();
|
|
|
|
|
if let Some(cached_instruction) = cache.peek(&cache_key) {
|
|
|
|
|
return cached_instruction.clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// Cache miss, compute new instruction
|
2025-09-08 17:12:29 +08:00
|
|
|
let instruction = compute_fn();
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// Store computation result in cache (using write lock)
|
2025-09-08 17:12:29 +08:00
|
|
|
{
|
|
|
|
|
let mut cache = INSTRUCTION_CACHE.write();
|
|
|
|
|
cache.put(cache_key, instruction.clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instruction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------- Associated Token Account ---------------------
|
|
|
|
|
|
2025-09-08 17:33:53 +08:00
|
|
|
pub fn create_associated_token_account_idempotent_fast(
|
2025-09-08 17:12:29 +08:00
|
|
|
payer: &Pubkey,
|
|
|
|
|
owner: &Pubkey,
|
|
|
|
|
mint: &Pubkey,
|
|
|
|
|
token_program: &Pubkey,
|
|
|
|
|
) -> Instruction {
|
2025-09-09 00:50:24 +08:00
|
|
|
// Create cache key
|
2025-09-08 17:12:29 +08:00
|
|
|
let cache_key = InstructionCacheKey::CreateAssociatedTokenAccount {
|
|
|
|
|
payer: *payer,
|
|
|
|
|
owner: *owner,
|
|
|
|
|
mint: *mint,
|
|
|
|
|
token_program: *token_program,
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// Use cache to get instruction
|
2025-09-08 17:12:29 +08:00
|
|
|
get_cached_instruction(cache_key, || {
|
2025-09-09 00:50:24 +08:00
|
|
|
// Get Associated Token Address using cache
|
2025-09-08 17:33:53 +08:00
|
|
|
let associated_token_address =
|
|
|
|
|
get_associated_token_address_with_program_id_fast(owner, mint, token_program);
|
2025-09-08 17:12:29 +08:00
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// Create Associated Token Account instruction
|
|
|
|
|
// Reference implementation of spl_associated_token_account::instruction::create_associated_token_account
|
2025-09-08 17:12:29 +08:00
|
|
|
Instruction {
|
|
|
|
|
program_id: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
|
|
|
accounts: vec![
|
2025-09-09 00:50:24 +08:00
|
|
|
AccountMeta::new(*payer, true), // Payer (signer, writable)
|
|
|
|
|
AccountMeta::new(associated_token_address, false), // ATA address (writable, non-signer)
|
|
|
|
|
AccountMeta::new_readonly(*owner, false), // Token account owner (readonly, non-signer)
|
|
|
|
|
AccountMeta::new_readonly(*mint, false), // Token mint address (readonly, non-signer)
|
2025-09-08 17:12:29 +08:00
|
|
|
crate::constants::SYSTEM_PROGRAM_META,
|
2025-09-09 00:50:24 +08:00
|
|
|
AccountMeta::new_readonly(*token_program, false), // Token program (readonly, non-signer)
|
2025-09-08 17:12:29 +08:00
|
|
|
],
|
2025-09-08 17:33:53 +08:00
|
|
|
data: vec![1],
|
2025-09-08 17:12:29 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------- PDA ---------------------
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
/// PDA cache key for uniquely identifying PDA computation input parameters
|
2025-09-08 17:12:29 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub enum PdaCacheKey {
|
|
|
|
|
PumpFunUserVolume(Pubkey),
|
|
|
|
|
PumpFunBondingCurve(Pubkey),
|
|
|
|
|
PumpFunCreatorVault(Pubkey),
|
2025-09-08 18:29:05 +08:00
|
|
|
BonkPool(Pubkey, Pubkey),
|
|
|
|
|
BonkVault(Pubkey, Pubkey),
|
2025-09-08 21:54:05 +08:00
|
|
|
PumpSwapUserVolume(Pubkey),
|
2025-09-08 17:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
/// Global PDA cache for storing computation results
|
2025-09-08 17:12:29 +08:00
|
|
|
static PDA_CACHE: Lazy<RwLock<CLruCache<PdaCacheKey, Pubkey>>> =
|
|
|
|
|
Lazy::new(|| RwLock::new(CLruCache::new(NonZeroUsize::new(MAX_PDA_CACHE_SIZE).unwrap())));
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
/// Get cached PDA, compute and cache if not exists
|
2025-09-08 17:12:29 +08:00
|
|
|
pub fn get_cached_pda<F>(cache_key: PdaCacheKey, compute_fn: F) -> Option<Pubkey>
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce() -> Option<Pubkey>,
|
|
|
|
|
{
|
2025-09-09 00:50:24 +08:00
|
|
|
// Try to get from cache (using read lock)
|
2025-09-08 17:12:29 +08:00
|
|
|
{
|
|
|
|
|
let cache = PDA_CACHE.read();
|
|
|
|
|
if let Some(cached_pda) = cache.peek(&cache_key) {
|
|
|
|
|
return Some(*cached_pda);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// Cache miss, compute new PDA
|
2025-09-08 17:12:29 +08:00
|
|
|
let pda_result = compute_fn();
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// If computation succeeds, store result in cache (using write lock)
|
2025-09-08 17:12:29 +08:00
|
|
|
if let Some(pda) = pda_result {
|
|
|
|
|
let mut cache = PDA_CACHE.write();
|
|
|
|
|
cache.put(cache_key, pda);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pda_result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------- ATA ---------------------
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
/// ATA cache key for Associated Token Address caching
|
2025-09-08 17:12:29 +08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
|
struct AtaCacheKey {
|
|
|
|
|
wallet_address: Pubkey,
|
|
|
|
|
token_mint_address: Pubkey,
|
2025-09-08 17:33:53 +08:00
|
|
|
token_program_id: Pubkey,
|
2025-09-08 17:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
/// Global ATA cache for storing Associated Token Address computation results
|
2025-09-08 17:12:29 +08:00
|
|
|
static ATA_CACHE: Lazy<RwLock<CLruCache<AtaCacheKey, Pubkey>>> =
|
|
|
|
|
Lazy::new(|| RwLock::new(CLruCache::new(NonZeroUsize::new(MAX_ATA_CACHE_SIZE).unwrap())));
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
/// Get cached Associated Token Address, compute and cache if not exists
|
2025-09-08 17:33:53 +08:00
|
|
|
pub fn get_associated_token_address_with_program_id_fast(
|
2025-09-08 17:12:29 +08:00
|
|
|
wallet_address: &Pubkey,
|
|
|
|
|
token_mint_address: &Pubkey,
|
2025-09-08 17:33:53 +08:00
|
|
|
token_program_id: &Pubkey,
|
2025-09-08 17:12:29 +08:00
|
|
|
) -> Pubkey {
|
2025-09-08 17:33:53 +08:00
|
|
|
let cache_key = AtaCacheKey {
|
|
|
|
|
wallet_address: *wallet_address,
|
|
|
|
|
token_mint_address: *token_mint_address,
|
|
|
|
|
token_program_id: *token_program_id,
|
|
|
|
|
};
|
2025-09-08 17:12:29 +08:00
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// Try to get from cache (using read lock)
|
2025-09-08 17:12:29 +08:00
|
|
|
{
|
|
|
|
|
let cache = ATA_CACHE.read();
|
|
|
|
|
if let Some(cached_ata) = cache.peek(&cache_key) {
|
|
|
|
|
return *cached_ata;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// Cache miss, compute new ATA
|
2025-09-08 17:33:53 +08:00
|
|
|
let ata = get_associated_token_address_with_program_id(
|
|
|
|
|
wallet_address,
|
|
|
|
|
token_mint_address,
|
|
|
|
|
token_program_id,
|
|
|
|
|
);
|
2025-09-08 17:12:29 +08:00
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// Store computation result in cache (using write lock)
|
2025-09-08 17:12:29 +08:00
|
|
|
{
|
|
|
|
|
let mut cache = ATA_CACHE.write();
|
|
|
|
|
cache.put(cache_key, ata);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ata
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 00:50:24 +08:00
|
|
|
// --------------------- Initialize Accounts ---------------------
|
2025-09-08 17:12:29 +08:00
|
|
|
|
|
|
|
|
pub fn fast_init(payer: &Pubkey) {
|
2025-09-09 00:50:24 +08:00
|
|
|
// Get PumpFun user volume accumulator PDA
|
2025-09-08 17:12:29 +08:00
|
|
|
crate::instruction::utils::pumpfun::get_user_volume_accumulator_pda(payer);
|
2025-09-09 00:50:24 +08:00
|
|
|
// Get PumpSwap user volume accumulator PDA
|
2025-09-08 21:54:05 +08:00
|
|
|
crate::instruction::utils::pumpswap::get_user_volume_accumulator_pda(payer);
|
2025-09-09 00:50:24 +08:00
|
|
|
// Get wSOL ATA address
|
2025-09-08 18:29:05 +08:00
|
|
|
let wsol_token_account = get_associated_token_address_with_program_id_fast(
|
|
|
|
|
payer,
|
|
|
|
|
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
|
|
|
|
&crate::constants::TOKEN_PROGRAM,
|
|
|
|
|
);
|
2025-09-09 00:50:24 +08:00
|
|
|
// Get Close wSOL Account instruction
|
2025-09-08 18:29:05 +08:00
|
|
|
get_cached_instruction(
|
|
|
|
|
crate::common::fast_fn::InstructionCacheKey::CloseWsolAccount {
|
|
|
|
|
payer: *payer,
|
|
|
|
|
wsol_token_account,
|
|
|
|
|
},
|
|
|
|
|
|| {
|
|
|
|
|
spl_token::instruction::close_account(
|
|
|
|
|
&crate::constants::TOKEN_PROGRAM,
|
|
|
|
|
&wsol_token_account,
|
|
|
|
|
&payer,
|
|
|
|
|
&payer,
|
|
|
|
|
&[],
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-09-08 17:12:29 +08:00
|
|
|
}
|