feat: swap support usdc quote
This commit is contained in:
@@ -83,10 +83,13 @@ fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|
||||
|event: Box<dyn UnifiedEvent>| {
|
||||
match_event!(event, {
|
||||
PumpSwapBuyEvent => |e: PumpSwapBuyEvent| {
|
||||
if e.base_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT || e.quote_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT {
|
||||
if e.quote_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT
|
||||
|| e.quote_mint == sol_trade_sdk::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
// Test code, only test one transaction
|
||||
if !ALREADY_EXECUTED.swap(true, Ordering::SeqCst) {
|
||||
let event_clone = e.clone();
|
||||
println!("PumpSwapBuyEvent: {:?}", event_clone);
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = pumpswap_trade_with_grpc_buy_event(event_clone).await {
|
||||
eprintln!("Error in trade: {:?}", err);
|
||||
@@ -97,10 +100,13 @@ fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|
||||
}
|
||||
},
|
||||
PumpSwapSellEvent => |e: PumpSwapSellEvent| {
|
||||
if e.base_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT || e.quote_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT {
|
||||
if e.quote_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT
|
||||
|| e.quote_mint == sol_trade_sdk::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
// Test code, only test one transaction
|
||||
if !ALREADY_EXECUTED.swap(true, Ordering::SeqCst) {
|
||||
let event_clone = e.clone();
|
||||
println!("PumpSwapSellEvent: {:?}", event_clone);
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = pumpswap_trade_with_grpc_sell_event(event_clone).await {
|
||||
eprintln!("Error in trade: {:?}", err);
|
||||
@@ -118,7 +124,7 @@ fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|
||||
/// Initializes a new SolanaTrade client with configuration
|
||||
async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
||||
println!("🚀 Initializing SolanaTrade client...");
|
||||
let payer = Keypair::from_base58_string("use_your_payer_keypair_here");
|
||||
let payer = Keypair::from_base58_string("5tnuyXTkvUmpHptH7ib8uTEfszdmAY1sqaxeMrQeMZiwFJHnmCig6yFcjtEp9dFHqhoXBCqhQusgxHapbZ5M4hV5");
|
||||
let rpc_url = "https://api.mainnet-beta.solana.com".to_string();
|
||||
let commitment = CommitmentConfig::confirmed();
|
||||
let swqos_configs: Vec<SwqosConfig> = vec![SwqosConfig::Default(rpc_url.clone())];
|
||||
@@ -144,11 +150,9 @@ async fn pumpswap_trade_with_grpc_buy_event(trade_info: PumpSwapBuyEvent) -> Any
|
||||
trade_info.base_token_program,
|
||||
trade_info.quote_token_program,
|
||||
);
|
||||
let mint = if trade_info.base_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT {
|
||||
trade_info.quote_mint
|
||||
} else {
|
||||
trade_info.base_mint
|
||||
};
|
||||
// Target the base token; quote is WSOL/USDC per filter
|
||||
let mint = trade_info.base_mint;
|
||||
println!("mint: {:?}", mint);
|
||||
pumpswap_trade_with_grpc(mint, params).await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -167,11 +171,9 @@ async fn pumpswap_trade_with_grpc_sell_event(trade_info: PumpSwapSellEvent) -> A
|
||||
trade_info.base_token_program,
|
||||
trade_info.quote_token_program,
|
||||
);
|
||||
let mint = if trade_info.base_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT {
|
||||
trade_info.quote_mint
|
||||
} else {
|
||||
trade_info.base_mint
|
||||
};
|
||||
// Target the base token; quote is WSOL/USDC per filter
|
||||
let mint = trade_info.base_mint;
|
||||
println!("mint: {:?}", mint);
|
||||
pumpswap_trade_with_grpc(mint, params).await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -182,22 +184,24 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) -
|
||||
let client = create_solana_trade_client().await?;
|
||||
let slippage_basis_points = Some(500);
|
||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
||||
let is_usdc_quote = params.quote_mint == sol_trade_sdk::constants::USDC_TOKEN_ACCOUNT;
|
||||
|
||||
// Buy tokens
|
||||
println!("Buying tokens from PumpSwap...");
|
||||
let buy_sol_amount = 100_000;
|
||||
// Use 0.0001 SOL (100_000 lamports) for WSOL quote, or 0.3 USDC (300_000 base units) for USDC quote
|
||||
let buy_input_amount: u64 = if is_usdc_quote { 300_000 } else { 100_000 };
|
||||
let buy_params = sol_trade_sdk::TradeBuyParams {
|
||||
dex_type: DexType::PumpSwap,
|
||||
input_token_type: TradeTokenType::SOL,
|
||||
input_token_type: if is_usdc_quote { TradeTokenType::USDC } else { TradeTokenType::SOL },
|
||||
mint: mint_pubkey,
|
||||
input_token_amount: buy_sol_amount,
|
||||
input_token_amount: buy_input_amount,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(params.clone()),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_input_token_ata: true,
|
||||
close_input_token_ata: true,
|
||||
create_input_token_ata: !is_usdc_quote,
|
||||
close_input_token_ata: !is_usdc_quote,
|
||||
create_mint_ata: true,
|
||||
open_seed_optimize: false,
|
||||
durable_nonce: None,
|
||||
@@ -220,7 +224,7 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) -
|
||||
let amount_token = balance.amount.parse::<u64>().unwrap();
|
||||
let sell_params = sol_trade_sdk::TradeSellParams {
|
||||
dex_type: DexType::PumpSwap,
|
||||
output_token_type: TradeTokenType::SOL,
|
||||
output_token_type: if is_usdc_quote { TradeTokenType::USDC } else { TradeTokenType::SOL },
|
||||
mint: mint_pubkey,
|
||||
input_token_amount: amount_token,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
@@ -229,8 +233,8 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) -
|
||||
extension_params: Box::new(params.clone()),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_output_token_ata: true,
|
||||
close_output_token_ata: true,
|
||||
create_output_token_ata: !is_usdc_quote,
|
||||
close_output_token_ata: !is_usdc_quote,
|
||||
open_seed_optimize: false,
|
||||
durable_nonce: None,
|
||||
fixed_output_token_amount: None,
|
||||
|
||||
@@ -101,7 +101,7 @@ fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|
||||
/// Initializes a new SolanaTrade client with configuration
|
||||
async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
||||
println!("🚀 Initializing SolanaTrade client...");
|
||||
let payer = Keypair::from_base58_string("use_your_payer_keypair_here");
|
||||
let payer = Keypair::from_base58_string("5tnuyXTkvUmpHptH7ib8uTEfszdmAY1sqaxeMrQeMZiwFJHnmCig6yFcjtEp9dFHqhoXBCqhQusgxHapbZ5M4hV5");
|
||||
let rpc_url = "https://api.mainnet-beta.solana.com".to_string();
|
||||
let commitment = CommitmentConfig::confirmed();
|
||||
let swqos_configs: Vec<SwqosConfig> = vec![SwqosConfig::Default(rpc_url.clone())];
|
||||
@@ -125,7 +125,15 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
|
||||
let amm_info = fetch_amm_info(&client.rpc, trade_info.amm).await?;
|
||||
let (coin_reserve, pc_reserve) =
|
||||
get_multi_token_balances(&client.rpc, &amm_info.token_coin, &amm_info.token_pc).await?;
|
||||
let mint_pubkey = if amm_info.pc_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT {
|
||||
// Only handle WSOL/USDC quote pools to align with SDK whitelist
|
||||
let is_usdc_quote = amm_info.pc_mint == sol_trade_sdk::constants::USDC_TOKEN_ACCOUNT;
|
||||
let is_wsol_quote = amm_info.pc_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT;
|
||||
if !is_usdc_quote && !is_wsol_quote {
|
||||
return Ok(());
|
||||
}
|
||||
let mint_pubkey = if amm_info.pc_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT
|
||||
|| amm_info.pc_mint == sol_trade_sdk::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
amm_info.coin_mint
|
||||
} else {
|
||||
amm_info.pc_mint
|
||||
@@ -141,19 +149,20 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
|
||||
);
|
||||
// Buy tokens
|
||||
println!("Buying tokens from Raydium_amm_v4...");
|
||||
let buy_sol_amount = 100_000;
|
||||
// Use 0.0001 SOL (100_000 lamports) for WSOL quote, or 0.3 USDC (300_000 base units) for USDC quote
|
||||
let buy_input_amount: u64 = if is_usdc_quote { 300_000 } else { 100_000 };
|
||||
let buy_params = sol_trade_sdk::TradeBuyParams {
|
||||
dex_type: DexType::RaydiumAmmV4,
|
||||
input_token_type: TradeTokenType::WSOL,
|
||||
input_token_type: if is_usdc_quote { TradeTokenType::USDC } else { TradeTokenType::SOL },
|
||||
mint: mint_pubkey,
|
||||
input_token_amount: buy_sol_amount,
|
||||
input_token_amount: buy_input_amount,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(params),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_input_token_ata: true,
|
||||
close_input_token_ata: true,
|
||||
create_input_token_ata: !is_usdc_quote,
|
||||
close_input_token_ata: !is_usdc_quote,
|
||||
create_mint_ata: true,
|
||||
open_seed_optimize: false,
|
||||
durable_nonce: None,
|
||||
@@ -175,7 +184,7 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
|
||||
let params = RaydiumAmmV4Params::from_amm_address_by_rpc(&client.rpc, trade_info.amm).await?;
|
||||
let sell_params = sol_trade_sdk::TradeSellParams {
|
||||
dex_type: DexType::RaydiumAmmV4,
|
||||
output_token_type: TradeTokenType::WSOL,
|
||||
output_token_type: if is_usdc_quote { TradeTokenType::USDC } else { TradeTokenType::SOL },
|
||||
mint: mint_pubkey,
|
||||
input_token_amount: amount_token,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
@@ -184,8 +193,8 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
|
||||
extension_params: Box::new(params),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_output_token_ata: true,
|
||||
close_output_token_ata: true,
|
||||
create_output_token_ata: !is_usdc_quote,
|
||||
close_output_token_ata: !is_usdc_quote,
|
||||
open_seed_optimize: false,
|
||||
durable_nonce: None,
|
||||
fixed_output_token_amount: None,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use sol_trade_sdk::common::spl_associated_token_account::get_associated_token_address;
|
||||
use sol_trade_sdk::common::TradeConfig;
|
||||
use sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT;
|
||||
use sol_trade_sdk::constants::{USDC_TOKEN_ACCOUNT, WSOL_TOKEN_ACCOUNT};
|
||||
use sol_trade_sdk::trading::core::params::RaydiumCpmmParams;
|
||||
use sol_trade_sdk::trading::factory::DexType;
|
||||
use sol_trade_sdk::TradeTokenType;
|
||||
@@ -79,7 +79,11 @@ fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|
||||
|event: Box<dyn UnifiedEvent>| {
|
||||
match_event!(event, {
|
||||
RaydiumCpmmSwapEvent => |e: RaydiumCpmmSwapEvent| {
|
||||
if e.input_token_mint != WSOL_TOKEN_ACCOUNT && e.output_token_mint != WSOL_TOKEN_ACCOUNT {
|
||||
if e.input_token_mint != WSOL_TOKEN_ACCOUNT
|
||||
&& e.output_token_mint != WSOL_TOKEN_ACCOUNT
|
||||
&& e.input_token_mint != USDC_TOKEN_ACCOUNT
|
||||
&& e.output_token_mint != USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Test code, only test one transaction
|
||||
@@ -101,7 +105,7 @@ fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|
||||
/// Initializes a new SolanaTrade client with configuration
|
||||
async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
||||
println!("🚀 Initializing SolanaTrade client...");
|
||||
let payer = Keypair::from_base58_string("use_your_payer_keypair_here");
|
||||
let payer = Keypair::from_base58_string("5tnuyXTkvUmpHptH7ib8uTEfszdmAY1sqaxeMrQeMZiwFJHnmCig6yFcjtEp9dFHqhoXBCqhQusgxHapbZ5M4hV5");
|
||||
let rpc_url = "https://api.mainnet-beta.solana.com".to_string();
|
||||
let commitment = CommitmentConfig::confirmed();
|
||||
let swqos_configs: Vec<SwqosConfig> = vec![SwqosConfig::Default(rpc_url.clone())];
|
||||
@@ -119,32 +123,34 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) ->
|
||||
println!("Testing Raydium_cpmm trading...");
|
||||
|
||||
let client = create_solana_trade_client().await?;
|
||||
let mint_pubkey = if trade_info.input_token_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
trade_info.output_token_mint
|
||||
} else {
|
||||
trade_info.input_token_mint
|
||||
};
|
||||
// Determine tradable mint: always the non-quote side; only handle WSOL/USDC quote pools
|
||||
let pool_info = RaydiumCpmmParams::from_pool_address_by_rpc(&client.rpc, &trade_info.pool_state).await?;
|
||||
let is_usdc_quote = pool_info.quote_mint == sol_trade_sdk::constants::USDC_TOKEN_ACCOUNT;
|
||||
let is_wsol_quote = pool_info.quote_mint == sol_trade_sdk::constants::WSOL_TOKEN_ACCOUNT;
|
||||
if !is_usdc_quote && !is_wsol_quote {
|
||||
return Ok(());
|
||||
}
|
||||
let mint_pubkey = pool_info.base_mint;
|
||||
let slippage_basis_points = Some(100);
|
||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
||||
|
||||
let buy_params =
|
||||
RaydiumCpmmParams::from_pool_address_by_rpc(&client.rpc, &trade_info.pool_state).await?;
|
||||
let buy_params = pool_info.clone();
|
||||
// Buy tokens
|
||||
println!("Buying tokens from Raydium_cpmm...");
|
||||
let buy_sol_amount = 100_000;
|
||||
// Use 0.0001 SOL (100_000 lamports) for WSOL quote, or 0.3 USDC (300_000 base units) for USDC quote
|
||||
let buy_input_amount: u64 = if is_usdc_quote { 300_000 } else { 100_000 };
|
||||
let buy_params = sol_trade_sdk::TradeBuyParams {
|
||||
dex_type: DexType::RaydiumCpmm,
|
||||
input_token_type: TradeTokenType::WSOL,
|
||||
input_token_type: if is_usdc_quote { TradeTokenType::USDC } else { TradeTokenType::SOL },
|
||||
mint: mint_pubkey,
|
||||
input_token_amount: buy_sol_amount,
|
||||
input_token_amount: buy_input_amount,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
recent_blockhash: Some(recent_blockhash),
|
||||
extension_params: Box::new(buy_params),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_input_token_ata: true,
|
||||
close_input_token_ata: true,
|
||||
create_input_token_ata: !is_usdc_quote,
|
||||
close_input_token_ata: !is_usdc_quote,
|
||||
create_mint_ata: true,
|
||||
open_seed_optimize: false,
|
||||
durable_nonce: None,
|
||||
@@ -162,13 +168,12 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) ->
|
||||
println!("Balance: {:?}", balance);
|
||||
let amount_token = balance.amount.parse::<u64>().unwrap();
|
||||
|
||||
let sell_params =
|
||||
RaydiumCpmmParams::from_pool_address_by_rpc(&client.rpc, &trade_info.pool_state).await?;
|
||||
let sell_params = pool_info;
|
||||
|
||||
println!("Selling {} tokens", amount_token);
|
||||
let sell_params = sol_trade_sdk::TradeSellParams {
|
||||
dex_type: DexType::RaydiumCpmm,
|
||||
output_token_type: TradeTokenType::WSOL,
|
||||
output_token_type: if is_usdc_quote { TradeTokenType::USDC } else { TradeTokenType::SOL },
|
||||
mint: mint_pubkey,
|
||||
input_token_amount: amount_token,
|
||||
slippage_basis_points: slippage_basis_points,
|
||||
@@ -177,8 +182,8 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) ->
|
||||
extension_params: Box::new(sell_params),
|
||||
lookup_table_key: None,
|
||||
wait_transaction_confirmed: true,
|
||||
create_output_token_ata: true,
|
||||
close_output_token_ata: true,
|
||||
create_output_token_ata: !is_usdc_quote,
|
||||
close_output_token_ata: !is_usdc_quote,
|
||||
open_seed_optimize: false,
|
||||
durable_nonce: None,
|
||||
fixed_output_token_amount: None,
|
||||
|
||||
@@ -42,6 +42,16 @@ pub const USD1_TOKEN_ACCOUNT_META: solana_sdk::instruction::AccountMeta =
|
||||
is_writable: false,
|
||||
};
|
||||
|
||||
// USDC (mainnet) mint and meta
|
||||
pub const USDC_TOKEN_ACCOUNT: Pubkey =
|
||||
pubkey!("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
|
||||
pub const USDC_TOKEN_ACCOUNT_META: solana_sdk::instruction::AccountMeta =
|
||||
solana_sdk::instruction::AccountMeta {
|
||||
pubkey: USDC_TOKEN_ACCOUNT,
|
||||
is_signer: false,
|
||||
is_writable: false,
|
||||
};
|
||||
|
||||
pub const RENT: Pubkey = solana_sdk::sysvar::rent::id();
|
||||
pub const RENT_META: solana_sdk::instruction::AccountMeta =
|
||||
solana_sdk::instruction::AccountMeta { pubkey: RENT, is_signer: false, is_writable: false };
|
||||
|
||||
@@ -29,10 +29,20 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
.downcast_ref::<MeteoraDammV2Params>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
|
||||
|
||||
// Whitelist: only allow WSOL/USDC as quote side
|
||||
if protocol_params.token_a_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& protocol_params.token_b_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& protocol_params.token_a_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
&& protocol_params.token_b_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Unsupported quote mint for Meteora DAMM v2"));
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// Trade calculation and account address preparation
|
||||
// ========================================
|
||||
let is_a_in = protocol_params.token_a_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
// Determine side A/B by comparing with actual input mint
|
||||
let is_a_in = protocol_params.token_a_mint == params.input_mint;
|
||||
let amount_in: u64 = params.input_amount.unwrap_or(0);
|
||||
let minimum_amount_out: u64 = match params.fixed_output_amount {
|
||||
Some(fixed) => fixed,
|
||||
@@ -67,7 +77,9 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
// ========================================
|
||||
let mut instructions = Vec::with_capacity(6);
|
||||
|
||||
if params.create_input_mint_ata {
|
||||
if params.create_input_mint_ata
|
||||
&& params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
instructions
|
||||
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
||||
}
|
||||
@@ -113,7 +125,9 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
accounts.to_vec(),
|
||||
));
|
||||
|
||||
if params.close_input_mint_ata {
|
||||
if params.close_input_mint_ata
|
||||
&& params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
// Close wSOL ATA account, reclaim rent
|
||||
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||
}
|
||||
@@ -131,6 +145,15 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
.downcast_ref::<MeteoraDammV2Params>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
|
||||
|
||||
// Whitelist: only allow WSOL/USDC as quote side
|
||||
if protocol_params.token_a_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& protocol_params.token_b_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& protocol_params.token_a_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
&& protocol_params.token_b_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Unsupported quote mint for Meteora DAMM v2"));
|
||||
}
|
||||
|
||||
if params.input_amount.is_none() || params.input_amount.unwrap_or(0) == 0 {
|
||||
return Err(anyhow!("Token amount is not set"));
|
||||
}
|
||||
@@ -138,7 +161,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
// ========================================
|
||||
// Trade calculation and account address preparation
|
||||
// ========================================
|
||||
let is_a_in = protocol_params.token_b_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
let is_a_in = protocol_params.token_a_mint == params.input_mint;
|
||||
let minimum_amount_out: u64 = match params.fixed_output_amount {
|
||||
Some(fixed) => fixed,
|
||||
None => return Err(anyhow!("fixed_output_amount must be set for MeteoraDammV2 swap")),
|
||||
@@ -172,7 +195,9 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
// ========================================
|
||||
let mut instructions = Vec::with_capacity(3);
|
||||
|
||||
if params.create_output_mint_ata {
|
||||
if params.create_output_mint_ata
|
||||
&& params.output_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
instructions.extend(crate::trading::common::create_wsol_ata(¶ms.payer.pubkey()));
|
||||
}
|
||||
|
||||
@@ -205,7 +230,9 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
accounts.to_vec(),
|
||||
));
|
||||
|
||||
if params.close_output_mint_ata {
|
||||
if params.close_output_mint_ata
|
||||
&& params.output_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||
}
|
||||
|
||||
|
||||
@@ -53,10 +53,11 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
let pool_base_token_account = protocol_params.pool_base_token_account;
|
||||
let pool_quote_token_account = protocol_params.pool_quote_token_account;
|
||||
|
||||
if base_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& quote_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
// Only support WSOL or USDC as quote mint for now
|
||||
if quote_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& quote_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Invalid base mint and quote mint"));
|
||||
return Err(anyhow!("Unsupported quote mint for PumpSwap"));
|
||||
}
|
||||
|
||||
// ========================================
|
||||
@@ -219,10 +220,11 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
let base_token_program = protocol_params.base_token_program;
|
||||
let quote_token_program = protocol_params.quote_token_program;
|
||||
|
||||
if base_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& quote_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
// Only support WSOL or USDC as quote mint for now
|
||||
if quote_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& quote_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Invalid base mint and quote mint"));
|
||||
return Err(anyhow!("Unsupported quote mint for PumpSwap"));
|
||||
}
|
||||
if params.input_amount.is_none() {
|
||||
return Err(anyhow!("Token amount is not set"));
|
||||
|
||||
@@ -31,10 +31,18 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
.downcast_ref::<RaydiumAmmV4Params>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
|
||||
|
||||
// Whitelist: only allow WSOL/USDC as quote (pc) mint
|
||||
if protocol_params.pc_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& protocol_params.pc_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Unsupported quote mint for Raydium AMM v4"));
|
||||
}
|
||||
|
||||
// ========================================
|
||||
// Trade calculation and account address preparation
|
||||
// ========================================
|
||||
let is_base_in = protocol_params.coin_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
// Determine direction based on actual input mint
|
||||
let is_base_in = protocol_params.coin_mint == params.input_mint;
|
||||
let amount_in: u64 = params.input_amount.unwrap_or(0);
|
||||
let swap_result = compute_swap_amount(
|
||||
protocol_params.coin_reserve,
|
||||
@@ -51,7 +59,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
let user_source_token_account =
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
¶ms.input_mint,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
@@ -68,7 +76,8 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
// ========================================
|
||||
let mut instructions = Vec::with_capacity(6);
|
||||
|
||||
if params.create_input_mint_ata {
|
||||
if params.create_input_mint_ata && params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
instructions
|
||||
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
||||
}
|
||||
@@ -117,7 +126,8 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
accounts.to_vec(),
|
||||
));
|
||||
|
||||
if params.close_input_mint_ata {
|
||||
if params.close_input_mint_ata && params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
// Close wSOL ATA account, reclaim rent
|
||||
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||
}
|
||||
@@ -135,6 +145,13 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
.downcast_ref::<RaydiumAmmV4Params>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
|
||||
|
||||
// Whitelist: only allow WSOL/USDC as quote (pc) mint
|
||||
if protocol_params.pc_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& protocol_params.pc_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Unsupported quote mint for Raydium AMM v4"));
|
||||
}
|
||||
|
||||
if params.input_amount.is_none() || params.input_amount.unwrap_or(0) == 0 {
|
||||
return Err(anyhow!("Token amount is not set"));
|
||||
}
|
||||
@@ -142,7 +159,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
// ========================================
|
||||
// Trade calculation and account address preparation
|
||||
// ========================================
|
||||
let is_base_in = protocol_params.pc_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
let is_base_in = protocol_params.coin_mint == params.input_mint;
|
||||
let swap_result = compute_swap_amount(
|
||||
protocol_params.coin_reserve,
|
||||
protocol_params.pc_reserve,
|
||||
@@ -165,7 +182,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
let user_destination_token_account =
|
||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
¶ms.output_mint,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
@@ -175,7 +192,9 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
// ========================================
|
||||
let mut instructions = Vec::with_capacity(3);
|
||||
|
||||
if params.create_output_mint_ata {
|
||||
if params.create_output_mint_ata
|
||||
&& params.output_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
instructions.extend(crate::trading::common::create_wsol_ata(¶ms.payer.pubkey()));
|
||||
}
|
||||
|
||||
@@ -211,7 +230,9 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
accounts.to_vec(),
|
||||
));
|
||||
|
||||
if params.close_output_mint_ata {
|
||||
if params.close_output_mint_ata
|
||||
&& params.output_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,13 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
.downcast_ref::<RaydiumCpmmParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
|
||||
|
||||
// Whitelist: only allow WSOL/USDC as quote mint
|
||||
if protocol_params.quote_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& protocol_params.quote_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Unsupported quote mint for Raydium CPMM"));
|
||||
}
|
||||
|
||||
let pool_state = if protocol_params.pool_state == Pubkey::default() {
|
||||
get_pool_pda(
|
||||
&protocol_params.amm_config,
|
||||
@@ -50,7 +57,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
// ========================================
|
||||
// Trade calculation and account address preparation
|
||||
// ========================================
|
||||
let is_base_in = protocol_params.base_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
let is_base_in = protocol_params.base_mint == params.input_mint;
|
||||
let mint_token_program = if is_base_in {
|
||||
protocol_params.quote_token_program
|
||||
} else {
|
||||
@@ -72,7 +79,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
|
||||
let wsol_token_account = get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
¶ms.input_mint,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
@@ -83,12 +90,8 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
|
||||
let wsol_vault_account = get_vault_account(
|
||||
&pool_state,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
protocol_params,
|
||||
true,
|
||||
);
|
||||
let wsol_vault_account =
|
||||
get_vault_account(&pool_state, ¶ms.input_mint, protocol_params, true);
|
||||
let mint_vault_account =
|
||||
get_vault_account(&pool_state, ¶ms.output_mint, protocol_params, false);
|
||||
|
||||
@@ -132,7 +135,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
AccountMeta::new(mint_vault_account, false), // Output Vault Account
|
||||
crate::constants::TOKEN_PROGRAM_META, // Input Token Program (readonly)
|
||||
AccountMeta::new_readonly(mint_token_program, false), // Output Token Program (readonly)
|
||||
crate::constants::WSOL_TOKEN_ACCOUNT_META, // Input token mint (readonly)
|
||||
AccountMeta::new_readonly(params.input_mint, false), // Input token mint (readonly)
|
||||
AccountMeta::new_readonly(params.output_mint, false), // Output token mint (readonly)
|
||||
AccountMeta::new(observation_state_account, false), // Observation State Account
|
||||
];
|
||||
@@ -166,6 +169,13 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
.downcast_ref::<RaydiumCpmmParams>()
|
||||
.ok_or_else(|| anyhow!("Invalid protocol params for RaydiumCpmm"))?;
|
||||
|
||||
// Whitelist: only allow WSOL/USDC as quote mint
|
||||
if protocol_params.quote_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& protocol_params.quote_mint != crate::constants::USDC_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Unsupported quote mint for Raydium CPMM"));
|
||||
}
|
||||
|
||||
if params.input_amount.is_none() || params.input_amount.unwrap_or(0) == 0 {
|
||||
return Err(anyhow!("Token amount is not set"));
|
||||
}
|
||||
@@ -207,7 +217,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
|
||||
let wsol_token_account = get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
¶ms.output_mint,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
@@ -218,12 +228,8 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
|
||||
let wsol_vault_account = get_vault_account(
|
||||
&pool_state,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
protocol_params,
|
||||
true,
|
||||
);
|
||||
let wsol_vault_account =
|
||||
get_vault_account(&pool_state, ¶ms.output_mint, protocol_params, true);
|
||||
let mint_vault_account =
|
||||
get_vault_account(&pool_state, ¶ms.input_mint, protocol_params, false);
|
||||
|
||||
@@ -255,7 +261,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
AccountMeta::new_readonly(mint_token_program, false), // Input Token Program (readonly)
|
||||
crate::constants::TOKEN_PROGRAM_META, // Output Token Program (readonly)
|
||||
AccountMeta::new_readonly(params.input_mint, false), // Input token mint (readonly)
|
||||
crate::constants::WSOL_TOKEN_ACCOUNT_META, // Output token mint (readonly)
|
||||
AccountMeta::new_readonly(params.output_mint, false), // Output token mint (readonly)
|
||||
AccountMeta::new(observation_state_account, false), // Observation State Account
|
||||
];
|
||||
// Create instruction data
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::constants::trade::trade::DEFAULT_SLIPPAGE;
|
||||
use crate::constants::SOL_TOKEN_ACCOUNT;
|
||||
use crate::constants::USD1_TOKEN_ACCOUNT;
|
||||
use crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
use crate::constants::USDC_TOKEN_ACCOUNT;
|
||||
use crate::swqos::SwqosClient;
|
||||
use crate::swqos::SwqosConfig;
|
||||
use crate::swqos::TradeType;
|
||||
@@ -38,6 +39,7 @@ pub enum TradeTokenType {
|
||||
SOL,
|
||||
WSOL,
|
||||
USD1,
|
||||
USDC,
|
||||
}
|
||||
|
||||
/// Main trading client for Solana DeFi protocols
|
||||
@@ -276,6 +278,8 @@ impl SolanaTrade {
|
||||
SOL_TOKEN_ACCOUNT
|
||||
} else if params.input_token_type == TradeTokenType::WSOL {
|
||||
WSOL_TOKEN_ACCOUNT
|
||||
} else if params.input_token_type == TradeTokenType::USDC {
|
||||
USDC_TOKEN_ACCOUNT
|
||||
} else {
|
||||
USD1_TOKEN_ACCOUNT
|
||||
};
|
||||
@@ -371,6 +375,8 @@ impl SolanaTrade {
|
||||
SOL_TOKEN_ACCOUNT
|
||||
} else if params.output_token_type == TradeTokenType::WSOL {
|
||||
WSOL_TOKEN_ACCOUNT
|
||||
} else if params.output_token_type == TradeTokenType::USDC {
|
||||
USDC_TOKEN_ACCOUNT
|
||||
} else {
|
||||
USD1_TOKEN_ACCOUNT
|
||||
};
|
||||
|
||||
@@ -28,11 +28,8 @@ impl GenericTradeExecutor {
|
||||
impl TradeExecutor for GenericTradeExecutor {
|
||||
async fn swap(&self, params: SwapParams) -> Result<(bool, Signature)> {
|
||||
let start = Instant::now();
|
||||
// 暂时支持这三种。后续重构扩展builder 支持所有的 swap
|
||||
let is_buy = params.input_mint == crate::constants::SOL_TOKEN_ACCOUNT
|
||||
|| params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
|| (params.input_mint == crate::constants::USD1_TOKEN_ACCOUNT
|
||||
&& params.output_mint != crate::constants::WSOL_TOKEN_ACCOUNT);
|
||||
// Use trade_type directly to determine direction
|
||||
let is_buy = matches!(params.trade_type, crate::swqos::TradeType::Buy);
|
||||
// Build instructions directly from params to avoid unnecessary cloning
|
||||
let instructions = if is_buy {
|
||||
self.instruction_builder.build_buy_instructions(¶ms).await?
|
||||
|
||||
Reference in New Issue
Block a user