Sol Trade SDK
A comprehensive Rust SDK for seamless interaction with Solana DEX trading programs. This SDK provides a robust set of tools and interfaces to integrate PumpFun, PumpSwap, and Bonk functionality into your applications.
Project Features
- PumpFun Trading: Support for
buyandselloperations - PumpSwap Trading: Support for PumpSwap pool trading operations
- Bonk Trading: Support for Bonk trading operations
- Raydium CPMM Trading: Support for Raydium CPMM (Concentrated Pool Market Maker) trading operations
- Raydium AMM V4 Trading: Support for Raydium AMM V4 (Automated Market Maker) trading operations
- Event Subscription: Subscribe to PumpFun, PumpSwap, Bonk, Raydium CPMM, and Raydium AMM V4 program trading events
- Yellowstone gRPC: Subscribe to program events using Yellowstone gRPC
- ShredStream Support: Subscribe to program events using ShredStream
- Multiple MEV Protection: Support for Jito, Nextblock, ZeroSlot, Temporal, Bloxroute, Node1, and other services
- Concurrent Trading: Send transactions using multiple MEV services simultaneously; the fastest succeeds while others fail
- Unified Trading Interface: Use unified trading protocol enums for trading operations
- Middleware System: Support for custom instruction middleware to modify, add, or remove instructions before transaction execution
Installation
Direct Clone
Clone this project to your project directory:
Add the dependency to your Cargo.toml:
# Add to your Cargo.toml
= { = "./sol-trade-sdk", = "0.5.3" }
Use crates.io
# Add to your Cargo.toml
= "0.5.3"
Usage Examples
Important Parameter Description
auto_handle_wsol Parameter
In PumpSwap, Bonk, and Raydium CPMM trading, the auto_handle_wsol parameter is used to automatically handle wSOL (Wrapped SOL):
- Mechanism:
- When
auto_handle_wsol: true, the SDK automatically handles the conversion between SOL and wSOL - When buying: automatically wraps SOL to wSOL for trading
- When selling: automatically unwraps the received wSOL to SOL
- Default value is
true
- When
lookup_table_key Parameter
The lookup_table_key parameter is an optional Pubkey that specifies an address lookup table for transaction optimization:
- Purpose: Address lookup tables can reduce transaction size and improve execution speed by storing frequently used addresses
- Usage:
- Can be set globally in
TradeConfigfor all transactions - Can be overridden per transaction in
buy()andsell()methods - If not provided, defaults to
None
- Can be set globally in
- Benefits:
- Reduces transaction size by referencing addresses from lookup tables
- Improves transaction success rate and speed
- Particularly useful for complex transactions with many account references
About ShredStream
When using shred to subscribe to events, due to the nature of shreds, you cannot get complete information about transaction events. Please ensure that the parameters your trading logic depends on are available in shreds when using them.
1. Event Subscription - Monitor Token Trading
See the example code in examples/event_subscription.
Run the example code:
2. Initialize SolanaTrade Instance
2.1 SWQOS Service Configuration
When configuring SWQOS services, note the different parameter requirements for each service:
- Jito: The first parameter is UUID, if you don't have a UUID, pass an empty string
"" - NextBlock: The first parameter is API Token
- Bloxroute: The first parameter is API Token
- ZeroSlot: The first parameter is API Token
- Temporal: The first parameter is API Token
- FlashBlock: The first parameter is API Token, Add the official TG support at https://t.me/FlashBlock_Official to get a free key and instantly accelerate your trades! Official docs: https://doc.flashblock.trade/
- Node1: The first parameter is API Token, Add the official TG support at https://t.me/node1_me to get a free key and instantly accelerate your trades! Official docs: https://node1.me/docs.html
When using multiple MEV services, you need to use Durable Nonce. You need to initialize a NonceCache class (or write your own nonce management class), get the latest nonce value, and use it as the blockhash when trading.
2.2 Creating SolanaTrade Instance
See the example code in examples/trading_client.
Run the example code:
3. PumpFun Trading Operations
3.1 Sniping
See the example code in examples/pumpfun_sniper_trading.
Run the example code:
3.2 Copy Trading
See the example code in examples/pumpfun_copy_trading.
Run the example code:
4. PumpSwap Trading Operations
See the example code in examples/pumpswap_trading.
Run the example code:
5. Raydium CPMM Trading Operations
See the example code in examples/raydium_cpmm_trading.
Run the example code:
6. Raydium AMM V4 Trading Operations
See the example code in examples/raydium_amm_v4_trading.
Run the example code:
7. Bonk Trading Operations
7.1 Sniping
See the example code in examples/bonk_sniper_trading.
Run the example code:
7.2 Copy Trading
See the example code in examples/bonk_copy_trading.
Run the example code:
8. Middleware System
The SDK provides a powerful middleware system that allows you to modify, add, or remove instructions before transaction execution. This gives you tremendous flexibility to customize trading behavior.
See the example code in examples/middleware_system.
Run the example code:
Middleware executes in the order they are added:
let middleware_manager = new
.add_middleware // Executes first
.add_middleware // Executes second
.add_middleware; // Executes last
9. Custom Priority Fee Configuration
use PriorityFee;
// Custom priority fee configuration
let priority_fee = PriorityFee ;
// Use custom priority fee in TradeConfig
let trade_config = TradeConfig ;
Supported Trading Platforms
- PumpFun: Primary meme coin trading platform
- PumpSwap: PumpFun's swap protocol
- Bonk: Token launch platform (letsbonk.fun)
- Raydium CPMM: Raydium's Concentrated Pool Market Maker protocol
- Raydium AMM V4: Raydium's Automated Market Maker V4 protocol
MEV Protection Services
- Jito: High-performance block space
- NextBlock: Fast transaction execution
- ZeroSlot: Zero-latency transactions
- Temporal: Time-sensitive transactions
- Bloxroute: Blockchain network acceleration
- FlashBlock: High-speed transaction execution with API key authentication - Official Docs
- Node1: High-speed transaction execution with API key authentication - Official Docs
New Architecture Features
Unified Trading Interface
- TradingProtocol Enum: Use unified protocol enums (PumpFun, PumpSwap, Bonk, RaydiumCpmm, RaydiumAmmV4)
- Unified buy/sell Methods: All protocols use the same trading method signatures
- Protocol-specific Parameters: Each protocol has its own parameter structure (PumpFunParams, RaydiumCpmmParams, RaydiumAmmV4Params, etc.)
Event Parsing System
- Unified Event Interface: All protocol events implement the UnifiedEvent trait
- Protocol-specific Events: Each protocol has its own event types
- Event Factory: Automatically identifies and parses events from different protocols
Trading Engine
- Unified Trading Interface: All trading operations use the same methods
- Protocol Abstraction: Supports trading operations across multiple protocols
- Concurrent Execution: Supports sending transactions to multiple MEV services simultaneously
Price Calculation Utilities
The SDK includes price calculation utilities for all supported protocols in src/utils/price/.
Amount Calculation Utilities
The SDK provides trading amount calculation functionality for various protocols, located in src/utils/calc/:
- Common Calculation Functions: Provides general fee calculation and division utilities
- Protocol-Specific Calculations: Specialized calculation logic for each protocol
- PumpFun: Token buy/sell amount calculations based on bonding curves
- PumpSwap: Amount calculations for multiple trading pairs
- Raydium AMM V4: Amount and fee calculations for automated market maker pools
- Raydium CPMM: Amount calculations for constant product market makers
- Bonk: Specialized calculation logic for Bonk tokens
Key features include:
- Calculate output amounts based on input amounts
- Fee calculation and distribution
- Slippage protection calculations
- Liquidity pool state calculations
Project Structure
src/
├── common/ # Common functionality and tools
├── constants/ # Constant definitions
├── instruction/ # Instruction building
├── swqos/ # MEV service clients
├── trading/ # Unified trading engine
│ ├── common/ # Common trading tools
│ ├── core/ # Core trading engine
│ ├── middleware/ # Middleware system
│ │ ├── builtin.rs # Built-in middleware implementations
│ │ ├── traits.rs # Middleware trait definitions
│ │ └── mod.rs # Middleware module
│ ├── bonk/ # Bonk trading implementation
│ ├── pumpfun/ # PumpFun trading implementation
│ ├── pumpswap/ # PumpSwap trading implementation
│ ├── raydium_cpmm/ # Raydium CPMM trading implementation
│ ├── raydium_amm_v4/ # Raydium AMM V4 trading implementation
│ └── factory.rs # Trading factory
├── utils/ # Utility functions
│ ├── price/ # Price calculation utilities
│ │ ├── common.rs # Common price functions
│ │ ├── bonk.rs # Bonk price calculations
│ │ ├── pumpfun.rs # PumpFun price calculations
│ │ ├── pumpswap.rs # PumpSwap price calculations
│ │ ├── raydium_cpmm.rs # Raydium CPMM price calculations
│ │ ├── raydium_clmm.rs # Raydium CLMM price calculations
│ │ └── raydium_amm_v4.rs # Raydium AMM V4 price calculations
│ └── calc/ # Amount calculation utilities
│ ├── common.rs # Common calculation functions
│ ├── bonk.rs # Bonk amount calculations
│ ├── pumpfun.rs # PumpFun amount calculations
│ ├── pumpswap.rs # PumpSwap amount calculations
│ ├── raydium_cpmm.rs # Raydium CPMM amount calculations
│ └── raydium_amm_v4.rs # Raydium AMM V4 amount calculations
├── lib.rs # Main library file
└── main.rs # Example program
License
MIT License
Contact
- Project Repository: https://github.com/0xfnzero/sol-trade-sdk
- Telegram Group: https://t.me/fnzero_group
Important Notes
- Test thoroughly before using on mainnet
- Properly configure private keys and API tokens
- Pay attention to slippage settings to avoid transaction failures
- Monitor balances and transaction fees
- Comply with relevant laws and regulations