refactor: optimize AccountMeta creation with lazy static constants

- Consolidate protocol constants to unified module
- Pre-build AccountMeta objects using once_cell::Lazy
- Reduce instruction building overhead across all DEX protocols
- Improve code maintainability and performance
This commit is contained in:
ysq
2025-09-07 21:36:57 +08:00
parent 51253c7aaf
commit 544e65318d
17 changed files with 323 additions and 185 deletions
+21 -5
View File
@@ -17,15 +17,30 @@ pub mod accounts {
pub const AUTHORITY: Pubkey = pubkey!("WLHv2UAZm6z4KyaaELi5pjdbJh6RESMva1Rnn8pJVVh");
pub const GLOBAL_CONFIG: Pubkey = pubkey!("6s1xP3hpbAfFoNtUNF8mfHsjr2Bd97JxFJRWLbL6aHuX");
pub const TOKEN_PROGRAM: Pubkey = spl_token::ID;
pub const EVENT_AUTHORITY: Pubkey = pubkey!("2DPAtwB8L12vrMRExbLuyGnC7n2J5LNoZQSejeQGpwkr");
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
pub const BONK: Pubkey = pubkey!("LanMV9sAd7wArD4vJFi2qDdfnVhFxYSUg6eADduJ3uj");
pub const SYSTEM_PROGRAM: Pubkey = solana_sdk::system_program::ID;
pub const PLATFORM_FEE_RATE: u128 = 100; // 1%
pub const PROTOCOL_FEE_RATE: u128 = 25; // 0.25%
pub const SHARE_FEE_RATE: u128 = 0; // 0%
// META
pub const AUTHORITY_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(AUTHORITY, false)
});
pub const GLOBAL_CONFIG_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(GLOBAL_CONFIG, false)
});
pub const EVENT_AUTHORITY_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(EVENT_AUTHORITY, false)
});
pub const BONK_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(BONK, false)
});
}
pub const BUY_EXECT_IN_DISCRIMINATOR: [u8; 8] = [250, 234, 13, 123, 213, 156, 19, 236];
@@ -142,14 +157,15 @@ pub fn get_vault_pda(pool_state: &Pubkey, mint: &Pubkey) -> Option<Pubkey> {
}
pub fn get_platform_associated_account(platform_config: &Pubkey) -> Option<Pubkey> {
let seeds: &[&[u8]; 2] = &[platform_config.as_ref(), accounts::WSOL_TOKEN_ACCOUNT.as_ref()];
let seeds: &[&[u8]; 2] =
&[platform_config.as_ref(), crate::constants::WSOL_TOKEN_ACCOUNT.as_ref()];
let program_id: &Pubkey = &accounts::BONK;
let pda: Option<(Pubkey, u8)> = Pubkey::try_find_program_address(seeds, program_id);
pda.map(|pubkey| pubkey.0)
}
pub fn get_creator_associated_account(creator: &Pubkey) -> Option<Pubkey> {
let seeds: &[&[u8]; 2] = &[creator.as_ref(), accounts::WSOL_TOKEN_ACCOUNT.as_ref()];
let seeds: &[&[u8]; 2] = &[creator.as_ref(), crate::constants::WSOL_TOKEN_ACCOUNT.as_ref()];
let program_id: &Pubkey = &accounts::BONK;
let pda: Option<(Pubkey, u8)> = Pubkey::try_find_program_address(seeds, program_id);
pda.map(|pubkey| pubkey.0)
+45 -6
View File
@@ -61,9 +61,19 @@ pub mod global_constants {
/// Public key for the fee recipient
pub const FEE_RECIPIENT: Pubkey = pubkey!("62qc2CNXwrYqQScmEdiZFFAnJR262PxWEuNQtxfafNgV");
/// Static AccountMeta for fee recipient (initialized once)
pub static FEE_RECIPIENT_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new(FEE_RECIPIENT, false)
});
/// Public key for the global PDA
pub const GLOBAL_ACCOUNT: Pubkey = pubkey!("4wTV1YmiEkRvAtNtsSGPtUrqRYQMe5SKy2uB4Jjaxnjf");
/// Static AccountMeta for global account (initialized once)
pub static GLOBAL_ACCOUNT_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(GLOBAL_ACCOUNT, false)
});
/// Public key for the authority
pub const AUTHORITY: Pubkey = pubkey!("FFWtrEQ4B4PKQoVuHYzZq8FabGkVatYzDpEVHsK5rrhF");
@@ -85,6 +95,10 @@ pub mod global_constants {
pub mod accounts {
use solana_sdk::{pubkey, pubkey::Pubkey};
use crate::instruction::utils::pumpfun::{
get_fee_config_pda, get_global_volume_accumulator_pda,
};
/// Public key for the Pump.fun program
pub const PUMPFUN: Pubkey = pubkey!("6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P");
@@ -94,12 +108,6 @@ pub mod accounts {
/// Authority for program events
pub const EVENT_AUTHORITY: Pubkey = pubkey!("Ce6TQqeHC9p8KetsN6JsjHK7UTZk7nasjjnr7XxXp9F1");
/// System Program ID
pub const SYSTEM_PROGRAM: Pubkey = pubkey!("11111111111111111111111111111111");
/// Token Program ID
pub const TOKEN_PROGRAM: Pubkey = spl_token::ID;
/// Associated Token Program ID
pub const ASSOCIATED_TOKEN_PROGRAM: Pubkey =
pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
@@ -110,6 +118,37 @@ pub mod accounts {
pub const AMM_PROGRAM: Pubkey = pubkey!("675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8");
pub const FEE_PROGRAM: Pubkey = pubkey!("pfeeUxB6jkeY1Hxd7CsFCAjcbHA9rWtchMGdZ6VojVZ");
// META
pub const PUMPFUN_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(PUMPFUN, false)
});
pub const EVENT_AUTHORITY_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(EVENT_AUTHORITY, false)
});
pub const FEE_PROGRAM_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(FEE_PROGRAM, false)
});
pub const GLOBAL_VOLUME_ACCUMULATOR_META: once_cell::sync::Lazy<
solana_sdk::instruction::AccountMeta,
> = once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(
get_global_volume_accumulator_pda().unwrap(),
false,
)
});
pub const FEE_CONFIG_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(get_fee_config_pda().unwrap(), false)
});
}
pub struct Symbol;
+57 -11
View File
@@ -1,4 +1,7 @@
use crate::{common::SolanaRpcClient, constants};
use crate::{
common::SolanaRpcClient,
constants::{self, TOKEN_PROGRAM},
};
use anyhow::anyhow;
use solana_account_decoder::UiAccountEncoding;
use solana_sdk::pubkey::Pubkey;
@@ -27,6 +30,10 @@ pub mod seeds {
pub mod accounts {
use solana_sdk::{pubkey, pubkey::Pubkey};
use crate::instruction::utils::pumpswap::{
get_fee_config_pda, get_global_volume_accumulator_pda,
};
/// Public key for the fee recipient
pub const FEE_RECIPIENT: Pubkey = pubkey!("62qc2CNXwrYqQScmEdiZFFAnJR262PxWEuNQtxfafNgV");
@@ -36,14 +43,6 @@ pub mod accounts {
/// Authority for program events
pub const EVENT_AUTHORITY: Pubkey = pubkey!("GS4CU59F31iL7aR2Q8zVS8DRrcRnXX1yjQ66TqNVQnaR");
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
/// System Program ID
pub const SYSTEM_PROGRAM: Pubkey = pubkey!("11111111111111111111111111111111");
/// Token Program ID
pub const TOKEN_PROGRAM: Pubkey = spl_token::ID;
/// Associated Token Program ID
pub const ASSOCIATED_TOKEN_PROGRAM: Pubkey =
pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
@@ -62,6 +61,53 @@ pub mod accounts {
pub const COIN_CREATOR_FEE_BASIS_POINTS: u64 = 5;
pub const FEE_PROGRAM: Pubkey = pubkey!("pfeeUxB6jkeY1Hxd7CsFCAjcbHA9rWtchMGdZ6VojVZ");
// META
pub const GLOBAL_ACCOUNT_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(GLOBAL_ACCOUNT, false)
});
pub const FEE_RECIPIENT_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(FEE_RECIPIENT, false)
});
pub const ASSOCIATED_TOKEN_PROGRAM_META: once_cell::sync::Lazy<
solana_sdk::instruction::AccountMeta,
> = once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM, false)
});
pub const EVENT_AUTHORITY_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(EVENT_AUTHORITY, false)
});
pub const AMM_PROGRAM_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(AMM_PROGRAM, false)
});
pub const GLOBAL_VOLUME_ACCUMULATOR_META: once_cell::sync::Lazy<
solana_sdk::instruction::AccountMeta,
> = once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(
get_global_volume_accumulator_pda().unwrap(),
false,
)
});
pub const FEE_CONFIG_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(get_fee_config_pda().unwrap(), false)
});
pub const FEE_PROGRAM_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(FEE_PROGRAM, false)
});
}
pub const BUY_DISCRIMINATOR: [u8; 8] = [102, 6, 61, 18, 1, 218, 235, 234];
@@ -87,7 +133,7 @@ pub(crate) fn coin_creator_vault_ata(coin_creator: Pubkey, quote_mint: Pubkey) -
spl_associated_token_account::get_associated_token_address_with_program_id(
&creator_vault_authority,
&quote_mint,
&accounts::TOKEN_PROGRAM,
&TOKEN_PROGRAM,
);
associated_token_creator_vault_authority
}
@@ -97,7 +143,7 @@ pub(crate) fn fee_recipient_ata(fee_recipient: Pubkey, quote_mint: Pubkey) -> Pu
spl_associated_token_account::get_associated_token_address_with_program_id(
&fee_recipient,
&quote_mint,
&accounts::TOKEN_PROGRAM,
&TOKEN_PROGRAM,
);
associated_token_fee_recipient
}
+7 -2
View File
@@ -15,14 +15,19 @@ pub mod seeds {
pub mod accounts {
use solana_sdk::{pubkey, pubkey::Pubkey};
pub const AUTHORITY: Pubkey = pubkey!("5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1");
pub const TOKEN_PROGRAM: Pubkey = spl_token::ID;
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
pub const RAYDIUM_AMM_V4: Pubkey = pubkey!("675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8");
pub const TRADE_FEE_NUMERATOR: u64 = 25;
pub const TRADE_FEE_DENOMINATOR: u64 = 10000;
pub const SWAP_FEE_NUMERATOR: u64 = 25;
pub const SWAP_FEE_DENOMINATOR: u64 = 10000;
// META
pub const AUTHORITY_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(AUTHORITY, false)
});
}
pub const SWAP_BASE_IN_DISCRIMINATOR: &[u8] = &[9];
+10 -3
View File
@@ -17,15 +17,22 @@ pub mod accounts {
use solana_sdk::{pubkey, pubkey::Pubkey};
pub const AUTHORITY: Pubkey = pubkey!("GpMZbSM2GgvTKHJirzeGfMFoaZ8UR2X7F4v8vHTvxFbL");
pub const AMM_CONFIG: Pubkey = pubkey!("D4FPEruKEHrG5TenZ2mpDGEfu1iUvTiqBxvpU8HLBvC2");
pub const TOKEN_PROGRAM: Pubkey = spl_token::ID;
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
pub const RAYDIUM_CPMM: Pubkey = pubkey!("CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C");
pub const FEE_RATE_DENOMINATOR_VALUE: u128 = 1_000_000;
pub const TRADE_FEE_RATE: u64 = 2500;
pub const CREATOR_FEE_RATE: u64 = 0;
pub const PROTOCOL_FEE_RATE: u64 = 120000;
pub const FUND_FEE_RATE: u64 = 40000;
// META
pub const AUTHORITY_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(AUTHORITY, false)
});
pub const AMM_CONFIG_META: once_cell::sync::Lazy<solana_sdk::instruction::AccountMeta> =
once_cell::sync::Lazy::new(|| {
solana_sdk::instruction::AccountMeta::new_readonly(AMM_CONFIG, false)
});
}
pub const SWAP_BASE_IN_DISCRIMINATOR: &[u8] = &[143, 190, 90, 218, 196, 30, 51, 222];