feat: integrate seed optimization and refactor CPMM config
- Upgrade solana-streamer-sdk to 0.4.2 - Add seed optimization support for Raydium instructions - Refactor CPMM params with dynamic AMM config - Remove hardcoded AMM_CONFIG constant
This commit is contained in:
+1
-1
@@ -36,7 +36,7 @@ members = [
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
solana-streamer-sdk = "0.4.1"
|
||||
solana-streamer-sdk = "0.4.2"
|
||||
solana-sdk = "2.3.0"
|
||||
solana-client = "2.3.6"
|
||||
solana-program = "2.3.0"
|
||||
|
||||
@@ -46,16 +46,18 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
let minimum_amount_out = swap_result.min_amount_out;
|
||||
|
||||
let user_source_token_account =
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
let user_destination_token_account =
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
|
||||
// ========================================
|
||||
@@ -69,11 +71,12 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
}
|
||||
|
||||
instructions.extend(
|
||||
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
||||
crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -145,16 +148,18 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
let minimum_amount_out = swap_result.min_amount_out;
|
||||
|
||||
let user_source_token_account =
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
let user_destination_token_account =
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
|
||||
// ========================================
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
common::fast_fn::get_associated_token_address_with_program_id_fast,
|
||||
common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed,
|
||||
constants::trade::trade::DEFAULT_SLIPPAGE,
|
||||
instruction::utils::raydium_cpmm::{
|
||||
accounts, get_observation_state_pda, get_pool_pda, get_vault_account,
|
||||
@@ -38,7 +38,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
|
||||
let pool_state = if protocol_params.pool_state == Pubkey::default() {
|
||||
get_pool_pda(
|
||||
&accounts::AMM_CONFIG,
|
||||
&protocol_params.amm_config,
|
||||
&protocol_params.base_mint,
|
||||
&protocol_params.quote_mint,
|
||||
)
|
||||
@@ -67,15 +67,17 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
);
|
||||
let minimum_amount_out = result.min_amount_out;
|
||||
|
||||
let wsol_token_account = get_associated_token_address_with_program_id_fast(
|
||||
let wsol_token_account = get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
let mint_token_account = get_associated_token_address_with_program_id_fast(
|
||||
let mint_token_account = get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&mint_token_program,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
|
||||
let wsol_vault_account = get_vault_account(
|
||||
@@ -104,11 +106,12 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
}
|
||||
|
||||
instructions.extend(
|
||||
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
||||
crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&mint_token_program,
|
||||
params.open_seed_optimize,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -116,7 +119,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
let accounts: [AccountMeta; 13] = [
|
||||
AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
|
||||
accounts::AUTHORITY_META, // Authority (readonly)
|
||||
accounts::AMM_CONFIG_META, // Amm Config (readonly)
|
||||
AccountMeta::new(protocol_params.amm_config, false), // Amm Config (readonly)
|
||||
AccountMeta::new(pool_state, false), // Pool State
|
||||
AccountMeta::new(wsol_token_account, false), // Input Token Account
|
||||
AccountMeta::new(mint_token_account, false), // Output Token Account
|
||||
@@ -164,7 +167,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
|
||||
let pool_state = if protocol_params.pool_state == Pubkey::default() {
|
||||
get_pool_pda(
|
||||
&accounts::AMM_CONFIG,
|
||||
&protocol_params.amm_config,
|
||||
&protocol_params.base_mint,
|
||||
&protocol_params.quote_mint,
|
||||
)
|
||||
@@ -192,15 +195,17 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
)
|
||||
.min_amount_out;
|
||||
|
||||
let wsol_token_account = get_associated_token_address_with_program_id_fast(
|
||||
let wsol_token_account = get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
let mint_token_account = get_associated_token_address_with_program_id_fast(
|
||||
let mint_token_account = get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&mint_token_program,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
|
||||
let wsol_vault_account = get_vault_account(
|
||||
@@ -231,7 +236,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
let accounts: [AccountMeta; 13] = [
|
||||
AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
|
||||
accounts::AUTHORITY_META, // Authority (readonly)
|
||||
accounts::AMM_CONFIG_META, // Amm Config (readonly)
|
||||
AccountMeta::new(protocol_params.amm_config, false), // Amm Config (readonly)
|
||||
AccountMeta::new(pool_state, false), // Pool State
|
||||
AccountMeta::new(mint_token_account, false), // Input Token Account
|
||||
AccountMeta::new(wsol_token_account, false), // Output Token Account
|
||||
|
||||
@@ -16,7 +16,6 @@ pub mod seeds {
|
||||
pub mod accounts {
|
||||
use solana_sdk::{pubkey, pubkey::Pubkey};
|
||||
pub const AUTHORITY: Pubkey = pubkey!("GpMZbSM2GgvTKHJirzeGfMFoaZ8UR2X7F4v8vHTvxFbL");
|
||||
pub const AMM_CONFIG: Pubkey = pubkey!("D4FPEruKEHrG5TenZ2mpDGEfu1iUvTiqBxvpU8HLBvC2");
|
||||
pub const RAYDIUM_CPMM: Pubkey = pubkey!("CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C");
|
||||
pub const FEE_RATE_DENOMINATOR_VALUE: u128 = 1_000_000;
|
||||
pub const TRADE_FEE_RATE: u64 = 2500;
|
||||
@@ -30,13 +29,6 @@ pub mod accounts {
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
};
|
||||
|
||||
pub const AMM_CONFIG_META: solana_sdk::instruction::AccountMeta =
|
||||
solana_sdk::instruction::AccountMeta {
|
||||
pubkey: AMM_CONFIG,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
};
|
||||
}
|
||||
|
||||
pub const SWAP_BASE_IN_DISCRIMINATOR: &[u8] = &[143, 190, 90, 218, 196, 30, 51, 222];
|
||||
@@ -93,11 +85,15 @@ pub async fn get_pool_token_balances(
|
||||
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 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 token1_amount = token1_balance
|
||||
.amount
|
||||
.parse::<u64>()
|
||||
.map_err(|e| anyhow!("Failed to parse token1 balance: {}", e))?;
|
||||
|
||||
Ok((token0_amount, token1_amount))
|
||||
}
|
||||
|
||||
@@ -422,6 +422,8 @@ impl ProtocolParams for BonkParams {
|
||||
pub struct RaydiumCpmmParams {
|
||||
/// Pool address
|
||||
pub pool_state: Pubkey,
|
||||
/// Amm config address
|
||||
pub amm_config: Pubkey,
|
||||
/// Base token mint address
|
||||
pub base_mint: Pubkey,
|
||||
/// Quote token mint address
|
||||
@@ -452,6 +454,7 @@ impl RaydiumCpmmParams {
|
||||
) -> Self {
|
||||
Self {
|
||||
pool_state: trade_info.pool_state,
|
||||
amm_config: trade_info.amm_config,
|
||||
base_mint: trade_info.input_token_mint,
|
||||
quote_mint: trade_info.output_token_mint,
|
||||
base_reserve: base_reserve,
|
||||
@@ -482,6 +485,7 @@ impl RaydiumCpmmParams {
|
||||
.await?;
|
||||
Ok(Self {
|
||||
pool_state: pool_address.clone(),
|
||||
amm_config: pool.amm_config,
|
||||
base_mint: pool.token0_mint,
|
||||
quote_mint: pool.token1_mint,
|
||||
base_reserve: token0_balance,
|
||||
|
||||
Reference in New Issue
Block a user