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:
@@ -18,10 +18,14 @@ pub async fn get_multi_token_balances(
|
||||
let token0_balance = rpc.get_token_account_balance(&token0_vault).await?;
|
||||
let token1_balance = rpc.get_token_account_balance(&token1_vault).await?;
|
||||
// Parse balance string to u64
|
||||
let token0_amount =
|
||||
token0_balance.amount.parse::<u64>().map_err(|e| anyhow!("Failed to parse token0 balance: {}", e))?;
|
||||
let token1_amount =
|
||||
token1_balance.amount.parse::<u64>().map_err(|e| anyhow!("Failed to parse token1 balance: {}", e))?;
|
||||
let token0_amount = token0_balance
|
||||
.amount
|
||||
.parse::<u64>()
|
||||
.map_err(|e| anyhow!("Failed to parse token0 balance: {}", e))?;
|
||||
let token1_amount = token1_balance
|
||||
.amount
|
||||
.parse::<u64>()
|
||||
.map_err(|e| anyhow!("Failed to parse token1 balance: {}", e))?;
|
||||
Ok((token0_amount, token1_amount))
|
||||
}
|
||||
|
||||
@@ -107,8 +111,13 @@ pub async fn close_token_account(
|
||||
}
|
||||
|
||||
// Build close account instruction
|
||||
let close_account_ix =
|
||||
close_account(&spl_token::ID, &ata, &payer.pubkey(), &payer.pubkey(), &[&payer.pubkey()])?;
|
||||
let close_account_ix = close_account(
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
&ata,
|
||||
&payer.pubkey(),
|
||||
&payer.pubkey(),
|
||||
&[&payer.pubkey()],
|
||||
)?;
|
||||
|
||||
// Build transaction
|
||||
let recent_blockhash = rpc.get_latest_blockhash().await?;
|
||||
|
||||
@@ -189,13 +189,13 @@ impl PumpSwapParams {
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool_address,
|
||||
&pool_data.base_mint,
|
||||
&crate::instruction::utils::pumpswap::accounts::TOKEN_PROGRAM,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
);
|
||||
let quote_token_program_ata =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool_address,
|
||||
&pool_data.quote_mint,
|
||||
&crate::instruction::utils::pumpswap::accounts::TOKEN_PROGRAM,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
@@ -207,14 +207,14 @@ impl PumpSwapParams {
|
||||
coin_creator_vault_ata: coin_creator_vault_ata,
|
||||
coin_creator_vault_authority: coin_creator_vault_authority,
|
||||
base_token_program: if pool_data.pool_base_token_account == base_token_program_ata {
|
||||
crate::instruction::utils::pumpswap::accounts::TOKEN_PROGRAM
|
||||
crate::constants::TOKEN_PROGRAM
|
||||
} else {
|
||||
spl_token_2022::ID
|
||||
crate::constants::TOKEN_PROGRAM_2022
|
||||
},
|
||||
quote_token_program: if pool_data.pool_quote_token_account == quote_token_program_ata {
|
||||
crate::instruction::utils::pumpswap::accounts::TOKEN_PROGRAM
|
||||
crate::constants::TOKEN_PROGRAM
|
||||
} else {
|
||||
spl_token_2022::ID
|
||||
crate::constants::TOKEN_PROGRAM_2022
|
||||
},
|
||||
auto_handle_wsol: true,
|
||||
})
|
||||
@@ -337,7 +337,7 @@ impl BonkParams {
|
||||
) -> Result<Self, anyhow::Error> {
|
||||
let pool_address = crate::instruction::utils::bonk::get_pool_pda(
|
||||
mint,
|
||||
&crate::instruction::utils::bonk::accounts::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
)
|
||||
.unwrap();
|
||||
let pool_data =
|
||||
|
||||
Reference in New Issue
Block a user