High-performance Solana trading SDK with multi-protocol support
Project description
๐ Sol Trade SDK for Python
A comprehensive Python SDK for seamless Solana DEX trading
A high-performance Python SDK for low-latency Solana DEX trading bots. Built for speed and efficiency, it enables seamless, high-throughput interaction with PumpFun, Pump AMM (PumpSwap), Bonk, Meteora DAMM v2, Raydium AMM v4, and Raydium CPMM for latency-critical trading strategies.
ไธญๆ | English | Website | Telegram | Discord
โ Support This Project
This SDK is completely free and open source. However, maintaining and continuously updating it requires significant AI computing resources and token consumption. If this SDK helps with your development, consider making a monthly SOL donation โ any amount is appreciated and helps keep this project alive!
Donation Wallet:
6oW7AXz1yRb57pYSxysuXnMs2aR1ha5rzGzReZ1MjPV8
๐ Table of Contents
- โจ Features
- ๐ฆ Installation
- ๐ ๏ธ Usage Examples
- ๐ฐ Cashback Support (PumpFun / PumpSwap)
- ๐ก๏ธ MEV Protection Services
- ๐ Project Structure
- ๐ License
- ๐ฌ Contact
- โ ๏ธ Important Notes
๐ฆ SDK Versions
This SDK is available in multiple languages:
| Language | Repository | Description |
|---|---|---|
| Rust | sol-trade-sdk | Ultra-low latency with zero-copy optimization |
| Node.js | sol-trade-sdk-nodejs | TypeScript/JavaScript for Node.js |
| Python | sol-trade-sdk-python | Async/await native support |
| Go | sol-trade-sdk-golang | Concurrent-safe with goroutine support |
โจ 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
- Meteora DAMM V2 Trading: Support for Meteora DAMM V2 (Dynamic AMM) trading operations
- Multiple MEV Protection: Support for Jito, Nextblock, ZeroSlot, Temporal, Bloxroute, FlashBlock, BlockRazor, Node1, Astralane 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 types for trading operations
- Middleware System: Support for custom instruction middleware to modify, add, or remove instructions before transaction execution
- Shared Infrastructure: Share expensive RPC and SWQoS clients across multiple wallets for reduced resource usage
๐ฆ Installation
Direct Clone (Recommended)
Clone this project to your project directory:
cd your_project_root_directory
git clone https://github.com/0xfnzero/sol-trade-sdk-python
Install dependencies:
cd sol-trade-sdk-python
pip install -e .
Or add to your requirements.txt:
sol-trade-sdk @ ./sol-trade-sdk-python
Or add to your pyproject.toml:
[project]
dependencies = [
"sol-trade-sdk @ ./sol-trade-sdk-python",
]
Use PyPI
pip install sol-trade-sdk
๐ ๏ธ Usage Examples
๐ Example Usage
1. Create TradingClient Instance
You can refer to Example: Create TradingClient Instance.
Method 1: Simple (single wallet)
import asyncio
from sol_trade_sdk import TradingClient, TradeConfig, SwqosConfig, SwqosRegion
async def main():
# Wallet
payer = Keypair.from_secret_key(/* your keypair */)
# RPC URL
rpc_url = "https://mainnet.helius-rpc.com/?api-key=xxxxxx"
# Multiple SWQoS services can be configured
swqos_configs = [
SwqosConfig(type="Default", rpc_url=rpc_url),
SwqosConfig(type="Jito", uuid="your_uuid", region=SwqosRegion.FRANKFURT),
SwqosConfig(type="Bloxroute", api_token="your_api_token", region=SwqosRegion.FRANKFURT),
SwqosConfig(type="Astralane", api_key="your_api_key", region=SwqosRegion.FRANKFURT),
]
# Create TradeConfig instance
trade_config = TradeConfig(rpc_url, swqos_configs)
# Create TradingClient
client = TradingClient(payer, trade_config)
asyncio.run(main())
Method 2: Shared infrastructure (multiple wallets)
For multi-wallet scenarios, create the infrastructure once and share it across wallets. See Example: Shared Infrastructure.
from sol_trade_sdk import TradingInfrastructure, InfrastructureConfig
# Create infrastructure once (expensive)
infra_config = InfrastructureConfig(rpc_url, swqos_configs)
infrastructure = TradingInfrastructure(infra_config)
# Create multiple clients sharing the same infrastructure (fast)
client1 = TradingClient.from_infrastructure(payer1, infrastructure)
client2 = TradingClient.from_infrastructure(payer2, infrastructure)
2. Configure Gas Fee Strategy
from sol_trade_sdk import GasFeeStrategy
# Create GasFeeStrategy instance
gas_fee_strategy = GasFeeStrategy()
# Set global strategy
gas_fee_strategy.set_global_fee_strategy(150000, 150000, 500000, 500000, 0.001, 0.001)
3. Build Trading Parameters
from sol_trade_sdk import TradeBuyParams, DexType, TradeTokenType
buy_params = TradeBuyParams(
dex_type=DexType.PUMPSWAP,
input_token_type=TradeTokenType.WSOL,
mint=mint_pubkey,
input_token_amount=buy_sol_amount,
slippage_basis_points=500,
recent_blockhash=recent_blockhash,
# Use extension_params for protocol-specific parameters
extension_params={"type": "PumpSwap", "params": pumpswap_params},
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,
)
4. Execute Trading
result = await client.buy(buy_params)
print(f"Transaction signature: {result.signature}")
โก Trading Parameters
For comprehensive information about all trading parameters including TradeBuyParams and TradeSellParams, see the Trading Parameters documentation.
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.
๐ Usage Examples Summary Table
| Description | Run Command | Source Code |
|---|---|---|
| Create and configure TradingClient instance | python examples/trading_client.py |
examples/trading_client.py |
| Share infrastructure across multiple wallets | python examples/shared_infrastructure.py |
examples/shared_infrastructure.py |
| PumpFun token sniping trading | python examples/pumpfun_sniper_trading.py |
examples/pumpfun_sniper_trading.py |
| PumpFun token copy trading | python examples/pumpfun_copy_trading.py |
examples/pumpfun_copy_trading.py |
| PumpSwap trading operations | python examples/pumpswap_trading.py |
examples/pumpswap_trading.py |
| PumpSwap direct trading (via RPC) | python examples/pumpswap_direct_trading.py |
examples/pumpswap_direct_trading.py |
| Raydium CPMM trading operations | python examples/raydium_cpmm_trading.py |
examples/raydium_cpmm_trading.py |
| Raydium AMM V4 trading operations | python examples/raydium_amm_v4_trading.py |
examples/raydium_amm_v4_trading.py |
| Meteora DAMM V2 trading operations | python examples/meteora_damm_v2_trading.py |
examples/meteora_damm_v2_trading.py |
| Bonk token sniping trading | python examples/bonk_sniper_trading.py |
examples/bonk_sniper_trading.py |
| Bonk token copy trading | python examples/bonk_copy_trading.py |
examples/bonk_copy_trading.py |
| Custom instruction middleware example | python examples/middleware_system.py |
examples/middleware_system.py |
| Address lookup table example | python examples/address_lookup.py |
examples/address_lookup.py |
| Nonce cache (durable nonce) example | python examples/nonce_cache.py |
examples/nonce_cache.py |
| Wrap/unwrap SOL to/from WSOL example | python examples/wsol_wrapper.py |
examples/wsol_wrapper.py |
| Seed trading example | python examples/seed_trading.py |
examples/seed_trading.py |
| Gas fee strategy example | python examples/gas_fee_strategy.py |
examples/gas_fee_strategy.py |
| Hot path trading (zero-RPC) | python examples/hot_path_trading.py |
examples/hot_path_trading.py |
โ๏ธ SWQoS Service Configuration
When configuring SWQoS services, note the different parameter requirements for each service:
- Jito: The first parameter is UUID (if no UUID, pass an empty string
"") - Other MEV services: The first parameter is the API Token
Custom URL Support
Each SWQoS service supports an optional custom URL parameter:
# Using custom URL
jito_config = SwqosConfig(
type="Jito",
uuid="your_uuid",
region=SwqosRegion.FRANKFURT,
custom_url="https://custom-jito-endpoint.com"
)
# Using default regional endpoint
bloxroute_config = SwqosConfig(
type="Bloxroute",
api_token="your_api_token",
region=SwqosRegion.NEW_YORK
)
URL Priority Logic:
- If a custom URL is provided, it will be used instead of the regional endpoint
- If no custom URL is provided, the system will use the default endpoint for the specified region
- This allows for maximum flexibility while maintaining backward compatibility
When using multiple MEV services, you need to use Durable Nonce. You need to use the fetch_nonce_info function to get the latest nonce value, and use it as the durable_nonce when trading.
๐ง Middleware System
The SDK provides a powerful middleware system that allows you to modify, add, or remove instructions before transaction execution. Middleware executes in the order they are added:
from sol_trade_sdk import MiddlewareManager
manager = MiddlewareManager() \
.add_middleware(FirstMiddleware()) \
.add_middleware(SecondMiddleware()) \
.add_middleware(ThirdMiddleware())
๐ Address Lookup Tables
Address Lookup Tables (ALT) allow you to optimize transaction size and reduce fees by storing frequently used addresses in a compact table format.
from sol_trade_sdk import fetch_address_lookup_table_account, AddressLookupTableCache
# Fetch ALT from chain
alt = await fetch_address_lookup_table_account(rpc, alt_address)
print(f"ALT contains {len(alt.addresses)} addresses")
# Use cache for performance
cache = AddressLookupTableCache(rpc)
await cache.prefetch([alt_address1, alt_address2, alt_address3])
cached = cache.get(alt_address1)
๐ Durable Nonce
Use Durable Nonce to implement transaction replay protection and optimize transaction processing.
from sol_trade_sdk import fetch_nonce_info, NonceCache
# Fetch nonce info
nonce_info = await fetch_nonce_info(rpc, nonce_account)
๐ฐ Cashback Support (PumpFun / PumpSwap)
PumpFun and PumpSwap support cashback for eligible tokens: part of the trading fee can be returned to the user. The SDK must know whether the token has cashback enabled so that buy/sell instructions include the correct accounts.
- When params come from RPC: If you use
PumpFunParams.from_mint_by_rpcorPumpSwapParams.from_pool_address_by_rpc, the SDK readsis_cashback_coinfrom chainโno extra step. - When params come from event/parser: If you build params from trade events (e.g. sol-parser-sdk), you must pass the cashback flag into the SDK:
- PumpFun: Set
is_cashback_coinwhen building params from parsed events. - PumpSwap: Set
is_cashback_coinfield when constructing params manually.
- PumpFun: Set
๐ก๏ธ MEV Protection Services
You can apply for a key through the official website: Community Website
- Jito: High-performance block space
- ZeroSlot: Zero-latency transactions
- Temporal: Time-sensitive transactions
- Bloxroute: Blockchain network acceleration
- FlashBlock: High-speed transaction execution with API key authentication
- BlockRazor: High-speed transaction execution with API key authentication
- Node1: High-speed transaction execution with API key authentication
- Astralane: Blockchain network acceleration
๐ Project Structure
src/
โโโ common/ # Common functionality and tools
โโโ constants/ # Constant definitions
โโโ instruction/ # Instruction building
โ โโโ utils/ # Instruction utilities
โโโ swqos/ # MEV service clients
โโโ trading/ # Unified trading engine
โ โโโ common/ # Common trading tools
โ โโโ core/ # Core trading engine
โ โโโ middleware/ # Middleware system
โ โโโ factory.py # Trading factory
โโโ utils/ # Utility functions
โ โโโ calc/ # Amount calculation utilities
โ โโโ price/ # Price calculation utilities
โโโ __init__.py # Main library file
๐ License
MIT License
๐ฌ Contact
- Official Website: https://fnzero.dev/
- Project Repository: https://github.com/0xfnzero/sol-trade-sdk-python
- Telegram Group: https://t.me/fnzero_group
- Discord: https://discord.gg/vuazbGkqQE
โ ๏ธ 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sol_trade_sdk-0.1.0.tar.gz.
File metadata
- Download URL: sol_trade_sdk-0.1.0.tar.gz
- Upload date:
- Size: 210.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bcf8c6df5ad9ed0662ff791ca151805de61bf39b409eaf6391595f55116ff39
|
|
| MD5 |
39e53bd8361eee586278f1422524e8cd
|
|
| BLAKE2b-256 |
805c6bd64b9b024d732e866d794d0d91cdfefa6f8bd80650265db7ab6238c107
|
File details
Details for the file sol_trade_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sol_trade_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 223.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1fa1a396bfc793b86e724d68441701be5e3652e05891a52dd62b297cb28d80d4
|
|
| MD5 |
1379fddf706ad78ad0436fa208c3e26b
|
|
| BLAKE2b-256 |
3c39be2cdc61bf97ff3fd62213175ca1f8d7aef4727b41fcc30b9a91146c1f06
|