Sync supported DEX IDLs from bitquery/solana-idl-lib, upgrade protocol builders, and keep trade submission hot paths free of RPC query calls. Include exact-output coverage, token-account setup fixes for WSOL and stable pairs, and low-latency transaction build improvements.
98 lines
4.3 KiB
Rust
98 lines
4.3 KiB
Rust
//! TradingClient Creation Example
|
||
//!
|
||
//! This example demonstrates two ways to create a TradingClient:
|
||
//!
|
||
//! 1. Simple method: `TradingClient::new()` - creates client with its own infrastructure
|
||
//! 2. Shared method: `TradingClient::from_infrastructure()` - reuses existing infrastructure
|
||
//!
|
||
//! For multi-wallet scenarios, see the `shared_infrastructure` example.
|
||
|
||
use sol_trade_sdk::{
|
||
common::{AnyResult, InfrastructureConfig, TradeConfig},
|
||
swqos::{SwqosConfig, SwqosRegion},
|
||
AstralaneTransport, TradingClient, TradingInfrastructure,
|
||
};
|
||
use solana_commitment_config::CommitmentConfig;
|
||
use solana_sdk::signature::Keypair;
|
||
use std::sync::Arc;
|
||
|
||
#[tokio::main]
|
||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||
// Method 1: Simple - TradingClient::new() (recommended for single wallet)
|
||
let client = create_trading_client_simple().await?;
|
||
println!("Method 1: Created TradingClient with new()");
|
||
println!(" Wallet: {}", client.get_payer_pubkey());
|
||
|
||
// Method 2: From infrastructure (recommended for multiple wallets)
|
||
let client2 = create_trading_client_from_infrastructure().await?;
|
||
println!("\nMethod 2: Created TradingClient with from_infrastructure()");
|
||
println!(" Wallet: {}", client2.get_payer_pubkey());
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// Method 1: Create TradingClient using TradeConfig (simple, self-contained)
|
||
///
|
||
/// Use this when you have a single wallet or don't need to share infrastructure.
|
||
async fn create_trading_client_simple() -> AnyResult<TradingClient> {
|
||
let payer = Keypair::from_base58_string("use_your_payer_keypair_here");
|
||
let rpc_url = "https://mainnet.helius-rpc.com/?api-key=xxxxxx".to_string();
|
||
let commitment = CommitmentConfig::processed();
|
||
|
||
let swqos_configs: Vec<SwqosConfig> = vec![
|
||
SwqosConfig::Default(rpc_url.clone()),
|
||
SwqosConfig::Jito("your_uuid".to_string(), SwqosRegion::Frankfurt, None),
|
||
SwqosConfig::Bloxroute("your_api_token".to_string(), SwqosRegion::Frankfurt, None),
|
||
SwqosConfig::ZeroSlot("your_api_token".to_string(), SwqosRegion::Frankfurt, None),
|
||
SwqosConfig::Temporal("your_api_token".to_string(), SwqosRegion::Frankfurt, None),
|
||
SwqosConfig::FlashBlock("your_api_token".to_string(), SwqosRegion::Frankfurt, None),
|
||
SwqosConfig::Node1("your_api_token".to_string(), SwqosRegion::Frankfurt, None, None),
|
||
SwqosConfig::BlockRazor("your_api_token".to_string(), SwqosRegion::Frankfurt, None, None),
|
||
SwqosConfig::Astralane(
|
||
"your_api_token".to_string(),
|
||
SwqosRegion::Frankfurt,
|
||
None,
|
||
Some(AstralaneTransport::Quic),
|
||
), // QUIC;None / Some(Binary) / Some(Plain) 为 HTTP
|
||
// Helius Sender: 4th param swqos_only Some(true) => min tip 0.000005 SOL; None => 0.0002 SOL
|
||
SwqosConfig::Helius("".to_string(), SwqosRegion::Default, None, Some(true)),
|
||
];
|
||
|
||
let trade_config = TradeConfig::builder(rpc_url, swqos_configs, commitment)
|
||
.create_wsol_ata_on_startup(true)
|
||
.use_seed_optimize(true)
|
||
.build();
|
||
|
||
// Creates new infrastructure internally
|
||
let client = TradingClient::new(Arc::new(payer), trade_config).await;
|
||
Ok(client)
|
||
}
|
||
|
||
/// Method 2: Create TradingClient from shared infrastructure
|
||
///
|
||
/// Use this when you have multiple wallets sharing the same configuration.
|
||
/// The infrastructure (RPC client, SWQOS clients) is created once and shared.
|
||
async fn create_trading_client_from_infrastructure() -> AnyResult<TradingClient> {
|
||
let payer = Keypair::from_base58_string("use_your_payer_keypair_here");
|
||
let rpc_url = "https://mainnet.helius-rpc.com/?api-key=xxxxxx".to_string();
|
||
let commitment = CommitmentConfig::processed();
|
||
|
||
let swqos_configs: Vec<SwqosConfig> = vec![
|
||
SwqosConfig::Default(rpc_url.clone()),
|
||
SwqosConfig::Jito("your_uuid".to_string(), SwqosRegion::Frankfurt, None),
|
||
];
|
||
|
||
// Create infrastructure separately (can be shared across multiple wallets)
|
||
let infra_config = InfrastructureConfig::new(rpc_url, swqos_configs, commitment);
|
||
let infrastructure = Arc::new(TradingInfrastructure::new(infra_config).await);
|
||
|
||
// Create client from existing infrastructure (fast, no async needed)
|
||
let client = TradingClient::from_infrastructure(
|
||
Arc::new(payer),
|
||
infrastructure,
|
||
true, // use_seed_optimize
|
||
);
|
||
|
||
Ok(client)
|
||
}
|