Crates.io | solana-trading-sdk |
lib.rs | solana-trading-sdk |
version | 1.0.6 |
created_at | 2025-05-27 11:46:26.662347+00 |
updated_at | 2025-08-08 08:33:28.542487+00 |
description | A Rust SDK for trading on Solana |
homepage | |
repository | https://github.com/LulzFi/solana-trading-sdk |
max_upload_size | |
id | 1690957 |
size | 317,162 |
A comprehensive Rust SDK for trading on the Solana blockchain, with support for multiple DEXs and advanced transaction submission strategies.
cargo add solana-trading-sdk
use solana_trading_sdk::{
common::{TradingClient, TradingConfig},
swqos::SWQoSType,
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let client = TradingClient::new(TradingConfig {
rpc_url: "https://solana-rpc.publicnode.com".to_string(),
swqos: vec![
SWQoSType::Default("https://solana-rpc.publicnode.com".to_string(), None),
SWQoSType::Jito("https://mainnet.block-engine.jito.wtf".to_string()),
],
})?;
client.initialize().await?;
Ok(())
}
use solana_trading_sdk::{
dex::types::DexType,
instruction::builder::PriorityFee,
};
use solana_sdk::{native_token::sol_to_lamports, pubkey::Pubkey, signature::Keypair};
async fn buy_token() -> anyhow::Result<()> {
let client = get_trading_client().await?;
let payer = Keypair::from_base58_string("your_private_key");
let mint = Pubkey::from_str("token_mint_address")?;
let sol_amount = sol_to_lamports(1.0); // 1 SOL
let slippage_basis_points = 3000; // 30%
let fee = PriorityFee {
unit_limit: 100000,
unit_price: 10000000,
};
let tip = sol_to_lamports(0.001); // 0.001 SOL tip
// Buy on Pump.fun
client.dexs[&DexType::Pumpfun]
.buy(&payer, &mint, sol_amount, slippage_basis_points, Some(fee), Some(tip))
.await?;
Ok(())
}
use solana_trading_sdk::{
ipfs::{metadata::create_token_metadata, types::CreateTokenMetadata},
dex::{pumpfun::Pumpfun, types::Create},
};
async fn create_token() -> anyhow::Result<()> {
// 1. Create metadata
let token_info = CreateTokenMetadata {
name: "My Token".to_string(),
symbol: "MTK".to_string(),
description: "A revolutionary new token".to_string(),
file: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==".to_string(),
twitter: Some("@mytoken".to_string()),
telegram: Some("@mytokengroup".to_string()),
website: Some("https://mytoken.com".to_string()),
metadata_uri: None,
};
let metadata = create_token_metadata(token_info, "your_pinata_jwt_token").await?;
// 2. Create token on Pump.fun
let payer = Keypair::from_base58_string("your_private_key");
let mint = Keypair::new();
let create = Create {
name: metadata.metadata.name,
symbol: metadata.metadata.symbol,
uri: metadata.metadata_uri,
mint: mint.pubkey(),
buy_sol_amount: Some(sol_to_lamports(0.1)),
slippage_basis_points: Some(3000),
};
let pumpfun_client = get_pumpfun_client().await?;
pumpfun_client.create(payer, create, Some(fee), Some(tip)).await?;
Ok(())
}
async fn transfer_sol() -> anyhow::Result<()> {
let from = Keypair::from_base58_string("sender_private_key");
let to = Pubkey::from_str("recipient_address")?;
let amount = sol_to_lamports(0.1); // 0.1 SOL
let swqos_client = get_swqos_client();
swqos_client.transfer(&from, &to, amount, Some(fee)).await?;
Ok(())
}
async fn transfer_token() -> anyhow::Result<()> {
let from = Keypair::from_base58_string("sender_private_key");
let to = Pubkey::from_str("recipient_address")?;
let mint = Pubkey::from_str("token_mint_address")?;
let amount = 1000; // Token amount in smallest units
let swqos_client = get_swqos_client();
swqos_client.spl_transfer(&from, &to, &mint, amount, Some(fee)).await?;
Ok(())
}
Configure multiple SWQoS providers for optimal transaction routing:
let swqos = vec![
SWQoSType::Default("https://solana-rpc.publicnode.com".to_string(), None),
SWQoSType::Jito("https://mainnet.block-engine.jito.wtf".to_string()),
SWQoSType::NextBlock("https://fra.nextblock.io".to_string(), "your_api_key".to_string()),
SWQoSType::Blox("https://fra.blox.so".to_string(), "your_api_key".to_string()),
SWQoSType::ZeroSlot("https://fra.zeroslot.io".to_string(), "your_api_key".to_string()),
SWQoSType::Temporal("https://fra.temporal.io".to_string(), "your_api_key".to_string()),
];
Set custom priority fees for faster transaction confirmation:
let fee = PriorityFee {
unit_limit: 100000, // Compute unit limit
unit_price: 10000000, // Micro-lamports per compute unit
};
Check the main.rs
file for complete working examples of:
TradingClient
- Main client for trading operationsTradingEndpoint
- RPC and SWQoS endpoint managementDexTrait
- Common interface for all DEX implementationsDefaultSWQoSClient
- Standard RPC clientJitoClient
- Jito MEV protectionNextBlockClient
- NextBlock routingBloxClient
- Blox executionZeroSlotClient
- ZeroSlot confirmationTemporalClient
- Temporal optimizationcreate_token_metadata
- Upload token metadata to IPFSCreateTokenMetadata
- Token metadata structureThe SDK uses anyhow::Result
for comprehensive error handling. All functions return detailed error information for debugging.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This SDK is for educational and development purposes. Always test thoroughly on devnet before using on mainnet. Trading cryptocurrencies involves risk of loss.