- Add `mev_protection: bool` to `TradeConfig` and `InfrastructureConfig` (default: false) - Astralane QUIC: switches to port 9000 (MEV-protected endpoint) when enabled - BlockRazor HTTP: uses `mode=sandwichMitigation` query param when enabled - BlockRazor gRPC: uses `mode=sandwichMitigation` when enabled - Add `SWQOS_ENDPOINTS_ASTRALANE_QUIC_MEV` constants (port 9000) to `constants/swqos.rs` - Fix `astralane_quic.rs` IP candidates to use the actual port from the address (supports both 7000 and 9000) - Refactor `TradeConfig` to builder pattern via `TradeConfig::builder()` - Introduce `TradeConfigBuilder` with all optional fields and clear defaults - `TradeConfig::new()` kept as a shortcut (calls `builder().build()`) for backward compatibility - Remove old `with_wsol_ata_config` / `with_check_min_tip` / `with_swqos_cores_from_end` / `with_mev_protection` chain methods - Update all 16 examples to use `TradeConfig::builder()` with commented-out options so users can discover all available settings at a glance - Update README.md and README_CN.md code snippets to use builder pattern 🤖 Generated with [Qoder][https://qoder.com]
121 lines
4.6 KiB
Rust
121 lines
4.6 KiB
Rust
use anyhow::Result;
|
|
use sol_trade_sdk::{
|
|
common::{AnyResult, TradeConfig},
|
|
swqos::SwqosConfig,
|
|
trading::{
|
|
core::params::{DexParamEnum, PumpSwapParams},
|
|
factory::DexType,
|
|
InstructionMiddleware, MiddlewareManager,
|
|
},
|
|
SolanaTrade, TradeTokenType,
|
|
};
|
|
use solana_commitment_config::CommitmentConfig;
|
|
use solana_sdk::{instruction::Instruction, pubkey::Pubkey, signature::Keypair};
|
|
use std::{str::FromStr, sync::Arc};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
test_middleware().await?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Custom middleware
|
|
#[derive(Clone)]
|
|
pub struct CustomMiddleware;
|
|
|
|
impl InstructionMiddleware for CustomMiddleware {
|
|
fn name(&self) -> &'static str {
|
|
"CustomMiddleware"
|
|
}
|
|
|
|
fn process_protocol_instructions(
|
|
&self,
|
|
protocol_instructions: Vec<Instruction>,
|
|
_protocol_name: &str,
|
|
_is_buy: bool,
|
|
) -> Result<Vec<Instruction>> {
|
|
// do anything you want here
|
|
// you can modify the instructions here
|
|
Ok(protocol_instructions)
|
|
}
|
|
|
|
fn process_full_instructions(
|
|
&self,
|
|
full_instructions: Vec<Instruction>,
|
|
_protocol_name: &str,
|
|
_is_buy: bool,
|
|
) -> Result<Vec<Instruction>> {
|
|
// do anything you want here
|
|
// you can modify the instructions here
|
|
Ok(full_instructions)
|
|
}
|
|
|
|
fn clone_box(&self) -> Box<dyn InstructionMiddleware> {
|
|
Box::new(self.clone())
|
|
}
|
|
}
|
|
|
|
/// Create SolanaTrade client
|
|
/// 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 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())];
|
|
let trade_config = TradeConfig::builder(rpc_url, swqos_configs, commitment)
|
|
// .create_wsol_ata_on_startup(true) // default: true
|
|
// .use_seed_optimize(true) // default: true
|
|
// .log_enabled(true) // default: true
|
|
// .check_min_tip(false) // default: false
|
|
// .swqos_cores_from_end(false) // default: false
|
|
// .mev_protection(false) // default: false
|
|
.build();
|
|
let solana_trade = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
|
println!("✅ SolanaTrade client initialized successfully!");
|
|
Ok(solana_trade)
|
|
}
|
|
|
|
async fn test_middleware() -> AnyResult<()> {
|
|
let mut client = create_solana_trade_client().await?;
|
|
// SDK example middleware that prints instruction information
|
|
// You can reference LoggingMiddleware to implement the InstructionMiddleware trait for your own middleware
|
|
let middleware_manager = MiddlewareManager::new().add_middleware(Box::new(CustomMiddleware));
|
|
client = client.with_middleware_manager(middleware_manager);
|
|
let mint_pubkey = Pubkey::from_str("pumpCmXqMfrsAkQ5r49WcJnRayYRqmXz6ae8H7H9Dfn")?;
|
|
let buy_sol_cost = 100_000;
|
|
let slippage_basis_points = Some(100);
|
|
let recent_blockhash = client.infrastructure.rpc.get_latest_blockhash().await?;
|
|
let pool_address = Pubkey::from_str("539m4mVWt6iduB6W8rDGPMarzNCMesuqY5eUTiiYHAgR")?;
|
|
|
|
let gas_fee_strategy = sol_trade_sdk::common::GasFeeStrategy::new();
|
|
gas_fee_strategy.set_global_fee_strategy(150000, 150000, 500000, 500000, 0.001, 0.001);
|
|
|
|
let buy_params = sol_trade_sdk::TradeBuyParams {
|
|
dex_type: DexType::PumpSwap,
|
|
input_token_type: TradeTokenType::WSOL,
|
|
mint: mint_pubkey,
|
|
input_token_amount: buy_sol_cost,
|
|
slippage_basis_points: slippage_basis_points,
|
|
recent_blockhash: Some(recent_blockhash),
|
|
extension_params: DexParamEnum::PumpSwap(
|
|
PumpSwapParams::from_pool_address_by_rpc(&client.infrastructure.rpc, &pool_address)
|
|
.await?,
|
|
),
|
|
address_lookup_table_account: None,
|
|
wait_transaction_confirmed: true,
|
|
create_input_token_ata: true,
|
|
close_input_token_ata: true,
|
|
create_mint_ata: true,
|
|
durable_nonce: None,
|
|
fixed_output_token_amount: None,
|
|
gas_fee_strategy: gas_fee_strategy,
|
|
simulate: false,
|
|
use_exact_sol_amount: None,
|
|
grpc_recv_us: None,
|
|
};
|
|
client.buy(buy_params).await?;
|
|
println!("tip: This transaction will not succeed because we're using a test account. You can modify the code to initialize the payer with your own private key");
|
|
Ok(())
|
|
}
|