feat: Add Token 2022 support for Raydium CPMM, update constants to use spl_token::ID
This commit is contained in:
@@ -382,6 +382,8 @@ use sol_trade_sdk::{
|
|||||||
raydium_cpmm::common::{get_buy_token_amount, get_sell_sol_amount}
|
raydium_cpmm::common::{get_buy_token_amount, get_sell_sol_amount}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use spl_token; // For standard SPL Token
|
||||||
|
// use spl_token_2022; // For Token 2022 standard (if needed)
|
||||||
|
|
||||||
async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("Testing Raydium CPMM trading...");
|
println!("Testing Raydium CPMM trading...");
|
||||||
@@ -408,6 +410,7 @@ async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
None,
|
None,
|
||||||
Some(Box::new(RaydiumCpmmParams {
|
Some(Box::new(RaydiumCpmmParams {
|
||||||
pool_state: Some(pool_state), // If not provided, will auto-calculate wsol-mint direction pool
|
pool_state: Some(pool_state), // If not provided, will auto-calculate wsol-mint direction pool
|
||||||
|
mint_token_program: Some(spl_token::ID), // Support spl_token or spl_token_2022::ID
|
||||||
minimum_amount_out: Some(buy_amount_out), // If not provided, defaults to 0
|
minimum_amount_out: Some(buy_amount_out), // If not provided, defaults to 0
|
||||||
auto_handle_wsol: true, // Automatically handle wSOL wrapping/unwrapping
|
auto_handle_wsol: true, // Automatically handle wSOL wrapping/unwrapping
|
||||||
})),
|
})),
|
||||||
@@ -427,6 +430,7 @@ async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
None,
|
None,
|
||||||
Some(Box::new(RaydiumCpmmParams {
|
Some(Box::new(RaydiumCpmmParams {
|
||||||
pool_state: Some(pool_state), // If not provided, will auto-calculate wsol-mint direction pool
|
pool_state: Some(pool_state), // If not provided, will auto-calculate wsol-mint direction pool
|
||||||
|
mint_token_program: Some(spl_token::ID), // Support spl_token or spl_token_2022::ID
|
||||||
minimum_amount_out: Some(sell_sol_amount), // If not provided, defaults to 0
|
minimum_amount_out: Some(sell_sol_amount), // If not provided, defaults to 0
|
||||||
auto_handle_wsol: true, // Automatically handle wSOL wrapping/unwrapping
|
auto_handle_wsol: true, // Automatically handle wSOL wrapping/unwrapping
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -380,6 +380,8 @@ use sol_trade_sdk::{
|
|||||||
raydium_cpmm::common::{get_buy_token_amount, get_sell_sol_amount}
|
raydium_cpmm::common::{get_buy_token_amount, get_sell_sol_amount}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use spl_token; // 用于标准 SPL Token
|
||||||
|
// use spl_token_2022; // 用于 Token 2022 标准(如果需要)
|
||||||
|
|
||||||
async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("Testing Raydium CPMM trading...");
|
println!("Testing Raydium CPMM trading...");
|
||||||
@@ -406,6 +408,7 @@ async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
None,
|
None,
|
||||||
Some(Box::new(RaydiumCpmmParams {
|
Some(Box::new(RaydiumCpmmParams {
|
||||||
pool_state: Some(pool_state), // 如果不传,会自动计算 wsol-mint 方向的池子
|
pool_state: Some(pool_state), // 如果不传,会自动计算 wsol-mint 方向的池子
|
||||||
|
mint_token_program: Some(spl_token::ID), // 支持 spl_token 或 spl_token_2022::ID
|
||||||
minimum_amount_out: Some(buy_amount_out), // 如果不传,默认为0
|
minimum_amount_out: Some(buy_amount_out), // 如果不传,默认为0
|
||||||
auto_handle_wsol: true, // 自动处理 wSOL 包装/解包装
|
auto_handle_wsol: true, // 自动处理 wSOL 包装/解包装
|
||||||
})),
|
})),
|
||||||
@@ -425,6 +428,7 @@ async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
None,
|
None,
|
||||||
Some(Box::new(RaydiumCpmmParams {
|
Some(Box::new(RaydiumCpmmParams {
|
||||||
pool_state: Some(pool_state), // 如果不传,会自动计算 wsol-mint 方向的池子
|
pool_state: Some(pool_state), // 如果不传,会自动计算 wsol-mint 方向的池子
|
||||||
|
mint_token_program: Some(spl_token::ID), // 支持 spl_token 或 spl_token_2022::ID
|
||||||
minimum_amount_out: Some(sell_sol_amount), // 如果不传,默认为0
|
minimum_amount_out: Some(sell_sol_amount), // 如果不传,默认为0
|
||||||
auto_handle_wsol: true, // 自动处理 wSOL 包装/解包装
|
auto_handle_wsol: true, // 自动处理 wSOL 包装/解包装
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ pub mod accounts {
|
|||||||
pub const AUTHORITY: Pubkey = pubkey!("WLHv2UAZm6z4KyaaELi5pjdbJh6RESMva1Rnn8pJVVh");
|
pub const AUTHORITY: Pubkey = pubkey!("WLHv2UAZm6z4KyaaELi5pjdbJh6RESMva1Rnn8pJVVh");
|
||||||
pub const GLOBAL_CONFIG: Pubkey = pubkey!("6s1xP3hpbAfFoNtUNF8mfHsjr2Bd97JxFJRWLbL6aHuX");
|
pub const GLOBAL_CONFIG: Pubkey = pubkey!("6s1xP3hpbAfFoNtUNF8mfHsjr2Bd97JxFJRWLbL6aHuX");
|
||||||
pub const PLATFORM_CONFIG: Pubkey = pubkey!("FfYek5vEz23cMkWsdJwG2oa6EphsvXSHrGpdALN4g6W1");
|
pub const PLATFORM_CONFIG: Pubkey = pubkey!("FfYek5vEz23cMkWsdJwG2oa6EphsvXSHrGpdALN4g6W1");
|
||||||
pub const TOKEN_PROGRAM: Pubkey = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
|
pub const TOKEN_PROGRAM: Pubkey = spl_token::ID;
|
||||||
pub const EVENT_AUTHORITY: Pubkey = pubkey!("2DPAtwB8L12vrMRExbLuyGnC7n2J5LNoZQSejeQGpwkr");
|
pub const EVENT_AUTHORITY: Pubkey = pubkey!("2DPAtwB8L12vrMRExbLuyGnC7n2J5LNoZQSejeQGpwkr");
|
||||||
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
|
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
|
||||||
pub const BONK: Pubkey = pubkey!("LanMV9sAd7wArD4vJFi2qDdfnVhFxYSUg6eADduJ3uj");
|
pub const BONK: Pubkey = pubkey!("LanMV9sAd7wArD4vJFi2qDdfnVhFxYSUg6eADduJ3uj");
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ pub mod accounts {
|
|||||||
pub const SYSTEM_PROGRAM: Pubkey = pubkey!("11111111111111111111111111111111");
|
pub const SYSTEM_PROGRAM: Pubkey = pubkey!("11111111111111111111111111111111");
|
||||||
|
|
||||||
/// Token Program ID
|
/// Token Program ID
|
||||||
pub const TOKEN_PROGRAM: Pubkey = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
|
pub const TOKEN_PROGRAM: Pubkey = spl_token::ID;
|
||||||
|
|
||||||
/// Associated Token Program ID
|
/// Associated Token Program ID
|
||||||
pub const ASSOCIATED_TOKEN_PROGRAM: Pubkey =
|
pub const ASSOCIATED_TOKEN_PROGRAM: Pubkey =
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ pub mod accounts {
|
|||||||
pub const SYSTEM_PROGRAM: Pubkey = pubkey!("11111111111111111111111111111111");
|
pub const SYSTEM_PROGRAM: Pubkey = pubkey!("11111111111111111111111111111111");
|
||||||
|
|
||||||
/// Token Program ID
|
/// Token Program ID
|
||||||
pub const TOKEN_PROGRAM: Pubkey = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
|
pub const TOKEN_PROGRAM: Pubkey = spl_token::ID;
|
||||||
|
|
||||||
/// Associated Token Program ID
|
/// Associated Token Program ID
|
||||||
pub const ASSOCIATED_TOKEN_PROGRAM: Pubkey =
|
pub const ASSOCIATED_TOKEN_PROGRAM: Pubkey =
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ pub mod accounts {
|
|||||||
use solana_sdk::{pubkey, pubkey::Pubkey};
|
use solana_sdk::{pubkey, pubkey::Pubkey};
|
||||||
pub const AUTHORITY: Pubkey = pubkey!("GpMZbSM2GgvTKHJirzeGfMFoaZ8UR2X7F4v8vHTvxFbL");
|
pub const AUTHORITY: Pubkey = pubkey!("GpMZbSM2GgvTKHJirzeGfMFoaZ8UR2X7F4v8vHTvxFbL");
|
||||||
pub const AMM_CONFIG: Pubkey = pubkey!("D4FPEruKEHrG5TenZ2mpDGEfu1iUvTiqBxvpU8HLBvC2");
|
pub const AMM_CONFIG: Pubkey = pubkey!("D4FPEruKEHrG5TenZ2mpDGEfu1iUvTiqBxvpU8HLBvC2");
|
||||||
pub const TOKEN_PROGRAM: Pubkey = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
|
pub const TOKEN_PROGRAM: Pubkey = spl_token::ID;
|
||||||
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
|
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
|
||||||
pub const RAYDIUM_CPMM: Pubkey = pubkey!("CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C");
|
pub const RAYDIUM_CPMM: Pubkey = pubkey!("CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ impl RaydiumCpmmInstructionBuilder {
|
|||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
¶ms.mint,
|
¶ms.mint,
|
||||||
&accounts::TOKEN_PROGRAM,
|
&protocol_params.mint_token_program.unwrap_or(accounts::TOKEN_PROGRAM),
|
||||||
));
|
));
|
||||||
|
|
||||||
// 创建买入指令
|
// 创建买入指令
|
||||||
@@ -138,7 +138,7 @@ impl RaydiumCpmmInstructionBuilder {
|
|||||||
solana_sdk::instruction::AccountMeta::new(wsol_vault_account, false), // Input Vault Account
|
solana_sdk::instruction::AccountMeta::new(wsol_vault_account, false), // Input Vault Account
|
||||||
solana_sdk::instruction::AccountMeta::new(mint_vault_account, false), // Output Vault Account
|
solana_sdk::instruction::AccountMeta::new(mint_vault_account, false), // Output Vault Account
|
||||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Input Token Program (readonly)
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Input Token Program (readonly)
|
||||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Output Token Program (readonly)
|
solana_sdk::instruction::AccountMeta::new_readonly(protocol_params.mint_token_program.unwrap_or(accounts::TOKEN_PROGRAM), false), // Output Token Program (readonly)
|
||||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Input token mint (readonly)
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Input token mint (readonly)
|
||||||
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Output token mint (readonly)
|
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Output token mint (readonly)
|
||||||
solana_sdk::instruction::AccountMeta::new(observation_state_account, false), // Observation State Account
|
solana_sdk::instruction::AccountMeta::new(observation_state_account, false), // Observation State Account
|
||||||
@@ -253,7 +253,7 @@ impl RaydiumCpmmInstructionBuilder {
|
|||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||||
&accounts::TOKEN_PROGRAM,
|
&protocol_params.mint_token_program.unwrap(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -267,7 +267,7 @@ impl RaydiumCpmmInstructionBuilder {
|
|||||||
solana_sdk::instruction::AccountMeta::new(wsol_token_account, false), // Output Token Account
|
solana_sdk::instruction::AccountMeta::new(wsol_token_account, false), // Output Token Account
|
||||||
solana_sdk::instruction::AccountMeta::new(mint_vault_account, false), // Input Vault Account
|
solana_sdk::instruction::AccountMeta::new(mint_vault_account, false), // Input Vault Account
|
||||||
solana_sdk::instruction::AccountMeta::new(wsol_vault_account, false), // Output Vault Account
|
solana_sdk::instruction::AccountMeta::new(wsol_vault_account, false), // Output Vault Account
|
||||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Input Token Program (readonly)
|
solana_sdk::instruction::AccountMeta::new_readonly(protocol_params.mint_token_program.unwrap_or(accounts::TOKEN_PROGRAM), false), // Input Token Program (readonly)
|
||||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Output Token Program (readonly)
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // Output Token Program (readonly)
|
||||||
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Input token mint (readonly)
|
solana_sdk::instruction::AccountMeta::new_readonly(params.mint, false), // Input token mint (readonly)
|
||||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Output token mint (readonly)
|
solana_sdk::instruction::AccountMeta::new_readonly(accounts::WSOL_TOKEN_ACCOUNT, false), // Output token mint (readonly)
|
||||||
|
|||||||
@@ -354,6 +354,7 @@ async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
None,
|
None,
|
||||||
Some(Box::new(RaydiumCpmmParams {
|
Some(Box::new(RaydiumCpmmParams {
|
||||||
pool_state: Some(pool_state), // 如果不传,会自动计算 wsol-mint 方向的池子
|
pool_state: Some(pool_state), // 如果不传,会自动计算 wsol-mint 方向的池子
|
||||||
|
mint_token_program: Some(spl_token::ID), // spl_token_2022::ID
|
||||||
minimum_amount_out: Some(buy_amount_out), // 如果不传、默认为0
|
minimum_amount_out: Some(buy_amount_out), // 如果不传、默认为0
|
||||||
auto_handle_wsol: true,
|
auto_handle_wsol: true,
|
||||||
})),
|
})),
|
||||||
@@ -373,6 +374,7 @@ async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
None,
|
None,
|
||||||
Some(Box::new(RaydiumCpmmParams {
|
Some(Box::new(RaydiumCpmmParams {
|
||||||
pool_state: Some(pool_state), // 如果不传,会自动计算 wsol-mint 方向的池子
|
pool_state: Some(pool_state), // 如果不传,会自动计算 wsol-mint 方向的池子
|
||||||
|
mint_token_program: Some(spl_token::ID), // spl_token_2022::ID
|
||||||
minimum_amount_out: Some(sell_sol_amount), // 如果不传、默认为0
|
minimum_amount_out: Some(sell_sol_amount), // 如果不传、默认为0
|
||||||
auto_handle_wsol: true,
|
auto_handle_wsol: true,
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -219,6 +219,7 @@ impl ProtocolParams for BonkParams {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RaydiumCpmmParams {
|
pub struct RaydiumCpmmParams {
|
||||||
pub pool_state: Option<Pubkey>,
|
pub pool_state: Option<Pubkey>,
|
||||||
|
pub mint_token_program: Option<Pubkey>, // spl_token_2022::ID
|
||||||
pub minimum_amount_out: Option<u64>,
|
pub minimum_amount_out: Option<u64>,
|
||||||
pub auto_handle_wsol: bool,
|
pub auto_handle_wsol: bool,
|
||||||
}
|
}
|
||||||
@@ -227,6 +228,7 @@ impl RaydiumCpmmParams {
|
|||||||
pub fn default() -> Self {
|
pub fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
pool_state: None,
|
pool_state: None,
|
||||||
|
mint_token_program: Some(spl_token::ID),
|
||||||
minimum_amount_out: None,
|
minimum_amount_out: None,
|
||||||
auto_handle_wsol: true,
|
auto_handle_wsol: true,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user