@@ -42,16 +42,6 @@ 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,20 +29,10 @@ 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
|
||||
// ========================================
|
||||
// Determine side A/B by comparing with actual input mint
|
||||
let is_a_in = protocol_params.token_a_mint == params.input_mint;
|
||||
let is_a_in = protocol_params.token_a_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
let amount_in: u64 = params.input_amount.unwrap_or(0);
|
||||
let minimum_amount_out: u64 = match params.fixed_output_amount {
|
||||
Some(fixed) => fixed,
|
||||
@@ -77,9 +67,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
// ========================================
|
||||
let mut instructions = Vec::with_capacity(6);
|
||||
|
||||
if params.create_input_mint_ata
|
||||
&& params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
if params.create_input_mint_ata {
|
||||
instructions
|
||||
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
||||
}
|
||||
@@ -125,9 +113,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
accounts.to_vec(),
|
||||
));
|
||||
|
||||
if params.close_input_mint_ata
|
||||
&& params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
if params.close_input_mint_ata {
|
||||
// Close wSOL ATA account, reclaim rent
|
||||
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||
}
|
||||
@@ -145,15 +131,6 @@ 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"));
|
||||
}
|
||||
@@ -161,7 +138,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
// ========================================
|
||||
// Trade calculation and account address preparation
|
||||
// ========================================
|
||||
let is_a_in = protocol_params.token_a_mint == params.input_mint;
|
||||
let is_a_in = protocol_params.token_b_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
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")),
|
||||
@@ -195,9 +172,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
// ========================================
|
||||
let mut instructions = Vec::with_capacity(3);
|
||||
|
||||
if params.create_output_mint_ata
|
||||
&& params.output_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
if params.create_output_mint_ata {
|
||||
instructions.extend(crate::trading::common::create_wsol_ata(¶ms.payer.pubkey()));
|
||||
}
|
||||
|
||||
@@ -230,9 +205,7 @@ impl InstructionBuilder for MeteoraDammV2InstructionBuilder {
|
||||
accounts.to_vec(),
|
||||
));
|
||||
|
||||
if params.close_output_mint_ata
|
||||
&& params.output_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
if params.close_output_mint_ata {
|
||||
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||
}
|
||||
|
||||
|
||||
@@ -53,11 +53,10 @@ 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;
|
||||
|
||||
// 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
|
||||
if base_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& quote_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Unsupported quote mint for PumpSwap"));
|
||||
return Err(anyhow!("Invalid base mint and quote mint"));
|
||||
}
|
||||
|
||||
// ========================================
|
||||
@@ -220,11 +219,10 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
let base_token_program = protocol_params.base_token_program;
|
||||
let quote_token_program = protocol_params.quote_token_program;
|
||||
|
||||
// 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
|
||||
if base_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
&& quote_mint != crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
return Err(anyhow!("Unsupported quote mint for PumpSwap"));
|
||||
return Err(anyhow!("Invalid base mint and quote mint"));
|
||||
}
|
||||
if params.input_amount.is_none() {
|
||||
return Err(anyhow!("Token amount is not set"));
|
||||
|
||||
@@ -31,18 +31,10 @@ 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
|
||||
// ========================================
|
||||
// Determine direction based on actual input mint
|
||||
let is_base_in = protocol_params.coin_mint == params.input_mint;
|
||||
let is_base_in = protocol_params.coin_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
let amount_in: u64 = params.input_amount.unwrap_or(0);
|
||||
let swap_result = compute_swap_amount(
|
||||
protocol_params.coin_reserve,
|
||||
@@ -59,7 +51,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(),
|
||||
¶ms.input_mint,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
@@ -76,8 +68,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
// ========================================
|
||||
let mut instructions = Vec::with_capacity(6);
|
||||
|
||||
if params.create_input_mint_ata && params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
if params.create_input_mint_ata {
|
||||
instructions
|
||||
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
||||
}
|
||||
@@ -126,8 +117,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
accounts.to_vec(),
|
||||
));
|
||||
|
||||
if params.close_input_mint_ata && params.input_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
if params.close_input_mint_ata {
|
||||
// Close wSOL ATA account, reclaim rent
|
||||
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||
}
|
||||
@@ -145,13 +135,6 @@ 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"));
|
||||
}
|
||||
@@ -159,7 +142,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
// ========================================
|
||||
// Trade calculation and account address preparation
|
||||
// ========================================
|
||||
let is_base_in = protocol_params.coin_mint == params.input_mint;
|
||||
let is_base_in = protocol_params.pc_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
let swap_result = compute_swap_amount(
|
||||
protocol_params.coin_reserve,
|
||||
protocol_params.pc_reserve,
|
||||
@@ -182,7 +165,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(),
|
||||
¶ms.output_mint,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
@@ -192,9 +175,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
// ========================================
|
||||
let mut instructions = Vec::with_capacity(3);
|
||||
|
||||
if params.create_output_mint_ata
|
||||
&& params.output_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
if params.create_output_mint_ata {
|
||||
instructions.extend(crate::trading::common::create_wsol_ata(¶ms.payer.pubkey()));
|
||||
}
|
||||
|
||||
@@ -230,9 +211,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
||||
accounts.to_vec(),
|
||||
));
|
||||
|
||||
if params.close_output_mint_ata
|
||||
&& params.output_mint == crate::constants::WSOL_TOKEN_ACCOUNT
|
||||
{
|
||||
if params.close_output_mint_ata {
|
||||
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||
}
|
||||
|
||||
|
||||
@@ -36,13 +36,6 @@ 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,
|
||||
@@ -57,7 +50,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
// ========================================
|
||||
// Trade calculation and account address preparation
|
||||
// ========================================
|
||||
let is_base_in = protocol_params.base_mint == params.input_mint;
|
||||
let is_base_in = protocol_params.base_mint == crate::constants::WSOL_TOKEN_ACCOUNT;
|
||||
let mint_token_program = if is_base_in {
|
||||
protocol_params.quote_token_program
|
||||
} else {
|
||||
@@ -79,7 +72,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
|
||||
let wsol_token_account = get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.input_mint,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
@@ -90,8 +83,12 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
|
||||
let wsol_vault_account =
|
||||
get_vault_account(&pool_state, ¶ms.input_mint, protocol_params, true);
|
||||
let wsol_vault_account = get_vault_account(
|
||||
&pool_state,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
protocol_params,
|
||||
true,
|
||||
);
|
||||
let mint_vault_account =
|
||||
get_vault_account(&pool_state, ¶ms.output_mint, protocol_params, false);
|
||||
|
||||
@@ -135,7 +132,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)
|
||||
AccountMeta::new_readonly(params.input_mint, false), // Input token mint (readonly)
|
||||
crate::constants::WSOL_TOKEN_ACCOUNT_META, // Input token mint (readonly)
|
||||
AccountMeta::new_readonly(params.output_mint, false), // Output token mint (readonly)
|
||||
AccountMeta::new(observation_state_account, false), // Observation State Account
|
||||
];
|
||||
@@ -169,13 +166,6 @@ 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"));
|
||||
}
|
||||
@@ -217,7 +207,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
|
||||
let wsol_token_account = get_associated_token_address_with_program_id_fast_use_seed(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.output_mint,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
&crate::constants::TOKEN_PROGRAM,
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
@@ -228,8 +218,12 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
||||
params.open_seed_optimize,
|
||||
);
|
||||
|
||||
let wsol_vault_account =
|
||||
get_vault_account(&pool_state, ¶ms.output_mint, protocol_params, true);
|
||||
let wsol_vault_account = get_vault_account(
|
||||
&pool_state,
|
||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||
protocol_params,
|
||||
true,
|
||||
);
|
||||
let mint_vault_account =
|
||||
get_vault_account(&pool_state, ¶ms.input_mint, protocol_params, false);
|
||||
|
||||
@@ -261,7 +255,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)
|
||||
AccountMeta::new_readonly(params.output_mint, false), // Output token mint (readonly)
|
||||
crate::constants::WSOL_TOKEN_ACCOUNT_META, // Output token mint (readonly)
|
||||
AccountMeta::new(observation_state_account, false), // Observation State Account
|
||||
];
|
||||
// Create instruction data
|
||||
|
||||
@@ -10,7 +10,6 @@ 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;
|
||||
@@ -39,7 +38,6 @@ pub enum TradeTokenType {
|
||||
SOL,
|
||||
WSOL,
|
||||
USD1,
|
||||
USDC,
|
||||
}
|
||||
|
||||
/// Main trading client for Solana DeFi protocols
|
||||
@@ -278,8 +276,6 @@ 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
|
||||
};
|
||||
@@ -375,8 +371,6 @@ 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,8 +28,11 @@ impl GenericTradeExecutor {
|
||||
impl TradeExecutor for GenericTradeExecutor {
|
||||
async fn swap(&self, params: SwapParams) -> Result<(bool, Signature)> {
|
||||
let start = Instant::now();
|
||||
// Use trade_type directly to determine direction
|
||||
let is_buy = matches!(params.trade_type, crate::swqos::TradeType::Buy);
|
||||
// 暂时支持这三种。后续重构扩展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);
|
||||
// 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